|
|
@@ -143,79 +143,6 @@ describe('Page', () => {
|
|
|
},
|
|
|
]);
|
|
|
|
|
|
- const pageIdCreate1 = new mongoose.Types.ObjectId();
|
|
|
- const pageIdCreate2 = new mongoose.Types.ObjectId();
|
|
|
- const pageIdCreate3 = new mongoose.Types.ObjectId();
|
|
|
- const pageIdCreate4 = new mongoose.Types.ObjectId();
|
|
|
-
|
|
|
- /**
|
|
|
- * create
|
|
|
- * mc_ => model create
|
|
|
- * emp => empty => page with isEmpty: true
|
|
|
- * pub => public => GRANT_PUBLIC
|
|
|
- */
|
|
|
- await Page.insertMany([
|
|
|
- {
|
|
|
- _id: pageIdCreate1,
|
|
|
- path: '/v5_empty_create_4',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- parent: rootPage._id,
|
|
|
- isEmpty: true,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/v5_empty_create_4/v5_create_5',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- parent: pageIdCreate1,
|
|
|
- isEmpty: false,
|
|
|
- },
|
|
|
- {
|
|
|
- _id: pageIdCreate2,
|
|
|
- path: '/mc4_top/mc1_emp',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- parent: rootPage._id,
|
|
|
- isEmpty: true,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/mc4_top/mc1_emp/mc2_pub',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- parent: pageIdCreate2,
|
|
|
- isEmpty: false,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/mc5_top/mc3_awl',
|
|
|
- grant: Page.GRANT_RESTRICTED,
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- isEmpty: false,
|
|
|
- },
|
|
|
- {
|
|
|
- _id: pageIdCreate3,
|
|
|
- path: '/mc4_top',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- isEmpty: false,
|
|
|
- parent: rootPage._id,
|
|
|
- descendantCount: 1,
|
|
|
- },
|
|
|
- {
|
|
|
- _id: pageIdCreate4,
|
|
|
- path: '/mc5_top',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- isEmpty: false,
|
|
|
- parent: rootPage._id,
|
|
|
- descendantCount: 0,
|
|
|
- },
|
|
|
- ]);
|
|
|
-
|
|
|
/**
|
|
|
* update
|
|
|
* mup_ => model update
|
|
|
@@ -421,182 +348,6 @@ describe('Page', () => {
|
|
|
},
|
|
|
]);
|
|
|
|
|
|
- /**
|
|
|
- * getParentAndFillAncestors
|
|
|
- */
|
|
|
- const pageIdPAF1 = new mongoose.Types.ObjectId();
|
|
|
- const pageIdPAF2 = new mongoose.Types.ObjectId();
|
|
|
- const pageIdPAF3 = new mongoose.Types.ObjectId();
|
|
|
-
|
|
|
- await Page.insertMany([
|
|
|
- {
|
|
|
- _id: pageIdPAF1,
|
|
|
- path: '/PAF1',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- isEmpty: false,
|
|
|
- parent: rootPage._id,
|
|
|
- descendantCount: 0,
|
|
|
- },
|
|
|
- {
|
|
|
- _id: pageIdPAF2,
|
|
|
- path: '/emp_anc3',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: true,
|
|
|
- descendantCount: 1,
|
|
|
- parent: rootPage._id,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/emp_anc3/PAF3',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- isEmpty: false,
|
|
|
- descendantCount: 0,
|
|
|
- parent: pageIdPAF2,
|
|
|
- },
|
|
|
- {
|
|
|
- _id: pageIdPAF3,
|
|
|
- path: '/emp_anc4',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: true,
|
|
|
- descendantCount: 1,
|
|
|
- parent: rootPage._id,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/emp_anc4/PAF4',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- isEmpty: false,
|
|
|
- descendantCount: 0,
|
|
|
- parent: pageIdPAF3,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/emp_anc4',
|
|
|
- grant: Page.GRANT_OWNER,
|
|
|
- grantedUsers: [dummyUser1._id],
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1._id,
|
|
|
- isEmpty: false,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/get_parent_A',
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1,
|
|
|
- parent: null,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/get_parent_A/get_parent_B',
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1,
|
|
|
- parent: null,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/get_parent_C',
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1,
|
|
|
- parent: rootPage._id,
|
|
|
- },
|
|
|
- {
|
|
|
- path: '/get_parent_C/get_parent_D',
|
|
|
- creator: dummyUser1,
|
|
|
- lastUpdateUser: dummyUser1,
|
|
|
- parent: null,
|
|
|
- },
|
|
|
- ]);
|
|
|
-
|
|
|
- });
|
|
|
- describe('create', () => {
|
|
|
-
|
|
|
- test('Should create single page', async() => {
|
|
|
- const page = await crowi.pageService.create('/v5_create1', 'create1', dummyUser1, {});
|
|
|
- expect(page).toBeTruthy();
|
|
|
- expect(page.parent).toStrictEqual(rootPage._id);
|
|
|
- });
|
|
|
-
|
|
|
- test('Should create empty-child and non-empty grandchild', async() => {
|
|
|
- const grandchildPage = await crowi.pageService.create('/v5_empty_create2/v5_create_3', 'grandchild', dummyUser1, {});
|
|
|
- const childPage = await Page.findOne({ path: '/v5_empty_create2' });
|
|
|
-
|
|
|
- expect(childPage.isEmpty).toBe(true);
|
|
|
- expect(grandchildPage).toBeTruthy();
|
|
|
- expect(childPage).toBeTruthy();
|
|
|
- expect(childPage.parent).toStrictEqual(rootPage._id);
|
|
|
- expect(grandchildPage.parent).toStrictEqual(childPage._id);
|
|
|
- });
|
|
|
-
|
|
|
- test('Should create on empty page', async() => {
|
|
|
- const beforeCreatePage = await Page.findOne({ path: '/v5_empty_create_4' });
|
|
|
- expect(beforeCreatePage.isEmpty).toBe(true);
|
|
|
-
|
|
|
- const childPage = await crowi.pageService.create('/v5_empty_create_4', 'body', dummyUser1, {});
|
|
|
- const grandchildPage = await Page.findOne({ parent: childPage._id });
|
|
|
-
|
|
|
- expect(childPage).toBeTruthy();
|
|
|
- expect(childPage.isEmpty).toBe(false);
|
|
|
- expect(childPage.revision.body).toBe('body');
|
|
|
- expect(grandchildPage).toBeTruthy();
|
|
|
- expect(childPage.parent).toStrictEqual(rootPage._id);
|
|
|
- expect(grandchildPage.parent).toStrictEqual(childPage._id);
|
|
|
- });
|
|
|
-
|
|
|
- describe('Creating a page using existing path', () => {
|
|
|
- test('with grant RESTRICTED should only create the page and change nothing else', async() => {
|
|
|
- const pathT = '/mc4_top';
|
|
|
- const path1 = '/mc4_top/mc1_emp';
|
|
|
- const path2 = '/mc4_top/mc1_emp/mc2_pub';
|
|
|
- const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
|
|
|
- const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
|
|
|
- const page2 = await Page.findOne({ path: path2 });
|
|
|
- const page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
|
|
|
- expect(pageT).toBeTruthy();
|
|
|
- expect(page1).toBeTruthy();
|
|
|
- expect(page2).toBeTruthy();
|
|
|
- expect(page3).toBeNull();
|
|
|
-
|
|
|
- // use existing path
|
|
|
- await crowi.pageService.create(path1, 'new body', dummyUser1, { grant: Page.GRANT_RESTRICTED });
|
|
|
-
|
|
|
- const _pageT = await Page.findOne({ path: pathT });
|
|
|
- const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
|
|
|
- const _page2 = await Page.findOne({ path: path2 });
|
|
|
- const _page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
|
|
|
- expect(_pageT).toBeTruthy();
|
|
|
- expect(_page1).toBeTruthy();
|
|
|
- expect(_page2).toBeTruthy();
|
|
|
- expect(_page3).toBeTruthy();
|
|
|
- expect(_pageT.descendantCount).toBe(1);
|
|
|
- });
|
|
|
- });
|
|
|
- describe('Creating a page under a page with grant RESTRICTED', () => {
|
|
|
- test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
|
|
|
- const pathT = '/mc5_top';
|
|
|
- const path1 = '/mc5_top/mc3_awl';
|
|
|
- const pathN = '/mc5_top/mc3_awl/mc4_pub'; // used to create
|
|
|
- const pageT = await Page.findOne({ path: pathT });
|
|
|
- const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
|
|
|
- const page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
|
|
|
- expect(pageT).toBeTruthy();
|
|
|
- expect(page1).toBeTruthy();
|
|
|
- expect(page2).toBeNull();
|
|
|
-
|
|
|
- await crowi.pageService.create(pathN, 'new body', dummyUser1, { grant: Page.GRANT_PUBLIC });
|
|
|
-
|
|
|
- const _pageT = await Page.findOne({ path: pathT });
|
|
|
- const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
|
|
|
- const _page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC, isEmpty: true });
|
|
|
- const _pageN = await Page.findOne({ path: pathN, grant: Page.GRANT_PUBLIC }); // newly crated
|
|
|
- expect(_pageT).toBeTruthy();
|
|
|
- expect(_page1).toBeTruthy();
|
|
|
- expect(_page2).toBeTruthy();
|
|
|
- expect(_pageN).toBeTruthy();
|
|
|
- expect(_pageN.parent).toStrictEqual(_page2._id);
|
|
|
- expect(_pageT.descendantCount).toStrictEqual(1);
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
});
|
|
|
|
|
|
describe('update', () => {
|
|
|
@@ -810,182 +561,4 @@ describe('Page', () => {
|
|
|
});
|
|
|
|
|
|
});
|
|
|
-
|
|
|
- describe('getParentAndFillAncestors', () => {
|
|
|
- test('return parent if exist', async() => {
|
|
|
- const page1 = await Page.findOne({ path: '/PAF1' });
|
|
|
- const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, page1.path);
|
|
|
- expect(parent).toBeTruthy();
|
|
|
- expect(page1.parent).toStrictEqual(parent._id);
|
|
|
- });
|
|
|
- test('create parent and ancestors when they do not exist, and return the new parent', async() => {
|
|
|
- const path1 = '/emp_anc1';
|
|
|
- const path2 = '/emp_anc1/emp_anc2';
|
|
|
- const path3 = '/emp_anc1/emp_anc2/PAF2';
|
|
|
- const _page1 = await Page.findOne({ path: path1 }); // not exist
|
|
|
- const _page2 = await Page.findOne({ path: path2 }); // not exist
|
|
|
- const _page3 = await Page.findOne({ path: path3 }); // not exist
|
|
|
- expect(_page1).toBeNull();
|
|
|
- expect(_page2).toBeNull();
|
|
|
- expect(_page3).toBeNull();
|
|
|
-
|
|
|
- const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, path3);
|
|
|
- const page1 = await Page.findOne({ path: path1 });
|
|
|
- const page2 = await Page.findOne({ path: path2 });
|
|
|
- const page3 = await Page.findOne({ path: path3 });
|
|
|
-
|
|
|
- expect(parent._id).toStrictEqual(page2._id);
|
|
|
- expect(parent.path).toStrictEqual(page2.path);
|
|
|
- expect(parent.parent).toStrictEqual(page2.parent);
|
|
|
-
|
|
|
- expect(parent).toBeTruthy();
|
|
|
- expect(page1).toBeTruthy();
|
|
|
- expect(page2).toBeTruthy();
|
|
|
- expect(page3).toBeNull();
|
|
|
-
|
|
|
- expect(page1.parent).toStrictEqual(rootPage._id);
|
|
|
- expect(page2.parent).toStrictEqual(page1._id);
|
|
|
- });
|
|
|
- test('return parent even if the parent page is empty', async() => {
|
|
|
- const path1 = '/emp_anc3';
|
|
|
- const path2 = '/emp_anc3/PAF3';
|
|
|
- const _page1 = await Page.findOne({ path: path1, isEmpty: true });
|
|
|
- const _page2 = await Page.findOne({ path: path2, isEmpty: false });
|
|
|
- expect(_page1).toBeTruthy();
|
|
|
- expect(_page2).toBeTruthy();
|
|
|
-
|
|
|
- const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, _page2.path);
|
|
|
- const page1 = await Page.findOne({ path: path1, isEmpty: true }); // parent
|
|
|
- const page2 = await Page.findOne({ path: path2, isEmpty: false });
|
|
|
-
|
|
|
- // check for the parent (should be the same as page1)
|
|
|
- expect(parent._id).toStrictEqual(page1._id);
|
|
|
- expect(parent.path).toStrictEqual(page1.path);
|
|
|
- expect(parent.parent).toStrictEqual(page1.parent);
|
|
|
-
|
|
|
- expect(page1.parent).toStrictEqual(rootPage._id);
|
|
|
- expect(page2.parent).toStrictEqual(page1._id);
|
|
|
- });
|
|
|
- test('should find parent while NOT updating private legacy page\'s parent', async() => {
|
|
|
- const path1 = '/emp_anc4';
|
|
|
- const path2 = '/emp_anc4/PAF4';
|
|
|
- const _page1 = await Page.findOne({ path: path1, isEmpty: true, grant: Page.GRANT_PUBLIC });
|
|
|
- const _page2 = await Page.findOne({ path: path2, isEmpty: false, grant: Page.GRANT_PUBLIC });
|
|
|
- const _page3 = await Page.findOne({ path: path1, isEmpty: false, grant: Page.GRANT_OWNER });
|
|
|
- expect(_page1).toBeTruthy();
|
|
|
- expect(_page2).toBeTruthy();
|
|
|
- expect(_page3).toBeTruthy();
|
|
|
- expect(_page3.parent).toBeNull();
|
|
|
-
|
|
|
- const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, _page2.path);
|
|
|
- const page1 = await Page.findOne({ path: path1, isEmpty: true, grant: Page.GRANT_PUBLIC });
|
|
|
- const page2 = await Page.findOne({ path: path2, isEmpty: false, grant: Page.GRANT_PUBLIC });
|
|
|
- const page3 = await Page.findOne({ path: path1, isEmpty: false, grant: Page.GRANT_OWNER });
|
|
|
- expect(page1).toBeTruthy();
|
|
|
- expect(page2).toBeTruthy();
|
|
|
- expect(page3).toBeTruthy();
|
|
|
- expect(page3.parent).toBeNull(); // parent property of page in private legacy pages should be null
|
|
|
-
|
|
|
- expect(page1._id).toStrictEqual(parent._id);
|
|
|
- expect(page2.parent).toStrictEqual(parent._id);
|
|
|
-
|
|
|
- });
|
|
|
- test('should find parent while NOT creating unnecessary empty pages with all v4 public pages', async() => {
|
|
|
- // All pages does not have parent (v4 schema)
|
|
|
- const _pageA = await Page.findOne({
|
|
|
- path: '/get_parent_A',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: false,
|
|
|
- parent: null,
|
|
|
- });
|
|
|
- const _pageAB = await Page.findOne({
|
|
|
- path: '/get_parent_A/get_parent_B',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: false,
|
|
|
- parent: null,
|
|
|
- });
|
|
|
- const _emptyA = await Page.findOne({
|
|
|
- path: '/get_parent_A',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: true,
|
|
|
- });
|
|
|
- const _emptyAB = await Page.findOne({
|
|
|
- path: '/get_parent_A/get_parent_B',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: true,
|
|
|
- });
|
|
|
-
|
|
|
- expect(_pageA).not.toBeNull();
|
|
|
- expect(_pageAB).not.toBeNull();
|
|
|
- expect(_emptyA).toBeNull();
|
|
|
- expect(_emptyAB).toBeNull();
|
|
|
-
|
|
|
- const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, '/get_parent_A/get_parent_B/get_parent_C');
|
|
|
-
|
|
|
- const pageA = await Page.findOne({ path: '/get_parent_A', grant: Page.GRANT_PUBLIC, isEmpty: false });
|
|
|
- const pageAB = await Page.findOne({ path: '/get_parent_A/get_parent_B', grant: Page.GRANT_PUBLIC, isEmpty: false });
|
|
|
- const emptyA = await Page.findOne({ path: '/get_parent_A', grant: Page.GRANT_PUBLIC, isEmpty: true });
|
|
|
- const emptyAB = await Page.findOne({ path: '/get_parent_A/get_parent_B', grant: Page.GRANT_PUBLIC, isEmpty: true });
|
|
|
-
|
|
|
- // -- Check existance
|
|
|
- expect(parent).not.toBeNull();
|
|
|
- expect(pageA).not.toBeNull();
|
|
|
- expect(pageAB).not.toBeNull();
|
|
|
- expect(emptyA).toBeNull();
|
|
|
- expect(emptyAB).toBeNull();
|
|
|
-
|
|
|
- // -- Check parent
|
|
|
- expect(pageA.parent).not.toBeNull();
|
|
|
- expect(pageAB.parent).not.toBeNull();
|
|
|
- });
|
|
|
- test('should find parent while NOT creating unnecessary empty pages with some v5 public pages', async() => {
|
|
|
- const _pageC = await Page.findOne({
|
|
|
- path: '/get_parent_C',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: false,
|
|
|
- parent: { $ne: null },
|
|
|
- });
|
|
|
- const _pageCD = await Page.findOne({
|
|
|
- path: '/get_parent_C/get_parent_D',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: false,
|
|
|
- });
|
|
|
- const _emptyC = await Page.findOne({
|
|
|
- path: '/get_parent_C',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: true,
|
|
|
- });
|
|
|
- const _emptyCD = await Page.findOne({
|
|
|
- path: '/get_parent_C/get_parent_D',
|
|
|
- grant: Page.GRANT_PUBLIC,
|
|
|
- isEmpty: true,
|
|
|
- });
|
|
|
-
|
|
|
- expect(_pageC).not.toBeNull();
|
|
|
- expect(_pageCD).not.toBeNull();
|
|
|
- expect(_emptyC).toBeNull();
|
|
|
- expect(_emptyCD).toBeNull();
|
|
|
-
|
|
|
- const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, '/get_parent_C/get_parent_D/get_parent_E');
|
|
|
-
|
|
|
- const pageC = await Page.findOne({ path: '/get_parent_C', grant: Page.GRANT_PUBLIC, isEmpty: false });
|
|
|
- const pageCD = await Page.findOne({ path: '/get_parent_C/get_parent_D', grant: Page.GRANT_PUBLIC, isEmpty: false });
|
|
|
- const emptyC = await Page.findOne({ path: '/get_parent_C', grant: Page.GRANT_PUBLIC, isEmpty: true });
|
|
|
- const emptyCD = await Page.findOne({ path: '/get_parent_C/get_parent_D', grant: Page.GRANT_PUBLIC, isEmpty: true });
|
|
|
-
|
|
|
- // -- Check existance
|
|
|
- expect(parent).not.toBeNull();
|
|
|
- expect(pageC).not.toBeNull();
|
|
|
- expect(pageCD).not.toBeNull();
|
|
|
- expect(emptyC).toBeNull();
|
|
|
- expect(emptyCD).toBeNull();
|
|
|
-
|
|
|
- // -- Check parent attribute
|
|
|
- expect(pageC.parent).toStrictEqual(rootPage._id);
|
|
|
- expect(pageCD.parent).toStrictEqual(pageC._id);
|
|
|
-
|
|
|
- // -- Check the found parent
|
|
|
- expect(parent.toObject()).toStrictEqual(pageCD.toObject());
|
|
|
- });
|
|
|
- });
|
|
|
});
|