v5.page.test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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: '/mc1_emp',
  65. grant: Page.GRANT_PUBLIC,
  66. creator: dummyUser1,
  67. lastUpdateUser: dummyUser1._id,
  68. parent: rootPage._id,
  69. isEmpty: true,
  70. },
  71. {
  72. path: '/mc1_emp/mc2_pub',
  73. grant: Page.GRANT_PUBLIC,
  74. creator: dummyUser1,
  75. lastUpdateUser: dummyUser1._id,
  76. parent: pageIdCreate2,
  77. },
  78. {
  79. path: '/mc3_awl',
  80. grant: Page.GRANT_RESTRICTED,
  81. creator: dummyUser1,
  82. lastUpdateUser: dummyUser1._id,
  83. },
  84. ]);
  85. /**
  86. * update
  87. * mup_ => model update
  88. */
  89. const pageIdUpd1 = new mongoose.Types.ObjectId();
  90. const pageIdUpd2 = new mongoose.Types.ObjectId();
  91. const pageIdUpd3 = new mongoose.Types.ObjectId();
  92. const revisionIdUpd2 = new mongoose.Types.ObjectId();
  93. await Page.insertMany([
  94. {
  95. _id: pageIdUpd1,
  96. path: '/mup1_empty',
  97. grant: Page.GRANT_PUBLIC,
  98. parent: rootPage._id,
  99. isEmpty: true,
  100. },
  101. {
  102. _id: pageIdUpd2,
  103. path: '/mup1_empty/mup2_public',
  104. grant: Page.GRANT_PUBLIC,
  105. parent: pageIdUpd1._id,
  106. creator: dummyUser1,
  107. lastUpdateUser: dummyUser1._id,
  108. revision: revisionIdUpd2,
  109. isEmpty: false,
  110. },
  111. {
  112. _id: pageIdUpd3,
  113. path: '/mup3_empty/mup4_empty/mup5_link',
  114. grant: Page.GRANT_RESTRICTED,
  115. isEmpty: true,
  116. },
  117. {
  118. _id: pageIdUpd2,
  119. path: '/mup6_public',
  120. grant: Page.GRANT_PUBLIC,
  121. parent: pageIdUpd1._id,
  122. creator: dummyUser1,
  123. lastUpdateUser: dummyUser1._id,
  124. revision: revisionIdUpd2,
  125. isEmpty: false,
  126. },
  127. ]);
  128. await Revision.insertMany([
  129. {
  130. _id: revisionIdUpd2,
  131. pageId: pageIdUpd2,
  132. format: 'markdown',
  133. body: '/mup1_empty/mup2_public',
  134. },
  135. ]);
  136. });
  137. describe('create', () => {
  138. test('Should create single page', async() => {
  139. const page = await Page.create('/v5_create1', 'create1', dummyUser1, {});
  140. expect(page).toBeTruthy();
  141. expect(page.parent).toStrictEqual(rootPage._id);
  142. });
  143. test('Should create empty-child and non-empty grandchild', async() => {
  144. const grandchildPage = await Page.create('/v5_empty_create2/v5_create_3', 'grandchild', dummyUser1, {});
  145. const childPage = await Page.findOne({ path: '/v5_empty_create2' });
  146. expect(childPage.isEmpty).toBe(true);
  147. expect(grandchildPage).toBeTruthy();
  148. expect(childPage).toBeTruthy();
  149. expect(childPage.parent).toStrictEqual(rootPage._id);
  150. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  151. });
  152. test('Should create on empty page', async() => {
  153. const beforeCreatePage = await Page.findOne({ path: '/v5_empty_create_4' });
  154. expect(beforeCreatePage.isEmpty).toBe(true);
  155. const childPage = await Page.create('/v5_empty_create_4', 'body', dummyUser1, {});
  156. const grandchildPage = await Page.findOne({ parent: childPage._id });
  157. expect(childPage).toBeTruthy();
  158. expect(childPage.isEmpty).toBe(false);
  159. expect(childPage.revision.body).toBe('body');
  160. expect(grandchildPage).toBeTruthy();
  161. expect(childPage.parent).toStrictEqual(rootPage._id);
  162. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  163. });
  164. describe('Creating a page using existing path', () => {
  165. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  166. const page1 = await Page.findOne({ path: '/mc1_emp' });
  167. const page2 = await Page.findOne({ path: '/mc1_emp/mc2_pub' });
  168. const count = await Page.count({ path: '/mc1_emp' });
  169. expectAllToBeTruthy([page1, page2]);
  170. expect(count).toBe(1);
  171. await Page.create('/mc1_emp', 'new body', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  172. // AF => After Create
  173. const page1AF = await Page.findOne({ _id: page1._id });
  174. const page2AF = await Page.findOne({ _id: page2._id });
  175. const countAF = await Page.count({ path: '/mc1_emp' });
  176. const newPage = await Page.find({ path: '/mc1_emp', grant: Page.GRANT_RESTRICTED });
  177. expectAllToBeTruthy([page1AF, page2AF, newPage]);
  178. expect(countAF).toBe(2);
  179. });
  180. });
  181. describe('Creating a page under a page with grant RESTRICTED', () => {
  182. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
  183. const page1 = await Page.findOne({ path: '/mc3_awl', grant: Page.GRANT_RESTRICTED });
  184. const count = await Page.count({ path: '/mc3_awl' });
  185. expectAllToBeTruthy([page1]);
  186. expect(count).toBe(1);
  187. await Page.create('/mc3_awl/mc4_pub', 'new body', dummyUser1, { grant: Page.GRANT_PUBLIC });
  188. // AF => After Create
  189. const page1AF = await Page.findOne({ path: '/mc3_awl', grant: Page.GRANT_RESTRICTED });
  190. const countAF = await Page.count({ path: '/mc3_awl' });
  191. const newPage = await Page.findOne({ path: '/mc3_awl/mc4_pub', grant: Page.GRANT_PUBLIC });
  192. const newPageParent = await Page.findOne({ path: '/mc3_awl', grant: Page.GRANT_PUBLIC, isEmpty: true });
  193. expectAllToBeTruthy([page1AF, newPageParent, newPage]);
  194. expect(countAF).toBe(2);
  195. expect(newPage.parent).toStrictEqual(newPageParent._id);
  196. expect(newPageParent.parent).toStrictEqual(rootPage._id);
  197. });
  198. });
  199. });
  200. describe('update', () => {
  201. describe('Changing grant from PUBLIC to RESTRICTED of', () => {
  202. test('an only-child page will delete its empty parent page', async() => {
  203. const page1 = await Page.findOne({ path: '/mup1_empty', isEmpty: true });
  204. const page2 = await Page.findOne({ path: '/mup1_empty/mup2_public' }).populate({ path: 'revision', model: 'Revision' });
  205. const revision = page2.revision;
  206. const newBody = 'newBody';
  207. const options = { isSyncRevisionToHackmd: false, grant: 2, grantUserGroupId: null };
  208. expectAllToBeTruthy([page1, page2, revision]);
  209. await Page.updatePage(page2, newBody, revision.body, dummyUser1, options);
  210. // AU => After Update
  211. const page1AU = await Page.findOne({ path: '/mup1_empty', isEmpty: true });
  212. const page2AU = await Page.findOne({ path: '/mup1_empty/mup2_public' }).populate({ path: 'revision', model: 'Revision' });
  213. expect(page2AU).toBeTruthy();
  214. expect(page1AU).toBeNull();
  215. });
  216. test('a page that has children will create an empty page with the same path and it becomes a new parent', async() => {
  217. });
  218. test('of a leaf page will NOT have empty page with the same path', async() => {});
  219. });
  220. describe('Changing grant from RESTRICTED to PUBLIC of', () => {
  221. test('a page with no ancestors will create ancestors with isEmpty: true', async() => {});
  222. test('a page will replace an empty page with the same path if any', async() => {});
  223. });
  224. });
  225. });