page-grant.test.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. import mongoose from 'mongoose';
  2. import { PageGrant } from '~/interfaces/page';
  3. import UserGroup from '~/server/models/user-group';
  4. import { getInstance } from '../setup-crowi';
  5. /*
  6. * There are 3 grant types to test.
  7. * GRANT_PUBLIC, GRANT_OWNER, GRANT_USER_GROUP
  8. */
  9. describe('PageGrantService', () => {
  10. /*
  11. * models
  12. */
  13. let User;
  14. let Page;
  15. let UserGroupRelation;
  16. /*
  17. * global instances
  18. */
  19. let crowi;
  20. let pageGrantService;
  21. let xssSpy;
  22. let user1;
  23. let user2;
  24. let groupParent;
  25. let groupChild;
  26. const userGroupIdParent = new mongoose.Types.ObjectId();
  27. let rootPage;
  28. let rootPublicPage;
  29. let rootOnlyMePage;
  30. let rootOnlyInsideTheGroup;
  31. let emptyPage1;
  32. let emptyPage2;
  33. let emptyPage3;
  34. const emptyPagePath1 = '/E1';
  35. const emptyPagePath2 = '/E2';
  36. const emptyPagePath3 = '/E3';
  37. let pageRootPublic;
  38. let pageRootGroupParent;
  39. const pageRootPublicPath = '/Public';
  40. const pageRootGroupParentPath = '/GroupParent';
  41. const v4PageRootOnlyMePagePath = '/v4OnlyMe';
  42. const v4PageRootAnyoneWithTheLinkPagePath = '/v4AnyoneWithTheLink';
  43. const v4PageRootOnlyInsideTheGroupPagePath = '/v4OnlyInsideTheGroup';
  44. const pagePublicOnlyMePath = `${pageRootPublicPath}/OnlyMe`;
  45. const pagePublicAnyoneWithTheLinkPath = `${pageRootPublicPath}/AnyoneWithTheLink`;
  46. const pagePublicOnlyInsideTheGroupPath = `${pageRootPublicPath}/OnlyInsideTheGroup`;
  47. const pageOnlyMePublicPath = `${v4PageRootOnlyMePagePath}/Public`;
  48. const pageOnlyMeAnyoneWithTheLinkPath = `${v4PageRootOnlyMePagePath}/AnyoneWithTheLink`;
  49. const pageOnlyMeOnlyInsideTheGroupPath = `${v4PageRootOnlyMePagePath}/OnlyInsideTheGroup`;
  50. const pageOnlyInsideTheGroupPublicPath = `${v4PageRootOnlyInsideTheGroupPagePath}/Public`;
  51. const pageOnlyInsideTheGroupOnlyMePath = `${v4PageRootOnlyInsideTheGroupPagePath}/OnlyMe`;
  52. const pageOnlyInsideTheGroupAnyoneWithTheLinkPath = `${v4PageRootOnlyInsideTheGroupPagePath}/AnyoneWithTheLink`;
  53. let pageE1Public;
  54. let pageE2User1;
  55. let pageE3GroupParent;
  56. let pageE3GroupChild;
  57. let pageE3User1;
  58. const pageE1PublicPath = '/E1/Public';
  59. const pageE2User1Path = '/E2/User1';
  60. const pageE3GroupParentPath = '/E3/GroupParent';
  61. const pageE3GroupChildPath = '/E3/GroupChild';
  62. const pageE3User1Path = '/E3/User1';
  63. const createDocumentsToTestIsGrantNormalized = async() => {
  64. // Users
  65. await User.insertMany([
  66. { name: 'User1', username: 'User1', email: 'user1@example.com' },
  67. { name: 'User2', username: 'User2', email: 'user2@example.com' },
  68. ]);
  69. user1 = await User.findOne({ username: 'User1' });
  70. user2 = await User.findOne({ username: 'User2' });
  71. await UserGroup.insertMany([
  72. {
  73. _id: userGroupIdParent,
  74. name: 'GroupParent',
  75. parent: null,
  76. },
  77. {
  78. name: 'GroupChild',
  79. parent: userGroupIdParent,
  80. },
  81. ]);
  82. groupParent = await UserGroup.findOne({ name: 'GroupParent' });
  83. groupChild = await UserGroup.findOne({ name: 'GroupChild' });
  84. // UserGroupRelations
  85. await UserGroupRelation.insertMany([
  86. {
  87. relatedGroup: groupParent._id,
  88. relatedUser: user1._id,
  89. },
  90. {
  91. relatedGroup: groupParent._id,
  92. relatedUser: user2._id,
  93. },
  94. {
  95. relatedGroup: groupChild._id,
  96. relatedUser: user1._id,
  97. },
  98. ]);
  99. // Root page (Depth: 0)
  100. rootPage = await Page.findOne({ path: '/' });
  101. // Empty pages (Depth: 1)
  102. await Page.insertMany([
  103. {
  104. path: emptyPagePath1,
  105. grant: Page.GRANT_PUBLIC,
  106. isEmpty: true,
  107. parent: rootPage._id,
  108. },
  109. {
  110. path: emptyPagePath2,
  111. grant: Page.GRANT_PUBLIC,
  112. isEmpty: true,
  113. parent: rootPage._id,
  114. },
  115. {
  116. path: emptyPagePath3,
  117. grant: Page.GRANT_PUBLIC,
  118. isEmpty: true,
  119. parent: rootPage._id,
  120. },
  121. {
  122. path: pageRootPublicPath,
  123. grant: Page.GRANT_PUBLIC,
  124. creator: user1,
  125. lastUpdateUser: user1,
  126. grantedUsers: null,
  127. grantedGroup: null,
  128. parent: rootPage._id,
  129. },
  130. {
  131. path: pageRootGroupParentPath,
  132. grant: Page.GRANT_USER_GROUP,
  133. creator: user1,
  134. lastUpdateUser: user1,
  135. grantedUsers: null,
  136. grantedGroup: groupParent._id,
  137. parent: rootPage._id,
  138. },
  139. ]);
  140. await Page.insertMany([
  141. // Root Page
  142. {
  143. path: rootPage,
  144. grant: Page.GRANT_PUBLIC,
  145. parent: null,
  146. },
  147. // OnlyMe v4
  148. {
  149. path: v4PageRootOnlyMePagePath,
  150. grant: Page.GRANT_OWNER,
  151. grantedUsers: [user1._id],
  152. parent: null,
  153. },
  154. // AnyoneWithTheLink v4
  155. {
  156. path: v4PageRootAnyoneWithTheLinkPagePath,
  157. grant: Page.GRANT_RESTRICTED,
  158. parent: null,
  159. },
  160. // OnlyInsideTheGroup v4
  161. {
  162. path: v4PageRootOnlyInsideTheGroupPagePath,
  163. grant: Page.GRANT_USER_GROUP,
  164. parent: null,
  165. grantedGroup: groupParent._id,
  166. },
  167. ]);
  168. rootPublicPage = await Page.findOne({ path: pageRootPublicPath });
  169. rootOnlyMePage = await Page.findOne({ path: v4PageRootOnlyMePagePath });
  170. rootOnlyInsideTheGroup = await Page.findOne({ path: v4PageRootOnlyInsideTheGroupPagePath });
  171. // Leaf pages (Depth: 2)
  172. await Page.insertMany([
  173. /*
  174. * Parent is public
  175. */
  176. {
  177. path: pagePublicOnlyMePath,
  178. grant: Page.GRANT_OWNER,
  179. parent: rootPublicPage._id,
  180. },
  181. {
  182. path: pagePublicAnyoneWithTheLinkPath,
  183. grant: Page.GRANT_RESTRICTED,
  184. parent: rootPublicPage._id,
  185. },
  186. {
  187. path: pagePublicOnlyInsideTheGroupPath,
  188. grant: Page.GRANT_USER_GROUP,
  189. parent: rootPublicPage._id,
  190. },
  191. /*
  192. * Parent is onlyMe
  193. */
  194. {
  195. path: pageOnlyMePublicPath,
  196. grant: Page.GRANT_PUBLIC,
  197. parent: rootOnlyMePage._id,
  198. },
  199. {
  200. path: pageOnlyMeAnyoneWithTheLinkPath,
  201. grant: Page.GRANT_RESTRICTED,
  202. parent: rootOnlyMePage._id,
  203. },
  204. {
  205. path: pageOnlyMeOnlyInsideTheGroupPath,
  206. grant: Page.GRANT_USER_GROUP,
  207. parent: rootOnlyMePage._id,
  208. },
  209. /*
  210. * Parent is OnlyInsideTheGroup
  211. */
  212. {
  213. path: pageOnlyInsideTheGroupPublicPath,
  214. grant: Page.GRANT_PUBLIC,
  215. parent: rootOnlyInsideTheGroup._id,
  216. },
  217. {
  218. path: pageOnlyInsideTheGroupOnlyMePath,
  219. grant: Page.GRANT_PUBLIC,
  220. parent: rootOnlyInsideTheGroup._id,
  221. },
  222. {
  223. path: pageOnlyInsideTheGroupAnyoneWithTheLinkPath,
  224. grant: Page.GRANT_PUBLIC,
  225. parent: rootOnlyInsideTheGroup._id,
  226. },
  227. ]);
  228. emptyPage1 = await Page.findOne({ path: emptyPagePath1 });
  229. emptyPage2 = await Page.findOne({ path: emptyPagePath2 });
  230. emptyPage3 = await Page.findOne({ path: emptyPagePath3 });
  231. // Leaf pages (Depth: 2)
  232. await Page.insertMany([
  233. {
  234. path: pageE1PublicPath,
  235. grant: Page.GRANT_PUBLIC,
  236. creator: user1,
  237. lastUpdateUser: user1,
  238. grantedUsers: null,
  239. grantedGroup: null,
  240. parent: emptyPage1._id,
  241. },
  242. {
  243. path: pageE2User1Path,
  244. grant: Page.GRANT_OWNER,
  245. creator: user1,
  246. lastUpdateUser: user1,
  247. grantedUsers: [user1._id],
  248. grantedGroup: null,
  249. parent: emptyPage2._id,
  250. },
  251. {
  252. path: pageE3GroupParentPath,
  253. grant: Page.GRANT_USER_GROUP,
  254. creator: user1,
  255. lastUpdateUser: user1,
  256. grantedUsers: null,
  257. grantedGroup: groupParent._id,
  258. parent: emptyPage3._id,
  259. },
  260. {
  261. path: pageE3GroupChildPath,
  262. grant: Page.GRANT_USER_GROUP,
  263. creator: user1,
  264. lastUpdateUser: user1,
  265. grantedUsers: null,
  266. grantedGroup: groupChild._id,
  267. parent: emptyPage3._id,
  268. },
  269. {
  270. path: pageE3User1Path,
  271. grant: Page.GRANT_OWNER,
  272. creator: user1,
  273. lastUpdateUser: user1,
  274. grantedUsers: [user1._id],
  275. grantedGroup: null,
  276. parent: emptyPage3._id,
  277. },
  278. ]);
  279. pageE1Public = await Page.findOne({ path: pageE1PublicPath });
  280. pageE2User1 = await Page.findOne({ path: pageE2User1Path });
  281. pageE3GroupParent = await Page.findOne({ path: pageE3GroupParentPath });
  282. pageE3GroupChild = await Page.findOne({ path: pageE3GroupChildPath });
  283. pageE3User1 = await Page.findOne({ path: pageE3User1Path });
  284. };
  285. /*
  286. * prepare before all tests
  287. */
  288. beforeAll(async() => {
  289. crowi = await getInstance();
  290. pageGrantService = crowi.pageGrantService;
  291. User = mongoose.model('User');
  292. Page = mongoose.model('Page');
  293. UserGroupRelation = mongoose.model('UserGroupRelation');
  294. rootPage = await Page.findOne({ path: '/' });
  295. await createDocumentsToTestIsGrantNormalized();
  296. xssSpy = jest.spyOn(crowi.xss, 'process').mockImplementation(path => path);
  297. });
  298. describe('Test isGrantNormalized method with shouldCheckDescendants false', () => {
  299. test('Should return true when Ancestor: root, Target: public', async() => {
  300. const targetPath = '/NEW';
  301. const grant = Page.GRANT_PUBLIC;
  302. const grantedUserIds = null;
  303. const grantedGroupId = null;
  304. const shouldCheckDescendants = false;
  305. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  306. expect(result).toBe(true);
  307. });
  308. test('Should return true when Ancestor: root, Target: GroupParent', async() => {
  309. const targetPath = '/NEW_GroupParent';
  310. const grant = Page.GRANT_USER_GROUP;
  311. const grantedUserIds = null;
  312. const grantedGroupId = groupParent._id;
  313. const shouldCheckDescendants = false;
  314. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  315. expect(result).toBe(true);
  316. });
  317. test('Should return true when Ancestor: under-root public, Target: public', async() => {
  318. const targetPath = `${pageRootPublicPath}/NEW`;
  319. const grant = Page.GRANT_PUBLIC;
  320. const grantedUserIds = null;
  321. const grantedGroupId = null;
  322. const shouldCheckDescendants = false;
  323. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  324. expect(result).toBe(true);
  325. });
  326. test('Should return true when Ancestor: under-root GroupParent, Target: GroupParent', async() => {
  327. const targetPath = `${pageRootGroupParentPath}/NEW`;
  328. const grant = Page.GRANT_USER_GROUP;
  329. const grantedUserIds = null;
  330. const grantedGroupId = groupParent._id;
  331. const shouldCheckDescendants = false;
  332. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  333. expect(result).toBe(true);
  334. });
  335. test('Should return true when Ancestor: public, Target: public', async() => {
  336. const targetPath = `${pageE1PublicPath}/NEW`;
  337. const grant = Page.GRANT_PUBLIC;
  338. const grantedUserIds = null;
  339. const grantedGroupId = null;
  340. const shouldCheckDescendants = false;
  341. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  342. expect(result).toBe(true);
  343. });
  344. test('Should return true when Ancestor: owned by User1, Target: owned by User1', async() => {
  345. const targetPath = `${pageE2User1Path}/NEW`;
  346. const grant = Page.GRANT_OWNER;
  347. const grantedUserIds = [user1._id];
  348. const grantedGroupId = null;
  349. const shouldCheckDescendants = false;
  350. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  351. expect(result).toBe(true);
  352. });
  353. test('Should return false when Ancestor: owned by GroupParent, Target: public', async() => {
  354. const targetPath = `${pageE3GroupParentPath}/NEW`;
  355. const grant = Page.GRANT_PUBLIC;
  356. const grantedUserIds = null;
  357. const grantedGroupId = null;
  358. const shouldCheckDescendants = false;
  359. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  360. expect(result).toBe(false);
  361. });
  362. test('Should return false when Ancestor: owned by GroupChild, Target: GroupParent', async() => {
  363. const targetPath = `${pageE3GroupChildPath}/NEW`;
  364. const grant = Page.GRANT_USER_GROUP;
  365. const grantedUserIds = null;
  366. const grantedGroupId = groupParent._id;
  367. const shouldCheckDescendants = false;
  368. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  369. expect(result).toBe(false);
  370. });
  371. });
  372. describe('Test isGrantNormalized method with shouldCheckDescendants true', () => {
  373. test('Should return true when Target: public, Descendant: public', async() => {
  374. const targetPath = emptyPagePath1;
  375. const grant = Page.GRANT_PUBLIC;
  376. const grantedUserIds = null;
  377. const grantedGroupId = null;
  378. const shouldCheckDescendants = true;
  379. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  380. expect(result).toBe(true);
  381. });
  382. test('Should return true when Target: owned by User1, Descendant: User1 only', async() => {
  383. const targetPath = emptyPagePath2;
  384. const grant = Page.GRANT_OWNER;
  385. const grantedUserIds = [user1._id];
  386. const grantedGroupId = null;
  387. const shouldCheckDescendants = true;
  388. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  389. expect(result).toBe(true);
  390. });
  391. test('Should return true when Target: owned by GroupParent, Descendant: GroupParent, GroupChild and User1', async() => {
  392. const targetPath = emptyPagePath3;
  393. const grant = Page.GRANT_USER_GROUP;
  394. const grantedUserIds = null;
  395. const grantedGroupId = groupParent._id;
  396. const shouldCheckDescendants = true;
  397. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  398. expect(result).toBe(true);
  399. });
  400. test('Should return false when Target: owned by UserA, Descendant: public', async() => {
  401. const targetPath = emptyPagePath1;
  402. const grant = Page.GRANT_OWNER;
  403. const grantedUserIds = [user1._id];
  404. const grantedGroupId = null;
  405. const shouldCheckDescendants = true;
  406. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  407. expect(result).toBe(false);
  408. });
  409. });
  410. describe('Test for calcApplicableGrantData', () => {
  411. test('Only Public is Applicable in case of top page', async() => {
  412. const result = await pageGrantService.calcApplicableGrantData(rootPage, user1);
  413. expect(result).toStrictEqual(
  414. {
  415. [PageGrant.GRANT_PUBLIC]: null,
  416. },
  417. );
  418. });
  419. // parent property of all private pages is null
  420. test('Any grant is allowed if parent is null', async() => {
  421. const userGroupRelation = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user1);
  422. // OnlyMe
  423. const rootOnlyMePage = await Page.findOne({ path: v4PageRootOnlyMePagePath });
  424. const rootOnlyMePageRes = await pageGrantService.calcApplicableGrantData(rootOnlyMePage, user1);
  425. expect(rootOnlyMePageRes).toStrictEqual(
  426. {
  427. [PageGrant.GRANT_PUBLIC]: null,
  428. [PageGrant.GRANT_RESTRICTED]: null,
  429. [PageGrant.GRANT_OWNER]: null,
  430. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  431. },
  432. );
  433. // AnyoneWithTheLink
  434. const rootAnyoneWithTheLinkPage = await Page.findOne({ path: v4PageRootAnyoneWithTheLinkPagePath });
  435. const anyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(rootAnyoneWithTheLinkPage, user1);
  436. expect(anyoneWithTheLinkRes).toStrictEqual(
  437. {
  438. [PageGrant.GRANT_PUBLIC]: null,
  439. [PageGrant.GRANT_RESTRICTED]: null,
  440. [PageGrant.GRANT_OWNER]: null,
  441. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  442. },
  443. );
  444. // OnlyInsideTheGroup
  445. const rootOnlyInsideTheGroupPage = await Page.findOne({ path: v4PageRootOnlyInsideTheGroupPagePath });
  446. const onlyInsideTheGroupRes = await pageGrantService.calcApplicableGrantData(rootOnlyInsideTheGroupPage, user1);
  447. expect(onlyInsideTheGroupRes).toStrictEqual(
  448. {
  449. [PageGrant.GRANT_PUBLIC]: null,
  450. [PageGrant.GRANT_RESTRICTED]: null,
  451. [PageGrant.GRANT_OWNER]: null,
  452. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  453. },
  454. );
  455. });
  456. test('Any grant is allowed if parent is public', async() => {
  457. const userGroupRelation = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user1);
  458. // OnlyMe
  459. const publicOnlyMePage = await Page.findOne({ path: pagePublicOnlyMePath });
  460. const publicOnlyMeRes = await pageGrantService.calcApplicableGrantData(publicOnlyMePage, user1);
  461. expect(publicOnlyMeRes).toStrictEqual(
  462. {
  463. [PageGrant.GRANT_PUBLIC]: null,
  464. [PageGrant.GRANT_RESTRICTED]: null,
  465. [PageGrant.GRANT_OWNER]: null,
  466. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  467. },
  468. );
  469. // AnyoneWithTheLink
  470. const publicAnyoneWithTheLinkPage = await Page.findOne({ path: pagePublicAnyoneWithTheLinkPath });
  471. const publicAnyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(publicAnyoneWithTheLinkPage, user1);
  472. expect(publicAnyoneWithTheLinkRes).toStrictEqual(
  473. {
  474. [PageGrant.GRANT_PUBLIC]: null,
  475. [PageGrant.GRANT_RESTRICTED]: null,
  476. [PageGrant.GRANT_OWNER]: null,
  477. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  478. },
  479. );
  480. // OnlyInsideTheGroup
  481. const publicOnlyInsideTheGroupPage = await Page.findOne({ path: pagePublicOnlyInsideTheGroupPath });
  482. const publicOnlyInsideTheGroupRes = await pageGrantService.calcApplicableGrantData(publicOnlyInsideTheGroupPage, user1);
  483. expect(publicOnlyInsideTheGroupRes).toStrictEqual(
  484. {
  485. [PageGrant.GRANT_PUBLIC]: null,
  486. [PageGrant.GRANT_RESTRICTED]: null,
  487. [PageGrant.GRANT_OWNER]: null,
  488. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  489. },
  490. );
  491. });
  492. test('Only "GRANT_OWNER" is allowed if the user is the parent page\'s grantUser', async() => {
  493. // Public
  494. const onlyMePublicPage = await Page.findOne({ path: pageOnlyMePublicPath });
  495. const onlyMePublicRes = await pageGrantService.calcApplicableGrantData(onlyMePublicPage, user1);
  496. expect(onlyMePublicRes).toStrictEqual(
  497. {
  498. [PageGrant.GRANT_RESTRICTED]: null,
  499. [PageGrant.GRANT_OWNER]: null,
  500. },
  501. );
  502. // AnyoneWithTheLink
  503. const onlyMeAnyoneWithTheLinkPage = await Page.findOne({ path: pageOnlyMeAnyoneWithTheLinkPath });
  504. const onlyMeAnyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(onlyMeAnyoneWithTheLinkPage, user1);
  505. expect(onlyMeAnyoneWithTheLinkRes).toStrictEqual(
  506. {
  507. [PageGrant.GRANT_RESTRICTED]: null,
  508. [PageGrant.GRANT_OWNER]: null,
  509. },
  510. );
  511. // OnlyInsideTheGroup
  512. const publicOnlyInsideTheGroupPage = await Page.findOne({ path: pageOnlyMeOnlyInsideTheGroupPath });
  513. const publicOnlyInsideTheGroupRes = await pageGrantService.calcApplicableGrantData(publicOnlyInsideTheGroupPage, user1);
  514. expect(publicOnlyInsideTheGroupRes).toStrictEqual(
  515. {
  516. [PageGrant.GRANT_RESTRICTED]: null,
  517. [PageGrant.GRANT_OWNER]: null,
  518. },
  519. );
  520. });
  521. test('"GRANT_OWNER" is not allowed if the user is not the parent page\'s grantUser', async() => {
  522. // Public
  523. const onlyMePublicPage = await Page.findOne({ path: pageOnlyMePublicPath });
  524. const onlyMePublicRes = await pageGrantService.calcApplicableGrantData(onlyMePublicPage, user2);
  525. expect(onlyMePublicRes).toStrictEqual(
  526. {
  527. [PageGrant.GRANT_RESTRICTED]: null,
  528. },
  529. );
  530. // AnyoneWithTheLink
  531. const onlyMeAnyoneWithTheLinkPage = await Page.findOne({ path: pageOnlyMeAnyoneWithTheLinkPath });
  532. const onlyMeAnyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(onlyMeAnyoneWithTheLinkPage, user2);
  533. expect(onlyMeAnyoneWithTheLinkRes).toStrictEqual(
  534. {
  535. [PageGrant.GRANT_RESTRICTED]: null,
  536. },
  537. );
  538. // OnlyInsideTheGroup
  539. const publicOnlyInsideTheGroupPage = await Page.findOne({ path: pageOnlyMeOnlyInsideTheGroupPath });
  540. const publicOnlyInsideTheGroupRes = await pageGrantService.calcApplicableGrantData(publicOnlyInsideTheGroupPage, user2);
  541. expect(publicOnlyInsideTheGroupRes).toStrictEqual(
  542. {
  543. [PageGrant.GRANT_RESTRICTED]: null,
  544. },
  545. );
  546. });
  547. test('"GRANT_USER_GROUP" is allowed if the parent\'s grant is GRANT_USER_GROUP and the user is included in the group', async() => {
  548. const applicableGroups = await UserGroupRelation.findGroupsWithDescendantsByGroupAndUser(groupParent, user1);
  549. // Public
  550. const onlyInsideGroupPublicPage = await Page.findOne({ path: pageOnlyInsideTheGroupPublicPath });
  551. const onlyInsideGroupPublicRes = await pageGrantService.calcApplicableGrantData(onlyInsideGroupPublicPage, user1);
  552. expect(onlyInsideGroupPublicRes).toStrictEqual(
  553. {
  554. [PageGrant.GRANT_RESTRICTED]: null,
  555. [PageGrant.GRANT_OWNER]: null,
  556. [PageGrant.GRANT_USER_GROUP]: { applicableGroups },
  557. },
  558. );
  559. // OnlyMe
  560. const onlyInsideTheGroupOnlyMePage = await Page.findOne({ path: pageOnlyInsideTheGroupOnlyMePath });
  561. const onlyInsideTheGroupOnlyMeRes = await pageGrantService.calcApplicableGrantData(onlyInsideTheGroupOnlyMePage, user1);
  562. expect(onlyInsideTheGroupOnlyMeRes).toStrictEqual(
  563. {
  564. [PageGrant.GRANT_RESTRICTED]: null,
  565. [PageGrant.GRANT_OWNER]: null,
  566. [PageGrant.GRANT_USER_GROUP]: { applicableGroups },
  567. },
  568. );
  569. // AnyoneWithTheLink
  570. const onlyInsideTheGroupAnyoneWithTheLinkPage = await Page.findOne({ path: pageOnlyInsideTheGroupAnyoneWithTheLinkPath });
  571. const onlyInsideTheGroupAnyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(onlyInsideTheGroupAnyoneWithTheLinkPage, user1);
  572. expect(onlyInsideTheGroupAnyoneWithTheLinkRes).toStrictEqual(
  573. {
  574. [PageGrant.GRANT_RESTRICTED]: null,
  575. [PageGrant.GRANT_OWNER]: null,
  576. [PageGrant.GRANT_USER_GROUP]: { applicableGroups },
  577. },
  578. );
  579. });
  580. });
  581. });