Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marode-cap committed Nov 27, 2024
1 parent 847c69f commit 5b43bf4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 208 deletions.
159 changes: 0 additions & 159 deletions src/core/ldap/domain/ldap-event-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,131 +473,6 @@ describe('LDAP Event Handler', () => {
expect(ldapClientServiceMock.createLehrer).toHaveBeenCalledTimes(0);
});

it('should call ldap client for every deleted personenkontext with correct role (if person has PK with rollenArt LEHR left)', async () => {
const event: PersonenkontextUpdatedEvent = new PersonenkontextUpdatedEvent(
{
id: faker.string.uuid(),
vorname: faker.person.firstName(),
familienname: faker.person.lastName(),
referrer: faker.internet.userName(),
},
[],
[
{
id: faker.string.uuid(),
orgaId: faker.string.uuid(),
rolle: RollenArt.LEHR,
rolleId: faker.string.uuid(),
orgaKennung: faker.string.numeric(7),
isItslearningOrga: false,
serviceProviderExternalSystems: [],
},
{
id: faker.string.uuid(),
orgaId: faker.string.uuid(),
rolle: RollenArt.EXTERN,
rolleId: faker.string.uuid(),
orgaKennung: faker.string.numeric(7),
isItslearningOrga: false,
serviceProviderExternalSystems: [],
},
],
[],
);

organisationRepositoryMock.findEmailDomainForOrganisation.mockResolvedValueOnce('schule-sh.de');

await ldapEventHandler.handlePersonenkontextUpdatedEvent(event);

expect(ldapClientServiceMock.deleteLehrer).toHaveBeenCalledTimes(1);
});

it('should NOT call ldap client for deleting person in LDAP when person still has at least one PK with rollenArt LEHR left', async () => {
const event: PersonenkontextUpdatedEvent = new PersonenkontextUpdatedEvent(
{
id: faker.string.uuid(),
vorname: faker.person.firstName(),
familienname: faker.person.lastName(),
referrer: faker.internet.userName(),
},
[],
[
{
id: faker.string.uuid(),
orgaId: faker.string.uuid(),
rolle: RollenArt.LEHR,
rolleId: faker.string.uuid(),
orgaKennung: faker.string.numeric(7),
isItslearningOrga: false,
serviceProviderExternalSystems: [],
},
],
[
{
id: faker.string.uuid(),
orgaId: faker.string.uuid(),
rolle: RollenArt.LEHR,
rolleId: faker.string.uuid(),
orgaKennung: faker.string.numeric(7),
isItslearningOrga: false,
serviceProviderExternalSystems: [],
},
],
);

organisationRepositoryMock.findEmailDomainForOrganisation.mockResolvedValueOnce('schule-sh.de');

await ldapEventHandler.handlePersonenkontextUpdatedEvent(event);

expect(ldapClientServiceMock.deleteLehrer).toHaveBeenCalledTimes(0);
expect(loggerMock.info).toHaveBeenLastCalledWith(
`Keep lehrer in LDAP, personId:${event.person.id}, because person keeps PK(s) with rollenArt LEHR`,
);
});

it('when organisation of deleted PK has no valid emailDomain should log error', async () => {
const removedOrgaId: string = faker.string.uuid();
const event: PersonenkontextUpdatedEvent = new PersonenkontextUpdatedEvent(
{
id: faker.string.uuid(),
vorname: faker.person.firstName(),
familienname: faker.person.lastName(),
referrer: faker.internet.userName(),
},
[],
[
{
id: faker.string.uuid(),
orgaId: removedOrgaId,
rolle: RollenArt.LEHR,
rolleId: faker.string.uuid(),
orgaKennung: faker.string.numeric(7),
isItslearningOrga: false,
serviceProviderExternalSystems: [],
},
{
id: faker.string.uuid(),
orgaId: faker.string.uuid(),
rolle: RollenArt.EXTERN,
rolleId: faker.string.uuid(),
orgaKennung: faker.string.numeric(7),
isItslearningOrga: false,
serviceProviderExternalSystems: [],
},
],
[],
);

organisationRepositoryMock.findEmailDomainForOrganisation.mockResolvedValueOnce(undefined);

await ldapEventHandler.handlePersonenkontextUpdatedEvent(event);

expect(loggerMock.error).toHaveBeenLastCalledWith(
`LdapClientService deleteLehrer NOT called, because organisation:${removedOrgaId} has no valid emailDomain`,
);
expect(ldapClientServiceMock.deleteLehrer).toHaveBeenCalledTimes(0);
});

describe('when ldap client fails', () => {
it('should execute without errors, if creation of lehrer fails', async () => {
const event: PersonenkontextUpdatedEvent = new PersonenkontextUpdatedEvent(
Expand Down Expand Up @@ -630,40 +505,6 @@ describe('LDAP Event Handler', () => {
expect(ldapClientServiceMock.createLehrer).toHaveBeenCalledTimes(1);
});
});

it('should execute without errors, if deletion of lehrer fails', async () => {
const event: PersonenkontextUpdatedEvent = new PersonenkontextUpdatedEvent(
{
id: faker.string.uuid(),
vorname: faker.person.firstName(),
familienname: faker.person.lastName(),
referrer: faker.internet.userName(),
},
[],
[
{
id: faker.string.uuid(),
orgaId: faker.string.uuid(),
rolle: RollenArt.LEHR,
rolleId: faker.string.uuid(),
orgaKennung: faker.string.numeric(7),
isItslearningOrga: false,
serviceProviderExternalSystems: [],
},
],
[],
);
ldapClientServiceMock.deleteLehrer.mockResolvedValueOnce({
ok: false,
error: new Error('Error'),
});

organisationRepositoryMock.findEmailDomainForOrganisation.mockResolvedValueOnce('schule-sh.de');

await ldapEventHandler.handlePersonenkontextUpdatedEvent(event);

expect(ldapClientServiceMock.deleteLehrer).toHaveBeenCalledTimes(1);
});
});

describe('handleEmailAddressGeneratedEvent', () => {
Expand Down
39 changes: 18 additions & 21 deletions src/modules/email/domain/email-event-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe('Email Event Handler', () => {
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockResolvedValueOnce(undefined); //no existing email is found
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockResolvedValueOnce([]); //no existing email is found

const persistenceResult: EmailAddress<true> = getEmail();
emailRepoMock.save.mockResolvedValueOnce(persistenceResult); //mock: error during saving the entity
Expand Down Expand Up @@ -326,16 +326,16 @@ describe('Email Event Handler', () => {
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

// eslint-disable-next-line @typescript-eslint/require-await
emailRepoMock.findEnabledByPerson.mockImplementationOnce(async (personId: PersonID) => {
return new EmailAddress<true>(
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockImplementationOnce(async (personId: PersonID) => [
new EmailAddress<true>(
faker.string.uuid(),
faker.date.past(),
faker.date.recent(),
personId,
faker.internet.email(),
EmailAddressStatus.ENABLED,
);
});
),
]);

await emailEventHandler.handlePersonenkontextUpdatedEvent(event);

Expand All @@ -352,16 +352,16 @@ describe('Email Event Handler', () => {
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

// eslint-disable-next-line @typescript-eslint/require-await
emailRepoMock.findEnabledByPerson.mockImplementationOnce(async (personId: PersonID) => {
return new EmailAddress<true>(
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockImplementationOnce(async (personId: PersonID) => [
new EmailAddress<true>(
faker.string.uuid(),
faker.date.past(),
faker.date.recent(),
personId,
faker.internet.email(),
EmailAddressStatus.DISABLED,
);
});
),
]);

const persistedEmail: EmailAddress<true> = getEmail();
emailRepoMock.save.mockResolvedValueOnce(persistedEmail);
Expand All @@ -381,16 +381,16 @@ describe('Email Event Handler', () => {
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

// eslint-disable-next-line @typescript-eslint/require-await
emailRepoMock.findEnabledByPerson.mockImplementationOnce(async (personId: PersonID) => {
return new EmailAddress<true>(
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockImplementationOnce(async (personId: PersonID) => [
new EmailAddress<true>(
faker.string.uuid(),
faker.date.past(),
faker.date.recent(),
personId,
faker.internet.email(),
EmailAddressStatus.DISABLED,
);
});
),
]);

emailRepoMock.save.mockResolvedValueOnce(new EmailAddressNotFoundError(fakeEmailAddressString));

Expand All @@ -408,7 +408,7 @@ describe('Email Event Handler', () => {
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockResolvedValueOnce(undefined); //no existing email is found
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockResolvedValueOnce([]); //no existing email is found

const persistenceResult: EmailAddress<true> = getEmail();
emailRepoMock.save.mockResolvedValueOnce(persistenceResult); //mock: error during saving the entity
Expand Down Expand Up @@ -443,7 +443,7 @@ describe('Email Event Handler', () => {
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

emailRepoMock.findEnabledByPerson.mockResolvedValueOnce(undefined); //no existing email is found
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockResolvedValueOnce([]); //no existing email is found

// eslint-disable-next-line @typescript-eslint/require-await
emailFactoryMock.createNew.mockImplementationOnce(async (personId: PersonID) => {
Expand Down Expand Up @@ -481,7 +481,7 @@ describe('Email Event Handler', () => {
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

emailRepoMock.findEnabledByPerson.mockResolvedValueOnce(undefined); //no existing email is found
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockResolvedValueOnce([]); //no existing email is found

// eslint-disable-next-line @typescript-eslint/require-await
emailFactoryMock.createNew.mockImplementationOnce(async (personId: PersonID) => {
Expand Down Expand Up @@ -509,7 +509,7 @@ describe('Email Event Handler', () => {
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

emailRepoMock.findEnabledByPerson.mockResolvedValueOnce(undefined); //no existing email is found
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockResolvedValueOnce([]); //no existing email is found

emailRepoMock.save.mockResolvedValueOnce(new EmailAddressNotFoundError(fakeEmailAddressString)); //mock: error during saving the entity

Expand Down Expand Up @@ -640,10 +640,7 @@ describe('Email Event Handler', () => {
inputEmailAdress,
);

// eslint-disable-next-line @typescript-eslint/require-await
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockImplementationOnce(async () => {
return undefined;
});
emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockResolvedValueOnce([]);
emailRepoMock.save.mockResolvedValueOnce(createMock<EmailAddress<true>>());

await emailEventHandler.handlePersonenkontextCreatedMigrationEvent(event);
Expand Down
27 changes: 15 additions & 12 deletions src/modules/email/domain/email-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,17 @@ export class EmailEventHandler {
const organisationKennung: Result<string> = await this.getOrganisationKennung(organisationId);
if (!organisationKennung.ok) return;

// TODO: Marvin
const existingEmail: Option<EmailAddress<true>> = await this.emailRepo.findEnabledByPerson(personId);
if (existingEmail) {
this.logger.info(`Existing email found for personId:${personId}`);
const existingEmails: EmailAddress<true>[] = await this.emailRepo.findByPersonSortedByUpdatedAtDesc(personId);

if (existingEmail.enabled) {
for (const email of existingEmails) {
if (email.enabled) {
return this.logger.info(`Existing email for personId:${personId} already enabled`);
} else {
existingEmail.request();
const persistenceResult: EmailAddress<true> | DomainError = await this.emailRepo.save(existingEmail);
} else if (email.disabled) {
email.request();

// Will return after the first iteration, so it's not an await in a loop
// eslint-disable-next-line no-await-in-loop
const persistenceResult: EmailAddress<true> | DomainError = await this.emailRepo.save(email);
if (persistenceResult instanceof EmailAddress) {
this.logger.info(`Set Requested status and persisted address:${persistenceResult.address}`);
this.eventService.publish(
Expand All @@ -361,13 +362,15 @@ export class EmailEventHandler {
),
);
} else {
return this.logger.error(`Could not enable email, error is ${persistenceResult.message}`);
this.logger.error(`Could not enable email, error is ${persistenceResult.message}`);
}

return;
}
} else {
this.logger.info(`No existing email found for personId:${personId}, creating a new one`);
await this.createNewEmail(personId, organisationId);
}

this.logger.info(`No existing email found for personId:${personId}, creating a new one`);
await this.createNewEmail(personId, organisationId);
}

private async createAndPersistFailedEmailAddress(personId: PersonID): Promise<void> {
Expand Down
12 changes: 6 additions & 6 deletions src/modules/email/persistence/email.repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,17 @@ describe('EmailRepo', () => {
});

describe('when no status is provided and person does NOT exist', () => {
it('should return undefined', async () => {
it('should return empty array', async () => {
const foundEmails: Option<EmailAddress<true>[]> = await sut.findByPersonSortedByUpdatedAtDesc(
faker.string.uuid(),
);

expect(foundEmails).toBeUndefined();
expect(foundEmails).toEqual([]);
});
});

describe('when ENABLED status is provided but only requested email-address is found for personId', () => {
it('should return undefined', async () => {
it('should return empty array', async () => {
const person: Person<true> = await createPerson();
const organisation: Organisation<true> = await createOrganisation();
const email: Result<EmailAddress<false>> = await emailFactory.createNew(person.id, organisation.id);
Expand All @@ -230,7 +230,7 @@ describe('EmailRepo', () => {
EmailAddressStatus.ENABLED,
);

expect(foundEmails).toBeUndefined();
expect(foundEmails).toEqual([]);
});
});

Expand All @@ -256,13 +256,13 @@ describe('EmailRepo', () => {
});

describe('when status is provided and person does NOT exist', () => {
it('should return undefined', async () => {
it('should return empty array', async () => {
const foundEmails: Option<EmailAddress<true>[]> = await sut.findByPersonSortedByUpdatedAtDesc(
faker.string.uuid(),
EmailAddressStatus.ENABLED,
);

expect(foundEmails).toBeUndefined();
expect(foundEmails).toEqual([]);
});
});
});
Expand Down
Loading

0 comments on commit 5b43bf4

Please sign in to comment.