page-grant.test.js 11 KB

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