Przeglądaj źródła

Added tests (WIP: need to create page trees

Taichi Masuyama 3 lat temu
rodzic
commit
caffaf1afb

+ 1 - 0
packages/app/package.json

@@ -36,6 +36,7 @@
     "lint:swagger2openapi": "node node_modules/.bin/oas-validate tmp/swagger.json",
     "lint": "run-p lint:*",
     "test": "cross-env NODE_ENV=test jest --passWithNoTests -- ",
+    "TAICHI": "yarn test packages/app/test/integration/service/page-grant.test.js",
     "test:ci": "cross-env NODE_ENV=test jest",
     "// prelint:eslint": "yarn resources:plugin",
     "prelint:swagger2openapi": "yarn openapi:v3",

+ 43 - 0
packages/app/src/server/service/page-grant.ts

@@ -473,6 +473,49 @@ class PageGrantService {
     return data;
   }
 
+  /**
+   * see: https://dev.growi.org/635a314eac6bcd85cbf359fc
+   * @param operator
+   * @param updateGrantInfo
+   * @returns {Promise<boolean>}
+   */
+  async canOverwriteDescendants(operator, updateGrantInfo): Promise<boolean> {
+    // info needed to calc
+    const _updateGrantInfo = {
+      targetPageId: '',
+      grant: 5,
+      grantedUser: {},
+      grantedUserGroup: {},
+    };
+    const descendantPagesGrantInfo = {
+      grantSet: new Set([1, 4, 5]),
+      grantedUsers: new Set([{}, {}]), // only me users
+      grantedUserGroups: new Set([{}, {}]), // user groups
+    };
+
+    return this.calcCanOverwriteDescendants(operator, _updateGrantInfo, descendantPagesGrantInfo);
+  }
+
+  private calcCanOverwriteDescendants(operator, updateGrantInfo, descendantPagesGrantInfo): boolean {
+    // -- preprocess
+    // 1. run isGrantNormalized for ancestors
+    // * isGrantNormalized for descendants is unnecessary since it will overwrite all the descendants
+    // -- process
+    // METHOD consideration
+    // 1. check is tree GRANTED and it returns true when GRANTED
+    //   - GRANTED is the tree with all pages granted by the operator
+    // 2. if not 1. then,
+    //   - when update grant is ONLYME, return false
+    //   - when update grant is PUBLIC, return true
+    //   - when update grant is USER_GROUP, return true if meets 2 conditions below
+    //      a. if all descendants user groups are children or itself of update user group
+    //      b. if all descendants grantedUsers belong to update user group
+    // 3. return false otherwise
+    // 4. true means you can do "update all granted descendants", vice versa
+
+    return false;
+  }
+
 }
 
 export default PageGrantService;

+ 90 - 27
packages/app/test/integration/service/page-grant.test.js

@@ -665,35 +665,98 @@ describe('PageGrantService', () => {
     });
   });
 
-  describe('new method for overwrite scopes of all descendants', () => {
-    test('it should wip', async() => {
-      // -- preprocess
-      // 1. run isGrantNormalized for ancestors
-      // * isGrantNormalized for descendants is unnecessary since it will overwrite all the descendants
-      // -- process
-      // info needed to calc
-      const operator = {}; // user id
+  describe('canOverwriteDescendants', () => {
+    test('it should return true when update grant is GRANT_PUBLIC', async() => {
+      const userA = {};
       const updateGrantInfo = {
-        grant: 5,
-        grantedUser: {},
-        grantedUserGroup: {},
-      }; // target page document
-      const descendantPagesGrantInfo = {
-        grantSet: new Set([1, 4, 5]),
-        grantedUsers: new Set([{}, {}]), // only me users
-        grantedUserGroups: new Set([{}, {}]), // user groups
+        targetPage: {},
+        grant: PageGrant.GRANT_PUBLIC,
+        grantedUser: null,
+        grantedUserGroup: null,
       };
-      // METHOD consideration
-      // 1. check is tree GRANTED and it returns true when GRANTED
-      //   - GRANTED is the tree with all pages granted by the operator
-      // 2. if not 1. then,
-      //   - when update grant is ONLYME, return false
-      //   - when update grant is PUBLIC, update all granted descendants
-      //   - when update grant is USER_GROUP, update all granted descendants if meets 2 conditions below
-      //      a. if all descendants user groups are children or itself of update user group
-      //      b. if all descendants grantedUsers belong to update user group
-      // 3. return false otherwise
-      expect(1).toBe(1);
+      // TODO: expect page tree
+
+      const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
+
+      expect(res).toBe(true);
+    });
+    test('it should return true when all descendant pages are granted by the operator', async() => {
+      const userA = {};
+      const updateGrantInfo = {
+        targetPage: {},
+        grant: PageGrant.GRANT_OWNER,
+        grantedUser: userA,
+        grantedUserGroup: null,
+      };
+      // TODO: expect page tree
+
+      const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
+
+      expect(res).toBe(true);
+    });
+    test(`it should return true when update grant is GRANT_USER_GROUP
+    , all user groups of descendants are the children or itself of the update user group
+    , and all users of descendants belong to the update user group`, async() => {
+      const userA = {};
+      const userGroupAB = {};
+      const updateGrantInfo = {
+        targetPage: {},
+        grant: PageGrant.GRANT_USER_GROUP,
+        grantedUser: null,
+        grantedUserGroup: userGroupAB,
+      };
+      // TODO: expect page tree
+
+      const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
+
+      expect(res).toBe(true);
+    });
+    test(`it should return false when some of descendants is not granted
+    , update grant is GRANT_USER_GROUP
+    , and some of user groups of descendants are not children or itself of the update user group`, async() => {
+      const userA = {};
+      const userGroupAB = {};
+      const updateGrantInfo = {
+        targetPage: {},
+        grant: PageGrant.GRANT_USER_GROUP,
+        grantedUser: null,
+        grantedUserGroup: userGroupAB,
+      };
+      // TODO: expect page tree (include page with gC)
+
+      const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
+
+      expect(res).toBe(false);
+    });
+    test(`it should return false when some of descendants is not granted
+    , update grant is GRANT_USER_GROUP
+    , and some of users of descendants does NOT belong to the update user group`, async() => {
+      const userA = {};
+      const updateGrantInfo = {
+        targetPage: {},
+        grant: PageGrant.GRANT_USER_GROUP,
+        grantedUser: null,
+        grantedUserGroup: null,
+      };
+      // TODO: expect page tree (include page with onlyC)
+
+      const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
+
+      expect(res).toBe(false);
+    });
+    test('it should return false when some of descendants is not granted and update grant is GRANT_OWNER', async() => {
+      const userA = {};
+      const updateGrantInfo = {
+        targetPage: {},
+        grant: PageGrant.GRANT_OWNER,
+        grantedUser: userA,
+        grantedUserGroup: null,
+      };
+      // TODO: expect page tree (include page with onlyC)
+
+      const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
+
+      expect(res).toBe(false);
     });
   });
 });