Просмотр исходного кода

Added test code for normalizeParentByPath

Taichi Masuyama 3 лет назад
Родитель
Сommit
03f0f0bf1b
1 измененных файлов с 220 добавлено и 28 удалено
  1. 220 28
      packages/app/test/integration/service/v5.migration.test.js

+ 220 - 28
packages/app/test/integration/service/v5.migration.test.js

@@ -3,16 +3,6 @@ const mongoose = require('mongoose');
 
 
 const { getInstance } = require('../setup-crowi');
 const { getInstance } = require('../setup-crowi');
 
 
-const public = filter => ({ grant: Page.GRANT_PUBLIC, ...filter });
-const owned = filter => ({ grant: Page.GRANT_OWNER, grantedUsers: [testUser1._id], ...filter });
-const root = filter => ({ grantedUsers: [rootUser._id], ...filter });
-const rootUserGroup = filter => ({ grantedGroup: rootUserGroupId, ...filter });
-const testUser1Group = filter => ({ grantedGroup: testUser1GroupId, ...filter });
-
-const normalized = { parent: { $ne: null } };
-const notNormalized = { parent: null };
-const empty = { isEmpty: true };
-
 describe('V5 page migration', () => {
 describe('V5 page migration', () => {
   let crowi;
   let crowi;
   let Page;
   let Page;
@@ -44,6 +34,16 @@ describe('V5 page migration', () => {
   const pageId10 = new mongoose.Types.ObjectId();
   const pageId10 = new mongoose.Types.ObjectId();
   const pageId11 = new mongoose.Types.ObjectId();
   const pageId11 = new mongoose.Types.ObjectId();
 
 
+  const public = filter => ({ grant: Page.GRANT_PUBLIC, ...filter });
+  const ownedByTestUser1 = filter => ({ grant: Page.GRANT_OWNER, grantedUsers: [testUser1._id], ...filter });
+  const root = filter => ({ grantedUsers: [rootUser._id], ...filter });
+  const rootUserGroup = filter => ({ grantedGroup: rootUserGroupId, ...filter });
+  const testUser1Group = filter => ({ grantedGroup: testUser1GroupId, ...filter });
+
+  const normalized = { parent: { $ne: null } };
+  const notNormalized = { parent: null };
+  const empty = { isEmpty: true };
+
   beforeAll(async() => {
   beforeAll(async() => {
     jest.restoreAllMocks();
     jest.restoreAllMocks();
 
 
@@ -557,10 +557,10 @@ describe('V5 page migration', () => {
 
 
     test('should replace all unnecessary empty pages and normalization succeeds', async() => {
     test('should replace all unnecessary empty pages and normalization succeeds', async() => {
       const _pageG = await Page.findOne(public({ path: '/normalize_g', ...normalized }));
       const _pageG = await Page.findOne(public({ path: '/normalize_g', ...normalized }));
-      const _pageGH = await Page.findOne(owned({ path: '/normalize_g/normalize_h', ...notNormalized }));
-      const _pageGI = await Page.findOne(owned({ path: '/normalize_g/normalize_i', ...notNormalized }));
-      const _pageGHJ = await Page.findOne(owned({ path: '/normalize_g/normalize_h/normalize_j', ...notNormalized }));
-      const _pageGIK = await Page.findOne(owned({ path: '/normalize_g/normalize_i/normalize_k', ...notNormalized }));
+      const _pageGH = await Page.findOne(ownedByTestUser1({ path: '/normalize_g/normalize_h', ...notNormalized }));
+      const _pageGI = await Page.findOne(ownedByTestUser1({ path: '/normalize_g/normalize_i', ...notNormalized }));
+      const _pageGHJ = await Page.findOne(ownedByTestUser1({ path: '/normalize_g/normalize_h/normalize_j', ...notNormalized }));
+      const _pageGIK = await Page.findOne(ownedByTestUser1({ path: '/normalize_g/normalize_i/normalize_k', ...notNormalized }));
 
 
       expect(_pageG).not.toBeNull();
       expect(_pageG).not.toBeNull();
       expect(_pageGH).not.toBeNull();
       expect(_pageGH).not.toBeNull();
@@ -610,8 +610,8 @@ describe('V5 page migration', () => {
       expect(pageGIK.descendantCount).toStrictEqual(0);
       expect(pageGIK.descendantCount).toStrictEqual(0);
 
 
       // -- not normalized pages
       // -- not normalized pages
-      const pageGH = await Page.findOne(owned({ path: '/normalize_g/normalize_h' }));
-      const pageGI = await Page.findOne(owned({ path: '/normalize_g/normalize_i' }));
+      const pageGH = await Page.findOne(ownedByTestUser1({ path: '/normalize_g/normalize_h' }));
+      const pageGI = await Page.findOne(ownedByTestUser1({ path: '/normalize_g/normalize_i' }));
       // Check existence
       // Check existence
       expect(pageGH).not.toBeNull();
       expect(pageGH).not.toBeNull();
       expect(pageGI).not.toBeNull();
       expect(pageGI).not.toBeNull();
@@ -780,10 +780,10 @@ describe('V5 page migration', () => {
 
 
 
 
     test('Should normalize a single page without including other pages', async() => {
     test('Should normalize a single page without including other pages', async() => {
-      const _owned13 = await Page.findOne(owned({ path: '/normalize_13_owned', ...notNormalized }));
-      const _owned14 = await Page.findOne(owned({ path: '/normalize_13_owned/normalize_14_owned', ...notNormalized }));
-      const _owned15 = await Page.findOne(owned({ path: '/normalize_13_owned/normalize_14_owned/normalize_15_owned', ...notNormalized }));
-      const _owned16 = await Page.findOne(owned({ path: '/normalize_13_owned/normalize_14_owned/normalize_15_owned/normalize_16_owned', ...notNormalized }));
+      const _owned13 = await Page.findOne(ownedByTestUser1({ path: '/normalize_13_owned', ...notNormalized }));
+      const _owned14 = await Page.findOne(ownedByTestUser1({ path: '/normalize_13_owned/normalize_14_owned', ...notNormalized }));
+      const _owned15 = await Page.findOne(ownedByTestUser1({ path: '/normalize_13_owned/normalize_14_owned/normalize_15_owned', ...notNormalized }));
+      const _owned16 = await Page.findOne(ownedByTestUser1({ path: '/normalize_13_owned/normalize_14_owned/normalize_15_owned/normalize_16_owned', ...notNormalized }));
       const _root16 = await Page.findOne(root({ path: '/normalize_13_owned/normalize_14_owned/normalize_15_owned/normalize_16_root', ...notNormalized }));
       const _root16 = await Page.findOne(root({ path: '/normalize_13_owned/normalize_14_owned/normalize_15_owned/normalize_16_root', ...notNormalized }));
       const _group16 = await Page.findOne(testUser1Group({ path: '/normalize_13_owned/normalize_14_owned/normalize_15_owned/normalize_16_group', ...notNormalized }));
       const _group16 = await Page.findOne(testUser1Group({ path: '/normalize_13_owned/normalize_14_owned/normalize_15_owned/normalize_16_group', ...notNormalized }));
 
 
@@ -829,10 +829,10 @@ describe('V5 page migration', () => {
     });
     });
 
 
     test('Should normalize pages recursively excluding the pages not selected', async() => {
     test('Should normalize pages recursively excluding the pages not selected', async() => {
-      const _owned17 = await Page.findOne(owned({ path: '/normalize_17_owned', ...normalized }));
-      const _owned18 = await Page.findOne(owned({ path: '/normalize_17_owned/normalize_18_owned', ...normalized }));
-      const _owned19 = await Page.findOne(owned({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned', ...notNormalized }));
-      const _owned20 = await Page.findOne(owned({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_owned', ...notNormalized }));
+      const _owned17 = await Page.findOne(ownedByTestUser1({ path: '/normalize_17_owned', ...normalized }));
+      const _owned18 = await Page.findOne(ownedByTestUser1({ path: '/normalize_17_owned/normalize_18_owned', ...normalized }));
+      const _owned19 = await Page.findOne(ownedByTestUser1({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned', ...notNormalized }));
+      const _owned20 = await Page.findOne(ownedByTestUser1({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_owned', ...notNormalized }));
       const _root20 = await Page.findOne(root({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_root', ...notNormalized }));
       const _root20 = await Page.findOne(root({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_root', ...notNormalized }));
       const _group20 = await Page.findOne(rootUserGroup({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_group', ...notNormalized }));
       const _group20 = await Page.findOne(rootUserGroup({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_group', ...notNormalized }));
 
 
@@ -877,11 +877,11 @@ describe('V5 page migration', () => {
     });
     });
 
 
     test('Should normalize pages recursively excluding the pages of not user\'s & Should delete unnecessary empty pages', async() => {
     test('Should normalize pages recursively excluding the pages of not user\'s & Should delete unnecessary empty pages', async() => {
-      const _owned21 = await Page.findOne(owned({ path: '/normalize_21_owned', ...normalized }));
-      const _owned22 = await Page.findOne(owned({ path: '/normalize_21_owned/normalize_22_owned', ...normalized }));
-      const _owned23 = await Page.findOne(owned({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned', ...notNormalized }));
+      const _owned21 = await Page.findOne(ownedByTestUser1({ path: '/normalize_21_owned', ...normalized }));
+      const _owned22 = await Page.findOne(ownedByTestUser1({ path: '/normalize_21_owned/normalize_22_owned', ...normalized }));
+      const _owned23 = await Page.findOne(ownedByTestUser1({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned', ...notNormalized }));
       const _empty23 = await Page.findOne({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned', ...normalized, ...empty });
       const _empty23 = await Page.findOne({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned', ...normalized, ...empty });
-      const _owned24 = await Page.findOne(owned({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned/normalize_24_owned', ...normalized }));
+      const _owned24 = await Page.findOne(ownedByTestUser1({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned/normalize_24_owned', ...normalized }));
       const _root24 = await Page.findOne(root({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned/normalize_24_root', ...notNormalized }));
       const _root24 = await Page.findOne(root({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned/normalize_24_root', ...notNormalized }));
       const _rootGroup24 = await Page.findOne(rootUserGroup({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned/normalize_24_rootGroup', ...notNormalized }));
       const _rootGroup24 = await Page.findOne(rootUserGroup({ path: '/normalize_21_owned/normalize_22_owned/normalize_23_owned/normalize_24_rootGroup', ...notNormalized }));
 
 
@@ -1109,4 +1109,196 @@ describe('V5 page migration', () => {
     expect(privatePage).toStrictEqual(expectedPrivatePage);
     expect(privatePage).toStrictEqual(expectedPrivatePage);
   });
   });
 
 
+  describe('normalizeParentByPath', () => {
+    const normalizeParentByPath = async(path, user) => {
+      const mock = jest.spyOn(crowi.pageService, 'normalizeParentRecursivelyMainOperation').mockReturnValue(null);
+      const result = await crowi.pageService.normalizeParentByPath(path, user);
+      const args = mock.mock.calls[0];
+
+      mock.mockRestore();
+
+      await crowi.pageService.normalizeParentRecursivelyMainOperation(...args);
+
+      return result;
+    };
+
+    beforeAll(async() => {
+      const pageIdD = new mongoose.Types.ObjectId();
+      const pageIdG = new mongoose.Types.ObjectId();
+
+      await Page.insertMany([
+        {
+          path: '/norm_parent_by_path_A',
+          grant: Page.GRANT_OWNER,
+          grantedUsers: [testUser1._id],
+          creator: testUser1._id,
+          lastUpdateUser: testUser1._id,
+          parent: rootPage._id,
+        },
+        {
+          path: '/norm_parent_by_path_B/norm_parent_by_path_C',
+          grant: Page.GRANT_OWNER,
+          grantedUsers: [rootUser._id],
+          creator: rootUser._id,
+          lastUpdateUser: rootUser._id,
+        },
+        {
+          _id: pageIdD,
+          path: '/norm_parent_by_path_D',
+          isEmpty: true,
+          parent: rootPage._id,
+          descendantCount: 1,
+        },
+        {
+          path: '/norm_parent_by_path_D/norm_parent_by_path_E',
+          grant: Page.GRANT_PUBLIC,
+          creator: rootUser._id,
+          lastUpdateUser: rootUser._id,
+          parent: pageIdD,
+        },
+        {
+          path: '/norm_parent_by_path_D/norm_parent_by_path_F',
+          grant: Page.GRANT_OWNER,
+          grantedUsers: [rootUser._id],
+          creator: rootUser._id,
+          lastUpdateUser: rootUser._id,
+        },
+        {
+          _id: pageIdG,
+          path: '/norm_parent_by_path_G',
+          grant: Page.GRANT_PUBLIC,
+          creator: rootUser._id,
+          lastUpdateUser: rootUser._id,
+          parent: rootPage._id,
+          descendantCount: 1,
+        },
+        {
+          path: '/norm_parent_by_path_G/norm_parent_by_path_H',
+          grant: Page.GRANT_PUBLIC,
+          creator: rootUser._id,
+          lastUpdateUser: rootUser._id,
+          parent: pageIdG,
+        },
+        {
+          path: '/norm_parent_by_path_G/norm_parent_by_path_I',
+          grant: Page.GRANT_OWNER,
+          grantedUsers: [rootUser._id],
+          creator: rootUser._id,
+          lastUpdateUser: rootUser._id,
+        },
+      ]);
+    });
+
+    test('should fail when the user is not allowed to edit the target page found by path', async() => {
+      const pageTestUser1 = await Page.findOne(ownedByTestUser1({ path: '/norm_parent_by_path_A' }));
+
+      expect(pageTestUser1).not.toBeNull();
+
+      await expect(normalizeParentByPath('/norm_parent_by_path_A', rootUser)).rejects.toThrowError();
+    });
+
+    test('should normalize all granted pages under the path when no page exists at the path', async() => {
+      const _pageB = await Page.findOne({ path: '/norm_parent_by_path_B' });
+      const _pageBC = await Page.findOne(root({ path: '/norm_parent_by_path_B/norm_parent_by_path_C' }));
+
+      expect(_pageB).toBeNull();
+      expect(_pageBC).not.toBeNull();
+
+      await normalizeParentByPath('/norm_parent_by_path_B', rootUser);
+
+      const pageB = await Page.findOne({ path: '/norm_parent_by_path_B' });
+      const pageBC = await Page.findOne(root({ path: '/norm_parent_by_path_B/norm_parent_by_path_C' }));
+
+      // -- check existance
+      expect(pageB).not.toBeNull();
+      expect(pageBC).not.toBeNull();
+
+      // -- check parent
+      expect(pageB.parent).toStrictEqual(rootPage._id);
+      expect(pageBC.parent).toStrictEqual(pageB._id);
+
+      // -- check descendantCount
+      expect(pageB.descendantCount).toBe(1);
+      expect(pageBC.descendantCount).toBe(0);
+    });
+
+    test('should normalize all granted pages under the path when an empty page exists at the path', async() => {
+      const _emptyD = await Page.findOne({ path: '/norm_parent_by_path_D', ...empty, ...normalized });
+      const _pageDE = await Page.findOne(public({ path: '/norm_parent_by_path_D/norm_parent_by_path_E', ...normalized }));
+      const _pageDF = await Page.findOne(root({ path: '/norm_parent_by_path_D/norm_parent_by_path_F', ...notNormalized }));
+
+      expect(_emptyD).not.toBeNull();
+      expect(_pageDE).not.toBeNull();
+      expect(_pageDF).not.toBeNull();
+
+      await normalizeParentByPath('/norm_parent_by_path_D', rootUser);
+
+      const countD = await Page.count({ path: '/norm_parent_by_path_D' });
+
+      // -- check count
+      expect(countD).toBe(1);
+
+      const pageD = await Page.findOne({ path: '/norm_parent_by_path_D' });
+      const pageDE = await Page.findOne(public({ path: '/norm_parent_by_path_D/norm_parent_by_path_E' }));
+      const pageDF = await Page.findOne(root({ path: '/norm_parent_by_path_D/norm_parent_by_path_F' }));
+
+      // -- check existance
+      expect(pageD).not.toBeNull();
+      expect(pageDE).not.toBeNull();
+      expect(pageDF).not.toBeNull();
+
+      // -- check isEmpty
+      expect(pageD.isEmpty).toBe(false);
+
+      // -- check parent
+      expect(pageD.parent).toStrictEqual(rootPage._id);
+      expect(pageDE.parent).toStrictEqual(pageD._id);
+      expect(pageDF.parent).toStrictEqual(pageD._id);
+
+      // -- check descendantCount
+      expect(pageD.descendantCount).toBe(2);
+      expect(pageDE.descendantCount).toBe(0);
+      expect(pageDF.descendantCount).toBe(0);
+    });
+
+    test('should normalize all granted pages under the path when a non-empty page exists at the path', async() => {
+      const _pageG = await Page.findOne(public({ path: '/norm_parent_by_path_G', ...normalized }));
+      const _pageGH = await Page.findOne(public({ path: '/norm_parent_by_path_G/norm_parent_by_path_H', ...normalized }));
+      const _pageGI = await Page.findOne(root({ path: '/norm_parent_by_path_G/norm_parent_by_path_I', ...notNormalized }));
+
+      expect(_pageG).not.toBeNull();
+      expect(_pageGH).not.toBeNull();
+      expect(_pageGI).not.toBeNull();
+
+      await normalizeParentByPath('/norm_parent_by_path_G', rootUser);
+
+      const countG = await Page.count({ path: '/norm_parent_by_path_G' });
+
+      // -- check count
+      expect(countG).toBe(1);
+
+      const pageG = await Page.findOne(public({ path: '/norm_parent_by_path_G' }));
+      const pageGH = await Page.findOne(public({ path: '/norm_parent_by_path_G/norm_parent_by_path_H' }));
+      const pageGI = await Page.findOne(root({ path: '/norm_parent_by_path_G/norm_parent_by_path_I' }));
+
+      // -- check existance
+      expect(pageG).not.toBeNull();
+      expect(pageGH).not.toBeNull();
+      expect(pageGI).not.toBeNull();
+
+      // -- check isEmpty
+      expect(pageG.isEmpty).toBe(false);
+
+      // -- check parent
+      expect(pageG.parent).toStrictEqual(rootPage._id);
+      expect(pageGH.parent).toStrictEqual(pageG._id);
+      expect(pageGI.parent).toStrictEqual(pageG._id);
+
+      // -- check descendantCount
+      expect(pageG.descendantCount).toBe(2);
+      expect(pageGH.descendantCount).toBe(0);
+      expect(pageGI.descendantCount).toBe(0);
+    });
+  });
+
 });
 });