Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N21 1309 fix empty class sync #4443

Merged
merged 13 commits into from
Oct 6, 2023
5 changes: 5 additions & 0 deletions src/services/sync/strategies/consumerActions/ClassAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class ClassAction extends BaseConsumerAction {
}

async addUsersToClass(schoolId, classId, uniqueMembers) {
if (!uniqueMembers) {
console.warn('uniqueMembers are undefined or null.');
IgorCapCoder marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const students = [];
const teachers = [];
const ldapDns = !Array.isArray(uniqueMembers) ? [uniqueMembers] : uniqueMembers;
Expand Down
72 changes: 49 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,71 @@ 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 return empty list if ldapDns are empty', async () => {
const { school } = await setup();

const res = await UserRepo.findByLdapDnsAndSchool([], 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
12 changes: 12 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,17 @@ 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('should not add any user to the class', async () => {
IgorCapCoder marked this conversation as resolved.
Show resolved Hide resolved
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;
});
});
});