page-grant.test.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 user3;
  25. let userA;
  26. let userB;
  27. let userC;
  28. let groupParent;
  29. let groupChild;
  30. let groupAB;
  31. let groupA;
  32. let groupAIsolated;
  33. let groupB;
  34. let groupC;
  35. const userGroupIdParent = new mongoose.Types.ObjectId();
  36. const userGroupIdA = new mongoose.Types.ObjectId();
  37. const userGroupIdAIsolated = new mongoose.Types.ObjectId();
  38. const userGroupIdB = new mongoose.Types.ObjectId();
  39. const userGroupIdC = new mongoose.Types.ObjectId();
  40. const userGroupIdAB = new mongoose.Types.ObjectId();
  41. let rootPage;
  42. let rootPublicPage;
  43. let rootOnlyMePage;
  44. let rootOnlyInsideTheGroup;
  45. let emptyPage1;
  46. let emptyPage2;
  47. let emptyPage3;
  48. const emptyPagePath1 = '/E1';
  49. const emptyPagePath2 = '/E2';
  50. const emptyPagePath3 = '/E3';
  51. let pageRootPublic;
  52. let pageRootGroupParent;
  53. const pageRootPublicPath = '/Public';
  54. const pageRootGroupParentPath = '/GroupParent';
  55. const v4PageRootOnlyMePagePath = '/v4OnlyMe';
  56. const v4PageRootAnyoneWithTheLinkPagePath = '/v4AnyoneWithTheLink';
  57. const v4PageRootOnlyInsideTheGroupPagePath = '/v4OnlyInsideTheGroup';
  58. const pagePublicOnlyMePath = `${pageRootPublicPath}/OnlyMe`;
  59. const pagePublicAnyoneWithTheLinkPath = `${pageRootPublicPath}/AnyoneWithTheLink`;
  60. const pagePublicOnlyInsideTheGroupPath = `${pageRootPublicPath}/OnlyInsideTheGroup`;
  61. const pageOnlyMePublicPath = `${v4PageRootOnlyMePagePath}/Public`;
  62. const pageOnlyMeAnyoneWithTheLinkPath = `${v4PageRootOnlyMePagePath}/AnyoneWithTheLink`;
  63. const pageOnlyMeOnlyInsideTheGroupPath = `${v4PageRootOnlyMePagePath}/OnlyInsideTheGroup`;
  64. const pageOnlyInsideTheGroupPublicPath = `${v4PageRootOnlyInsideTheGroupPagePath}/Public`;
  65. const pageOnlyInsideTheGroupOnlyMePath = `${v4PageRootOnlyInsideTheGroupPagePath}/OnlyMe`;
  66. const pageOnlyInsideTheGroupAnyoneWithTheLinkPath = `${v4PageRootOnlyInsideTheGroupPagePath}/AnyoneWithTheLink`;
  67. let pageE1Public;
  68. let pageE2User1;
  69. let pageE3GroupParent;
  70. let pageE3GroupChild;
  71. let pageE3User1;
  72. const pageE1PublicPath = '/E1/Public';
  73. const pageE2User1Path = '/E2/User1';
  74. const pageE3GroupParentPath = '/E3/GroupParent';
  75. const pageE3GroupChildPath = '/E3/GroupChild';
  76. const pageE3User1Path = '/E3/User1';
  77. /*
  78. * prepare before all tests
  79. */
  80. beforeAll(async() => {
  81. crowi = await getInstance();
  82. pageGrantService = crowi.pageGrantService;
  83. User = mongoose.model('User');
  84. Page = mongoose.model('Page');
  85. UserGroupRelation = mongoose.model('UserGroupRelation');
  86. // Users
  87. await User.insertMany([
  88. // For tests of isGrantNormalized
  89. { name: 'User1', username: 'User1', email: 'user1@example.com' },
  90. { name: 'User2', username: 'User2', email: 'user2@example.com' },
  91. // For tests of canOverwriteDescendants
  92. { name: 'UserA', username: 'UserA', email: 'userA@example.com' },
  93. { name: 'UserB', username: 'UserB', email: 'userB@example.com' },
  94. { name: 'UserC', username: 'UserC', email: 'userC@example.com' },
  95. ]);
  96. user1 = await User.findOne({ username: 'User1' });
  97. user2 = await User.findOne({ username: 'User2' });
  98. user3 = await User.findOne({ username: 'User3' });
  99. userA = await User.findOne({ username: 'UserA' });
  100. userB = await User.findOne({ username: 'UserB' });
  101. userC = await User.findOne({ username: 'UserC' });
  102. await UserGroup.insertMany([
  103. // For tests of isGrantNormalized
  104. {
  105. _id: userGroupIdParent,
  106. name: 'GroupParent',
  107. parent: null,
  108. },
  109. {
  110. name: 'GroupChild',
  111. parent: userGroupIdParent,
  112. },
  113. // For tests of canOverwriteDescendants
  114. {
  115. _id: userGroupIdAB,
  116. name: 'GroupAB',
  117. parent: null,
  118. },
  119. {
  120. _id: userGroupIdA,
  121. name: 'GroupA',
  122. parent: userGroupIdAB,
  123. },
  124. {
  125. _id: userGroupIdAIsolated,
  126. name: 'GroupAIsolated',
  127. parent: null, // isolated
  128. },
  129. {
  130. _id: userGroupIdB,
  131. name: 'GroupB',
  132. parent: userGroupIdAB,
  133. },
  134. {
  135. _id: userGroupIdC,
  136. name: 'GroupC',
  137. parent: null,
  138. },
  139. ]);
  140. groupParent = await UserGroup.findOne({ name: 'GroupParent' });
  141. groupChild = await UserGroup.findOne({ name: 'GroupChild' });
  142. groupAB = await UserGroup.findOne({ name: 'GroupAB' });
  143. groupA = await UserGroup.findOne({ name: 'GroupA' });
  144. groupAIsolated = await UserGroup.findOne({ name: 'GroupAIsolated' });
  145. groupB = await UserGroup.findOne({ name: 'GroupB' });
  146. groupC = await UserGroup.findOne({ name: 'GroupC' });
  147. // UserGroupRelations
  148. await UserGroupRelation.insertMany([
  149. // For tests of isGrantNormalized
  150. {
  151. relatedGroup: groupParent._id,
  152. relatedUser: user1._id,
  153. },
  154. {
  155. relatedGroup: groupParent._id,
  156. relatedUser: user2._id,
  157. },
  158. {
  159. relatedGroup: groupChild._id,
  160. relatedUser: user1._id,
  161. },
  162. // For tests of canOverwriteDescendants
  163. {
  164. relatedGroup: userGroupIdAB,
  165. relatedUser: userA._id,
  166. },
  167. {
  168. relatedGroup: userGroupIdAB,
  169. relatedUser: userB._id,
  170. },
  171. {
  172. relatedGroup: userGroupIdA,
  173. relatedUser: userA._id,
  174. },
  175. {
  176. relatedGroup: userGroupIdAIsolated,
  177. relatedUser: userA._id,
  178. },
  179. {
  180. relatedGroup: userGroupIdB,
  181. relatedUser: userB._id,
  182. },
  183. {
  184. relatedGroup: userGroupIdC,
  185. relatedUser: userC._id,
  186. },
  187. ]);
  188. // Root page (Depth: 0)
  189. rootPage = await Page.findOne({ path: '/' });
  190. // Empty pages (Depth: 1)
  191. await Page.insertMany([
  192. {
  193. path: emptyPagePath1,
  194. grant: Page.GRANT_PUBLIC,
  195. isEmpty: true,
  196. parent: rootPage._id,
  197. },
  198. {
  199. path: emptyPagePath2,
  200. grant: Page.GRANT_PUBLIC,
  201. isEmpty: true,
  202. parent: rootPage._id,
  203. },
  204. {
  205. path: emptyPagePath3,
  206. grant: Page.GRANT_PUBLIC,
  207. isEmpty: true,
  208. parent: rootPage._id,
  209. },
  210. {
  211. path: pageRootPublicPath,
  212. grant: Page.GRANT_PUBLIC,
  213. creator: user1,
  214. lastUpdateUser: user1,
  215. grantedUsers: null,
  216. grantedGroup: null,
  217. parent: rootPage._id,
  218. },
  219. {
  220. path: pageRootGroupParentPath,
  221. grant: Page.GRANT_USER_GROUP,
  222. creator: user1,
  223. lastUpdateUser: user1,
  224. grantedUsers: null,
  225. grantedGroup: groupParent._id,
  226. parent: rootPage._id,
  227. },
  228. ]);
  229. await Page.insertMany([
  230. // Root Page
  231. {
  232. path: rootPage,
  233. grant: Page.GRANT_PUBLIC,
  234. parent: null,
  235. },
  236. // OnlyMe v4
  237. {
  238. path: v4PageRootOnlyMePagePath,
  239. grant: Page.GRANT_OWNER,
  240. grantedUsers: [user1._id],
  241. parent: null,
  242. },
  243. // AnyoneWithTheLink v4
  244. {
  245. path: v4PageRootAnyoneWithTheLinkPagePath,
  246. grant: Page.GRANT_RESTRICTED,
  247. parent: null,
  248. },
  249. // OnlyInsideTheGroup v4
  250. {
  251. path: v4PageRootOnlyInsideTheGroupPagePath,
  252. grant: Page.GRANT_USER_GROUP,
  253. parent: null,
  254. grantedGroup: groupParent._id,
  255. },
  256. ]);
  257. rootPublicPage = await Page.findOne({ path: pageRootPublicPath });
  258. rootOnlyMePage = await Page.findOne({ path: v4PageRootOnlyMePagePath });
  259. rootOnlyInsideTheGroup = await Page.findOne({ path: v4PageRootOnlyInsideTheGroupPagePath });
  260. // Leaf pages (Depth: 2)
  261. await Page.insertMany([
  262. /*
  263. * Parent is public
  264. */
  265. {
  266. path: pagePublicOnlyMePath,
  267. grant: Page.GRANT_OWNER,
  268. parent: rootPublicPage._id,
  269. },
  270. {
  271. path: pagePublicAnyoneWithTheLinkPath,
  272. grant: Page.GRANT_RESTRICTED,
  273. parent: rootPublicPage._id,
  274. },
  275. {
  276. path: pagePublicOnlyInsideTheGroupPath,
  277. grant: Page.GRANT_USER_GROUP,
  278. parent: rootPublicPage._id,
  279. },
  280. /*
  281. * Parent is onlyMe
  282. */
  283. {
  284. path: pageOnlyMePublicPath,
  285. grant: Page.GRANT_PUBLIC,
  286. parent: rootOnlyMePage._id,
  287. },
  288. {
  289. path: pageOnlyMeAnyoneWithTheLinkPath,
  290. grant: Page.GRANT_RESTRICTED,
  291. parent: rootOnlyMePage._id,
  292. },
  293. {
  294. path: pageOnlyMeOnlyInsideTheGroupPath,
  295. grant: Page.GRANT_USER_GROUP,
  296. parent: rootOnlyMePage._id,
  297. },
  298. /*
  299. * Parent is OnlyInsideTheGroup
  300. */
  301. {
  302. path: pageOnlyInsideTheGroupPublicPath,
  303. grant: Page.GRANT_PUBLIC,
  304. parent: rootOnlyInsideTheGroup._id,
  305. },
  306. {
  307. path: pageOnlyInsideTheGroupOnlyMePath,
  308. grant: Page.GRANT_PUBLIC,
  309. parent: rootOnlyInsideTheGroup._id,
  310. },
  311. {
  312. path: pageOnlyInsideTheGroupAnyoneWithTheLinkPath,
  313. grant: Page.GRANT_PUBLIC,
  314. parent: rootOnlyInsideTheGroup._id,
  315. },
  316. ]);
  317. emptyPage1 = await Page.findOne({ path: emptyPagePath1 });
  318. emptyPage2 = await Page.findOne({ path: emptyPagePath2 });
  319. emptyPage3 = await Page.findOne({ path: emptyPagePath3 });
  320. // Leaf pages (Depth: 2)
  321. await Page.insertMany([
  322. {
  323. path: pageE1PublicPath,
  324. grant: Page.GRANT_PUBLIC,
  325. creator: user1,
  326. lastUpdateUser: user1,
  327. grantedUsers: null,
  328. grantedGroup: null,
  329. parent: emptyPage1._id,
  330. },
  331. {
  332. path: pageE2User1Path,
  333. grant: Page.GRANT_OWNER,
  334. creator: user1,
  335. lastUpdateUser: user1,
  336. grantedUsers: [user1._id],
  337. grantedGroup: null,
  338. parent: emptyPage2._id,
  339. },
  340. {
  341. path: pageE3GroupParentPath,
  342. grant: Page.GRANT_USER_GROUP,
  343. creator: user1,
  344. lastUpdateUser: user1,
  345. grantedUsers: null,
  346. grantedGroup: groupParent._id,
  347. parent: emptyPage3._id,
  348. },
  349. {
  350. path: pageE3GroupChildPath,
  351. grant: Page.GRANT_USER_GROUP,
  352. creator: user1,
  353. lastUpdateUser: user1,
  354. grantedUsers: null,
  355. grantedGroup: groupChild._id,
  356. parent: emptyPage3._id,
  357. },
  358. {
  359. path: pageE3User1Path,
  360. grant: Page.GRANT_OWNER,
  361. creator: user1,
  362. lastUpdateUser: user1,
  363. grantedUsers: [user1._id],
  364. grantedGroup: null,
  365. parent: emptyPage3._id,
  366. },
  367. ]);
  368. pageE1Public = await Page.findOne({ path: pageE1PublicPath });
  369. pageE2User1 = await Page.findOne({ path: pageE2User1Path });
  370. pageE3GroupParent = await Page.findOne({ path: pageE3GroupParentPath });
  371. pageE3GroupChild = await Page.findOne({ path: pageE3GroupChildPath });
  372. pageE3User1 = await Page.findOne({ path: pageE3User1Path });
  373. xssSpy = jest.spyOn(crowi.xss, 'process').mockImplementation(path => path);
  374. });
  375. describe('Test isGrantNormalized method with shouldCheckDescendants false', () => {
  376. test('Should return true when Ancestor: root, Target: public', async() => {
  377. const targetPath = '/NEW';
  378. const grant = Page.GRANT_PUBLIC;
  379. const grantedUserIds = null;
  380. const grantedGroupId = null;
  381. const shouldCheckDescendants = false;
  382. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  383. expect(result).toBe(true);
  384. });
  385. test('Should return true when Ancestor: root, Target: GroupParent', async() => {
  386. const targetPath = '/NEW_GroupParent';
  387. const grant = Page.GRANT_USER_GROUP;
  388. const grantedUserIds = null;
  389. const grantedGroupId = groupParent._id;
  390. const shouldCheckDescendants = false;
  391. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  392. expect(result).toBe(true);
  393. });
  394. test('Should return true when Ancestor: under-root public, Target: public', async() => {
  395. const targetPath = `${pageRootPublicPath}/NEW`;
  396. const grant = Page.GRANT_PUBLIC;
  397. const grantedUserIds = null;
  398. const grantedGroupId = null;
  399. const shouldCheckDescendants = false;
  400. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  401. expect(result).toBe(true);
  402. });
  403. test('Should return true when Ancestor: under-root GroupParent, Target: GroupParent', async() => {
  404. const targetPath = `${pageRootGroupParentPath}/NEW`;
  405. const grant = Page.GRANT_USER_GROUP;
  406. const grantedUserIds = null;
  407. const grantedGroupId = groupParent._id;
  408. const shouldCheckDescendants = false;
  409. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  410. expect(result).toBe(true);
  411. });
  412. test('Should return true when Ancestor: public, Target: public', async() => {
  413. const targetPath = `${pageE1PublicPath}/NEW`;
  414. const grant = Page.GRANT_PUBLIC;
  415. const grantedUserIds = null;
  416. const grantedGroupId = null;
  417. const shouldCheckDescendants = false;
  418. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  419. expect(result).toBe(true);
  420. });
  421. test('Should return true when Ancestor: owned by User1, Target: owned by User1', async() => {
  422. const targetPath = `${pageE2User1Path}/NEW`;
  423. const grant = Page.GRANT_OWNER;
  424. const grantedUserIds = [user1._id];
  425. const grantedGroupId = null;
  426. const shouldCheckDescendants = false;
  427. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  428. expect(result).toBe(true);
  429. });
  430. test('Should return false when Ancestor: owned by GroupParent, Target: public', async() => {
  431. const targetPath = `${pageE3GroupParentPath}/NEW`;
  432. const grant = Page.GRANT_PUBLIC;
  433. const grantedUserIds = null;
  434. const grantedGroupId = null;
  435. const shouldCheckDescendants = false;
  436. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  437. expect(result).toBe(false);
  438. });
  439. test('Should return false when Ancestor: owned by GroupChild, Target: GroupParent', async() => {
  440. const targetPath = `${pageE3GroupChildPath}/NEW`;
  441. const grant = Page.GRANT_USER_GROUP;
  442. const grantedUserIds = null;
  443. const grantedGroupId = groupParent._id;
  444. const shouldCheckDescendants = false;
  445. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  446. expect(result).toBe(false);
  447. });
  448. });
  449. describe('Test isGrantNormalized method with shouldCheckDescendants true', () => {
  450. test('Should return true when Target: public, Descendant: public', async() => {
  451. const targetPath = emptyPagePath1;
  452. const grant = Page.GRANT_PUBLIC;
  453. const grantedUserIds = null;
  454. const grantedGroupId = null;
  455. const shouldCheckDescendants = true;
  456. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  457. expect(result).toBe(true);
  458. });
  459. test('Should return true when Target: owned by User1, Descendant: User1 only', async() => {
  460. const targetPath = emptyPagePath2;
  461. const grant = Page.GRANT_OWNER;
  462. const grantedUserIds = [user1._id];
  463. const grantedGroupId = null;
  464. const shouldCheckDescendants = true;
  465. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  466. expect(result).toBe(true);
  467. });
  468. test('Should return true when Target: owned by GroupParent, Descendant: GroupParent, GroupChild and User1', async() => {
  469. const targetPath = emptyPagePath3;
  470. const grant = Page.GRANT_USER_GROUP;
  471. const grantedUserIds = null;
  472. const grantedGroupId = groupParent._id;
  473. const shouldCheckDescendants = true;
  474. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  475. expect(result).toBe(true);
  476. });
  477. test('Should return false when Target: owned by UserA, Descendant: public', async() => {
  478. const targetPath = emptyPagePath1;
  479. const grant = Page.GRANT_OWNER;
  480. const grantedUserIds = [user1._id];
  481. const grantedGroupId = null;
  482. const shouldCheckDescendants = true;
  483. const result = await pageGrantService.isGrantNormalized(user1, targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  484. expect(result).toBe(false);
  485. });
  486. });
  487. describe('Test for calcApplicableGrantData', () => {
  488. test('Only Public is Applicable in case of top page', async() => {
  489. const result = await pageGrantService.calcApplicableGrantData(rootPage, user1);
  490. expect(result).toStrictEqual(
  491. {
  492. [PageGrant.GRANT_PUBLIC]: null,
  493. },
  494. );
  495. });
  496. // parent property of all private pages is null
  497. test('Any grant is allowed if parent is null', async() => {
  498. const userGroupRelation = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user1);
  499. // OnlyMe
  500. const rootOnlyMePage = await Page.findOne({ path: v4PageRootOnlyMePagePath });
  501. const rootOnlyMePageRes = await pageGrantService.calcApplicableGrantData(rootOnlyMePage, user1);
  502. expect(rootOnlyMePageRes).toStrictEqual(
  503. {
  504. [PageGrant.GRANT_PUBLIC]: null,
  505. [PageGrant.GRANT_RESTRICTED]: null,
  506. [PageGrant.GRANT_OWNER]: null,
  507. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  508. },
  509. );
  510. // AnyoneWithTheLink
  511. const rootAnyoneWithTheLinkPage = await Page.findOne({ path: v4PageRootAnyoneWithTheLinkPagePath });
  512. const anyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(rootAnyoneWithTheLinkPage, user1);
  513. expect(anyoneWithTheLinkRes).toStrictEqual(
  514. {
  515. [PageGrant.GRANT_PUBLIC]: null,
  516. [PageGrant.GRANT_RESTRICTED]: null,
  517. [PageGrant.GRANT_OWNER]: null,
  518. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  519. },
  520. );
  521. // OnlyInsideTheGroup
  522. const rootOnlyInsideTheGroupPage = await Page.findOne({ path: v4PageRootOnlyInsideTheGroupPagePath });
  523. const onlyInsideTheGroupRes = await pageGrantService.calcApplicableGrantData(rootOnlyInsideTheGroupPage, user1);
  524. expect(onlyInsideTheGroupRes).toStrictEqual(
  525. {
  526. [PageGrant.GRANT_PUBLIC]: null,
  527. [PageGrant.GRANT_RESTRICTED]: null,
  528. [PageGrant.GRANT_OWNER]: null,
  529. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  530. },
  531. );
  532. });
  533. test('Any grant is allowed if parent is public', async() => {
  534. const userGroupRelation = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user1);
  535. // OnlyMe
  536. const publicOnlyMePage = await Page.findOne({ path: pagePublicOnlyMePath });
  537. const publicOnlyMeRes = await pageGrantService.calcApplicableGrantData(publicOnlyMePage, user1);
  538. expect(publicOnlyMeRes).toStrictEqual(
  539. {
  540. [PageGrant.GRANT_PUBLIC]: null,
  541. [PageGrant.GRANT_RESTRICTED]: null,
  542. [PageGrant.GRANT_OWNER]: null,
  543. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  544. },
  545. );
  546. // AnyoneWithTheLink
  547. const publicAnyoneWithTheLinkPage = await Page.findOne({ path: pagePublicAnyoneWithTheLinkPath });
  548. const publicAnyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(publicAnyoneWithTheLinkPage, user1);
  549. expect(publicAnyoneWithTheLinkRes).toStrictEqual(
  550. {
  551. [PageGrant.GRANT_PUBLIC]: null,
  552. [PageGrant.GRANT_RESTRICTED]: null,
  553. [PageGrant.GRANT_OWNER]: null,
  554. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  555. },
  556. );
  557. // OnlyInsideTheGroup
  558. const publicOnlyInsideTheGroupPage = await Page.findOne({ path: pagePublicOnlyInsideTheGroupPath });
  559. const publicOnlyInsideTheGroupRes = await pageGrantService.calcApplicableGrantData(publicOnlyInsideTheGroupPage, user1);
  560. expect(publicOnlyInsideTheGroupRes).toStrictEqual(
  561. {
  562. [PageGrant.GRANT_PUBLIC]: null,
  563. [PageGrant.GRANT_RESTRICTED]: null,
  564. [PageGrant.GRANT_OWNER]: null,
  565. [PageGrant.GRANT_USER_GROUP]: userGroupRelation,
  566. },
  567. );
  568. });
  569. test('Only "GRANT_OWNER" is allowed if the user is the parent page\'s grantUser', async() => {
  570. // Public
  571. const onlyMePublicPage = await Page.findOne({ path: pageOnlyMePublicPath });
  572. const onlyMePublicRes = await pageGrantService.calcApplicableGrantData(onlyMePublicPage, user1);
  573. expect(onlyMePublicRes).toStrictEqual(
  574. {
  575. [PageGrant.GRANT_RESTRICTED]: null,
  576. [PageGrant.GRANT_OWNER]: null,
  577. },
  578. );
  579. // AnyoneWithTheLink
  580. const onlyMeAnyoneWithTheLinkPage = await Page.findOne({ path: pageOnlyMeAnyoneWithTheLinkPath });
  581. const onlyMeAnyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(onlyMeAnyoneWithTheLinkPage, user1);
  582. expect(onlyMeAnyoneWithTheLinkRes).toStrictEqual(
  583. {
  584. [PageGrant.GRANT_RESTRICTED]: null,
  585. [PageGrant.GRANT_OWNER]: null,
  586. },
  587. );
  588. // OnlyInsideTheGroup
  589. const publicOnlyInsideTheGroupPage = await Page.findOne({ path: pageOnlyMeOnlyInsideTheGroupPath });
  590. const publicOnlyInsideTheGroupRes = await pageGrantService.calcApplicableGrantData(publicOnlyInsideTheGroupPage, user1);
  591. expect(publicOnlyInsideTheGroupRes).toStrictEqual(
  592. {
  593. [PageGrant.GRANT_RESTRICTED]: null,
  594. [PageGrant.GRANT_OWNER]: null,
  595. },
  596. );
  597. });
  598. test('"GRANT_OWNER" is not allowed if the user is not the parent page\'s grantUser', async() => {
  599. // Public
  600. const onlyMePublicPage = await Page.findOne({ path: pageOnlyMePublicPath });
  601. const onlyMePublicRes = await pageGrantService.calcApplicableGrantData(onlyMePublicPage, user2);
  602. expect(onlyMePublicRes).toStrictEqual(
  603. {
  604. [PageGrant.GRANT_RESTRICTED]: null,
  605. },
  606. );
  607. // AnyoneWithTheLink
  608. const onlyMeAnyoneWithTheLinkPage = await Page.findOne({ path: pageOnlyMeAnyoneWithTheLinkPath });
  609. const onlyMeAnyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(onlyMeAnyoneWithTheLinkPage, user2);
  610. expect(onlyMeAnyoneWithTheLinkRes).toStrictEqual(
  611. {
  612. [PageGrant.GRANT_RESTRICTED]: null,
  613. },
  614. );
  615. // OnlyInsideTheGroup
  616. const publicOnlyInsideTheGroupPage = await Page.findOne({ path: pageOnlyMeOnlyInsideTheGroupPath });
  617. const publicOnlyInsideTheGroupRes = await pageGrantService.calcApplicableGrantData(publicOnlyInsideTheGroupPage, user2);
  618. expect(publicOnlyInsideTheGroupRes).toStrictEqual(
  619. {
  620. [PageGrant.GRANT_RESTRICTED]: null,
  621. },
  622. );
  623. });
  624. test('"GRANT_USER_GROUP" is allowed if the parent\'s grant is GRANT_USER_GROUP and the user is included in the group', async() => {
  625. const applicableGroups = await UserGroupRelation.findGroupsWithDescendantsByGroupAndUser(groupParent, user1);
  626. // Public
  627. const onlyInsideGroupPublicPage = await Page.findOne({ path: pageOnlyInsideTheGroupPublicPath });
  628. const onlyInsideGroupPublicRes = await pageGrantService.calcApplicableGrantData(onlyInsideGroupPublicPage, user1);
  629. expect(onlyInsideGroupPublicRes).toStrictEqual(
  630. {
  631. [PageGrant.GRANT_RESTRICTED]: null,
  632. [PageGrant.GRANT_OWNER]: null,
  633. [PageGrant.GRANT_USER_GROUP]: { applicableGroups },
  634. },
  635. );
  636. // OnlyMe
  637. const onlyInsideTheGroupOnlyMePage = await Page.findOne({ path: pageOnlyInsideTheGroupOnlyMePath });
  638. const onlyInsideTheGroupOnlyMeRes = await pageGrantService.calcApplicableGrantData(onlyInsideTheGroupOnlyMePage, user1);
  639. expect(onlyInsideTheGroupOnlyMeRes).toStrictEqual(
  640. {
  641. [PageGrant.GRANT_RESTRICTED]: null,
  642. [PageGrant.GRANT_OWNER]: null,
  643. [PageGrant.GRANT_USER_GROUP]: { applicableGroups },
  644. },
  645. );
  646. // AnyoneWithTheLink
  647. const onlyInsideTheGroupAnyoneWithTheLinkPage = await Page.findOne({ path: pageOnlyInsideTheGroupAnyoneWithTheLinkPath });
  648. const onlyInsideTheGroupAnyoneWithTheLinkRes = await pageGrantService.calcApplicableGrantData(onlyInsideTheGroupAnyoneWithTheLinkPage, user1);
  649. expect(onlyInsideTheGroupAnyoneWithTheLinkRes).toStrictEqual(
  650. {
  651. [PageGrant.GRANT_RESTRICTED]: null,
  652. [PageGrant.GRANT_OWNER]: null,
  653. [PageGrant.GRANT_USER_GROUP]: { applicableGroups },
  654. },
  655. );
  656. });
  657. });
  658. describe('canOverwriteDescendants', () => {
  659. test('it should return true when update grant is GRANT_PUBLIC', async() => {
  660. const userA = {};
  661. const updateGrantInfo = {
  662. targetPage: {},
  663. grant: PageGrant.GRANT_PUBLIC,
  664. grantedUser: null,
  665. grantedUserGroup: null,
  666. };
  667. // TODO: expect page tree
  668. const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
  669. expect(res).toBe(true);
  670. });
  671. test('it should return true when all descendant pages are granted by the operator', async() => {
  672. const userA = {};
  673. const updateGrantInfo = {
  674. targetPage: {},
  675. grant: PageGrant.GRANT_OWNER,
  676. grantedUser: userA,
  677. grantedUserGroup: null,
  678. };
  679. // TODO: expect page tree
  680. const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
  681. expect(res).toBe(true);
  682. });
  683. test(`it should return true when update grant is GRANT_USER_GROUP
  684. , all user groups of descendants are the children or itself of the update user group
  685. , and all users of descendants belong to the update user group`, async() => {
  686. const userA = {};
  687. const userGroupAB = {};
  688. const updateGrantInfo = {
  689. targetPage: {},
  690. grant: PageGrant.GRANT_USER_GROUP,
  691. grantedUser: null,
  692. grantedUserGroup: userGroupAB,
  693. };
  694. // TODO: expect page tree
  695. const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
  696. expect(res).toBe(true);
  697. });
  698. test(`it should return false when some of descendants is not granted
  699. , update grant is GRANT_USER_GROUP
  700. , and some of user groups of descendants are not children or itself of the update user group`, async() => {
  701. const userA = {};
  702. const userGroupAB = {};
  703. const updateGrantInfo = {
  704. targetPage: {},
  705. grant: PageGrant.GRANT_USER_GROUP,
  706. grantedUser: null,
  707. grantedUserGroup: userGroupAB,
  708. };
  709. // TODO: expect page tree (include page with gC)
  710. const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
  711. expect(res).toBe(false);
  712. });
  713. test(`it should return false when some of descendants is not granted
  714. , update grant is GRANT_USER_GROUP
  715. , and some of users of descendants does NOT belong to the update user group`, async() => {
  716. const userA = {};
  717. const updateGrantInfo = {
  718. targetPage: {},
  719. grant: PageGrant.GRANT_USER_GROUP,
  720. grantedUser: null,
  721. grantedUserGroup: null,
  722. };
  723. // TODO: expect page tree (include page with onlyC)
  724. const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
  725. expect(res).toBe(false);
  726. });
  727. test('it should return false when some of descendants is not granted and update grant is GRANT_OWNER', async() => {
  728. const userA = {};
  729. const updateGrantInfo = {
  730. targetPage: {},
  731. grant: PageGrant.GRANT_OWNER,
  732. grantedUser: userA,
  733. grantedUserGroup: null,
  734. };
  735. // TODO: expect page tree (include page with onlyC)
  736. const res = await pageGrantService.canOverwriteDescendants(userA, updateGrantInfo);
  737. expect(res).toBe(false);
  738. });
  739. });
  740. });