user-groups.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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: 'description5',
  57. },
  58. // No parent
  59. {
  60. _id: groupId7,
  61. name: 'v5_group7',
  62. description: 'description5',
  63. },
  64. // No parent
  65. {
  66. _id: groupId8,
  67. name: 'v5_group8',
  68. description: 'description5',
  69. },
  70. ]);
  71. // Create UserGroupRelations
  72. await UserGroupRelation.insertMany([
  73. {
  74. relatedGroup: groupId4,
  75. relatedUser: userId1,
  76. },
  77. {
  78. relatedGroup: groupId6,
  79. relatedUser: userId1,
  80. },
  81. {
  82. relatedGroup: groupId8,
  83. relatedUser: userId1,
  84. },
  85. ]);
  86. });
  87. /*
  88. * Update UserGroup
  89. */
  90. test('Updated values should be reflected. (name, description, parent)', async() => {
  91. const userGroup2 = await UserGroup.findOne({ _id: groupId2 });
  92. const newGroupName = 'v5_group1_new';
  93. const newGroupDescription = 'description1_new';
  94. const newParentId = userGroup2._id;
  95. const updatedUserGroup = await crowi.userGroupService.updateGroup(groupId1, newGroupName, newGroupDescription, newParentId);
  96. expect(updatedUserGroup.name).toBe(newGroupName);
  97. expect(updatedUserGroup.description).toBe(newGroupDescription);
  98. expect(updatedUserGroup.parent).toStrictEqual(newParentId);
  99. });
  100. test('Should throw an error when trying to set existing group name', async() => {
  101. const userGroup2 = await UserGroup.findOne({ _id: groupId2 });
  102. const result = crowi.userGroupService.updateGroup(groupId1, userGroup2.name);
  103. await expect(result).rejects.toThrow('The group name is already taken');
  104. });
  105. test('Parent should be null when parent group is released', async() => {
  106. const userGroup = await UserGroup.findOne({ _id: groupId3 });
  107. const updatedUserGroup = await crowi.userGroupService.updateGroup(userGroup._id, userGroup.name, userGroup.description, null);
  108. expect(updatedUserGroup.parent).toBeNull();
  109. });
  110. /*
  111. * forceUpdateParents: false
  112. */
  113. test('Should throw an error when users in child group do not exist in parent group', async() => {
  114. const userGroup4 = await UserGroup.findOne({ _id: groupId4 });
  115. const result = crowi.userGroupService.updateGroup(userGroup4._id, userGroup4.name, userGroup4.description, groupId5);
  116. await expect(result).rejects.toThrow('The parent group does not contain the users in this group.');
  117. });
  118. /*
  119. * forceUpdateParents: true
  120. */
  121. test('User should be included to parent group (2 groups ver)', async() => {
  122. const userGroup4 = await UserGroup.findOne({ _id: groupId4 });
  123. const userGroup4Relation = await UserGroupRelation.findOne({ relatedGroup: userGroup4, relatedUser: userId1 });
  124. const forceUpdateParents = true;
  125. const updatedUserGroup = await crowi.userGroupService.updateGroup(
  126. userGroup4._id, userGroup4.name, userGroup4.description, groupId5, forceUpdateParents,
  127. );
  128. const relatedGroup = await UserGroupRelation.findOne({ relatedGroup: groupId5, relatedUser: userGroup4Relation.relatedUser });
  129. expect(updatedUserGroup.parent).toStrictEqual(groupId5);
  130. expect(relatedGroup).toBeTruthy();
  131. });
  132. test('User should be included to parent group (3 groups ver)', async() => {
  133. const userGroup8 = await UserGroup.findOne({ _id: groupId8 });
  134. const forceUpdateParents = true;
  135. await crowi.userGroupService.updateGroup(
  136. userGroup8._id, userGroup8.name, userGroup8.description, groupId7, forceUpdateParents,
  137. );
  138. const relatedGroup6 = await UserGroupRelation.findOne({ relatedGroup: groupId6, relatedUser: userId1 });
  139. const relatedGroup7 = await UserGroupRelation.findOne({ relatedGroup: groupId7, relatedUser: userId1 });
  140. const relatedGroup8 = await UserGroupRelation.findOne({ relatedGroup: groupId8, relatedUser: userId1 });
  141. expect(relatedGroup6).toBeTruthy();
  142. expect(relatedGroup7).toBeTruthy();
  143. expect(relatedGroup8).toBeTruthy();
  144. });
  145. });