user-groups.test.ts 8.1 KB

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