Skip to content

Commit

Permalink
fixing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderUngefug committed Dec 16, 2024
1 parent 897f21b commit 066e047
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
24 changes: 24 additions & 0 deletions src/core/ldap/domain/ldap-client.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,30 @@ describe('LDAP Client Service', () => {
expect(clientMock.modify).not.toHaveBeenCalled();
expect(clientMock.modifyDN).not.toHaveBeenCalled();
});

it('should return error if updateMemberDnInGroups fails', async () => {
const oldReferrer: string = faker.internet.userName();
const newUid: string = faker.string.alphanumeric(6);

jest.spyOn(ldapClientService, 'updateMemberDnInGroups').mockResolvedValueOnce({
ok: false,
error: new Error('Failed to update groups'),
});

const result: Result<PersonID> = await ldapClientService.modifyPersonAttributes(
oldReferrer,
undefined,
undefined,
newUid,
);

expect(result.ok).toBeFalsy();
if (result.ok) throw Error();
expect(result.error?.message).toBe('Failed to update groups');
expect(loggerMock.error).toHaveBeenCalledWith(
`LDAP: Failed to update groups for person: ${oldReferrer}`,
);
});
});
});
});
Expand Down
28 changes: 16 additions & 12 deletions src/core/ldap/domain/ldap-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export class LdapClientService {

private static readonly GROUPS: string = 'groups';

private static readonly USERS_OU: string = 'users';

private mutex: Mutex;

public constructor(
Expand Down Expand Up @@ -298,7 +300,10 @@ export class LdapClientService {
newUid,
client,
);
if (!groupUpdateResult.ok) return groupUpdateResult;
if (!groupUpdateResult.ok) {
this.logger.error(`LDAP: Failed to update groups for person: ${oldReferrer}`);
return groupUpdateResult;
}
}

return { ok: true, value: oldReferrer };
Expand All @@ -310,8 +315,8 @@ export class LdapClientService {
newReferrer: string,
client: Client,
): Promise<Result<string>> {
const oldReferrerUid: string = this.getLehrerUid(oldReferrer, 'users');
const newReferrerUid: string = this.getLehrerUid(newReferrer, 'users');
const oldReferrerUid: string = this.getLehrerUid(oldReferrer, LdapClientService.USERS_OU);
const newReferrerUid: string = this.getLehrerUid(newReferrer, LdapClientService.USERS_OU);

const searchResult: SearchResult = await client.search(`${this.ldapInstanceConfig.BASE_DN}`, {
scope: 'sub',
Expand Down Expand Up @@ -562,7 +567,7 @@ export class LdapClientService {
const newLehrerGroup: { cn: string; objectclass: string[]; member: string[] } = {
cn: groupId,
objectclass: ['groupOfNames'],
member: [this.getLehrerUid(personUid, 'users')],
member: [this.getLehrerUid(personUid, LdapClientService.USERS_OU)],
};
try {
await client.add(lehrerDn, newLehrerGroup);
Expand All @@ -575,7 +580,7 @@ export class LdapClientService {
}
}

if (this.isPersonInSearchResult(searchResultGroupOfNames, personUid)) {
if (this.isPersonInSearchResult(searchResultGroupOfNames.searchEntries[0], personUid)) {
this.logger.info(`LDAP: Person ${personUid} is already in group ${groupId}`);
return { ok: true, value: false };
}
Expand All @@ -586,7 +591,7 @@ export class LdapClientService {
operation: 'add',
modification: new Attribute({
type: 'member',
values: [this.getLehrerUid(personUid, 'users')],
values: [this.getLehrerUid(personUid, LdapClientService.USERS_OU)],
}),
}),
]);
Expand Down Expand Up @@ -618,7 +623,7 @@ export class LdapClientService {
return { ok: false, error: new Error(errMsg) };
}

if (!this.isPersonInSearchResult(searchResultOrgUnit, personUid)) {
if (!this.isPersonInSearchResult(searchResultOrgUnit.searchEntries[0], personUid)) {
this.logger.info(`LDAP: Person ${personUid} is not in group ${groupId}`);
return { ok: false, error: new Error(`Person ${personUid} is not in group ${groupId}`) };
}
Expand All @@ -635,7 +640,7 @@ export class LdapClientService {
operation: 'delete',
modification: new Attribute({
type: 'member',
values: [this.getLehrerUid(personUid, 'users')],
values: [this.getLehrerUid(personUid, LdapClientService.USERS_OU)],
}),
}),
]);
Expand All @@ -648,10 +653,9 @@ export class LdapClientService {
}
}

private isPersonInSearchResult(searchResult: SearchResult, personUid: string): boolean | undefined {
if (!searchResult.searchEntries[0]) return;
const member: string | string[] | Buffer | Buffer[] | undefined = searchResult.searchEntries[0]['member'];
const lehrerUid: string = this.getLehrerUid(personUid, 'users');
private isPersonInSearchResult(searchEntry: Entry, personUid: string): boolean | undefined {
const member: string | string[] | Buffer | Buffer[] | undefined = searchEntry['member'];
const lehrerUid: string = this.getLehrerUid(personUid, LdapClientService.USERS_OU);

if (typeof member === 'string') {
return member === lehrerUid;
Expand Down

0 comments on commit 066e047

Please sign in to comment.