user-groups.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import mongoose from 'mongoose';
  2. import { getInstance } from '../setup-crowi';
  3. describe('UserGroupService', () => {
  4. let crowi;
  5. let User;
  6. let UserGroup;
  7. let UserGroupRelation;
  8. const groupId1 = new mongoose.Types.ObjectId();
  9. const groupId2 = new mongoose.Types.ObjectId();
  10. const groupId3 = new mongoose.Types.ObjectId();
  11. const groupId4 = new mongoose.Types.ObjectId();
  12. const groupId5 = new mongoose.Types.ObjectId();
  13. const groupId6 = new mongoose.Types.ObjectId();
  14. const groupId7 = new mongoose.Types.ObjectId();
  15. const groupId8 = new mongoose.Types.ObjectId();
  16. const groupId9 = new mongoose.Types.ObjectId();
  17. const groupId10 = new mongoose.Types.ObjectId();
  18. const groupId11 = new mongoose.Types.ObjectId();
  19. const groupId12 = new mongoose.Types.ObjectId();
  20. const userId1 = new mongoose.Types.ObjectId();
  21. beforeAll(async() => {
  22. crowi = await getInstance();
  23. User = mongoose.model('User');
  24. UserGroup = mongoose.model('UserGroup');
  25. UserGroupRelation = mongoose.model('UserGroupRelation');
  26. await User.insertMany([
  27. // ug -> User Group
  28. {
  29. _id: userId1, name: 'ug_test_user1', username: 'ug_test_user1', email: 'ug_test_user1@example.com',
  30. },
  31. ]);
  32. // Create Groups
  33. await UserGroup.insertMany([
  34. // No parent
  35. {
  36. _id: groupId1,
  37. name: 'v5_group1',
  38. description: 'description1',
  39. },
  40. // No parent
  41. {
  42. _id: groupId2,
  43. name: 'v5_group2',
  44. description: 'description2',
  45. },
  46. // No parent
  47. {
  48. _id: groupId3,
  49. name: 'v5_group3',
  50. description: 'description3',
  51. },
  52. // No parent
  53. {
  54. _id: groupId4,
  55. name: 'v5_group4',
  56. description: 'description4',
  57. },
  58. // No parent
  59. {
  60. _id: groupId5,
  61. name: 'v5_group5',
  62. description: 'description5',
  63. },
  64. // No parent
  65. {
  66. _id: groupId6,
  67. name: 'v5_group6',
  68. description: 'description6',
  69. },
  70. // No parent
  71. {
  72. _id: groupId7,
  73. name: 'v5_group7',
  74. description: 'description7',
  75. parent: groupId6,
  76. },
  77. // No parent
  78. {
  79. _id: groupId8,
  80. name: 'v5_group8',
  81. description: 'description8',
  82. },
  83. {
  84. _id: groupId9,
  85. name: 'v5_group9',
  86. description: 'description9',
  87. },
  88. {
  89. _id: groupId10,
  90. name: 'v5_group10',
  91. description: 'description10',
  92. parent: groupId9,
  93. },
  94. {
  95. _id: groupId11,
  96. name: 'v5_group11',
  97. description: 'descriptio11',
  98. },
  99. {
  100. _id: groupId12,
  101. name: 'v5_group12',
  102. description: 'description12',
  103. parent: groupId11,
  104. },
  105. ]);
  106. // Create UserGroupRelations
  107. await UserGroupRelation.insertMany([
  108. {
  109. relatedGroup: groupId4,
  110. relatedUser: userId1,
  111. },
  112. {
  113. relatedGroup: groupId6,
  114. relatedUser: userId1,
  115. },
  116. {
  117. relatedGroup: groupId8,
  118. relatedUser: userId1,
  119. },
  120. {
  121. relatedGroup: groupId9,
  122. relatedUser: userId1,
  123. },
  124. {
  125. relatedGroup: groupId10,
  126. relatedUser: userId1,
  127. },
  128. {
  129. relatedGroup: groupId11,
  130. relatedUser: userId1,
  131. },
  132. {
  133. relatedGroup: groupId12,
  134. relatedUser: userId1,
  135. },
  136. ]);
  137. });
  138. /*
  139. * Update UserGroup
  140. */
  141. test('Updated values should be reflected. (name, description, parent)', async() => {
  142. const userGroup2 = await UserGroup.findOne({ _id: groupId2 });
  143. const newGroupName = 'v5_group1_new';
  144. const newGroupDescription = 'description1_new';
  145. const newParentId = userGroup2._id;
  146. const updatedUserGroup = await crowi.userGroupService.updateGroup(groupId1, newGroupName, newGroupDescription, newParentId);
  147. expect(updatedUserGroup.name).toBe(newGroupName);
  148. expect(updatedUserGroup.description).toBe(newGroupDescription);
  149. expect(updatedUserGroup.parent).toStrictEqual(newParentId);
  150. });
  151. test('Should throw an error when trying to set existing group name', async() => {
  152. const userGroup2 = await UserGroup.findOne({ _id: groupId2 });
  153. const result = crowi.userGroupService.updateGroup(groupId1, userGroup2.name);
  154. await expect(result).rejects.toThrow('The group name is already taken');
  155. });
  156. test('Parent should be null when parent group is released', async() => {
  157. const userGroup = await UserGroup.findOne({ _id: groupId3 });
  158. const updatedUserGroup = await crowi.userGroupService.updateGroup(userGroup._id, userGroup.name, userGroup.description, null);
  159. expect(updatedUserGroup.parent).toBeNull();
  160. });
  161. /*
  162. * forceUpdateParents: false
  163. */
  164. test('Should throw an error when users in child group do not exist in parent group', async() => {
  165. const userGroup4 = await UserGroup.findOne({ _id: groupId4, parent: null });
  166. const result = crowi.userGroupService.updateGroup(userGroup4._id, userGroup4.name, userGroup4.description, groupId5);
  167. await expect(result).rejects.toThrow('The parent group does not contain the users in this group.');
  168. });
  169. /*
  170. * forceUpdateParents: true
  171. */
  172. test('User should be included to parent group (2 groups ver)', async() => {
  173. const userGroup4 = await UserGroup.findOne({ _id: groupId4, parent: null });
  174. const userGroup5 = await UserGroup.findOne({ _id: groupId5, parent: null });
  175. // userGroup4 has userId1
  176. const userGroupRelation4BeforeUpdate = await UserGroupRelation.findOne({ relatedGroup: userGroup4, relatedUser: userId1 });
  177. expect(userGroupRelation4BeforeUpdate).not.toBeNull();
  178. // userGroup5 has not userId1
  179. const userGroupRelation5BeforeUpdate = await UserGroupRelation.findOne({ relatedGroup: userGroup5, relatedUser: userId1 });
  180. expect(userGroupRelation5BeforeUpdate).toBeNull();
  181. // update userGroup4's parent with userGroup5 (forceUpdate: true)
  182. const forceUpdateParents = true;
  183. const updatedUserGroup = await crowi.userGroupService.updateGroup(
  184. userGroup4._id, userGroup4.name, userGroup4.description, groupId5, forceUpdateParents,
  185. );
  186. expect(updatedUserGroup.parent).toStrictEqual(groupId5);
  187. // userGroup5 should have userId1
  188. const userGroupRelation5AfterUpdate = await UserGroupRelation.findOne({ relatedGroup: groupId5, relatedUser: userGroupRelation4BeforeUpdate.relatedUser });
  189. expect(userGroupRelation5AfterUpdate).not.toBeNull();
  190. });
  191. test('User should be included to parent group (3 groups ver)', async() => {
  192. const userGroup8 = await UserGroup.findOne({ _id: groupId8, parent: null });
  193. // userGroup7 has not userId1
  194. const userGroupRelation6BeforeUpdate = await UserGroupRelation.findOne({ relatedGroup: groupId6, relatedUser: userId1 });
  195. const userGroupRelation7BeforeUpdate = await UserGroupRelation.findOne({ relatedGroup: groupId7, relatedUser: userId1 });
  196. const userGroupRelation8BeforeUpdate = await UserGroupRelation.findOne({ relatedGroup: groupId8, relatedUser: userId1 });
  197. expect(userGroupRelation6BeforeUpdate).not.toBeNull();
  198. // userGroup7 does not have userId1
  199. expect(userGroupRelation7BeforeUpdate).toBeNull();
  200. expect(userGroupRelation8BeforeUpdate).not.toBeNull();
  201. // update userGroup8's parent with userGroup7 (forceUpdate: true)
  202. const forceUpdateParents = true;
  203. await crowi.userGroupService.updateGroup(
  204. userGroup8._id, userGroup8.name, userGroup8.description, groupId7, forceUpdateParents,
  205. );
  206. const userGroupRelation6AfterUpdate = await UserGroupRelation.findOne({ relatedGroup: groupId6, relatedUser: userId1 });
  207. const userGroupRelation7AfterUpdate = await UserGroupRelation.findOne({ relatedGroup: groupId7, relatedUser: userId1 });
  208. const userGroupRelation8AfterUpdate = await UserGroupRelation.findOne({ relatedGroup: groupId8, relatedUser: userId1 });
  209. expect(userGroupRelation6AfterUpdate).not.toBeNull();
  210. // userGroup7 should have userId1
  211. expect(userGroupRelation7AfterUpdate).not.toBeNull();
  212. expect(userGroupRelation8AfterUpdate).not.toBeNull();
  213. });
  214. test('Should throw an error when trying to choose parent from descendant groups.', async() => {
  215. const userGroup9 = await UserGroup.findOne({ _id: groupId9, parent: null });
  216. const userGroup10 = await UserGroup.findOne({ _id: groupId10, parent: groupId9 });
  217. const userGroupRelation9BeforeUpdate = await UserGroupRelation.findOne({ relatedGroup: userGroup9._id, relatedUser: userId1 });
  218. const userGroupRelation10BeforeUpdate = await UserGroupRelation.findOne({ relatedGroup: userGroup10._id, relatedUser: userId1 });
  219. expect(userGroupRelation9BeforeUpdate).not.toBeNull();
  220. expect(userGroupRelation10BeforeUpdate).not.toBeNull();
  221. const result = crowi.userGroupService.updateGroup(
  222. userGroup9._id, userGroup9.name, userGroup9.description, userGroup10._id,
  223. );
  224. await expect(result).rejects.toThrow('It is not allowed to choose parent from descendant groups.');
  225. });
  226. test('User should be deleted from child groups when the user excluded from the parent group', async() => {
  227. const userGroup11 = await UserGroup.findOne({ _id: groupId11, parent: null });
  228. const userGroup12 = await UserGroup.findOne({ _id: groupId12, parent: groupId11 });
  229. // Both groups have user1
  230. const userGroupRelation11BeforeRemove = await UserGroupRelation.findOne({ relatedGroup: userGroup11._id, relatedUser: userId1 });
  231. const userGroupRelation12BeforeRemove = await UserGroupRelation.findOne({ relatedGroup: userGroup12._id, relatedUser: userId1 });
  232. expect(userGroupRelation11BeforeRemove).not.toBeNull();
  233. expect(userGroupRelation12BeforeRemove).not.toBeNull();
  234. // remove user1 from the parent group
  235. await crowi.userGroupService.removeUserByUsername(
  236. userGroup11._id, 'ug_test_user1',
  237. );
  238. // Both groups have not user1
  239. const userGroupRelation11AfterRemove = await UserGroupRelation.findOne({ relatedGroup: userGroup11._id, relatedUser: userId1 });
  240. const userGroupRelation12AfterRemove = await UserGroupRelation.findOne({ relatedGroup: userGroup12._id, relatedUser: userId1 });
  241. await expect(userGroupRelation11AfterRemove).toBeNull();
  242. await expect(userGroupRelation12AfterRemove).toBeNull();
  243. });
  244. });