From 2f5f66c05cd716e53af6122352d6060abcb2a0ea Mon Sep 17 00:00:00 2001 From: Igor Richter Date: Wed, 27 Sep 2023 12:19:50 +0200 Subject: [PATCH] unit tests WIP2 --- .../strategies/consumerActions/ClassAction.js | 15 ++--- .../sync/repo/user.repo.integration.test.js | 64 ++++++++++++------- .../consumerActions/ClassAction.test.js | 21 ++++++ 3 files changed, 69 insertions(+), 31 deletions(-) diff --git a/src/services/sync/strategies/consumerActions/ClassAction.js b/src/services/sync/strategies/consumerActions/ClassAction.js index 151281b0407..bc39ac3ffd2 100644 --- a/src/services/sync/strategies/consumerActions/ClassAction.js +++ b/src/services/sync/strategies/consumerActions/ClassAction.js @@ -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); diff --git a/test/services/sync/repo/user.repo.integration.test.js b/test/services/sync/repo/user.repo.integration.test.js index eecb30e9e3c..08948756cfe 100644 --- a/test/services/sync/repo/user.repo.integration.test.js +++ b/test/services/sync/repo/user.repo.integration.test.js @@ -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()); }); }); }); diff --git a/test/services/sync/strategies/consumerActions/ClassAction.test.js b/test/services/sync/strategies/consumerActions/ClassAction.test.js index 4b450df5622..7f0165c4014 100644 --- a/test/services/sync/strategies/consumerActions/ClassAction.test.js +++ b/test/services/sync/strategies/consumerActions/ClassAction.test.js @@ -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([]); + }); }); });