v5.page.test.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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 UserGroup;
  15. let UserGroupRelation;
  16. let xssSpy;
  17. let rootPage;
  18. let dummyUser1;
  19. let pModelUser1;
  20. let pModelUser2;
  21. let pModelUser3;
  22. let groupIdIsolate;
  23. let groupIdA;
  24. let groupIdB;
  25. let groupIdC;
  26. beforeAll(async() => {
  27. crowi = await getInstance();
  28. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': true });
  29. jest.restoreAllMocks();
  30. User = mongoose.model('User');
  31. Page = mongoose.model('Page');
  32. Revision = mongoose.model('Revision');
  33. Tag = mongoose.model('Tag');
  34. PageTagRelation = mongoose.model('PageTagRelation');
  35. Bookmark = mongoose.model('Bookmark');
  36. Comment = mongoose.model('Comment');
  37. ShareLink = mongoose.model('ShareLink');
  38. PageRedirect = mongoose.model('PageRedirect');
  39. UserGroup = mongoose.model('UserGroup');
  40. UserGroupRelation = mongoose.model('UserGroupRelation');
  41. dummyUser1 = await User.findOne({ username: 'v5DummyUser1' });
  42. rootPage = await Page.findOne({ path: '/' });
  43. const pModelUserId1 = new mongoose.Types.ObjectId();
  44. const pModelUserId2 = new mongoose.Types.ObjectId();
  45. const pModelUserId3 = new mongoose.Types.ObjectId();
  46. await User.insertMany([
  47. {
  48. _id: pModelUserId1, name: 'pmodelUser1', username: 'pmodelUser1', email: 'pmodelUser1@example.com',
  49. },
  50. {
  51. _id: pModelUserId2, name: 'pmodelUser2', username: 'pmodelUser2', email: 'pmodelUser2@example.com',
  52. },
  53. {
  54. _id: pModelUserId3, name: 'pModelUser3', username: 'pModelUser3', email: 'pModelUser3@example.com',
  55. },
  56. ]);
  57. pModelUser1 = await User.findOne({ _id: pModelUserId1 });
  58. pModelUser2 = await User.findOne({ _id: pModelUserId2 });
  59. pModelUser3 = await User.findOne({ _id: pModelUserId3 });
  60. groupIdIsolate = new mongoose.Types.ObjectId();
  61. groupIdA = new mongoose.Types.ObjectId();
  62. groupIdB = new mongoose.Types.ObjectId();
  63. groupIdC = new mongoose.Types.ObjectId();
  64. await UserGroup.insertMany([
  65. {
  66. _id: groupIdIsolate,
  67. name: 'pModel_groupIsolate',
  68. },
  69. {
  70. _id: groupIdA,
  71. name: 'pModel_groupA',
  72. },
  73. {
  74. _id: groupIdB,
  75. name: 'pModel_groupB',
  76. parent: groupIdA,
  77. },
  78. {
  79. _id: groupIdC,
  80. name: 'pModel_groupC',
  81. parent: groupIdB,
  82. },
  83. ]);
  84. await UserGroupRelation.insertMany([
  85. {
  86. relatedGroup: groupIdIsolate,
  87. relatedUser: pModelUserId1,
  88. createdAt: new Date(),
  89. },
  90. {
  91. relatedGroup: groupIdIsolate,
  92. relatedUser: pModelUserId2,
  93. createdAt: new Date(),
  94. },
  95. {
  96. relatedGroup: groupIdA,
  97. relatedUser: pModelUserId1,
  98. createdAt: new Date(),
  99. },
  100. {
  101. relatedGroup: groupIdA,
  102. relatedUser: pModelUserId2,
  103. createdAt: new Date(),
  104. },
  105. {
  106. relatedGroup: groupIdA,
  107. relatedUser: pModelUserId3,
  108. createdAt: new Date(),
  109. },
  110. {
  111. relatedGroup: groupIdB,
  112. relatedUser: pModelUserId2,
  113. createdAt: new Date(),
  114. },
  115. {
  116. relatedGroup: groupIdB,
  117. relatedUser: pModelUserId3,
  118. createdAt: new Date(),
  119. },
  120. {
  121. relatedGroup: groupIdC,
  122. relatedUser: pModelUserId3,
  123. createdAt: new Date(),
  124. },
  125. ]);
  126. const pageIdCreate1 = new mongoose.Types.ObjectId();
  127. const pageIdCreate2 = new mongoose.Types.ObjectId();
  128. const pageIdCreate3 = new mongoose.Types.ObjectId();
  129. const pageIdCreate4 = new mongoose.Types.ObjectId();
  130. /**
  131. * create
  132. * mc_ => model create
  133. * emp => empty => page with isEmpty: true
  134. * pub => public => GRANT_PUBLIC
  135. */
  136. await Page.insertMany([
  137. {
  138. _id: pageIdCreate1,
  139. path: '/v5_empty_create_4',
  140. grant: Page.GRANT_PUBLIC,
  141. parent: rootPage._id,
  142. isEmpty: true,
  143. },
  144. {
  145. path: '/v5_empty_create_4/v5_create_5',
  146. grant: Page.GRANT_PUBLIC,
  147. creator: dummyUser1,
  148. lastUpdateUser: dummyUser1._id,
  149. parent: pageIdCreate1,
  150. isEmpty: false,
  151. },
  152. {
  153. _id: pageIdCreate2,
  154. path: '/mc4_top/mc1_emp',
  155. grant: Page.GRANT_PUBLIC,
  156. creator: dummyUser1,
  157. lastUpdateUser: dummyUser1._id,
  158. parent: rootPage._id,
  159. isEmpty: true,
  160. },
  161. {
  162. path: '/mc4_top/mc1_emp/mc2_pub',
  163. grant: Page.GRANT_PUBLIC,
  164. creator: dummyUser1,
  165. lastUpdateUser: dummyUser1._id,
  166. parent: pageIdCreate2,
  167. isEmpty: false,
  168. },
  169. {
  170. path: '/mc5_top/mc3_awl',
  171. grant: Page.GRANT_RESTRICTED,
  172. creator: dummyUser1,
  173. lastUpdateUser: dummyUser1._id,
  174. isEmpty: false,
  175. },
  176. {
  177. _id: pageIdCreate3,
  178. path: '/mc4_top',
  179. grant: Page.GRANT_PUBLIC,
  180. creator: dummyUser1,
  181. lastUpdateUser: dummyUser1._id,
  182. isEmpty: false,
  183. parent: rootPage._id,
  184. descendantCount: 1,
  185. },
  186. {
  187. _id: pageIdCreate4,
  188. path: '/mc5_top',
  189. grant: Page.GRANT_PUBLIC,
  190. creator: dummyUser1,
  191. lastUpdateUser: dummyUser1._id,
  192. isEmpty: false,
  193. parent: rootPage._id,
  194. descendantCount: 0,
  195. },
  196. ]);
  197. /**
  198. * update
  199. * mup_ => model update
  200. * emp => empty => page with isEmpty: true
  201. * pub => public => GRANT_PUBLIC
  202. * awl => Anyone with the link => GRANT_RESTRICTED
  203. */
  204. const pageIdUpd1 = new mongoose.Types.ObjectId();
  205. const pageIdUpd2 = new mongoose.Types.ObjectId();
  206. const pageIdUpd3 = new mongoose.Types.ObjectId();
  207. const pageIdUpd4 = new mongoose.Types.ObjectId();
  208. const pageIdUpd5 = new mongoose.Types.ObjectId();
  209. const pageIdUpd6 = new mongoose.Types.ObjectId();
  210. const pageIdUpd7 = new mongoose.Types.ObjectId();
  211. const pageIdUpd8 = new mongoose.Types.ObjectId();
  212. const pageIdUpd9 = new mongoose.Types.ObjectId();
  213. const pageIdUpd10 = new mongoose.Types.ObjectId();
  214. const pageIdUpd11 = new mongoose.Types.ObjectId();
  215. const pageIdUpd12 = new mongoose.Types.ObjectId();
  216. const pageIdUpd13 = new mongoose.Types.ObjectId();
  217. const pageIdUpd14 = new mongoose.Types.ObjectId();
  218. const pageIdUpd15 = new mongoose.Types.ObjectId();
  219. const pageIdUpd16 = new mongoose.Types.ObjectId();
  220. const pageIdUpd17 = new mongoose.Types.ObjectId();
  221. const pageIdUpd18 = new mongoose.Types.ObjectId();
  222. const pageIdUpd19 = new mongoose.Types.ObjectId();
  223. await Page.insertMany([
  224. {
  225. _id: pageIdUpd1,
  226. path: '/mup13_top/mup1_emp',
  227. grant: Page.GRANT_PUBLIC,
  228. parent: pageIdUpd8._id,
  229. isEmpty: true,
  230. },
  231. {
  232. _id: pageIdUpd2,
  233. path: '/mup13_top/mup1_emp/mup2_pub',
  234. grant: Page.GRANT_PUBLIC,
  235. parent: pageIdUpd1._id,
  236. creator: dummyUser1,
  237. lastUpdateUser: dummyUser1._id,
  238. isEmpty: false,
  239. },
  240. {
  241. _id: pageIdUpd3,
  242. path: '/mup14_top/mup6_pub',
  243. grant: Page.GRANT_PUBLIC,
  244. creator: dummyUser1,
  245. lastUpdateUser: dummyUser1._id,
  246. parent: pageIdUpd9,
  247. isEmpty: false,
  248. descendantCount: 1,
  249. },
  250. {
  251. path: '/mup14_top/mup6_pub/mup7_pub',
  252. grant: Page.GRANT_PUBLIC,
  253. creator: dummyUser1,
  254. lastUpdateUser: dummyUser1._id,
  255. parent: pageIdUpd3,
  256. isEmpty: false,
  257. descendantCount: 0,
  258. },
  259. {
  260. _id: pageIdUpd4,
  261. path: '/mup15_top/mup8_pub',
  262. grant: Page.GRANT_PUBLIC,
  263. creator: dummyUser1,
  264. lastUpdateUser: dummyUser1._id,
  265. parent: pageIdUpd10._id,
  266. isEmpty: false,
  267. },
  268. {
  269. _id: pageIdUpd5,
  270. path: '/mup16_top/mup9_pub/mup10_pub/mup11_awl',
  271. grant: Page.GRANT_RESTRICTED,
  272. creator: dummyUser1,
  273. lastUpdateUser: dummyUser1._id,
  274. isEmpty: false,
  275. },
  276. {
  277. _id: pageIdUpd6,
  278. path: '/mup17_top/mup12_emp',
  279. isEmpty: true,
  280. parent: pageIdUpd12._id,
  281. descendantCount: 1,
  282. },
  283. {
  284. _id: pageIdUpd7,
  285. path: '/mup17_top/mup12_emp',
  286. grant: Page.GRANT_RESTRICTED,
  287. creator: dummyUser1,
  288. lastUpdateUser: dummyUser1._id,
  289. isEmpty: false,
  290. },
  291. {
  292. path: '/mup17_top/mup12_emp/mup18_pub',
  293. isEmpty: false,
  294. creator: dummyUser1,
  295. lastUpdateUser: dummyUser1._id,
  296. parent: pageIdUpd6._id,
  297. },
  298. {
  299. _id: pageIdUpd8,
  300. path: '/mup13_top',
  301. grant: Page.GRANT_PUBLIC,
  302. creator: dummyUser1,
  303. lastUpdateUser: dummyUser1._id,
  304. isEmpty: false,
  305. parent: rootPage._id,
  306. descendantCount: 2,
  307. },
  308. {
  309. _id: pageIdUpd9,
  310. path: '/mup14_top',
  311. grant: Page.GRANT_PUBLIC,
  312. creator: dummyUser1,
  313. lastUpdateUser: dummyUser1._id,
  314. isEmpty: false,
  315. parent: rootPage._id,
  316. descendantCount: 2,
  317. },
  318. {
  319. _id: pageIdUpd10,
  320. path: '/mup15_top',
  321. grant: Page.GRANT_PUBLIC,
  322. creator: dummyUser1,
  323. lastUpdateUser: dummyUser1._id,
  324. isEmpty: false,
  325. parent: rootPage._id,
  326. descendantCount: 1,
  327. },
  328. {
  329. _id: pageIdUpd11,
  330. path: '/mup16_top',
  331. grant: Page.GRANT_PUBLIC,
  332. creator: dummyUser1,
  333. lastUpdateUser: dummyUser1._id,
  334. isEmpty: false,
  335. parent: rootPage._id,
  336. descendantCount: 0,
  337. },
  338. {
  339. _id: pageIdUpd12,
  340. path: '/mup17_top',
  341. grant: Page.GRANT_PUBLIC,
  342. creator: dummyUser1,
  343. lastUpdateUser: dummyUser1._id,
  344. isEmpty: false,
  345. parent: rootPage._id,
  346. descendantCount: 1,
  347. },
  348. {
  349. path: '/mup19',
  350. grant: Page.GRANT_PUBLIC,
  351. creator: dummyUser1,
  352. lastUpdateUser: dummyUser1._id,
  353. isEmpty: false,
  354. parent: rootPage._id,
  355. descendantCount: 0,
  356. },
  357. {
  358. path: '/mup20',
  359. grant: Page.GRANT_USER_GROUP,
  360. grantedGroup: groupIdA,
  361. creator: pModelUserId1,
  362. lastUpdateUser: pModelUserId1,
  363. isEmpty: false,
  364. parent: rootPage._id,
  365. descendantCount: 0,
  366. },
  367. {
  368. path: '/mup21',
  369. grant: Page.GRANT_RESTRICTED,
  370. creator: dummyUser1,
  371. lastUpdateUser: dummyUser1._id,
  372. isEmpty: false,
  373. descendantCount: 0,
  374. },
  375. {
  376. _id: pageIdUpd13,
  377. path: '/mup22',
  378. grant: Page.GRANT_PUBLIC,
  379. creator: pModelUser1,
  380. lastUpdateUser: pModelUser1._id,
  381. isEmpty: false,
  382. parent: rootPage._id,
  383. descendantCount: 1,
  384. },
  385. {
  386. path: '/mup22/mup23',
  387. grant: Page.GRANT_USER_GROUP,
  388. grantedGroup: groupIdA,
  389. creator: pModelUserId1,
  390. lastUpdateUser: pModelUserId1,
  391. isEmpty: false,
  392. parent: pageIdUpd13,
  393. descendantCount: 0,
  394. },
  395. {
  396. _id: pageIdUpd14,
  397. path: '/mup24_pub',
  398. grant: Page.GRANT_PUBLIC,
  399. creator: pModelUserId1,
  400. lastUpdateUser: pModelUserId1,
  401. isEmpty: false,
  402. parent: rootPage,
  403. descendantCount: 1,
  404. },
  405. {
  406. path: '/mup24_pub/mup25_pub',
  407. grant: Page.GRANT_PUBLIC,
  408. creator: pModelUserId1,
  409. lastUpdateUser: pModelUserId1,
  410. isEmpty: false,
  411. parent: rootPage,
  412. descendantCount: 0,
  413. },
  414. {
  415. path: '/mup26_awl',
  416. grant: Page.GRANT_RESTRICTED,
  417. creator: pModelUserId1,
  418. lastUpdateUser: pModelUserId1,
  419. isEmpty: false,
  420. descendantCount: 0,
  421. },
  422. {
  423. _id: pageIdUpd15,
  424. path: '/mup27_pub',
  425. grant: Page.GRANT_PUBLIC,
  426. creator: pModelUserId1,
  427. lastUpdateUser: pModelUserId1,
  428. isEmpty: false,
  429. parent: rootPage,
  430. descendantCount: 1,
  431. },
  432. {
  433. path: '/mup27_pub/mup28_owner',
  434. grant: Page.GRANT_OWNER,
  435. creator: pModelUserId1,
  436. lastUpdateUser: pModelUserId1,
  437. isEmpty: false,
  438. parent: rootPage,
  439. grantedUsers: [pModelUserId1],
  440. descendantCount: 0,
  441. },
  442. {
  443. _id: pageIdUpd16,
  444. path: '/mup29_A',
  445. grant: Page.GRANT_USER_GROUP,
  446. grantedGroup: groupIdA,
  447. creator: pModelUserId1,
  448. lastUpdateUser: pModelUserId1,
  449. isEmpty: false,
  450. parent: rootPage,
  451. descendantCount: 1,
  452. },
  453. {
  454. path: '/mup29_A/mup30_owner',
  455. grant: Page.GRANT_OWNER,
  456. grantedUsers: [pModelUserId1],
  457. creator: pModelUserId1,
  458. lastUpdateUser: pModelUserId1,
  459. isEmpty: false,
  460. parent: pageIdUpd16,
  461. descendantCount: 0,
  462. },
  463. {
  464. _id: pageIdUpd17,
  465. path: '/mup31_A',
  466. grant: Page.GRANT_USER_GROUP,
  467. grantedGroup: groupIdA,
  468. creator: pModelUserId1,
  469. lastUpdateUser: pModelUserId1,
  470. isEmpty: false,
  471. parent: rootPage,
  472. descendantCount: 1,
  473. },
  474. {
  475. path: '/mup31_A/mup32_owner',
  476. grant: Page.GRANT_OWNER,
  477. grantedUsers: [pModelUserId1],
  478. creator: pModelUserId1,
  479. lastUpdateUser: pModelUserId1,
  480. isEmpty: false,
  481. parent: pageIdUpd17,
  482. descendantCount: 0,
  483. },
  484. {
  485. _id: pageIdUpd18,
  486. path: '/mup33_C',
  487. grant: Page.GRANT_USER_GROUP,
  488. grantedGroup: groupIdC,
  489. creator: pModelUserId3,
  490. lastUpdateUser: pModelUserId3,
  491. isEmpty: false,
  492. parent: rootPage,
  493. descendantCount: 1,
  494. },
  495. {
  496. path: '/mup33_C/mup34_owner',
  497. grant: Page.GRANT_OWNER,
  498. grantedUsers: [pModelUserId3],
  499. creator: pModelUserId3,
  500. lastUpdateUser: pModelUserId3,
  501. isEmpty: false,
  502. parent: pageIdUpd18,
  503. descendantCount: 0,
  504. },
  505. {
  506. _id: pageIdUpd19,
  507. path: '/mup35_owner',
  508. grant: Page.GRANT_OWNER,
  509. grantedUsers: [pModelUserId1],
  510. creator: pModelUserId1,
  511. lastUpdateUser: pModelUserId1,
  512. isEmpty: false,
  513. parent: rootPage,
  514. descendantCount: 1,
  515. },
  516. {
  517. path: '/mup35_owner/mup36_owner',
  518. grant: Page.GRANT_OWNER,
  519. grantedUsers: [pModelUserId1],
  520. creator: pModelUserId1,
  521. lastUpdateUser: pModelUserId1,
  522. isEmpty: false,
  523. parent: pageIdUpd19,
  524. descendantCount: 0,
  525. },
  526. ]);
  527. });
  528. describe('create', () => {
  529. test('Should create single page', async() => {
  530. const page = await Page.create('/v5_create1', 'create1', dummyUser1, {});
  531. expect(page).toBeTruthy();
  532. expect(page.parent).toStrictEqual(rootPage._id);
  533. });
  534. test('Should create empty-child and non-empty grandchild', async() => {
  535. const grandchildPage = await Page.create('/v5_empty_create2/v5_create_3', 'grandchild', dummyUser1, {});
  536. const childPage = await Page.findOne({ path: '/v5_empty_create2' });
  537. expect(childPage.isEmpty).toBe(true);
  538. expect(grandchildPage).toBeTruthy();
  539. expect(childPage).toBeTruthy();
  540. expect(childPage.parent).toStrictEqual(rootPage._id);
  541. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  542. });
  543. test('Should create on empty page', async() => {
  544. const beforeCreatePage = await Page.findOne({ path: '/v5_empty_create_4' });
  545. expect(beforeCreatePage.isEmpty).toBe(true);
  546. const childPage = await Page.create('/v5_empty_create_4', 'body', dummyUser1, {});
  547. const grandchildPage = await Page.findOne({ parent: childPage._id });
  548. expect(childPage).toBeTruthy();
  549. expect(childPage.isEmpty).toBe(false);
  550. expect(childPage.revision.body).toBe('body');
  551. expect(grandchildPage).toBeTruthy();
  552. expect(childPage.parent).toStrictEqual(rootPage._id);
  553. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  554. });
  555. describe('Creating a page using existing path', () => {
  556. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  557. const pathT = '/mc4_top';
  558. const path1 = '/mc4_top/mc1_emp';
  559. const path2 = '/mc4_top/mc1_emp/mc2_pub';
  560. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  561. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  562. const page2 = await Page.findOne({ path: path2 });
  563. const page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  564. expect(pageT).toBeTruthy();
  565. expect(page1).toBeTruthy();
  566. expect(page2).toBeTruthy();
  567. expect(page3).toBeNull();
  568. // use existing path
  569. await Page.create(path1, 'new body', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  570. const _pageT = await Page.findOne({ path: pathT });
  571. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  572. const _page2 = await Page.findOne({ path: path2 });
  573. const _page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  574. expect(_pageT).toBeTruthy();
  575. expect(_page1).toBeTruthy();
  576. expect(_page2).toBeTruthy();
  577. expect(_page3).toBeTruthy();
  578. expect(_pageT.descendantCount).toBe(1);
  579. });
  580. });
  581. describe('Creating a page under a page with grant RESTRICTED', () => {
  582. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
  583. const pathT = '/mc5_top';
  584. const path1 = '/mc5_top/mc3_awl';
  585. const pathN = '/mc5_top/mc3_awl/mc4_pub'; // used to create
  586. const pageT = await Page.findOne({ path: pathT });
  587. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  588. const page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  589. expect(pageT).toBeTruthy();
  590. expect(page1).toBeTruthy();
  591. expect(page2).toBeNull();
  592. await Page.create(pathN, 'new body', dummyUser1, { grant: Page.GRANT_PUBLIC });
  593. const _pageT = await Page.findOne({ path: pathT });
  594. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  595. const _page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC, isEmpty: true });
  596. const _pageN = await Page.findOne({ path: pathN, grant: Page.GRANT_PUBLIC }); // newly crated
  597. expect(_pageT).toBeTruthy();
  598. expect(_page1).toBeTruthy();
  599. expect(_page2).toBeTruthy();
  600. expect(_pageN).toBeTruthy();
  601. expect(_pageN.parent).toStrictEqual(_page2._id);
  602. expect(_pageT.descendantCount).toStrictEqual(1);
  603. });
  604. });
  605. });
  606. describe('update', () => {
  607. const updatePage = async(page, newRevisionBody, oldRevisionBody, user, options = {}) => {
  608. const mockedRenameSubOperation = jest.spyOn(Page, 'emitPageEventUpdate').mockReturnValue(null);
  609. const savedPage = await Page.updatePage(page, newRevisionBody, oldRevisionBody, user, options);
  610. mockedRenameSubOperation.mockRestore();
  611. return savedPage;
  612. };
  613. describe('Changing grant from PUBLIC to RESTRICTED of', () => {
  614. test('an only-child page will delete its empty parent page', async() => {
  615. const pathT = '/mup13_top';
  616. const path1 = '/mup13_top/mup1_emp';
  617. const path2 = '/mup13_top/mup1_emp/mup2_pub';
  618. const pageT = await Page.findOne({ path: pathT, descendantCount: 2 });
  619. const page1 = await Page.findOne({ path: path1, isEmpty: true });
  620. const page2 = await Page.findOne({ path: path2, grant: Page.GRANT_PUBLIC });
  621. expect(pageT).toBeTruthy();
  622. expect(page1).toBeTruthy();
  623. expect(page2).toBeTruthy();
  624. const options = { grant: Page.GRANT_RESTRICTED, grantUserGroupId: null };
  625. await Page.updatePage(page2, 'newRevisionBody', 'oldRevisionBody', dummyUser1, options);
  626. const _pageT = await Page.findOne({ path: pathT });
  627. const _page1 = await Page.findOne({ path: path1 });
  628. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_RESTRICTED });
  629. expect(_pageT).toBeTruthy();
  630. expect(_page1).toBeNull();
  631. expect(_page2).toBeTruthy();
  632. expect(_pageT.descendantCount).toBe(1);
  633. });
  634. test('a page that has children will create an empty page with the same path and it becomes a new parent', async() => {
  635. const pathT = '/mup14_top';
  636. const path1 = '/mup14_top/mup6_pub';
  637. const path2 = '/mup14_top/mup6_pub/mup7_pub';
  638. const top = await Page.findOne({ path: pathT, descendantCount: 2 });
  639. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  640. const page2 = await Page.findOne({ path: path2, grant: Page.GRANT_PUBLIC });
  641. expect(top).toBeTruthy();
  642. expect(page1).toBeTruthy();
  643. expect(page2).toBeTruthy();
  644. await Page.updatePage(page1, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  645. const _top = await Page.findOne({ path: pathT });
  646. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  647. const _page2 = await Page.findOne({ path: path2 });
  648. const _pageN = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  649. expect(_page1).toBeTruthy();
  650. expect(_page2).toBeTruthy();
  651. expect(_pageN).toBeTruthy();
  652. expect(_page1.parent).toBeNull();
  653. expect(_page2.parent).toStrictEqual(_pageN._id);
  654. expect(_pageN.parent).toStrictEqual(top._id);
  655. expect(_pageN.isEmpty).toBe(true);
  656. expect(_pageN.descendantCount).toBe(1);
  657. expect(_top.descendantCount).toBe(1);
  658. });
  659. test('of a leaf page will NOT have an empty page with the same path', async() => {
  660. const pathT = '/mup15_top';
  661. const path1 = '/mup15_top/mup8_pub';
  662. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  663. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  664. const count = await Page.count({ path: path1 });
  665. expect(pageT).toBeTruthy();
  666. expect(page1).toBeTruthy();
  667. expect(count).toBe(1);
  668. await Page.updatePage(page1, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  669. const _pageT = await Page.findOne({ path: pathT });
  670. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  671. const _pageNotExist = await Page.findOne({ path: path1, isEmpty: true });
  672. expect(_pageT).toBeTruthy();
  673. expect(_page1).toBeTruthy();
  674. expect(_pageNotExist).toBeNull();
  675. expect(_pageT.descendantCount).toBe(0);
  676. });
  677. });
  678. describe('Changing grant from RESTRICTED to PUBLIC of', () => {
  679. test('a page will create ancestors if they do not exist', async() => {
  680. const pathT = '/mup16_top';
  681. const path1 = '/mup16_top/mup9_pub';
  682. const path2 = '/mup16_top/mup9_pub/mup10_pub';
  683. const path3 = '/mup16_top/mup9_pub/mup10_pub/mup11_awl';
  684. const top = await Page.findOne({ path: pathT });
  685. const page1 = await Page.findOne({ path: path1 });
  686. const page2 = await Page.findOne({ path: path2 });
  687. const page3 = await Page.findOne({ path: path3, grant: Page.GRANT_RESTRICTED });
  688. expect(top).toBeTruthy();
  689. expect(page3).toBeTruthy();
  690. expect(page1).toBeNull();
  691. expect(page2).toBeNull();
  692. await Page.updatePage(page3, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_PUBLIC });
  693. const _pageT = await Page.findOne({ path: pathT });
  694. const _page1 = await Page.findOne({ path: path1, isEmpty: true });
  695. const _page2 = await Page.findOne({ path: path2, isEmpty: true });
  696. const _page3 = await Page.findOne({ path: path3, grant: Page.GRANT_PUBLIC });
  697. expect(_page1).toBeTruthy();
  698. expect(_page2).toBeTruthy();
  699. expect(_page3).toBeTruthy();
  700. expect(_page1.parent).toStrictEqual(top._id);
  701. expect(_page2.parent).toStrictEqual(_page1._id);
  702. expect(_page3.parent).toStrictEqual(_page2._id);
  703. expect(_pageT.descendantCount).toBe(1);
  704. });
  705. test('a page will replace an empty page with the same path if any', async() => {
  706. const pathT = '/mup17_top';
  707. const path1 = '/mup17_top/mup12_emp';
  708. const path2 = '/mup17_top/mup12_emp/mup18_pub';
  709. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  710. const page1 = await Page.findOne({ path: path1, isEmpty: true });
  711. const page2 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED, isEmpty: false });
  712. const page3 = await Page.findOne({ path: path2 });
  713. expect(pageT).toBeTruthy();
  714. expect(page1).toBeTruthy();
  715. expect(page2).toBeTruthy();
  716. expect(page3).toBeTruthy();
  717. await Page.updatePage(page2, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_PUBLIC });
  718. const _pageT = await Page.findOne({ path: pathT });
  719. const _page1 = await Page.findOne({ path: path1, isEmpty: true }); // should be replaced
  720. const _page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  721. const _page3 = await Page.findOne({ path: path2 });
  722. expect(_pageT).toBeTruthy();
  723. expect(_page1).toBeNull();
  724. expect(_page2).toBeTruthy();
  725. expect(_page3).toBeTruthy();
  726. expect(_page2.grant).toBe(Page.GRANT_PUBLIC);
  727. expect(_page2.parent).toStrictEqual(_pageT._id);
  728. expect(_page3.parent).toStrictEqual(_page2._id);
  729. expect(_pageT.descendantCount).toBe(2);
  730. });
  731. });
  732. describe('Changing grant to GRANT_OWNER(onlyme)', () => {
  733. test('successfully change to GRANT_OWNER from GRANT_PUBLIC', async() => {
  734. const path = '/mup19';
  735. const _page = await Page.findOne({ path, grant: Page.GRANT_PUBLIC });
  736. expect(_page).toBeTruthy();
  737. await updatePage(_page, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_OWNER });
  738. const page = await Page.findOne({ path });
  739. expect(page.grant).toBe(Page.GRANT_OWNER);
  740. });
  741. test('successfully change to GRANT_OWNER from GRANT_USER_GROUP', async() => {
  742. const path = '/mup20';
  743. const _page = await Page.findOne({ path, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdA });
  744. expect(_page).toBeTruthy();
  745. await updatePage(_page, 'newRevisionBody', 'oldRevisionBody', pModelUser1, { grant: Page.GRANT_OWNER });
  746. const page = await Page.findOne({ path });
  747. expect(page.grant).toBe(Page.GRANT_OWNER);
  748. });
  749. test('successfully change to GRANT_OWNER from GRANT_RESTRICTED', async() => {
  750. const path = '/mup21';
  751. const _page = await Page.findOne({ path, grant: Page.GRANT_RESTRICTED });
  752. expect(_page).toBeTruthy();
  753. await updatePage(_page, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_OWNER });
  754. const page = await Page.findOne({ path });
  755. expect(page.grant).toBe(Page.GRANT_OWNER);
  756. });
  757. test('Failed to change to GRANT_OWNER if one of the ancestors is GRANT_USER_GROUP page', async() => {
  758. const path1 = '/mup22';
  759. const path2 = '/mup22/mup23';
  760. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  761. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdA });
  762. expect(_page1).toBeTruthy();
  763. expect(_page2).toBeTruthy();
  764. let isThrown;
  765. try {
  766. await updatePage(_page1, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_OWNER });
  767. }
  768. catch (err) {
  769. isThrown = true;
  770. }
  771. expect(isThrown).toBe(true);
  772. const page1 = await Page.findOne({ path1 });
  773. const page2 = await Page.findOne({ path2 });
  774. });
  775. });
  776. describe('Changing grant to GRANT_USER_GROUP', () => {
  777. describe('update grant of a page under a page with GRANT_PUBLIC', () => {
  778. test('successfully change to GRANT_USER_GROUP from GRANT_PUBLIC if parent page is GRANT_PUBLIC', async() => {
  779. const path1 = '/mup24_pub';
  780. const path2 = '/mup24_pub/mup25_pub';
  781. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  782. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_PUBLIC });
  783. const options = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdA };
  784. expect(_page1).toBeTruthy();
  785. expect(_page2).toBeTruthy();
  786. const updatedPage = await updatePage(_page2, 'new', 'old', pModelUser1, options);
  787. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  788. const page2 = await Page.findOne({ path: path2 });
  789. expect(page1).toBeTruthy();
  790. expect(page2).toBeTruthy();
  791. expect(updatedPage).toBeTruthy();
  792. expect(updatedPage._id).toStrictEqual(page2._id);
  793. expect(page2.grant).toBe(Page.GRANT_USER_GROUP);
  794. expect(page2.grantedGroup._id).toStrictEqual(groupIdA);
  795. });
  796. test('successfully change to GRANT_USER_GROUP from GRANT_RESTRICTED if parent page is GRANT_PUBLIC', async() => {
  797. const path1 = '/mup26_awl';
  798. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  799. const options = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdA };
  800. expect(_page1).toBeTruthy();
  801. const updatedPage = await updatePage(_page1, 'new', 'old', pModelUser1, options);
  802. const page1 = await Page.findOne({ path: path1 });
  803. expect(page1).toBeTruthy();
  804. expect(updatedPage).toBeTruthy();
  805. expect(updatedPage._id).toStrictEqual(page1._id);
  806. expect(page1.grant).toBe(Page.GRANT_USER_GROUP);
  807. expect(page1.grantedGroup._id).toStrictEqual(groupIdA);
  808. expect(page1.parent).toStrictEqual(rootPage._id);
  809. });
  810. test('successfully change to GRANT_USER_GROUP from GRANT_OWNER if parent page is GRANT_PUBLIC', async() => {
  811. const path1 = '/mup27_pub';
  812. const path2 = '/mup27_pub/mup28_owner';
  813. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  814. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser1] });
  815. const options = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdA };
  816. expect(_page1).toBeTruthy();
  817. expect(_page2).toBeTruthy();
  818. const updatedPage = await updatePage(_page2, 'new', 'old', pModelUser1, options);
  819. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  820. const page2 = await Page.findOne({ path: path2 });
  821. expect(page1).toBeTruthy();
  822. expect(page2).toBeTruthy();
  823. expect(updatedPage).toBeTruthy();
  824. expect(updatedPage._id).toStrictEqual(page2._id);
  825. expect(page2.grant).toBe(Page.GRANT_USER_GROUP);
  826. expect(page2.grantedGroup._id).toStrictEqual(groupIdA);
  827. expect(page2.grantedUsers.length).toBe(0);
  828. });
  829. });
  830. describe('update grant of a page under a page with GRANT_USER_GROUP', () => {
  831. test('successfully change to GRANT_USER_GROUP if the group to set is the child or descendant of the parent page group', async() => {
  832. const path1 = '/mup29_A';
  833. const path2 = '/mup29_A/mup30_owner';
  834. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdA });
  835. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser1] });
  836. const options = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdB }; // change to groupB
  837. expect(_page1).toBeTruthy();
  838. expect(_page2).toBeTruthy();
  839. const updatedPage = await updatePage(_page2, 'new', 'old', pModelUser3, options);
  840. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdA });
  841. const page2 = await Page.findOne({ path: path2 });
  842. expect(page1).toBeTruthy();
  843. expect(page2).toBeTruthy();
  844. expect(updatedPage).toBeTruthy();
  845. expect(updatedPage._id).toStrictEqual(page2._id);
  846. expect(page2.grant).toBe(Page.GRANT_USER_GROUP);
  847. expect(page2.grantedGroup._id).toStrictEqual(groupIdB);
  848. expect(page2.grantedUsers.length).toBe(0);
  849. // second round to change to grandchild group
  850. const secondTimesOptions = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdC }; // change to groupC
  851. const secondTimesUpdatedPage = await updatePage(_page2, 'new', 'old', pModelUser3, secondTimesOptions);
  852. expect(secondTimesUpdatedPage).toBeTruthy();
  853. expect(secondTimesUpdatedPage.grant).toBe(Page.GRANT_USER_GROUP);
  854. expect(secondTimesUpdatedPage.grantedGroup._id).toStrictEqual(groupIdC);
  855. });
  856. test('Fail to change to GRANT_USER_GROUP if the group to set is NOT the child or descendant of the parent page group', async() => {
  857. const path1 = '/mup31_A';
  858. const path2 = '/mup31_A/mup32_owner';
  859. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdA });
  860. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser1] });
  861. const options = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdIsolate }; // change to isolated group
  862. expect(_page1).toBeTruthy();
  863. expect(_page2).toBeTruthy();
  864. await expect(updatePage(_page2, 'new', 'old', pModelUser1, options))
  865. .rejects.toThrow(new Error('The selected grant or grantedGroup is not assignable to this page.'));
  866. const page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser1] });
  867. const pageN = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedGroup: groupIdIsolate }); // not exist
  868. expect(page2).toBeTruthy();
  869. expect(pageN).toBeNull();
  870. expect(page2.grant).toBe(Page.GRANT_OWNER);
  871. expect(page2.grantedUsers).toStrictEqual([pModelUser1._id]);
  872. });
  873. test('Fail to change to GRANT_USER_GROUP if the group to set is an ancestor of the parent page group', async() => {
  874. const path1 = '/mup33_C';
  875. const path2 = '/mup33_C/mup34_owner';
  876. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdC }); // groupC
  877. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser3] });
  878. const options = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdA }; // change to groupA which is the grandParent
  879. expect(_page1).toBeTruthy();
  880. expect(_page2).toBeTruthy();
  881. await expect(updatePage(_page2, 'new', 'old', pModelUser3, options))
  882. .rejects.toThrow(new Error('The selected grant or grantedGroup is not assignable to this page.'));
  883. const page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser3] });
  884. const pageN = await Page.findOne({ path: path2, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdC }); // not exist
  885. expect(page2).toBeTruthy();
  886. expect(pageN).toBeNull();
  887. expect(page2.grant).toBe(Page.GRANT_OWNER);
  888. expect(page2.grantedUsers).toStrictEqual([pModelUser3._id]);
  889. });
  890. });
  891. describe('update grant of a page under a page with GRANT_OWNER', () => {
  892. test('Fail to change from GRNAT_OWNER', async() => {
  893. const path1 = '/mup35_owner';
  894. const path2 = '/mup35_owner/mup36_owner';
  895. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser1] });
  896. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser1] });
  897. const options = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdA }; // change to groupA
  898. expect(_page1).toBeTruthy();
  899. expect(_page2).toBeTruthy();
  900. await expect(updatePage(_page2, 'new', 'old', pModelUser1, options))
  901. .rejects.toThrow(new Error('The selected grant or grantedGroup is not assignable to this page.'));
  902. const page2 = await Page.findOne({ path: path2 });
  903. const pageN = await Page.findOne({ path: path2, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdA }); // not exist
  904. expect(page2).toBeTruthy();
  905. expect(pageN).toBeNull();
  906. expect(page2.grant).toBe(Page.GRANT_OWNER);
  907. expect(page2.grantedUsers).toStrictEqual([pModelUser1._id]);
  908. });
  909. });
  910. });
  911. });
  912. });