v5.page.test.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. let isThrown;
  865. try {
  866. await updatePage(_page2, 'new', 'old', pModelUser3, options);
  867. }
  868. catch (err) {
  869. isThrown = true;
  870. }
  871. const page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser1] });
  872. const pageN = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedGroup: groupIdIsolate }); // not exist
  873. expect(isThrown).toBe(true);
  874. expect(page2).toBeTruthy();
  875. expect(pageN).toBeNull();
  876. expect(page2.grant).toBe(Page.GRANT_OWNER);
  877. expect(page2.grantedUsers).toStrictEqual([pModelUser1._id]);
  878. });
  879. test('Fail to change to GRANT_USER_GROUP if the group to set is an ancestor of the parent page group', async() => {
  880. const path1 = '/mup33_C';
  881. const path2 = '/mup33_C/mup34_owner';
  882. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdC }); // groupC
  883. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser3] });
  884. const options = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdA }; // change to groupA which is the grandParent
  885. expect(_page1).toBeTruthy();
  886. expect(_page2).toBeTruthy();
  887. let isThrown;
  888. try {
  889. await updatePage(_page2, 'new', 'old', pModelUser3, options);
  890. }
  891. catch (err) {
  892. isThrown = true;
  893. }
  894. const page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser3] });
  895. const pageN = await Page.findOne({ path: path2, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdC }); // not exist
  896. expect(isThrown).toBe(true);
  897. expect(page2).toBeTruthy();
  898. expect(pageN).toBeNull();
  899. expect(page2.grant).toBe(Page.GRANT_OWNER);
  900. expect(page2.grantedUsers).toStrictEqual([pModelUser3._id]);
  901. });
  902. });
  903. describe('update grant of a page under a page with GRANT_OWNER', () => {
  904. test('Fail to change from GRNAT_OWNER', async() => {
  905. const path1 = '/mup35_owner';
  906. const path2 = '/mup35_owner/mup36_owner';
  907. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser1] });
  908. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_OWNER, grantedUsers: [pModelUser1] });
  909. const options = { grant: Page.GRANT_USER_GROUP, grantUserGroupId: groupIdA }; // change to groupA
  910. expect(_page1).toBeTruthy();
  911. expect(_page2).toBeTruthy();
  912. let isThrown;
  913. try {
  914. await updatePage(_page2, 'new', 'old', pModelUser1, options);
  915. }
  916. catch (err) {
  917. isThrown = true;
  918. }
  919. const page2 = await Page.findOne({ path: path2 });
  920. const pageN = await Page.findOne({ path: path2, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdA }); // not exist
  921. expect(isThrown).toBe(true);
  922. expect(page2).toBeTruthy();
  923. expect(pageN).toBeNull();
  924. expect(page2.grant).toBe(Page.GRANT_OWNER);
  925. expect(page2.grantedUsers).toStrictEqual([pModelUser1._id]);
  926. });
  927. });
  928. });
  929. });
  930. });