|
@@ -1,5 +1,6 @@
|
|
|
import mongoose from 'mongoose';
|
|
import mongoose from 'mongoose';
|
|
|
|
|
|
|
|
|
|
+import ExternalUserGroup from './external-user-group';
|
|
|
import ExternalUserGroupRelation from './external-user-group-relation';
|
|
import ExternalUserGroupRelation from './external-user-group-relation';
|
|
|
|
|
|
|
|
// TODO: use actual user model after ~/server/models/user.js becomes importable in vitest
|
|
// TODO: use actual user model after ~/server/models/user.js becomes importable in vitest
|
|
@@ -14,17 +15,33 @@ const userSchema = new mongoose.Schema({
|
|
|
const User = mongoose.model('User', userSchema);
|
|
const User = mongoose.model('User', userSchema);
|
|
|
|
|
|
|
|
describe('ExternalUserGroupRelation model', () => {
|
|
describe('ExternalUserGroupRelation model', () => {
|
|
|
|
|
+ let user1;
|
|
|
const userId1 = new mongoose.Types.ObjectId();
|
|
const userId1 = new mongoose.Types.ObjectId();
|
|
|
- const groupId1 = new mongoose.Types.ObjectId();
|
|
|
|
|
- const groupId2 = new mongoose.Types.ObjectId();
|
|
|
|
|
|
|
+
|
|
|
|
|
+ beforeAll(async() => {
|
|
|
|
|
+ user1 = await User.create({
|
|
|
|
|
+ _id: userId1, name: 'user1', username: 'user1', email: 'user1@example.com',
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ afterEach(async() => {
|
|
|
|
|
+ await ExternalUserGroup.deleteMany();
|
|
|
|
|
+ await ExternalUserGroupRelation.deleteMany();
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
describe('createRelations', () => {
|
|
describe('createRelations', () => {
|
|
|
- let user1;
|
|
|
|
|
|
|
+ const groupId1 = new mongoose.Types.ObjectId();
|
|
|
|
|
+ const groupId2 = new mongoose.Types.ObjectId();
|
|
|
|
|
|
|
|
beforeAll(async() => {
|
|
beforeAll(async() => {
|
|
|
- user1 = await User.create({
|
|
|
|
|
- _id: userId1, name: 'user1', username: 'user1', email: 'user1@example.com',
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ await ExternalUserGroup.insertMany([
|
|
|
|
|
+ {
|
|
|
|
|
+ _id: groupId1, name: 'test group 1', externalId: 'testExternalId', provider: 'testProvider',
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ _id: groupId2, name: 'test group 2', externalId: 'testExternalId', provider: 'testProvider',
|
|
|
|
|
+ },
|
|
|
|
|
+ ]);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('creates relation for user', async() => {
|
|
it('creates relation for user', async() => {
|
|
@@ -36,4 +53,23 @@ describe('ExternalUserGroupRelation model', () => {
|
|
|
expect(idCombinations).toStrictEqual([[groupId1, userId1], [groupId2, userId1]]);
|
|
expect(idCombinations).toStrictEqual([[groupId1, userId1], [groupId2, userId1]]);
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ describe('removeAllInvalidRelations', () => {
|
|
|
|
|
+ const groupId1 = new mongoose.Types.ObjectId();
|
|
|
|
|
+ const groupId2 = new mongoose.Types.ObjectId();
|
|
|
|
|
+
|
|
|
|
|
+ beforeAll(async() => {
|
|
|
|
|
+ await ExternalUserGroupRelation.createRelations([groupId1, groupId2], user1);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('removes invalid relations', async() => {
|
|
|
|
|
+ const relationsBeforeRemoval = await ExternalUserGroupRelation.find();
|
|
|
|
|
+ expect(relationsBeforeRemoval.length).not.toBe(0);
|
|
|
|
|
+
|
|
|
|
|
+ await ExternalUserGroupRelation.removeAllInvalidRelations();
|
|
|
|
|
+
|
|
|
|
|
+ const relationsAfterRemoval = await ExternalUserGroupRelation.find();
|
|
|
|
|
+ expect(relationsAfterRemoval.length).toBe(0);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|