v5.user-groups.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import mongoose from 'mongoose';
  2. import { getInstance } from '../setup-crowi';
  3. describe('UserGroupService', () => {
  4. let crowi;
  5. let UserGroup;
  6. const groupId1 = new mongoose.Types.ObjectId();
  7. const groupId2 = new mongoose.Types.ObjectId();
  8. const groupId3 = new mongoose.Types.ObjectId();
  9. beforeAll(async() => {
  10. crowi = await getInstance();
  11. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': true });
  12. UserGroup = mongoose.model('UserGroup');
  13. // Create Groups
  14. await UserGroup.insertMany([
  15. // No parent
  16. {
  17. _id: groupId1,
  18. name: 'v5_group1',
  19. description: 'description1',
  20. },
  21. // No parent
  22. {
  23. _id: groupId2,
  24. name: 'v5_group2',
  25. description: 'description2',
  26. },
  27. {
  28. _id: groupId3,
  29. name: 'v5_group3',
  30. parent: groupId1,
  31. description: 'description3',
  32. },
  33. ]);
  34. });
  35. /*
  36. * Update UserGroup
  37. */
  38. test('Updated values should be reflected. (name, description, parent)', async() => {
  39. const userGroup = await UserGroup.findOne({ _id: groupId1 });
  40. const newGroupName = 'v5_group1_new';
  41. const newGroupDescription = 'description1_new';
  42. const newParentId = groupId2;
  43. const updatedUserGroup = await crowi.userGroupService.updateGroup(userGroup._id, newGroupName, newGroupDescription, newParentId);
  44. expect(updatedUserGroup.name).toBe(newGroupName);
  45. expect(updatedUserGroup.description).toBe(newGroupDescription);
  46. expect(updatedUserGroup.parent).toStrictEqual(newParentId);
  47. });
  48. test('Should throw an error when trying to set existing group name', async() => {
  49. const userGroup1 = await UserGroup.findOne({ _id: groupId1 });
  50. const userGroup2 = await UserGroup.findOne({ _id: groupId2 });
  51. const result = crowi.userGroupService.updateGroup(userGroup1._id, userGroup2.name);
  52. await expect(result).rejects.toThrow('The group name is already taken');
  53. });
  54. test('Parent should be null when parent group is released', async() => {
  55. const userGroup = await UserGroup.findOne({ _id: groupId3 });
  56. const updatedUserGroup = await crowi.userGroupService.updateGroup(userGroup._id, userGroup.name, userGroup.description, null);
  57. expect(updatedUserGroup.parent).toBeNull();
  58. });
  59. });