v5.page.test.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 pageIdUpd4 = 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. isEmpty: false,
  109. },
  110. {
  111. _id: pageIdUpd3,
  112. path: '/mup3_empty/mup4_empty/mup5_link',
  113. grant: Page.GRANT_RESTRICTED,
  114. isEmpty: true,
  115. },
  116. {
  117. _id: pageIdUpd4,
  118. path: '/mup6_public',
  119. grant: Page.GRANT_PUBLIC,
  120. creator: dummyUser1,
  121. lastUpdateUser: dummyUser1._id,
  122. parent: rootPage._id,
  123. },
  124. {
  125. path: '/mup6_public/mup7_public',
  126. grant: Page.GRANT_PUBLIC,
  127. creator: dummyUser1,
  128. lastUpdateUser: dummyUser1._id,
  129. parent: pageIdUpd4,
  130. },
  131. ]);
  132. });
  133. describe('create', () => {
  134. test('Should create single page', async() => {
  135. const page = await Page.create('/v5_create1', 'create1', dummyUser1, {});
  136. expect(page).toBeTruthy();
  137. expect(page.parent).toStrictEqual(rootPage._id);
  138. });
  139. test('Should create empty-child and non-empty grandchild', async() => {
  140. const grandchildPage = await Page.create('/v5_empty_create2/v5_create_3', 'grandchild', dummyUser1, {});
  141. const childPage = await Page.findOne({ path: '/v5_empty_create2' });
  142. expect(childPage.isEmpty).toBe(true);
  143. expect(grandchildPage).toBeTruthy();
  144. expect(childPage).toBeTruthy();
  145. expect(childPage.parent).toStrictEqual(rootPage._id);
  146. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  147. });
  148. test('Should create on empty page', async() => {
  149. const beforeCreatePage = await Page.findOne({ path: '/v5_empty_create_4' });
  150. expect(beforeCreatePage.isEmpty).toBe(true);
  151. const childPage = await Page.create('/v5_empty_create_4', 'body', dummyUser1, {});
  152. const grandchildPage = await Page.findOne({ parent: childPage._id });
  153. expect(childPage).toBeTruthy();
  154. expect(childPage.isEmpty).toBe(false);
  155. expect(childPage.revision.body).toBe('body');
  156. expect(grandchildPage).toBeTruthy();
  157. expect(childPage.parent).toStrictEqual(rootPage._id);
  158. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  159. });
  160. describe('Creating a page using existing path', () => {
  161. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  162. const page1 = await Page.findOne({ path: '/mc1_emp' });
  163. const page2 = await Page.findOne({ path: '/mc1_emp/mc2_pub' });
  164. const count = await Page.count({ path: '/mc1_emp' });
  165. expectAllToBeTruthy([page1, page2]);
  166. expect(count).toBe(1);
  167. await Page.create('/mc1_emp', 'new body', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  168. // AF => After Create
  169. const page1AF = await Page.findOne({ _id: page1._id });
  170. const page2AF = await Page.findOne({ _id: page2._id });
  171. const countAF = await Page.count({ path: '/mc1_emp' });
  172. const newPage = await Page.find({ path: '/mc1_emp', grant: Page.GRANT_RESTRICTED });
  173. expectAllToBeTruthy([page1AF, page2AF, newPage]);
  174. expect(countAF).toBe(2);
  175. });
  176. });
  177. describe('Creating a page under a page with grant RESTRICTED', () => {
  178. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
  179. const page1 = await Page.findOne({ path: '/mc3_awl', grant: Page.GRANT_RESTRICTED });
  180. const count = await Page.count({ path: '/mc3_awl' });
  181. expectAllToBeTruthy([page1]);
  182. expect(count).toBe(1);
  183. await Page.create('/mc3_awl/mc4_pub', 'new body', dummyUser1, { grant: Page.GRANT_PUBLIC });
  184. // AF => After Create
  185. const page1AF = await Page.findOne({ path: '/mc3_awl', grant: Page.GRANT_RESTRICTED });
  186. const countAF = await Page.count({ path: '/mc3_awl' });
  187. const newPage = await Page.findOne({ path: '/mc3_awl/mc4_pub', grant: Page.GRANT_PUBLIC });
  188. const newPageParent = await Page.findOne({ path: '/mc3_awl', grant: Page.GRANT_PUBLIC, isEmpty: true });
  189. expectAllToBeTruthy([page1AF, newPageParent, newPage]);
  190. expect(countAF).toBe(2);
  191. expect(newPage.parent).toStrictEqual(newPageParent._id);
  192. expect(newPageParent.parent).toStrictEqual(rootPage._id);
  193. });
  194. });
  195. });
  196. describe('update', () => {
  197. describe('Changing grant from PUBLIC to RESTRICTED of', () => {
  198. test('an only-child page will delete its empty parent page', async() => {
  199. const page1 = await Page.findOne({ path: '/mup1_empty', isEmpty: true });
  200. const page2 = await Page.findOne({ path: '/mup1_empty/mup2_public' });
  201. const options = { grant: 2, grantUserGroupId: null };
  202. expectAllToBeTruthy([page1, page2]);
  203. await Page.updatePage(page2, 'newRevisionBody', 'oldRevisionBody', dummyUser1, options);
  204. // AU => After Update
  205. const page1AU = await Page.findOne({ path: '/mup1_empty', isEmpty: true });
  206. const page2AU = await Page.findOne({ path: '/mup1_empty/mup2_public' });
  207. expect(page2AU).toBeTruthy();
  208. expect(page1AU).toBeNull();
  209. });
  210. test('a page that has children will create an empty page with the same path and it becomes a new parent', async() => {
  211. const page1 = await Page.findOne({ path: '/mup6_public', grant: Page.GRANT_PUBLIC });
  212. const page2 = await Page.findOne({ path: '/mup6_public/mup7_public', grant: Page.GRANT_PUBLIC });
  213. const count = await Page.count({ path: '/mup6_public' });
  214. const options = { grant: 2, grantUserGroupId: null };
  215. expectAllToBeTruthy([page1, page2]);
  216. expect(count).toBe(1);
  217. await Page.updatePage(page1, 'newRevisionBody', 'oldRevisionBody', dummyUser1, options);
  218. // AU => After Update
  219. const page1AF = await Page.findOne({ path: '/mup6_public', grant: Page.GRANT_RESTRICTED });
  220. const page2AF = await Page.findOne({ path: '/mup6_public/mup7_public', grant: Page.GRANT_PUBLIC });
  221. const newlyCreatedPage = await Page.findOne({ path: '/mup6_public', grant: Page.GRANT_PUBLIC, isEmpty: true });
  222. const countAF = await Page.count({ path: '/mup6_public' });
  223. expectAllToBeTruthy([page1AF, page2AF, newlyCreatedPage]);
  224. expect(countAF).toBe(2);
  225. expect(page1AF.parent).toBeNull();
  226. expect(page2AF.parent).toStrictEqual(newlyCreatedPage._id);
  227. expect(newlyCreatedPage.parent).toStrictEqual(rootPage._id);
  228. });
  229. test('of a leaf page will NOT have empty page with the same path', async() => {});
  230. });
  231. describe('Changing grant from RESTRICTED to PUBLIC of', () => {
  232. test('a page with no ancestors will create ancestors with isEmpty: true', async() => {});
  233. test('a page will replace an empty page with the same path if any', async() => {});
  234. });
  235. });
  236. });