v5.user-groups.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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('Can update user group basic info (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 userGroup = await UserGroup.findOne({ _id: groupId1 });
  50. await expect(crowi.userGroupService.updateGroup(userGroup._id, 'v5_group2')).rejects.toThrow('The group name is already taken');
  51. });
  52. test('Parent should be null when parent group is released', async() => {
  53. const userGroup = await UserGroup.findOne({ _id: groupId3 });
  54. const updatedUserGroup = await crowi.userGroupService.updateGroup(userGroup._id, userGroup.name, userGroup.description, null);
  55. expect(updatedUserGroup.parent).toBeNull();
  56. });
  57. });