v5.page.test.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. await Page.insertMany([
  218. {
  219. _id: pageIdUpd1,
  220. path: '/mup13_top/mup1_emp',
  221. grant: Page.GRANT_PUBLIC,
  222. parent: pageIdUpd8._id,
  223. isEmpty: true,
  224. },
  225. {
  226. _id: pageIdUpd2,
  227. path: '/mup13_top/mup1_emp/mup2_pub',
  228. grant: Page.GRANT_PUBLIC,
  229. parent: pageIdUpd1._id,
  230. creator: dummyUser1,
  231. lastUpdateUser: dummyUser1._id,
  232. isEmpty: false,
  233. },
  234. {
  235. _id: pageIdUpd3,
  236. path: '/mup14_top/mup6_pub',
  237. grant: Page.GRANT_PUBLIC,
  238. creator: dummyUser1,
  239. lastUpdateUser: dummyUser1._id,
  240. parent: pageIdUpd9,
  241. isEmpty: false,
  242. descendantCount: 1,
  243. },
  244. {
  245. path: '/mup14_top/mup6_pub/mup7_pub',
  246. grant: Page.GRANT_PUBLIC,
  247. creator: dummyUser1,
  248. lastUpdateUser: dummyUser1._id,
  249. parent: pageIdUpd3,
  250. isEmpty: false,
  251. descendantCount: 0,
  252. },
  253. {
  254. _id: pageIdUpd4,
  255. path: '/mup15_top/mup8_pub',
  256. grant: Page.GRANT_PUBLIC,
  257. creator: dummyUser1,
  258. lastUpdateUser: dummyUser1._id,
  259. parent: pageIdUpd10._id,
  260. isEmpty: false,
  261. },
  262. {
  263. _id: pageIdUpd5,
  264. path: '/mup16_top/mup9_pub/mup10_pub/mup11_awl',
  265. grant: Page.GRANT_RESTRICTED,
  266. creator: dummyUser1,
  267. lastUpdateUser: dummyUser1._id,
  268. isEmpty: false,
  269. },
  270. {
  271. _id: pageIdUpd6,
  272. path: '/mup17_top/mup12_emp',
  273. isEmpty: true,
  274. parent: pageIdUpd12._id,
  275. descendantCount: 1,
  276. },
  277. {
  278. _id: pageIdUpd7,
  279. path: '/mup17_top/mup12_emp',
  280. grant: Page.GRANT_RESTRICTED,
  281. creator: dummyUser1,
  282. lastUpdateUser: dummyUser1._id,
  283. isEmpty: false,
  284. },
  285. {
  286. path: '/mup17_top/mup12_emp/mup18_pub',
  287. isEmpty: false,
  288. creator: dummyUser1,
  289. lastUpdateUser: dummyUser1._id,
  290. parent: pageIdUpd6._id,
  291. },
  292. {
  293. _id: pageIdUpd8,
  294. path: '/mup13_top',
  295. grant: Page.GRANT_PUBLIC,
  296. creator: dummyUser1,
  297. lastUpdateUser: dummyUser1._id,
  298. isEmpty: false,
  299. parent: rootPage._id,
  300. descendantCount: 2,
  301. },
  302. {
  303. _id: pageIdUpd9,
  304. path: '/mup14_top',
  305. grant: Page.GRANT_PUBLIC,
  306. creator: dummyUser1,
  307. lastUpdateUser: dummyUser1._id,
  308. isEmpty: false,
  309. parent: rootPage._id,
  310. descendantCount: 2,
  311. },
  312. {
  313. _id: pageIdUpd10,
  314. path: '/mup15_top',
  315. grant: Page.GRANT_PUBLIC,
  316. creator: dummyUser1,
  317. lastUpdateUser: dummyUser1._id,
  318. isEmpty: false,
  319. parent: rootPage._id,
  320. descendantCount: 1,
  321. },
  322. {
  323. _id: pageIdUpd11,
  324. path: '/mup16_top',
  325. grant: Page.GRANT_PUBLIC,
  326. creator: dummyUser1,
  327. lastUpdateUser: dummyUser1._id,
  328. isEmpty: false,
  329. parent: rootPage._id,
  330. descendantCount: 0,
  331. },
  332. {
  333. _id: pageIdUpd12,
  334. path: '/mup17_top',
  335. grant: Page.GRANT_PUBLIC,
  336. creator: dummyUser1,
  337. lastUpdateUser: dummyUser1._id,
  338. isEmpty: false,
  339. parent: rootPage._id,
  340. descendantCount: 1,
  341. },
  342. {
  343. path: '/mup19',
  344. grant: Page.GRANT_PUBLIC,
  345. creator: dummyUser1,
  346. lastUpdateUser: dummyUser1._id,
  347. isEmpty: false,
  348. parent: rootPage._id,
  349. descendantCount: 0,
  350. },
  351. {
  352. path: '/mup20',
  353. grant: Page.GRANT_USER_GROUP,
  354. grantedGroup: groupIdA,
  355. creator: pModelUserId1,
  356. lastUpdateUser: pModelUserId1,
  357. isEmpty: false,
  358. parent: rootPage._id,
  359. descendantCount: 0,
  360. },
  361. {
  362. path: '/mup21',
  363. grant: Page.GRANT_RESTRICTED,
  364. creator: dummyUser1,
  365. lastUpdateUser: dummyUser1._id,
  366. isEmpty: false,
  367. descendantCount: 0,
  368. },
  369. {
  370. _id: pageIdUpd13,
  371. path: '/mup22',
  372. grant: Page.GRANT_PUBLIC,
  373. creator: pModelUser1,
  374. lastUpdateUser: pModelUser1._id,
  375. isEmpty: false,
  376. parent: rootPage._id,
  377. descendantCount: 1,
  378. },
  379. {
  380. path: '/mup22/mup23',
  381. grant: Page.GRANT_USER_GROUP,
  382. grantedGroup: groupIdA,
  383. creator: pModelUserId1,
  384. lastUpdateUser: pModelUserId1,
  385. isEmpty: false,
  386. parent: pageIdUpd13,
  387. descendantCount: 0,
  388. },
  389. ]);
  390. });
  391. describe('create', () => {
  392. test('Should create single page', async() => {
  393. const page = await Page.create('/v5_create1', 'create1', dummyUser1, {});
  394. expect(page).toBeTruthy();
  395. expect(page.parent).toStrictEqual(rootPage._id);
  396. });
  397. test('Should create empty-child and non-empty grandchild', async() => {
  398. const grandchildPage = await Page.create('/v5_empty_create2/v5_create_3', 'grandchild', dummyUser1, {});
  399. const childPage = await Page.findOne({ path: '/v5_empty_create2' });
  400. expect(childPage.isEmpty).toBe(true);
  401. expect(grandchildPage).toBeTruthy();
  402. expect(childPage).toBeTruthy();
  403. expect(childPage.parent).toStrictEqual(rootPage._id);
  404. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  405. });
  406. test('Should create on empty page', async() => {
  407. const beforeCreatePage = await Page.findOne({ path: '/v5_empty_create_4' });
  408. expect(beforeCreatePage.isEmpty).toBe(true);
  409. const childPage = await Page.create('/v5_empty_create_4', 'body', dummyUser1, {});
  410. const grandchildPage = await Page.findOne({ parent: childPage._id });
  411. expect(childPage).toBeTruthy();
  412. expect(childPage.isEmpty).toBe(false);
  413. expect(childPage.revision.body).toBe('body');
  414. expect(grandchildPage).toBeTruthy();
  415. expect(childPage.parent).toStrictEqual(rootPage._id);
  416. expect(grandchildPage.parent).toStrictEqual(childPage._id);
  417. });
  418. describe('Creating a page using existing path', () => {
  419. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  420. const pathT = '/mc4_top';
  421. const path1 = '/mc4_top/mc1_emp';
  422. const path2 = '/mc4_top/mc1_emp/mc2_pub';
  423. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  424. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  425. const page2 = await Page.findOne({ path: path2 });
  426. const page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  427. expect(pageT).toBeTruthy();
  428. expect(page1).toBeTruthy();
  429. expect(page2).toBeTruthy();
  430. expect(page3).toBeNull();
  431. // use existing path
  432. await Page.create(path1, 'new body', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  433. const _pageT = await Page.findOne({ path: pathT });
  434. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  435. const _page2 = await Page.findOne({ path: path2 });
  436. const _page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  437. expect(_pageT).toBeTruthy();
  438. expect(_page1).toBeTruthy();
  439. expect(_page2).toBeTruthy();
  440. expect(_page3).toBeTruthy();
  441. expect(_pageT.descendantCount).toBe(1);
  442. });
  443. });
  444. describe('Creating a page under a page with grant RESTRICTED', () => {
  445. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
  446. const pathT = '/mc5_top';
  447. const path1 = '/mc5_top/mc3_awl';
  448. const pathN = '/mc5_top/mc3_awl/mc4_pub'; // used to create
  449. const pageT = await Page.findOne({ path: pathT });
  450. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  451. const page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  452. expect(pageT).toBeTruthy();
  453. expect(page1).toBeTruthy();
  454. expect(page2).toBeNull();
  455. await Page.create(pathN, 'new body', dummyUser1, { grant: Page.GRANT_PUBLIC });
  456. const _pageT = await Page.findOne({ path: pathT });
  457. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  458. const _page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC, isEmpty: true });
  459. const _pageN = await Page.findOne({ path: pathN, grant: Page.GRANT_PUBLIC }); // newly crated
  460. expect(_pageT).toBeTruthy();
  461. expect(_page1).toBeTruthy();
  462. expect(_page2).toBeTruthy();
  463. expect(_pageN).toBeTruthy();
  464. expect(_pageN.parent).toStrictEqual(_page2._id);
  465. expect(_pageT.descendantCount).toStrictEqual(1);
  466. });
  467. });
  468. });
  469. describe('update', () => {
  470. const updatePage = async(page, newRevisionBody, oldRevisionBody, user, options = {}) => {
  471. const mockedRenameSubOperation = jest.spyOn(Page, 'emitPageEventUpdate').mockReturnValue(null);
  472. const savedPage = await Page.updatePage(page, newRevisionBody, oldRevisionBody, user, options);
  473. mockedRenameSubOperation.mockRestore();
  474. return savedPage;
  475. };
  476. describe('Changing grant from PUBLIC to RESTRICTED of', () => {
  477. test('an only-child page will delete its empty parent page', async() => {
  478. const pathT = '/mup13_top';
  479. const path1 = '/mup13_top/mup1_emp';
  480. const path2 = '/mup13_top/mup1_emp/mup2_pub';
  481. const pageT = await Page.findOne({ path: pathT, descendantCount: 2 });
  482. const page1 = await Page.findOne({ path: path1, isEmpty: true });
  483. const page2 = await Page.findOne({ path: path2, grant: Page.GRANT_PUBLIC });
  484. expect(pageT).toBeTruthy();
  485. expect(page1).toBeTruthy();
  486. expect(page2).toBeTruthy();
  487. const options = { grant: Page.GRANT_RESTRICTED, grantUserGroupId: null };
  488. await Page.updatePage(page2, 'newRevisionBody', 'oldRevisionBody', dummyUser1, options);
  489. const _pageT = await Page.findOne({ path: pathT });
  490. const _page1 = await Page.findOne({ path: path1 });
  491. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_RESTRICTED });
  492. expect(_pageT).toBeTruthy();
  493. expect(_page1).toBeNull();
  494. expect(_page2).toBeTruthy();
  495. expect(_pageT.descendantCount).toBe(1);
  496. });
  497. test('a page that has children will create an empty page with the same path and it becomes a new parent', async() => {
  498. const pathT = '/mup14_top';
  499. const path1 = '/mup14_top/mup6_pub';
  500. const path2 = '/mup14_top/mup6_pub/mup7_pub';
  501. const top = await Page.findOne({ path: pathT, descendantCount: 2 });
  502. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  503. const page2 = await Page.findOne({ path: path2, grant: Page.GRANT_PUBLIC });
  504. expect(top).toBeTruthy();
  505. expect(page1).toBeTruthy();
  506. expect(page2).toBeTruthy();
  507. await Page.updatePage(page1, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  508. const _top = await Page.findOne({ path: pathT });
  509. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  510. const _page2 = await Page.findOne({ path: path2 });
  511. const _pageN = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  512. expect(_page1).toBeTruthy();
  513. expect(_page2).toBeTruthy();
  514. expect(_pageN).toBeTruthy();
  515. expect(_page1.parent).toBeNull();
  516. expect(_page2.parent).toStrictEqual(_pageN._id);
  517. expect(_pageN.parent).toStrictEqual(top._id);
  518. expect(_pageN.isEmpty).toBe(true);
  519. expect(_pageN.descendantCount).toBe(1);
  520. expect(_top.descendantCount).toBe(1);
  521. });
  522. test('of a leaf page will NOT have an empty page with the same path', async() => {
  523. const pathT = '/mup15_top';
  524. const path1 = '/mup15_top/mup8_pub';
  525. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  526. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  527. const count = await Page.count({ path: path1 });
  528. expect(pageT).toBeTruthy();
  529. expect(page1).toBeTruthy();
  530. expect(count).toBe(1);
  531. await Page.updatePage(page1, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  532. const _pageT = await Page.findOne({ path: pathT });
  533. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  534. const _pageNotExist = await Page.findOne({ path: path1, isEmpty: true });
  535. expect(_pageT).toBeTruthy();
  536. expect(_page1).toBeTruthy();
  537. expect(_pageNotExist).toBeNull();
  538. expect(_pageT.descendantCount).toBe(0);
  539. });
  540. });
  541. describe('Changing grant from RESTRICTED to PUBLIC of', () => {
  542. test('a page will create ancestors if they do not exist', async() => {
  543. const pathT = '/mup16_top';
  544. const path1 = '/mup16_top/mup9_pub';
  545. const path2 = '/mup16_top/mup9_pub/mup10_pub';
  546. const path3 = '/mup16_top/mup9_pub/mup10_pub/mup11_awl';
  547. const top = await Page.findOne({ path: pathT });
  548. const page1 = await Page.findOne({ path: path1 });
  549. const page2 = await Page.findOne({ path: path2 });
  550. const page3 = await Page.findOne({ path: path3, grant: Page.GRANT_RESTRICTED });
  551. expect(top).toBeTruthy();
  552. expect(page3).toBeTruthy();
  553. expect(page1).toBeNull();
  554. expect(page2).toBeNull();
  555. await Page.updatePage(page3, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_PUBLIC });
  556. const _pageT = await Page.findOne({ path: pathT });
  557. const _page1 = await Page.findOne({ path: path1, isEmpty: true });
  558. const _page2 = await Page.findOne({ path: path2, isEmpty: true });
  559. const _page3 = await Page.findOne({ path: path3, grant: Page.GRANT_PUBLIC });
  560. expect(_page1).toBeTruthy();
  561. expect(_page2).toBeTruthy();
  562. expect(_page3).toBeTruthy();
  563. expect(_page1.parent).toStrictEqual(top._id);
  564. expect(_page2.parent).toStrictEqual(_page1._id);
  565. expect(_page3.parent).toStrictEqual(_page2._id);
  566. expect(_pageT.descendantCount).toBe(1);
  567. });
  568. test('a page will replace an empty page with the same path if any', async() => {
  569. const pathT = '/mup17_top';
  570. const path1 = '/mup17_top/mup12_emp';
  571. const path2 = '/mup17_top/mup12_emp/mup18_pub';
  572. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  573. const page1 = await Page.findOne({ path: path1, isEmpty: true });
  574. const page2 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED, isEmpty: false });
  575. const page3 = await Page.findOne({ path: path2 });
  576. expect(pageT).toBeTruthy();
  577. expect(page1).toBeTruthy();
  578. expect(page2).toBeTruthy();
  579. expect(page3).toBeTruthy();
  580. await Page.updatePage(page2, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_PUBLIC });
  581. const _pageT = await Page.findOne({ path: pathT });
  582. const _page1 = await Page.findOne({ path: path1, isEmpty: true }); // should be replaced
  583. const _page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  584. const _page3 = await Page.findOne({ path: path2 });
  585. expect(_pageT).toBeTruthy();
  586. expect(_page1).toBeNull();
  587. expect(_page2).toBeTruthy();
  588. expect(_page3).toBeTruthy();
  589. expect(_page2.grant).toBe(Page.GRANT_PUBLIC);
  590. expect(_page2.parent).toStrictEqual(_pageT._id);
  591. expect(_page3.parent).toStrictEqual(_page2._id);
  592. expect(_pageT.descendantCount).toBe(2);
  593. });
  594. });
  595. describe('Changing grant to GRANT_OWNER(onlyme)', () => {
  596. test('successfully change to GRANT_OWNER from GRANT_PUBLIC', async() => {
  597. const path = '/mup19';
  598. const _page = await Page.findOne({ path, grant: Page.GRANT_PUBLIC });
  599. expect(_page).toBeTruthy();
  600. await updatePage(_page, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_OWNER });
  601. const page = await Page.findOne({ path });
  602. expect(page.grant).toBe(Page.GRANT_OWNER);
  603. expect(page.grantedUsers).toStrictEqual([dummyUser1._id]);
  604. });
  605. test('successfully change to GRANT_OWNER from GRANT_USER_GROUP', async() => {
  606. const path = '/mup20';
  607. const _page = await Page.findOne({ path, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdA });
  608. expect(_page).toBeTruthy();
  609. await updatePage(_page, 'newRevisionBody', 'oldRevisionBody', pModelUser1, { grant: Page.GRANT_OWNER });
  610. const page = await Page.findOne({ path });
  611. expect(page.grant).toBe(Page.GRANT_OWNER);
  612. expect(page.grantedUsers).toStrictEqual([pModelUser1._id]);
  613. expect(page.grantedGroup).toBeNull();
  614. });
  615. test('successfully change to GRANT_OWNER from GRANT_RESTRICTED', async() => {
  616. const path = '/mup21';
  617. const _page = await Page.findOne({ path, grant: Page.GRANT_RESTRICTED });
  618. expect(_page).toBeTruthy();
  619. await updatePage(_page, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_OWNER });
  620. const page = await Page.findOne({ path });
  621. expect(page.grant).toBe(Page.GRANT_OWNER);
  622. expect(page.grantedUsers).toStrictEqual([dummyUser1._id]);
  623. });
  624. test('Failed to change to GRANT_OWNER if one of the ancestors is GRANT_USER_GROUP page', async() => {
  625. const path1 = '/mup22';
  626. const path2 = '/mup22/mup23';
  627. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  628. const _page2 = await Page.findOne({ path: path2, grant: Page.GRANT_USER_GROUP, grantedGroup: groupIdA });
  629. expect(_page1).toBeTruthy();
  630. expect(_page2).toBeTruthy();
  631. await expect(updatePage(_page1, 'newRevisionBody', 'oldRevisionBody', dummyUser1, { grant: Page.GRANT_OWNER }))
  632. .rejects.toThrow(new Error('The selected grant or grantedGroup is not assignable to this page.'));
  633. const page1 = await Page.findOne({ path1 });
  634. expect(page1).toBeTruthy();
  635. expect(page1.grant).toBe(Page.GRANT_PUBLIC);
  636. expect(page1.grantedUsers).not.toStrictEqual([dummyUser1._id]);
  637. });
  638. });
  639. });
  640. });