Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into SPSH-1034
Browse files Browse the repository at this point in the history
  • Loading branch information
DeFi3298 committed Nov 27, 2024
2 parents 012eb13 + 7cb20b0 commit a45883a
Show file tree
Hide file tree
Showing 16 changed files with 1,078 additions and 64 deletions.
2 changes: 2 additions & 0 deletions src/core/ldap/domain/ldap-event-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ describe('LDAP Event Handler', () => {
faker.string.uuid(),
faker.internet.email(),
true,
faker.string.numeric(),
);

await ldapEventHandler.handleEmailAddressGeneratedEvent(event);
Expand All @@ -692,6 +693,7 @@ describe('LDAP Event Handler', () => {
faker.internet.email(),
faker.string.uuid(),
faker.internet.email(),
faker.string.numeric(),
);

await ldapEventHandler.handleEmailAddressChangedEvent(event);
Expand Down
4 changes: 4 additions & 0 deletions src/modules/email/domain/email-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export class EmailAddress<WasPersisted extends boolean> {
return this.addressStatus === EmailAddressStatus.ENABLED || this.addressStatus === EmailAddressStatus.REQUESTED;
}

public get disabled(): boolean {
return this.addressStatus === EmailAddressStatus.DISABLED;
}

public get personId(): PersonID {
return this.addressPersonId;
}
Expand Down
227 changes: 211 additions & 16 deletions src/modules/email/domain/email-event-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('Email Event Handler', () => {
let rolleRepoMock: DeepMocked<RolleRepo>;
let dbiamPersonenkontextRepoMock: DeepMocked<DBiamPersonenkontextRepo>;
let serviceProviderRepoMock: DeepMocked<ServiceProviderRepo>;
let organisationRepositoryMock: DeepMocked<OrganisationRepository>;
let loggerMock: DeepMocked<ClassLogger>;

beforeAll(async () => {
Expand Down Expand Up @@ -114,6 +115,7 @@ describe('Email Event Handler', () => {
rolleRepoMock = module.get(RolleRepo);
serviceProviderRepoMock = module.get(ServiceProviderRepo);
dbiamPersonenkontextRepoMock = module.get(DBiamPersonenkontextRepo);
organisationRepositoryMock = module.get(OrganisationRepository);
loggerMock = module.get(ClassLogger);

app = module.createNestApplication();
Expand Down Expand Up @@ -144,6 +146,148 @@ describe('Email Event Handler', () => {
});
}

describe('test private methods: createOrEnableEmail, createNewEmail, changeEmail', () => {
let fakePersonId: PersonID;
let fakeRolleId: RolleID;
let fakeOrgaId: string;
let fakeEmailAddress: string;
let event: PersonenkontextUpdatedEvent;
let personRenamedEvent: PersonRenamedEvent;
let personenkontext: Personenkontext<true>;
let personenkontexte: Personenkontext<true>[];
let rolle: Rolle<true>;
let rolleMap: Map<string, Rolle<true>>;
let sp: ServiceProvider<true>;
let spMap: Map<string, ServiceProvider<true>>;
let emailAddress: EmailAddress<true>;

beforeEach(() => {
jest.resetAllMocks();
fakePersonId = faker.string.uuid();
fakeRolleId = faker.string.uuid();
fakeOrgaId = faker.string.uuid();
fakeEmailAddress = faker.internet.email();
event = createMock<PersonenkontextUpdatedEvent>({ person: { id: fakePersonId } });
personRenamedEvent = new PersonRenamedEvent(
fakePersonId,
faker.person.firstName(),
faker.person.lastName(),
faker.internet.userName(),
faker.internet.userName(),
);
personenkontext = createMock<Personenkontext<true>>({ rolleId: fakeRolleId, organisationId: fakeOrgaId });
personenkontexte = [personenkontext];
rolle = createMock<Rolle<true>>({ id: fakeRolleId, serviceProviderIds: [] });
rolleMap = new Map<string, Rolle<true>>();
rolleMap.set(fakeRolleId, rolle);
sp = createMock<ServiceProvider<true>>({
kategorie: ServiceProviderKategorie.EMAIL,
});
spMap = new Map<string, ServiceProvider<true>>();
spMap.set(sp.id, sp);
emailAddress = EmailAddress.construct(
faker.string.uuid(),
faker.date.past(),
faker.date.recent(),
fakePersonId,
fakeEmailAddress,
EmailAddressStatus.ENABLED,
);
});

describe('createOrEnableEmail', () => {
describe('when orgaKennung CANNOT be found', () => {
it('should log matching error', async () => {
dbiamPersonenkontextRepoMock.findByPerson.mockResolvedValueOnce(personenkontexte);
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

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

const persistenceResult: EmailAddress<true> = getEmail();
emailRepoMock.save.mockResolvedValueOnce(persistenceResult); //mock: error during saving the entity

mockEmailFactoryCreateNewReturnsEnabledEmail(faker.internet.email());

// eslint-disable-next-line @typescript-eslint/require-await
emailFactoryMock.createNew.mockImplementationOnce(async (personId: PersonID) => {
const returnEmailAddress: EmailAddress<false> = EmailAddress.createNew(
personId,
faker.internet.email(),
EmailAddressStatus.ENABLED,
);

return {
ok: true,
value: returnEmailAddress,
};
});
organisationRepositoryMock.findById.mockResolvedValue(undefined);

await emailEventHandler.handlePersonenkontextUpdatedEvent(event);

expect(loggerMock.error).toHaveBeenCalledWith(
`Could not retrieve orgaKennung, orgaId:${fakeOrgaId}`,
);
});
});
});

describe('createNewEmail', () => {
describe('when orgaKennung CANNOT be found', () => {
it('should log matching error', async () => {
dbiamPersonenkontextRepoMock.findByPerson.mockResolvedValueOnce(personenkontexte);
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);

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

organisationRepositoryMock.findById.mockResolvedValue(undefined);

await emailEventHandler.handlePersonRenamedEvent(personRenamedEvent);

expect(loggerMock.error).toHaveBeenCalledWith(
`Could not retrieve orgaKennung, orgaId:${fakeOrgaId}`,
);
});
});
});

describe('changeEmail', () => {
describe('when orgaKennung CANNOT be found', () => {
it('should log error', async () => {
dbiamPersonenkontextRepoMock.findByPerson.mockResolvedValueOnce([personenkontext]);
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap);
emailRepoMock.findEnabledByPerson.mockResolvedValueOnce(emailAddress);

emailRepoMock.save.mockResolvedValueOnce(emailAddress);

//mock createNewEmail
emailFactoryMock.createNew.mockResolvedValueOnce({
ok: false,
error: new EntityCouldNotBeCreated('EmailAddress'),
});

//mock persisting new email
emailRepoMock.save.mockResolvedValueOnce(emailAddress);

organisationRepositoryMock.findById.mockResolvedValue(undefined);

await emailEventHandler.handlePersonRenamedEvent(personRenamedEvent);

expect(loggerMock.info).toHaveBeenCalledWith(
`Received PersonRenamedEvent, personId:${personRenamedEvent.personId}`,
);
expect(loggerMock.info).toHaveBeenCalledWith(`Disabled and saved address:${emailAddress.address}`);
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Could not retrieve orgaKennung, orgaId:${fakeOrgaId}`,
);
});
});
});
});

describe('handlePersonenkontextUpdatedEvent', () => {
let fakePersonId: PersonID;
let fakeRolleId: RolleID;
Expand Down Expand Up @@ -171,6 +315,8 @@ describe('Email Event Handler', () => {
});
spMap = new Map<string, ServiceProvider<true>>();
spMap.set(sp.id, sp);

organisationRepositoryMock.findById.mockResolvedValue(createMock<Organisation<true>>());
});

describe('when email exists and is enabled', () => {
Expand Down Expand Up @@ -388,6 +534,68 @@ describe('Email Event Handler', () => {
);
});
});

describe('when lehrer does not have any PK, email is enabled, disable email and error occurs during persisting', () => {
it('should log matching info', async () => {
dbiamPersonenkontextRepoMock.findByPerson.mockResolvedValueOnce(personenkontexte);
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(new Map<string, ServiceProvider<true>>());

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

await emailEventHandler.handlePersonenkontextUpdatedEvent(event);

expect(loggerMock.info).toHaveBeenCalledWith(
expect.stringContaining('Existing email found for personId'),
);
expect(loggerMock.error).toHaveBeenCalledWith(
`Could not disable email, error is requested EmailAddress with the address:${fakeEmailAddressString} was not found`,
);
});
});

[
{ status: EmailAddressStatus.ENABLED },
{ status: EmailAddressStatus.REQUESTED },
{ status: EmailAddressStatus.FAILED },
].forEach(({ status }: { status: EmailAddressStatus }) => {
describe(`when lehrer does not have any PK, email is ${status}, disable email is successfull`, () => {
it('should log matching info', async () => {
dbiamPersonenkontextRepoMock.findByPerson.mockResolvedValueOnce(personenkontexte);
rolleRepoMock.findByIds.mockResolvedValueOnce(rolleMap);
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(new Map<string, ServiceProvider<true>>());

const emailAddress: EmailAddress<true> = EmailAddress.construct(
faker.string.uuid(),
faker.date.past(),
faker.date.recent(),
fakePersonId,
faker.internet.email(),
EmailAddressStatus.DISABLED,
);

emailRepoMock.findByPersonSortedByUpdatedAtDesc.mockResolvedValueOnce([
EmailAddress.construct(
faker.string.uuid(),
faker.date.past(),
faker.date.recent(),
fakePersonId,
faker.internet.email(),
status,
),
]);

emailRepoMock.save.mockResolvedValueOnce(emailAddress);
await emailEventHandler.handlePersonenkontextUpdatedEvent(event);

expect(loggerMock.info).toHaveBeenCalledWith(
expect.stringContaining('Existing email found for personId'),
);
expect(loggerMock.info).toHaveBeenCalledWith(`Disabled and saved address:${emailAddress.address}`);
});
});
});
});

describe('handlePersonenkontextCreatedMigrationEvent', () => {
Expand Down Expand Up @@ -568,6 +776,8 @@ describe('Email Event Handler', () => {
fakeEmailAddress,
EmailAddressStatus.ENABLED,
);

organisationRepositoryMock.findById.mockResolvedValue(createMock<Organisation<true>>());
});

describe('when rolle exists and service provider with kategorie email is found', () => {
Expand Down Expand Up @@ -652,7 +862,7 @@ describe('Email Event Handler', () => {
});
});

describe('when enabled email DOES NOT exist and creating new email is successfull', () => {
describe('when enabled email DOES NOT exist and creating new email is successful', () => {
it('should log info', async () => {
dbiamPersonenkontextRepoMock.findByPerson.mockResolvedValueOnce([personenkontext]);
rolleRepoMock.findByIds.mockResolvedValueOnce(rollenMap);
Expand Down Expand Up @@ -823,21 +1033,6 @@ describe('Email Event Handler', () => {
expect(loggerMock.info).toHaveBeenCalledWith(`RolleUpdatedEvent affects:2 persons`);
});
});

describe('when rolle is updated but person should not get an email-address by event', () => {
it('should log info', async () => {
dbiamPersonenkontextRepoMock.findByRolle.mockResolvedValueOnce(personenkontexte);

//mock that no email-address is necessary for person to test handlePerson
dbiamPersonenkontextRepoMock.findByPerson.mockResolvedValue([]);
rolleRepoMock.findByIds.mockResolvedValue(new Map<string, Rolle<true>>());
serviceProviderRepoMock.findByIds.mockResolvedValue(new Map<string, ServiceProvider<true>>());

await emailEventHandler.handleRolleUpdatedEvent(event);

expect(loggerMock.info).toHaveBeenCalledWith(`Person with id:${fakePersonId} does not need an email`);
});
});
});

describe('handleOxUserAttributesCreatedEvent', () => {
Expand Down
Loading

0 comments on commit a45883a

Please sign in to comment.