v5.user-groups.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 userGroupRelationId1 = new mongoose.Types.ObjectId();
  12. const userGroupRelationId4 = new mongoose.Types.ObjectId();
  13. const userId1 = new mongoose.Types.ObjectId();
  14. const userId4 = new mongoose.Types.ObjectId();
  15. beforeAll(async() => {
  16. crowi = await getInstance();
  17. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': true });
  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. {
  41. _id: groupId4,
  42. name: 'v5_group4',
  43. parent: groupId4,
  44. description: 'description4',
  45. },
  46. ]);
  47. // Create UserGroupRelations
  48. await UserGroupRelation.insertMany([
  49. // Parent
  50. {
  51. _id: userGroupRelationId1,
  52. relatedGroup: groupId1,
  53. relatedUser: userId1,
  54. },
  55. {
  56. _id: userGroupRelationId4,
  57. relatedGroup: groupId4,
  58. relatedUser: userId4,
  59. },
  60. // Child
  61. // {
  62. // _id: userGroupRelationId2,
  63. // relatedGroup: groupId2,
  64. // relatedUser: userId1,
  65. // },
  66. ]);
  67. });
  68. /*
  69. * Update UserGroup
  70. */
  71. test('Updated values should be reflected. (name, description, parent)', async() => {
  72. const userGroup1 = await UserGroup.findOne({ _id: groupId1 });
  73. const userGroup2 = await UserGroup.findOne({ _id: groupId2 });
  74. const newGroupName = 'v5_group2_new';
  75. const newGroupDescription = 'description2_new';
  76. const newParentId = userGroup1._id;
  77. const updatedUserGroup = await crowi.userGroupService.updateGroup(userGroup2._id, newGroupName, newGroupDescription, newParentId);
  78. expect(updatedUserGroup.name).toBe(newGroupName);
  79. expect(updatedUserGroup.description).toBe(newGroupDescription);
  80. expect(updatedUserGroup.parent).toStrictEqual(newParentId);
  81. });
  82. test('Should throw an error when trying to set existing group name', async() => {
  83. const userGroup1 = await UserGroup.findOne({ _id: groupId1 });
  84. const userGroup2 = await UserGroup.findOne({ _id: groupId2 });
  85. const result = crowi.userGroupService.updateGroup(userGroup1._id, userGroup2.name);
  86. await expect(result).rejects.toThrow('The group name is already taken');
  87. });
  88. test('Should throw an error when users in clild group do not include in users in parent group', async() => {
  89. const userGroup1 = await UserGroup.findOne({ _id: groupId1 });
  90. const userGroup4 = await UserGroup.findOne({ _id: groupId4 });
  91. const result = crowi.userGroupService.updateGroup(userGroup1._id, userGroup1.name, userGroup1.description, userGroup4._id);
  92. await expect(result).rejects.toThrow('The parent group does not contain the users in this group.');
  93. });
  94. test('Parent should be null when parent group is released', async() => {
  95. const userGroup = await UserGroup.findOne({ _id: groupId3 });
  96. const updatedUserGroup = await crowi.userGroupService.updateGroup(userGroup._id, userGroup.name, userGroup.description, null);
  97. expect(updatedUserGroup.parent).toBeNull();
  98. });
  99. });