page-grant.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import mongoose from 'mongoose';
  2. import { getInstance } from '../setup-crowi';
  3. import UserGroup from '~/server/models/user-group';
  4. /*
  5. * There are 3 grant types to test.
  6. * GRANT_PUBLIC, GRANT_OWNER, GRANT_USER_GROUP
  7. */
  8. describe('PageGrantService', () => {
  9. /*
  10. * models
  11. */
  12. let User;
  13. let Page;
  14. let UserGroupRelation;
  15. /*
  16. * global instances
  17. */
  18. let crowi;
  19. let pageGrantService;
  20. let xssSpy;
  21. let user1;
  22. let user2;
  23. let groupParent;
  24. let groupChild;
  25. let rootPage;
  26. let emptyPage1;
  27. let emptyPage2;
  28. let emptyPage3;
  29. const emptyPagePath1 = '/E1';
  30. const emptyPagePath2 = '/E2';
  31. const emptyPagePath3 = '/E3';
  32. let pageRootPublic;
  33. let pageRootGroupParent;
  34. const pageRootPublicPath = '/Public';
  35. const pageRootGroupParentPath = '/GroupParent';
  36. let pageE1Public;
  37. let pageE2User1;
  38. let pageE3GroupParent;
  39. let pageE3GroupChild;
  40. let pageE3User1;
  41. const pageE1PublicPath = '/E1/Public';
  42. const pageE2User1Path = '/E2/User1';
  43. const pageE3GroupParentPath = '/E3/GroupParent';
  44. const pageE3GroupChildPath = '/E3/GroupChild';
  45. const pageE3User1Path = '/E3/User1';
  46. /*
  47. * prepare before all tests
  48. */
  49. beforeAll(async() => {
  50. crowi = await getInstance();
  51. pageGrantService = crowi.pageGrantService;
  52. User = mongoose.model('User');
  53. Page = mongoose.model('Page');
  54. UserGroupRelation = mongoose.model('UserGroupRelation');
  55. // Users
  56. await User.insertMany([
  57. { name: 'User1', username: 'User1', email: 'user1@example.com' },
  58. { name: 'User2', username: 'User2', email: 'user2@example.com' },
  59. ]);
  60. user1 = await User.findOne({ username: 'User1' });
  61. user2 = await User.findOne({ username: 'User2' });
  62. // Parent user groups
  63. await UserGroup.insertMany([
  64. {
  65. name: 'GroupParent',
  66. parent: null,
  67. },
  68. ]);
  69. groupParent = await UserGroup.findOne({ name: 'GroupParent' });
  70. // Child user groups
  71. await UserGroup.insertMany([
  72. {
  73. name: 'GroupChild',
  74. parent: groupParent._id,
  75. },
  76. ]);
  77. groupChild = await UserGroup.findOne({ name: 'GroupChild' });
  78. // UserGroupRelations
  79. await UserGroupRelation.insertMany([
  80. {
  81. relatedGroup: groupParent._id,
  82. relatedUser: user1._id,
  83. },
  84. {
  85. relatedGroup: groupParent._id,
  86. relatedUser: user2._id,
  87. },
  88. {
  89. relatedGroup: groupChild._id,
  90. relatedUser: user1._id,
  91. },
  92. ]);
  93. // Root page (Depth: 0)
  94. rootPage = await Page.findOne({ path: '/' });
  95. if (rootPage == null) {
  96. const pages = await Page.insertMany([
  97. {
  98. path: '/',
  99. grant: Page.GRANT_PUBLIC,
  100. },
  101. ]);
  102. rootPage = pages[0];
  103. }
  104. // Empty pages (Depth: 1)
  105. await Page.insertMany([
  106. {
  107. path: emptyPagePath1,
  108. grant: Page.GRANT_PUBLIC,
  109. isEmpty: true,
  110. parent: rootPage._id,
  111. },
  112. {
  113. path: emptyPagePath2,
  114. grant: Page.GRANT_PUBLIC,
  115. isEmpty: true,
  116. parent: rootPage._id,
  117. },
  118. {
  119. path: emptyPagePath3,
  120. grant: Page.GRANT_PUBLIC,
  121. isEmpty: true,
  122. parent: rootPage._id,
  123. },
  124. {
  125. path: pageRootPublicPath,
  126. grant: Page.GRANT_PUBLIC,
  127. creator: user1,
  128. lastUpdateUser: user1,
  129. grantedUsers: null,
  130. grantedGroup: null,
  131. parent: rootPage._id,
  132. },
  133. {
  134. path: pageRootGroupParentPath,
  135. grant: Page.GRANT_USER_GROUP,
  136. creator: user1,
  137. lastUpdateUser: user1,
  138. grantedUsers: null,
  139. grantedGroup: groupParent._id,
  140. parent: rootPage._id,
  141. },
  142. ]);
  143. emptyPage1 = await Page.findOne({ path: emptyPagePath1 });
  144. emptyPage2 = await Page.findOne({ path: emptyPagePath2 });
  145. emptyPage3 = await Page.findOne({ path: emptyPagePath3 });
  146. // Leaf pages (Depth: 2)
  147. await Page.insertMany([
  148. {
  149. path: pageE1PublicPath,
  150. grant: Page.GRANT_PUBLIC,
  151. creator: user1,
  152. lastUpdateUser: user1,
  153. grantedUsers: null,
  154. grantedGroup: null,
  155. parent: emptyPage1._id,
  156. },
  157. {
  158. path: pageE2User1Path,
  159. grant: Page.GRANT_OWNER,
  160. creator: user1,
  161. lastUpdateUser: user1,
  162. grantedUsers: [user1._id],
  163. grantedGroup: null,
  164. parent: emptyPage2._id,
  165. },
  166. {
  167. path: pageE3GroupParentPath,
  168. grant: Page.GRANT_USER_GROUP,
  169. creator: user1,
  170. lastUpdateUser: user1,
  171. grantedUsers: null,
  172. grantedGroup: groupParent._id,
  173. parent: emptyPage3._id,
  174. },
  175. {
  176. path: pageE3GroupChildPath,
  177. grant: Page.GRANT_USER_GROUP,
  178. creator: user1,
  179. lastUpdateUser: user1,
  180. grantedUsers: null,
  181. grantedGroup: groupChild._id,
  182. parent: emptyPage3._id,
  183. },
  184. {
  185. path: pageE3User1Path,
  186. grant: Page.GRANT_OWNER,
  187. creator: user1,
  188. lastUpdateUser: user1,
  189. grantedUsers: [user1._id],
  190. grantedGroup: null,
  191. parent: emptyPage3._id,
  192. },
  193. ]);
  194. pageE1Public = await Page.findOne({ path: pageE1PublicPath });
  195. pageE2User1 = await Page.findOne({ path: pageE2User1Path });
  196. pageE3GroupParent = await Page.findOne({ path: pageE3GroupParentPath });
  197. pageE3GroupChild = await Page.findOne({ path: pageE3GroupChildPath });
  198. pageE3User1 = await Page.findOne({ path: pageE3User1Path });
  199. xssSpy = jest.spyOn(crowi.xss, 'process').mockImplementation(path => path);
  200. });
  201. describe('Test isGrantNormalized method with shouldCheckDescendants false', () => {
  202. test('Should return true when Ancestor: root, Target: public', async() => {
  203. const targetPath = '/NEW';
  204. const grant = Page.GRANT_PUBLIC;
  205. const grantedUserIds = null;
  206. const grantedGroupId = null;
  207. const shouldCheckDescendants = false;
  208. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  209. expect(result).toBe(true);
  210. });
  211. test('Should return true when Ancestor: root, Target: GroupParent', async() => {
  212. const targetPath = '/NEW_GroupParent';
  213. const grant = Page.GRANT_USER_GROUP;
  214. const grantedUserIds = null;
  215. const grantedGroupId = groupParent._id;
  216. const shouldCheckDescendants = false;
  217. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  218. expect(result).toBe(true);
  219. });
  220. test('Should return true when Ancestor: under-root public, Target: public', async() => {
  221. const targetPath = `${pageRootPublicPath}/NEW`;
  222. const grant = Page.GRANT_PUBLIC;
  223. const grantedUserIds = null;
  224. const grantedGroupId = null;
  225. const shouldCheckDescendants = false;
  226. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  227. expect(result).toBe(true);
  228. });
  229. test('Should return true when Ancestor: under-root GroupParent, Target: GroupParent', async() => {
  230. const targetPath = `${pageRootGroupParentPath}/NEW`;
  231. const grant = Page.GRANT_USER_GROUP;
  232. const grantedUserIds = null;
  233. const grantedGroupId = groupParent._id;
  234. const shouldCheckDescendants = false;
  235. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  236. expect(result).toBe(true);
  237. });
  238. test('Should return true when Ancestor: public, Target: public', async() => {
  239. const targetPath = `${pageE1PublicPath}/NEW`;
  240. const grant = Page.GRANT_PUBLIC;
  241. const grantedUserIds = null;
  242. const grantedGroupId = null;
  243. const shouldCheckDescendants = false;
  244. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  245. expect(result).toBe(true);
  246. });
  247. test('Should return true when Ancestor: owned by User1, Target: owned by User1', async() => {
  248. const targetPath = `${pageE2User1Path}/NEW`;
  249. const grant = Page.GRANT_OWNER;
  250. const grantedUserIds = [user1._id];
  251. const grantedGroupId = null;
  252. const shouldCheckDescendants = false;
  253. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  254. expect(result).toBe(true);
  255. });
  256. test('Should return false when Ancestor: owned by GroupParent, Target: public', async() => {
  257. const targetPath = `${pageE3GroupParentPath}/NEW`;
  258. const grant = Page.GRANT_PUBLIC;
  259. const grantedUserIds = null;
  260. const grantedGroupId = null;
  261. const shouldCheckDescendants = false;
  262. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  263. expect(result).toBe(false);
  264. });
  265. test('Should return false when Ancestor: owned by GroupChild, Target: GroupParent', async() => {
  266. const targetPath = `${pageE3GroupChildPath}/NEW`;
  267. const grant = Page.GRANT_USER_GROUP;
  268. const grantedUserIds = null;
  269. const grantedGroupId = groupParent._id;
  270. const shouldCheckDescendants = false;
  271. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  272. expect(result).toBe(false);
  273. });
  274. });
  275. describe('Test isGrantNormalized method with shouldCheckDescendants true', () => {
  276. test('Should return true when Target: public, Descendant: public', async() => {
  277. const targetPath = emptyPagePath1;
  278. const grant = Page.GRANT_PUBLIC;
  279. const grantedUserIds = null;
  280. const grantedGroupId = null;
  281. const shouldCheckDescendants = true;
  282. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  283. expect(result).toBe(true);
  284. });
  285. test('Should return true when Target: owned by User1, Descendant: User1 only', async() => {
  286. const targetPath = emptyPagePath2;
  287. const grant = Page.GRANT_OWNER;
  288. const grantedUserIds = [user1._id];
  289. const grantedGroupId = null;
  290. const shouldCheckDescendants = true;
  291. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  292. expect(result).toBe(true);
  293. });
  294. test('Should return true when Target: owned by GroupParent, Descendant: GroupParent, GroupChild and User1', async() => {
  295. const targetPath = emptyPagePath3;
  296. const grant = Page.GRANT_USER_GROUP;
  297. const grantedUserIds = null;
  298. const grantedGroupId = groupParent._id;
  299. const shouldCheckDescendants = true;
  300. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  301. expect(result).toBe(true);
  302. });
  303. test('Should return false when Target: owned by UserA, Descendant: public', async() => {
  304. const targetPath = emptyPagePath1;
  305. const grant = Page.GRANT_OWNER;
  306. const grantedUserIds = [user1._id];
  307. const grantedGroupId = null;
  308. const shouldCheckDescendants = true;
  309. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  310. expect(result).toBe(false);
  311. });
  312. });
  313. });