external-user-group-relation.integ.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import mongoose from 'mongoose';
  2. import ExternalUserGroupRelation from './external-user-group-relation';
  3. // TODO: use actual user model after ~/server/models/user.js becomes importable in vitest
  4. // ref: https://github.com/vitest-dev/vitest/issues/846
  5. const userSchema = new mongoose.Schema({
  6. name: { type: String },
  7. username: { type: String, required: true, unique: true },
  8. email: { type: String, unique: true, sparse: true },
  9. }, {
  10. timestamps: true,
  11. });
  12. const User = mongoose.model('User', userSchema);
  13. describe('ExternalUserGroupRelation model', () => {
  14. const userId1 = new mongoose.Types.ObjectId();
  15. const groupId1 = new mongoose.Types.ObjectId();
  16. const groupId2 = new mongoose.Types.ObjectId();
  17. describe('createRelations', () => {
  18. let user1;
  19. beforeAll(async() => {
  20. user1 = await User.create({
  21. _id: userId1, name: 'user1', username: 'user1', email: 'user1@example.com',
  22. });
  23. });
  24. it('creates relation for user', async() => {
  25. await ExternalUserGroupRelation.createRelations([groupId1, groupId2], user1);
  26. const relations = await ExternalUserGroupRelation.find();
  27. const idCombinations = relations.map((relation) => {
  28. return [relation.relatedGroup, relation.relatedUser];
  29. });
  30. expect(idCombinations).toStrictEqual([[groupId1, userId1], [groupId2, userId1]]);
  31. });
  32. });
  33. });