page-grant.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. await Page.insertMany([
  95. {
  96. path: '/',
  97. grant: Page.GRANT_PUBLIC,
  98. },
  99. ]);
  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. emptyPage1 = await Page.findOne({ path: emptyPagePath1 });
  141. emptyPage2 = await Page.findOne({ path: emptyPagePath2 });
  142. emptyPage3 = await Page.findOne({ path: emptyPagePath3 });
  143. // Leaf pages (Depth: 2)
  144. await Page.insertMany([
  145. {
  146. path: pageE1PublicPath,
  147. grant: Page.GRANT_PUBLIC,
  148. creator: user1,
  149. lastUpdateUser: user1,
  150. grantedUsers: null,
  151. grantedGroup: null,
  152. parent: emptyPage1._id,
  153. },
  154. {
  155. path: pageE2User1Path,
  156. grant: Page.GRANT_OWNER,
  157. creator: user1,
  158. lastUpdateUser: user1,
  159. grantedUsers: [user1._id],
  160. grantedGroup: null,
  161. parent: emptyPage2._id,
  162. },
  163. {
  164. path: pageE3GroupParentPath,
  165. grant: Page.GRANT_USER_GROUP,
  166. creator: user1,
  167. lastUpdateUser: user1,
  168. grantedUsers: null,
  169. grantedGroup: groupParent._id,
  170. parent: emptyPage3._id,
  171. },
  172. {
  173. path: pageE3GroupChildPath,
  174. grant: Page.GRANT_USER_GROUP,
  175. creator: user1,
  176. lastUpdateUser: user1,
  177. grantedUsers: null,
  178. grantedGroup: groupChild._id,
  179. parent: emptyPage3._id,
  180. },
  181. {
  182. path: pageE3User1Path,
  183. grant: Page.GRANT_OWNER,
  184. creator: user1,
  185. lastUpdateUser: user1,
  186. grantedUsers: [user1._id],
  187. grantedGroup: null,
  188. parent: emptyPage3._id,
  189. },
  190. ]);
  191. pageE1Public = await Page.findOne({ path: pageE1PublicPath });
  192. pageE2User1 = await Page.findOne({ path: pageE2User1Path });
  193. pageE3GroupParent = await Page.findOne({ path: pageE3GroupParentPath });
  194. pageE3GroupChild = await Page.findOne({ path: pageE3GroupChildPath });
  195. pageE3User1 = await Page.findOne({ path: pageE3User1Path });
  196. xssSpy = jest.spyOn(crowi.xss, 'process').mockImplementation(path => path);
  197. });
  198. describe('Test isGrantNormalized method with shouldCheckDescendants false', () => {
  199. test('Should return true when Ancestor: root, Target: public', async() => {
  200. const targetPath = '/NEW';
  201. const grant = Page.GRANT_PUBLIC;
  202. const grantedUserIds = null;
  203. const grantedGroupId = null;
  204. const shouldCheckDescendants = false;
  205. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  206. expect(result).toBe(true);
  207. });
  208. test('Should return true when Ancestor: root, Target: GroupParent', async() => {
  209. const targetPath = '/NEW_GroupParent';
  210. const grant = Page.GRANT_USER_GROUP;
  211. const grantedUserIds = null;
  212. const grantedGroupId = groupParent._id;
  213. const shouldCheckDescendants = false;
  214. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  215. expect(result).toBe(true);
  216. });
  217. test('Should return true when Ancestor: under-root public, Target: public', async() => {
  218. const targetPath = `${pageRootPublicPath}/NEW`;
  219. const grant = Page.GRANT_PUBLIC;
  220. const grantedUserIds = null;
  221. const grantedGroupId = null;
  222. const shouldCheckDescendants = false;
  223. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  224. expect(result).toBe(true);
  225. });
  226. test('Should return true when Ancestor: under-root GroupParent, Target: GroupParent', async() => {
  227. const targetPath = `${pageRootGroupParentPath}/NEW`;
  228. const grant = Page.GRANT_USER_GROUP;
  229. const grantedUserIds = null;
  230. const grantedGroupId = groupParent._id;
  231. const shouldCheckDescendants = false;
  232. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  233. expect(result).toBe(true);
  234. });
  235. test('Should return true when Ancestor: public, Target: public', async() => {
  236. const targetPath = `${pageE1PublicPath}/NEW`;
  237. const grant = Page.GRANT_PUBLIC;
  238. const grantedUserIds = null;
  239. const grantedGroupId = null;
  240. const shouldCheckDescendants = false;
  241. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  242. expect(result).toBe(true);
  243. });
  244. test('Should return true when Ancestor: owned by User1, Target: owned by User1', async() => {
  245. const targetPath = `${pageE2User1Path}/NEW`;
  246. const grant = Page.GRANT_OWNER;
  247. const grantedUserIds = [user1._id];
  248. const grantedGroupId = null;
  249. const shouldCheckDescendants = false;
  250. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  251. expect(result).toBe(true);
  252. });
  253. test('Should return false when Ancestor: owned by GroupParent, Target: public', async() => {
  254. const targetPath = `${pageE3GroupParentPath}/NEW`;
  255. const grant = Page.GRANT_PUBLIC;
  256. const grantedUserIds = null;
  257. const grantedGroupId = null;
  258. const shouldCheckDescendants = false;
  259. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  260. expect(result).toBe(false);
  261. });
  262. test('Should return false when Ancestor: owned by GroupChild, Target: GroupParent', async() => {
  263. const targetPath = `${pageE3GroupChildPath}/NEW`;
  264. const grant = Page.GRANT_USER_GROUP;
  265. const grantedUserIds = null;
  266. const grantedGroupId = groupParent._id;
  267. const shouldCheckDescendants = false;
  268. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  269. expect(result).toBe(false);
  270. });
  271. });
  272. describe('Test isGrantNormalized method with shouldCheckDescendants true', () => {
  273. test('Should return true when Target: public, Descendant: public', async() => {
  274. const targetPath = emptyPagePath1;
  275. const grant = Page.GRANT_PUBLIC;
  276. const grantedUserIds = null;
  277. const grantedGroupId = null;
  278. const shouldCheckDescendants = true;
  279. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  280. expect(result).toBe(true);
  281. });
  282. test('Should return true when Target: owned by User1, Descendant: User1 only', async() => {
  283. const targetPath = emptyPagePath2;
  284. const grant = Page.GRANT_OWNER;
  285. const grantedUserIds = [user1._id];
  286. const grantedGroupId = null;
  287. const shouldCheckDescendants = true;
  288. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  289. expect(result).toBe(true);
  290. });
  291. test('Should return true when Target: owned by GroupParent, Descendant: GroupParent, GroupChild and User1', async() => {
  292. const targetPath = emptyPagePath3;
  293. const grant = Page.GRANT_USER_GROUP;
  294. const grantedUserIds = null;
  295. const grantedGroupId = groupParent._id;
  296. const shouldCheckDescendants = true;
  297. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  298. expect(result).toBe(true);
  299. });
  300. test('Should return false when Target: owned by UserA, Descendant: public', async() => {
  301. const targetPath = emptyPagePath1;
  302. const grant = Page.GRANT_OWNER;
  303. const grantedUserIds = [user1._id];
  304. const grantedGroupId = null;
  305. const shouldCheckDescendants = true;
  306. const result = await pageGrantService.isGrantNormalized(targetPath, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
  307. expect(result).toBe(false);
  308. });
  309. });
  310. });