user-groups.test.ts 6.7 KB

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