v5.page.test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import mongoose from 'mongoose';
  2. import { getInstance } from '../setup-crowi';
  3. describe('Page', () => {
  4. let crowi;
  5. let Page;
  6. let Revision;
  7. let User;
  8. let Tag;
  9. let PageTagRelation;
  10. let Bookmark;
  11. let Comment;
  12. let ShareLink;
  13. let PageRedirect;
  14. let xssSpy;
  15. let rootPage;
  16. let dummyUser1;
  17. // pass unless the data is one of [false, 0, '', null, undefined, NaN]
  18. const expectAllToBeTruthy = (dataList) => {
  19. dataList.forEach((data, i) => {
  20. if (data == null) { console.log(`index: ${i}`) }
  21. expect(data).toBeTruthy();
  22. });
  23. };
  24. beforeAll(async() => {
  25. crowi = await getInstance();
  26. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': true });
  27. jest.restoreAllMocks();
  28. User = mongoose.model('User');
  29. Page = mongoose.model('Page');
  30. Revision = mongoose.model('Revision');
  31. Tag = mongoose.model('Tag');
  32. PageTagRelation = mongoose.model('PageTagRelation');
  33. Bookmark = mongoose.model('Bookmark');
  34. Comment = mongoose.model('Comment');
  35. ShareLink = mongoose.model('ShareLink');
  36. PageRedirect = mongoose.model('PageRedirect');
  37. dummyUser1 = await User.findOne({ username: 'v5DummyUser1' });
  38. rootPage = await Page.findOne({ path: '/' });
  39. const pageIdCreate1 = new mongoose.Types.ObjectId();
  40. const pageIdCreate2 = new mongoose.Types.ObjectId();
  41. /**
  42. * create
  43. * mc_ => model create
  44. * emp => empty => page with isEmpty: true
  45. * pub => public => GRANT_PUBLIC
  46. */
  47. await Page.insertMany([
  48. {
  49. _id: pageIdCreate1,
  50. path: '/v5_empty_create_4',
  51. grant: Page.GRANT_PUBLIC,
  52. parent: rootPage._id,
  53. isEmpty: true,
  54. },
  55. {
  56. path: '/v5_empty_create_4/v5_create_5',
  57. grant: Page.GRANT_PUBLIC,
  58. creator: dummyUser1,
  59. lastUpdateUser: dummyUser1._id,
  60. parent: pageIdCreate1,
  61. },
  62. {
  63. _id: pageIdCreate2,
  64. path: '/mc_emp',
  65. grant: Page.GRANT_PUBLIC,
  66. creator: dummyUser1,
  67. lastUpdateUser: dummyUser1._id,
  68. parent: rootPage._id,
  69. isEmpty: true,
  70. },
  71. {
  72. path: '/mc_emp/mc_pub2',
  73. grant: Page.GRANT_PUBLIC,
  74. creator: dummyUser1,
  75. lastUpdateUser: dummyUser1._id,
  76. parent: pageIdCreate2,
  77. },
  78. ]);
  79. /**
  80. * update
  81. * mup_ => model update
  82. */
  83. const pageIdUpd1 = new mongoose.Types.ObjectId();
  84. const pageIdUpd2 = new mongoose.Types.ObjectId();
  85. const pageIdUpd3 = new mongoose.Types.ObjectId();
  86. const revisionIdUpd2 = new mongoose.Types.ObjectId();
  87. await Page.insertMany([
  88. {
  89. _id: pageIdUpd1,
  90. path: '/mup1_empty',
  91. grant: Page.GRANT_PUBLIC,
  92. parent: rootPage._id,
  93. isEmpty: true,
  94. },
  95. {
  96. _id: pageIdUpd2,
  97. path: '/mup1_empty/mup2_public',
  98. grant: Page.GRANT_PUBLIC,
  99. parent: pageIdUpd1._id,
  100. creator: dummyUser1,
  101. lastUpdateUser: dummyUser1._id,
  102. revision: revisionIdUpd2,
  103. isEmpty: false,
  104. },
  105. {
  106. _id: pageIdUpd3,
  107. path: '/mup3_empty/mup4_empty/mup5_link',
  108. grant: Page.GRANT_RESTRICTED,
  109. isEmpty: true,
  110. },
  111. ]);
  112. await Revision.insertMany([
  113. {
  114. _id: revisionIdUpd2,
  115. pageId: pageIdUpd2,
  116. format: 'markdown',
  117. body: '/mup1_empty/mup2_public',
  118. },
  119. ]);
  120. });
  121. describe('create', () => {
  122. test('Should create single page', async() => {
  123. const page = await Page.create('/v5_create1', 'create1', dummyUser1, {});
  124. expect(page).toBeTruthy();
  125. expect(page.parent).toStrictEqual(rootPage._id);
  126. });
  127. test('Should create empty-child and non-empty grandchild', async() => {
  128. const grandchildPage = await Page.create('/v5_empty_create2/v5_create_3', 'grandchild', dummyUser1, {});
  129. const childPage = await Page.findOne({ path: '/v5_empty_create2' });
  130. expect(childPage.isEmpty).toBe(true);
  131. expect(grandchildPage).toBeTruthy();
  132. expect(childPage).toBeTruthy();
  133. expect(childPage.parent).toStrictEqual(rootPage._id);
  134. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  135. });
  136. test('Should create on empty page', async() => {
  137. const beforeCreatePage = await Page.findOne({ path: '/v5_empty_create_4' });
  138. expect(beforeCreatePage.isEmpty).toBe(true);
  139. const childPage = await Page.create('/v5_empty_create_4', 'body', dummyUser1, {});
  140. const grandchildPage = await Page.findOne({ parent: childPage._id });
  141. expect(childPage).toBeTruthy();
  142. expect(childPage.isEmpty).toBe(false);
  143. expect(childPage.revision.body).toBe('body');
  144. expect(grandchildPage).toBeTruthy();
  145. expect(childPage.parent).toStrictEqual(rootPage._id);
  146. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  147. });
  148. describe('Creating a page using existing path', () => {
  149. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  150. const page1 = await Page.findOne({ path: '/mc_emp' });
  151. const page2 = await Page.findOne({ path: '/mc_emp/mc_pub2' });
  152. const count = await Page.count({ path: '/mc_emp' });
  153. expectAllToBeTruthy([page1, page2]);
  154. expect(count).toBe(1);
  155. await Page.create('/mc_emp', 'create1', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  156. const page1AF = await Page.findOne({ _id: page1._id });
  157. const page2AF = await Page.findOne({ _id: page2._id });
  158. const countAF = await Page.count({ path: '/mc_emp' });
  159. const newPage = await Page.find({ path: '/mc_emp', grant: Page.GRANT_RESTRICTED });
  160. expectAllToBeTruthy([page1AF, page2AF, newPage]);
  161. expect(countAF).toBe(2);
  162. });
  163. });
  164. describe('Creating a page under a page with grant RESTRICTED', () => {
  165. test('should create an new empty page with the same path as the grant RESTRECTED page', async() => {
  166. });
  167. });
  168. });
  169. describe('update', () => {
  170. describe('Changing grant from PUBLIC to RESTRICTED of', () => {
  171. test('an only-child page will delete its empty parent page', async() => {
  172. const page1 = await Page.findOne({ path: '/mup1_empty', isEmpty: true });
  173. const page2 = await Page.findOne({ path: '/mup1_empty/mup2_public' }).populate({ path: 'revision', model: 'Revision' });
  174. const revision = page2.revision;
  175. const newBody = 'newBody';
  176. const options = { isSyncRevisionToHackmd: false, grant: 2, grantUserGroupId: null };
  177. expectAllToBeTruthy([page1, page2, revision]);
  178. await Page.updatePage(page2, newBody, revision.body, dummyUser1, options);
  179. // AU => After Update
  180. const page1AU = await Page.findOne({ path: '/mup1_empty', isEmpty: true });
  181. const page2AU = await Page.findOne({ path: '/mup1_empty/mup2_public' }).populate({ path: 'revision', model: 'Revision' });
  182. expect(page2AU).toBeTruthy();
  183. expect(page1AU).toBeNull();
  184. });
  185. test('a page that has children will create an empty page with the same path and it becomes a new parent', async() => {});
  186. test('of a leaf page will NOT have empty page with the same path', async() => {});
  187. });
  188. describe('Changing grant from RESTRICTED to PUBLIC of', () => {
  189. test('a page with no ancestors will create ancestors with isEmpty: true', async() => {});
  190. test('a page will replace an empty page with the same path if any', async() => {});
  191. });
  192. });
  193. });