From eca8570c1c7f2bb84cd55519756f794df8b0bbc2 Mon Sep 17 00:00:00 2001 From: Igor Richter Date: Wed, 4 Oct 2023 15:51:52 +0200 Subject: [PATCH] change scope of unique members assertion to fix current classes --- .../strategies/consumerActions/ClassAction.js | 21 ++++---- .../consumerActions/ClassAction.test.js | 48 +++++++------------ 2 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/services/sync/strategies/consumerActions/ClassAction.js b/src/services/sync/strategies/consumerActions/ClassAction.js index e071dc458a1..387b30f5a61 100644 --- a/src/services/sync/strategies/consumerActions/ClassAction.js +++ b/src/services/sync/strategies/consumerActions/ClassAction.js @@ -86,25 +86,20 @@ class ClassAction extends BaseConsumerAction { } async addUsersToClass(schoolId, classId, uniqueMembers) { - if (!uniqueMembers || !uniqueMembers[0]) { - console.warn( - 'In LDAP-Sync->ClassAction.js->addUsersToClass: uniqueMembers are not given: No users are added to class.' - ); - return; - } - const students = []; const teachers = []; const ldapDns = !Array.isArray(uniqueMembers) ? [uniqueMembers] : uniqueMembers; - const users = await UserRepo.findByLdapDnsAndSchool(ldapDns, schoolId); + if (uniqueMembers[0]) { + 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/strategies/consumerActions/ClassAction.test.js b/test/services/sync/strategies/consumerActions/ClassAction.test.js index cc0e8654d24..efbd48da37b 100644 --- a/test/services/sync/strategies/consumerActions/ClassAction.test.js +++ b/test/services/sync/strategies/consumerActions/ClassAction.test.js @@ -343,30 +343,6 @@ describe('Class Actions', () => { expect(updateClassTeachersStub.getCall(0).lastArg).to.eql(['user2', 'user3']); }); - it('should not add any user to the class, when uniqueMembers are undefined', async () => { - const uniqueMembers = undefined; - const schoolObj = { _id: new ObjectId(), currentYear: new ObjectId() }; - const findByLdapDnsAndSchoolStub = sinon.stub(UserRepo, 'findByLdapDnsAndSchool'); - - await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers); - - expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true; - expect(updateClassStudentsStub.notCalled).to.be.true; - expect(updateClassTeachersStub.notCalled).to.be.true; - }); - - it('should not add any user to the class, when uniqueMembers are null', async () => { - const uniqueMembers = null; - const schoolObj = { _id: new ObjectId(), currentYear: new ObjectId() }; - const findByLdapDnsAndSchoolStub = sinon.stub(UserRepo, 'findByLdapDnsAndSchool'); - - await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers); - - expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true; - expect(updateClassStudentsStub.notCalled).to.be.true; - expect(updateClassTeachersStub.notCalled).to.be.true; - }); - it('should not add any user to the class, when uniqueMembers are []', async () => { const uniqueMembers = []; const schoolObj = { _id: new ObjectId(), currentYear: new ObjectId() }; @@ -375,8 +351,12 @@ describe('Class Actions', () => { await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers); expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true; - expect(updateClassStudentsStub.notCalled).to.be.true; - expect(updateClassTeachersStub.notCalled).to.be.true; + + expect(updateClassStudentsStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString()); + expect(updateClassStudentsStub.getCall(0).lastArg).to.eql([]); + + expect(updateClassTeachersStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString()); + expect(updateClassTeachersStub.getCall(0).lastArg).to.eql([]); }); it('should not add any user to the class, when uniqueMembers are [undefined]', async () => { @@ -387,8 +367,12 @@ describe('Class Actions', () => { await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers); expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true; - expect(updateClassStudentsStub.notCalled).to.be.true; - expect(updateClassTeachersStub.notCalled).to.be.true; + + expect(updateClassStudentsStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString()); + expect(updateClassStudentsStub.getCall(0).lastArg).to.eql([]); + + expect(updateClassTeachersStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString()); + expect(updateClassTeachersStub.getCall(0).lastArg).to.eql([]); }); it('should not add any user to the class, when uniqueMembers are [null]', async () => { @@ -399,8 +383,12 @@ describe('Class Actions', () => { await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers); expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true; - expect(updateClassStudentsStub.notCalled).to.be.true; - expect(updateClassTeachersStub.notCalled).to.be.true; + + expect(updateClassStudentsStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString()); + expect(updateClassStudentsStub.getCall(0).lastArg).to.eql([]); + + expect(updateClassTeachersStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString()); + expect(updateClassTeachersStub.getCall(0).lastArg).to.eql([]); }); }); });