v5.page.test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. isEmpty: false,
  62. },
  63. {
  64. _id: pageIdCreate2,
  65. path: '/mc1_emp',
  66. grant: Page.GRANT_PUBLIC,
  67. creator: dummyUser1,
  68. lastUpdateUser: dummyUser1._id,
  69. parent: rootPage._id,
  70. isEmpty: true,
  71. },
  72. {
  73. path: '/mc1_emp/mc2_pub',
  74. grant: Page.GRANT_PUBLIC,
  75. creator: dummyUser1,
  76. lastUpdateUser: dummyUser1._id,
  77. parent: pageIdCreate2,
  78. isEmpty: false,
  79. },
  80. {
  81. path: '/mc3_awl',
  82. grant: Page.GRANT_RESTRICTED,
  83. creator: dummyUser1,
  84. lastUpdateUser: dummyUser1._id,
  85. isEmpty: false,
  86. },
  87. ]);
  88. /**
  89. * update
  90. * mup_ => model update
  91. * emp => empty => page with isEmpty: true
  92. * pub => public => GRANT_PUBLIC
  93. * awl => Anyone with the link => GRANT_RESTRICTED
  94. */
  95. const pageIdUpd1 = new mongoose.Types.ObjectId();
  96. const pageIdUpd2 = new mongoose.Types.ObjectId();
  97. const pageIdUpd3 = new mongoose.Types.ObjectId();
  98. const pageIdUpd4 = new mongoose.Types.ObjectId();
  99. const pageIdUpd5 = new mongoose.Types.ObjectId();
  100. const pageIdUpd6 = new mongoose.Types.ObjectId();
  101. const pageIdUpd7 = new mongoose.Types.ObjectId();
  102. const pageIdUpd8 = new mongoose.Types.ObjectId();
  103. const pageIdUpd9 = new mongoose.Types.ObjectId();
  104. const pageIdUpd10 = new mongoose.Types.ObjectId();
  105. const pageIdUpd11 = new mongoose.Types.ObjectId();
  106. const pageIdUpd12 = new mongoose.Types.ObjectId();
  107. await Page.insertMany([
  108. {
  109. _id: pageIdUpd1,
  110. path: '/mup13_top/mup1_emp',
  111. grant: Page.GRANT_PUBLIC,
  112. parent: pageIdUpd8._id,
  113. isEmpty: true,
  114. },
  115. {
  116. _id: pageIdUpd2,
  117. path: '/mup13_top/mup1_emp/mup2_pub',
  118. grant: Page.GRANT_PUBLIC,
  119. parent: pageIdUpd1._id,
  120. creator: dummyUser1,
  121. lastUpdateUser: dummyUser1._id,
  122. isEmpty: false,
  123. },
  124. {
  125. _id: pageIdUpd3,
  126. path: '/mup14_top/mup6_pub',
  127. grant: Page.GRANT_PUBLIC,
  128. creator: dummyUser1,
  129. lastUpdateUser: dummyUser1._id,
  130. parent: pageIdUpd9,
  131. isEmpty: false,
  132. descendantCount: 1,
  133. },
  134. {
  135. path: '/mup14_top/mup6_pub/mup7_pub',
  136. grant: Page.GRANT_PUBLIC,
  137. creator: dummyUser1,
  138. lastUpdateUser: dummyUser1._id,
  139. parent: pageIdUpd3,
  140. isEmpty: false,
  141. descendantCount: 0,
  142. },
  143. {
  144. _id: pageIdUpd4,
  145. path: '/mup15_top/mup8_pub',
  146. grant: Page.GRANT_PUBLIC,
  147. creator: dummyUser1,
  148. lastUpdateUser: dummyUser1._id,
  149. parent: pageIdUpd10._id,
  150. isEmpty: false,
  151. },
  152. {
  153. _id: pageIdUpd5,
  154. path: '/mup16_top/mup9_pub/mup10_pub/mup11_awl',
  155. grant: Page.GRANT_RESTRICTED,
  156. creator: dummyUser1,
  157. lastUpdateUser: dummyUser1._id,
  158. isEmpty: false,
  159. },
  160. {
  161. _id: pageIdUpd6,
  162. path: '/mup17_top/mup12_emp',
  163. isEmpty: true,
  164. parent: pageIdUpd12._id,
  165. },
  166. {
  167. _id: pageIdUpd7,
  168. path: '/mup17_top/mup12_emp',
  169. grant: Page.GRANT_RESTRICTED,
  170. creator: dummyUser1,
  171. lastUpdateUser: dummyUser1._id,
  172. isEmpty: false,
  173. },
  174. {
  175. _id: pageIdUpd8,
  176. path: '/mup13_top',
  177. grant: Page.GRANT_PUBLIC,
  178. creator: dummyUser1,
  179. lastUpdateUser: dummyUser1._id,
  180. isEmpty: false,
  181. parent: rootPage._id,
  182. descendantCount: 2,
  183. },
  184. {
  185. _id: pageIdUpd9,
  186. path: '/mup14_top',
  187. grant: Page.GRANT_PUBLIC,
  188. creator: dummyUser1,
  189. lastUpdateUser: dummyUser1._id,
  190. isEmpty: false,
  191. parent: rootPage._id,
  192. descendantCount: 2,
  193. },
  194. {
  195. _id: pageIdUpd10,
  196. path: '/mup15_top',
  197. grant: Page.GRANT_PUBLIC,
  198. creator: dummyUser1,
  199. lastUpdateUser: dummyUser1._id,
  200. isEmpty: false,
  201. parent: rootPage._id,
  202. descendantCount: 1,
  203. },
  204. {
  205. _id: pageIdUpd11,
  206. path: '/mup16_top',
  207. grant: Page.GRANT_PUBLIC,
  208. creator: dummyUser1,
  209. lastUpdateUser: dummyUser1._id,
  210. isEmpty: false,
  211. parent: rootPage._id,
  212. descendantCount: 0,
  213. },
  214. {
  215. _id: pageIdUpd12,
  216. path: '/mup17_top',
  217. grant: Page.GRANT_PUBLIC,
  218. creator: dummyUser1,
  219. lastUpdateUser: dummyUser1._id,
  220. isEmpty: false,
  221. parent: rootPage._id,
  222. descendantCount: 0,
  223. },
  224. ]);
  225. });
  226. describe('create', () => {
  227. test('Should create single page', async() => {
  228. const page = await Page.create('/v5_create1', 'create1', dummyUser1, {});
  229. expect(page).toBeTruthy();
  230. expect(page.parent).toStrictEqual(rootPage._id);
  231. });
  232. test('Should create empty-child and non-empty grandchild', async() => {
  233. const grandchildPage = await Page.create('/v5_empty_create2/v5_create_3', 'grandchild', dummyUser1, {});
  234. const childPage = await Page.findOne({ path: '/v5_empty_create2' });
  235. expect(childPage.isEmpty).toBe(true);
  236. expect(grandchildPage).toBeTruthy();
  237. expect(childPage).toBeTruthy();
  238. expect(childPage.parent).toStrictEqual(rootPage._id);
  239. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  240. });
  241. test('Should create on empty page', async() => {
  242. const beforeCreatePage = await Page.findOne({ path: '/v5_empty_create_4' });
  243. expect(beforeCreatePage.isEmpty).toBe(true);
  244. const childPage = await Page.create('/v5_empty_create_4', 'body', dummyUser1, {});
  245. const grandchildPage = await Page.findOne({ parent: childPage._id });
  246. expect(childPage).toBeTruthy();
  247. expect(childPage.isEmpty).toBe(false);
  248. expect(childPage.revision.body).toBe('body');
  249. expect(grandchildPage).toBeTruthy();
  250. expect(childPage.parent).toStrictEqual(rootPage._id);
  251. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  252. });
  253. describe('Creating a page using existing path', () => {
  254. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  255. const page1 = await Page.findOne({ path: '/mc1_emp' });
  256. const page2 = await Page.findOne({ path: '/mc1_emp/mc2_pub' });
  257. const count = await Page.count({ path: '/mc1_emp' });
  258. expectAllToBeTruthy([page1, page2]);
  259. expect(count).toBe(1);
  260. await Page.create('/mc1_emp', 'new body', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  261. // AF => After Create
  262. const page1AF = await Page.findOne({ _id: page1._id });
  263. const page2AF = await Page.findOne({ _id: page2._id });
  264. const countAF = await Page.count({ path: '/mc1_emp' });
  265. const newPage = await Page.find({ path: '/mc1_emp', grant: Page.GRANT_RESTRICTED });
  266. expectAllToBeTruthy([page1AF, page2AF, newPage]);
  267. expect(countAF).toBe(2);
  268. });
  269. });
  270. describe('Creating a page under a page with grant RESTRICTED', () => {
  271. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
  272. const page1 = await Page.findOne({ path: '/mc3_awl', grant: Page.GRANT_RESTRICTED });
  273. const count = await Page.count({ path: '/mc3_awl' });
  274. expectAllToBeTruthy([page1]);
  275. expect(count).toBe(1);
  276. await Page.create('/mc3_awl/mc4_pub', 'new body', dummyUser1, { grant: Page.GRANT_PUBLIC });
  277. // AF => After Create
  278. const page1AF = await Page.findOne({ path: '/mc3_awl', grant: Page.GRANT_RESTRICTED });
  279. const countAF = await Page.count({ path: '/mc3_awl' });
  280. const newPage = await Page.findOne({ path: '/mc3_awl/mc4_pub', grant: Page.GRANT_PUBLIC });
  281. const newPageParent = await Page.findOne({ path: '/mc3_awl', grant: Page.GRANT_PUBLIC, isEmpty: true });
  282. expectAllToBeTruthy([page1AF, newPageParent, newPage]);
  283. expect(countAF).toBe(2);
  284. expect(newPage.parent).toStrictEqual(newPageParent._id);
  285. expect(newPageParent.parent).toStrictEqual(rootPage._id);
  286. });
  287. });
  288. });
  289. describe('update', () => {
  290. describe('Changing grant from PUBLIC to RESTRICTED of', () => {
  291. test('an only-child page will delete its empty parent page', async() => {
  292. const top = await Page.findOne({ path: '/mup13_top', descendantCount: 2 });
  293. const page1 = await Page.findOne({ path: '/mup13_top/mup1_emp', isEmpty: true });
  294. const page2 = await Page.findOne({ path: '/mup13_top/mup1_emp/mup2_pub' });
  295. const options = { grant: 2, grantUserGroupId: null };
  296. expectAllToBeTruthy([top, page1, page2]);
  297. await Page.updatePage(page2, 'newRevisionBody', 'oldRevisionBody', dummyUser1, options);
  298. // AU => After Update
  299. const topAF = await Page.findOne({ _id: top._id });
  300. const page1AU = await Page.findOne({ _id: page1._id });
  301. const page2AU = await Page.findOne({ _id: page2._id });
  302. expect(page2AU).toBeTruthy();
  303. expect(page1AU).toBeNull();
  304. expect(topAF.descendantCount).toBe(1);
  305. });
  306. test('a page that has children will create an empty page with the same path and it becomes a new parent', async() => {
  307. const top = await Page.findOne({ path: '/mup14_top', descendantCount: 2 });
  308. const page1 = await Page.findOne({ path: '/mup14_top/mup6_pub', grant: Page.GRANT_PUBLIC });
  309. const page2 = await Page.findOne({ path: '/mup14_top/mup6_pub/mup7_pub', grant: Page.GRANT_PUBLIC });
  310. const count = await Page.count({ path: '/mup14_top/mup6_pub' });
  311. expectAllToBeTruthy([top, page1, page2]);
  312. expect(count).toBe(1);
  313. await Page.updatePage(page1, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: 2 });
  314. // AU => After Update
  315. const topAF = await Page.findOne({ _id: top._id });
  316. const page1AF = await Page.findOne({ _id: page1._id });
  317. const page2AF = await Page.findOne({ _id: page2._id });
  318. const newlyCreatedPage = await Page.findOne({ _id: { $ne: page1._id }, path: '/mup14_top/mup6_pub' });
  319. const countAF = await Page.count({ path: '/mup14_top/mup6_pub' });
  320. expectAllToBeTruthy([page1AF, page2AF, newlyCreatedPage]);
  321. expect(countAF).toBe(2);
  322. expect(page1AF.grant).toBe(Page.GRANT_RESTRICTED);
  323. expect(page1AF.parent).toBeNull();
  324. expect(page2AF.grant).toBe(Page.GRANT_PUBLIC);
  325. expect(page2AF.parent).toStrictEqual(newlyCreatedPage._id);
  326. expect(newlyCreatedPage.isEmpty).toBe(true);
  327. expect(newlyCreatedPage.grant).toBe(Page.GRANT_PUBLIC);
  328. expect(newlyCreatedPage.parent).toStrictEqual(top._id);
  329. expect(topAF.descendantCount).toBe(1);
  330. });
  331. test('of a leaf page will NOT have an empty page with the same path', async() => {
  332. const top = await Page.findOne({ path: '/mup15_top', descendantCount: 1 });
  333. const page = await Page.findOne({ path: '/mup15_top/mup8_pub', grant: Page.GRANT_PUBLIC });
  334. const count = await Page.count({ path: '/mup15_top/mup8_pub' });
  335. expectAllToBeTruthy([top, page]);
  336. expect(count).toBe(1);
  337. await Page.updatePage(page, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: 2 });
  338. // AU => After Update
  339. const topAF = await Page.findOne({ _id: top._id });
  340. const pageAF = await Page.findOne({ _id: page._id });
  341. const emptyPage = await Page.findOne({ path: '/mup15_top/mup8_pub', isEmpty: true });
  342. const countAF = await Page.count({ path: '/mup15_top/mup8_pub' });
  343. expectAllToBeTruthy([pageAF]);
  344. expect(countAF).toBe(1);
  345. expect(emptyPage).toBeNull();
  346. expect(pageAF.grant).toBe(Page.GRANT_RESTRICTED);
  347. expect(topAF.descendantCount).toBe(0);
  348. });
  349. });
  350. describe('Changing grant from RESTRICTED to PUBLIC of', () => {
  351. test('a page will create ancestors if they do not exist', async() => {
  352. const top = await Page.findOne({ path: '/mup16_top' });
  353. const page = await Page.findOne({ path: '/mup16_top/mup9_pub/mup10_pub/mup11_awl', grant: Page.GRANT_RESTRICTED });
  354. const page1 = await Page.findOne({ path: '/mup16_top/mup9_pub' });
  355. const page2 = await Page.findOne({ path: '/mup16_top/mup9_pub/mup10_pub' });
  356. expectAllToBeTruthy([top, page]);
  357. expect(page1).toBeNull();
  358. expect(page2).toBeNull();
  359. await Page.updatePage(page, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: 1 });
  360. const topAF = await Page.findOne({ path: '/mup16_top' });
  361. const pageAF = await Page.findOne({ _id: page._id });
  362. const page1AF = await Page.findOne({ path: '/mup16_top/mup9_pub' });
  363. const page2AF = await Page.findOne({ path: '/mup16_top/mup9_pub/mup10_pub' });
  364. expectAllToBeTruthy([pageAF, page1AF, page2AF]);
  365. expect(pageAF.grant).toBe(Page.GRANT_PUBLIC);
  366. expect(pageAF.parent).toStrictEqual(page2AF._id);
  367. expect(page1AF.isEmpty).toBe(true);
  368. expect(page1AF.parent).toStrictEqual(top._id);
  369. expect(page2AF.isEmpty).toBe(true);
  370. expect(page2AF.parent).toStrictEqual(page1AF._id);
  371. expect(topAF.descendantCount).toBe(1);
  372. });
  373. test('a page will replace an empty page with the same path if any', async() => {
  374. const top = await Page.findOne({ path: '/mup17_top', descendantCount: 0 });
  375. const page1 = await Page.findOne({ path: '/mup17_top/mup12_emp', isEmpty: true });
  376. const page2 = await Page.findOne({ path: '/mup17_top/mup12_emp', grant: Page.GRANT_RESTRICTED, isEmpty: false });
  377. expectAllToBeTruthy([top, page1, page2]);
  378. await Page.updatePage(page2, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: 1 });
  379. const topAF = await Page.findOne({ _id: top._id });
  380. const page1AF = await Page.findOne({ _id: page1._id });
  381. const page2AF = await Page.findOne({ _id: page2._id });
  382. expect(page1AF).toBeNull();
  383. expect(page2AF).toBeTruthy();
  384. expect(page2AF.grant).toBe(Page.GRANT_PUBLIC);
  385. expect(page2AF.parent).toStrictEqual(topAF._id);
  386. expect(topAF.descendantCount).toBe(1);
  387. });
  388. });
  389. });
  390. });