Skip to content

Commit

Permalink
unit tests WIP2
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorCapCoder committed Sep 27, 2023
1 parent a8fbe10 commit 2f5f66c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 31 deletions.
15 changes: 7 additions & 8 deletions src/services/sync/strategies/consumerActions/ClassAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,16 @@ class ClassAction extends BaseConsumerAction {
const teachers = [];
const ldapDns = !Array.isArray(uniqueMembers) ? [uniqueMembers] : uniqueMembers;

let users = [];
if (ldapDns) {
if (ldapDns && ldapDns !== [undefined]) {
const users = await UserRepo.findByLdapDnsAndSchool(ldapDns, schoolId);
}

users.forEach((user) => {
user.roles.forEach((role) => {
if (role.name === 'student') students.push(user._id);
if (role.name === 'teacher') teachers.push(user._id);
users.forEach((user) => {
user.roles.forEach((role) => {
if (role.name === 'student') students.push(user._id);
if (role.name === 'teacher') teachers.push(user._id);
});
});
});
}

await ClassRepo.updateClassStudents(classId, students);
await ClassRepo.updateClassTeachers(classId, teachers);
Expand Down
64 changes: 41 additions & 23 deletions test/services/sync/repo/user.repo.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,45 +188,63 @@ describe('user repo', () => {
});

describe('findByLdapDnsAndSchool', () => {
it('should return empty list if not found', async () => {
const testSchool = await testObjects.createTestSchool();
const res = await UserRepo.findByLdapDnsAndSchool('Not existed dn', testSchool._id);
const setup = async () => {
const ldapDn = 'TEST_LDAP_DN';
const ldapDn2 = 'TEST_LDAP_DN2';
const previousLdapDn = 'PREVIOUS_LDAP_DN';
const notExistingLdapDn = 'NOT_EXISTING_LDAP_DN';
const ldapDns = [ldapDn, ldapDn2];

const school = await testObjects.createTestSchool();

const migratedUser = await testObjects.createTestUser({
previousExternalId: previousLdapDn,
schoolId: school._id,
ldapDn: 'NEW_ID',
});
const createdUsers = [
await testObjects.createTestUser({ ldapDn, schoolId: school._id }),
await testObjects.createTestUser({ ldapDn2, schoolId: school._id }),
];

return {
ldapDns,
notExistingLdapDn,
previousLdapDn,
migratedUser,
createdUsers,
school,
};
};

it('should return empty list if user with ldapDn does not exist', async () => {
const { school, notExistingLdapDn } = await setup();

const res = await UserRepo.findByLdapDnsAndSchool([notExistingLdapDn], school._id);

expect(res).to.eql([]);
});

it('should find user by ldap dn and school', async () => {
const ldapDns = ['TEST_LDAP_DN', 'TEST_LDAP_DN2'];
const school = await testObjects.createTestSchool();
const createdUsers = await Promise.all(
ldapDns.map((ldapDn) => testObjects.createTestUser({ ldapDn, schoolId: school._id }))
);
const { school, ldapDns, createdUsers } = await setup();

const res = await UserRepo.findByLdapDnsAndSchool(ldapDns, school._id);

const user1 = res.filter((user) => createdUsers[0]._id.toString() === user._id.toString());
const user2 = res.filter((user) => createdUsers[1]._id.toString() === user._id.toString());

expect(user1).not.to.be.undefined;
expect(user2).not.to.be.undefined;
});

describe('when the user has migrated', () => {
const setup = async () => {
const ldapDn = 'TEST_LDAP_DN';
const school = await testObjects.createTestSchool();
const user = await testObjects.createTestUser({ previousExternalId: ldapDn, schoolId: school._id });

return {
ldapDn,
user,
school,
};
};

it('should find the user by its old ldap dn and school', async () => {
const { ldapDn, school, user } = await setup();
const { previousLdapDn, school, migratedUser } = await setup();

const res = await UserRepo.findByLdapDnsAndSchool([ldapDn], school._id);
const res = await UserRepo.findByLdapDnsAndSchool([previousLdapDn], school._id);

expect(res.length).to.equal(1);
expect(res[0]._id.toString()).to.equal(user._id.toString());
expect(res[0]._id.toString()).to.equal(migratedUser._id.toString());
});
});
});
Expand Down
21 changes: 21 additions & 0 deletions test/services/sync/strategies/consumerActions/ClassAction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,26 @@ describe('Class Actions', () => {
expect(updateClassTeachersStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString());
expect(updateClassTeachersStub.getCall(0).lastArg).to.eql(['user2', 'user3']);
});

it.only('should not add any user to the class', async () => {
const uniqueMembers = undefined;

const foundUsers = [
{
_id: 'user1',
roles: [{ name: 'student' }],
},
];
// const findByLdapDnsAndSchoolStub = sinon.stub(UserRepo, 'findByLdapDnsAndSchool');
// findByLdapDnsAndSchoolStub.returns(foundUsers);

const schoolObj = { _id: new ObjectId(), currentYear: new ObjectId() };
await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers);

// expect(findByLdapDnsAndSchoolStub.calledOnce).to.be.false;
// expect(findByLdapDnsAndSchoolStub.getCall(0)).to.equal(schoolObj);
expect(updateClassStudentsStub.getCall(0).lastArg).to.eql([]);
expect(updateClassTeachersStub.getCall(0).lastArg).to.eql([]);
});
});
});

0 comments on commit 2f5f66c

Please sign in to comment.