page-grant.test.js 22 KB

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