Skip to content

Commit

Permalink
SPSH-1286: Fixed test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
phaelcg committed Nov 29, 2024
1 parent ff4eff8 commit 4f9ccae
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 25 deletions.
19 changes: 12 additions & 7 deletions src/modules/import/api/import.controller.integration-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { PagedResponse } from '../../../shared/paging/paged.response.js';
import { ImportVorgangResponse } from './importvorgang.response.js';
import { ImportStatus } from '../domain/import.enums.js';

describe('Rolle API', () => {
describe('Import API', () => {
let app: INestApplication;
let orm: MikroORM;
let em: EntityManager;
Expand Down Expand Up @@ -113,6 +113,7 @@ describe('Rolle API', () => {
personPermissionsMock = createMock<PersonPermissions>();
personpermissionsRepoMock.loadPersonPermissions.mockResolvedValue(personPermissionsMock);
personPermissionsMock.getOrgIdsWithSystemrecht.mockResolvedValue({ all: false, orgaIds: [] });
personPermissionsMock.personFields.username = faker.internet.userName();
await DatabaseTestModule.setupDatabase(module.get(MikroORM));
app = module.createNestApplication();
await app.init();
Expand Down Expand Up @@ -481,17 +482,19 @@ describe('Rolle API', () => {
);
if (sus instanceof DomainError) throw sus;

const importvorgangId: string = faker.string.uuid();
const importVorgang: ImportVorgang<true> = await importVorgangRepository.save(
DoFactory.createImportVorgang(false),
);
const importDataItem: ImportDataItem<true> = await importDataRepository.save(
DoFactory.createImportDataItem(false, {
importvorgangId: importvorgangId,
importvorgangId: importVorgang.id,
klasse: klasse.name,
personalnummer: undefined,
}),
);

const params: ImportvorgangByIdBodyParams = {
importvorgangId: importvorgangId,
importvorgangId: importVorgang.id,
organisationId: schule.id,
rolleId: sus.id,
};
Expand Down Expand Up @@ -528,17 +531,19 @@ describe('Rolle API', () => {

describe('/DELETE deleteImportTransaction', () => {
it('should return 204', async () => {
const importvorgangId: string = faker.string.uuid();
const importVorgang: ImportVorgang<true> = await importVorgangRepository.save(
DoFactory.createImportVorgang(false),
);
await importDataRepository.save(
DoFactory.createImportDataItem(false, {
importvorgangId: importvorgangId,
importvorgangId: importVorgang.id,
klasse: faker.lorem.word(),
personalnummer: undefined,
}),
);

const response: Response = await request(app.getHttpServer() as App)
.delete(`/import/${importvorgangId}`)
.delete(`/import/${importVorgang.id}`)
.send();

expect(response.status).toBe(204);
Expand Down
5 changes: 5 additions & 0 deletions src/modules/import/api/import.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ImportvorgangByIdBodyParams } from './importvorgang-by-id.body.params.j
import { HttpException } from '@nestjs/common';
import { MissingPermissionsError } from '../../../shared/error/missing-permissions.error.js';
import { ImportvorgangByIdParams } from './importvorgang-by-id.params.js';
import { ImportVorgangRepository } from '../persistence/import-vorgang.repository.js';

describe('Import API with mocked ImportWorkflow', () => {
let sut: ImportController;
Expand All @@ -37,6 +38,10 @@ describe('Import API with mocked ImportWorkflow', () => {
provide: ImportWorkflow,
useValue: createMock<ImportWorkflow>(),
},
{
provide: ImportVorgangRepository,
useValue: createMock<ImportVorgangRepository>(),
},
ImportController,
],
}).compile();
Expand Down
82 changes: 64 additions & 18 deletions src/modules/import/domain/import-workflow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ import { ImportNurLernAnSchuleUndKlasseError } from './import-nur-lern-an-schule
import { ImportCSVFileParsingError } from './import-csv-file-parsing.error.js';
import { ImportCSVFileInvalidHeaderError } from './import-csv-file-invalid-header.error.js';
import { ImportDataItem } from './import-data-item.js';
import { ImportVorgangRepository } from '../persistence/import-vorgang.repository.js';
import { ImportVorgang } from './import-vorgang.js';

describe('ImportWorkflow', () => {
let module: TestingModule;
let rolleRepoMock: DeepMocked<RolleRepo>;
let organisationRepoMock: DeepMocked<OrganisationRepository>;
let importDataRepositoryMock: DeepMocked<ImportDataRepository>;
let personenkontextCreationServiceMock: DeepMocked<PersonenkontextCreationService>;
let importVorgangRepositoryMock: DeepMocked<ImportVorgangRepository>;
let sut: ImportWorkflow;
let importWorkflowFactory: ImportWorkflowFactory;
let personpermissionsMock: DeepMocked<PersonPermissions>;
Expand Down Expand Up @@ -68,12 +71,17 @@ describe('ImportWorkflow', () => {
provide: PersonPermissions,
useValue: createMock<PersonPermissions>(),
},
{
provide: ImportVorgangRepository,
useValue: createMock<ImportVorgangRepository>(),
},
],
}).compile();
rolleRepoMock = module.get(RolleRepo);
organisationRepoMock = module.get(OrganisationRepository);
importDataRepositoryMock = module.get(ImportDataRepository);
personenkontextCreationServiceMock = module.get(PersonenkontextCreationService);
importVorgangRepositoryMock = module.get(ImportVorgangRepository);
importWorkflowFactory = module.get(ImportWorkflowFactory);
sut = importWorkflowFactory.createNew();
personpermissionsMock = module.get(PersonPermissions);
Expand Down Expand Up @@ -300,15 +308,32 @@ describe('ImportWorkflow', () => {
});
});

it('should return EntityNotFoundError if a klasse during the import execution was deleted', async () => {
it('should return EntityNotFoundError if a ImportVorgang does not exist', async () => {
personpermissionsMock.hasSystemrechteAtRootOrganisation.mockResolvedValue(true);
organisationRepoMock.findChildOrgasForIds.mockResolvedValueOnce([
DoFactory.createOrganisation(true, { typ: OrganisationsTyp.KLASSE, name: '1A' }),
]);

importVorgangRepositoryMock.findById.mockResolvedValueOnce(null);
const importvorgangId: string = faker.string.uuid();

const result: Result<Buffer> = await sut.executeImport(importvorgangId, personpermissionsMock);

expect(result).toEqual({
ok: false,
error: new EntityNotFoundError('ImportVorgang', importvorgangId),
});
expect(personenkontextCreationServiceMock.createPersonWithPersonenkontexte).not.toHaveBeenCalled();
});

it('should return EntityNotFoundError if a klasse during the import execution was deleted', async () => {
personpermissionsMock.hasSystemrechteAtRootOrganisation.mockResolvedValue(true);
organisationRepoMock.findChildOrgasForIds.mockResolvedValueOnce([
DoFactory.createOrganisation(true, { typ: OrganisationsTyp.KLASSE, name: '1A' }),
]);
const importvorgang: ImportVorgang<true> = DoFactory.createImportVorgang(true);
importVorgangRepositoryMock.findById.mockResolvedValueOnce(importvorgang);
const importDataItem: ImportDataItem<true> = DoFactory.createImportDataItem(true, {
importvorgangId,
importvorgangId: importvorgang.id,
klasse: '1B',
});
importDataRepositoryMock.findByImportVorgangId.mockResolvedValueOnce([[importDataItem], 1]);
Expand All @@ -317,7 +342,7 @@ describe('ImportWorkflow', () => {
`Klasse=${importDataItem.klasse} for ${importDataItem.vorname} ${importDataItem.nachname} was not found`,
]);

await expect(sut.executeImport(importvorgangId, personpermissionsMock)).rejects.toThrowError(error);
await expect(sut.executeImport(importvorgang.id, personpermissionsMock)).rejects.toThrowError(error);
expect(personenkontextCreationServiceMock.createPersonWithPersonenkontexte).not.toHaveBeenCalled();
});

Expand All @@ -332,9 +357,10 @@ describe('ImportWorkflow', () => {
});
organisationRepoMock.findChildOrgasForIds.mockResolvedValueOnce([klasse]);

const importvorgangId: string = faker.string.uuid();
const importvorgang: ImportVorgang<true> = DoFactory.createImportVorgang(true);
importVorgangRepositoryMock.findById.mockResolvedValueOnce(importvorgang);
const importDataItem: ImportDataItem<true> = DoFactory.createImportDataItem(true, {
importvorgangId,
importvorgangId: importvorgang.id,
klasse: '1A',
});

Expand All @@ -354,7 +380,7 @@ describe('ImportWorkflow', () => {
rolleRepoMock.findById.mockResolvedValueOnce(rolle);

sut.initialize(SELECTED_ORGANISATION_ID, SELECTED_ROLLE_ID);
const result: Result<Buffer> = await sut.executeImport(importvorgangId, personpermissionsMock);
const result: Result<Buffer> = await sut.executeImport(importvorgang.id, personpermissionsMock);

if (!result.ok) {
throw new Error(result.error.message);
Expand All @@ -367,7 +393,7 @@ describe('ImportWorkflow', () => {
expect(resultString).toContain(person.vorname);
expect(resultString).toContain(person.familienname);
expect(resultString).toContain(klasse.name);
expect(importDataRepositoryMock.deleteByImportVorgangId).toHaveBeenCalledWith(importvorgangId);
expect(importDataRepositoryMock.deleteByImportVorgangId).toHaveBeenCalledWith(importvorgang.id);
});

it('should return EntityNotFoundError if the schule is not found', async () => {
Expand All @@ -381,9 +407,10 @@ describe('ImportWorkflow', () => {
});
organisationRepoMock.findChildOrgasForIds.mockResolvedValueOnce([klasse]);

const importvorgangId: string = faker.string.uuid();
const importvorgang: ImportVorgang<true> = DoFactory.createImportVorgang(true);
importVorgangRepositoryMock.findById.mockResolvedValueOnce(importvorgang);
const importDataItem: ImportDataItem<true> = DoFactory.createImportDataItem(true, {
importvorgangId,
importvorgangId: importvorgang.id,
klasse: '1A',
});
const pks: PersonPersonenkontext = {
Expand All @@ -398,7 +425,7 @@ describe('ImportWorkflow', () => {
organisationRepoMock.findById.mockResolvedValueOnce(undefined);

sut.initialize(SELECTED_ORGANISATION_ID, SELECTED_ROLLE_ID);
const result: Result<Buffer> = await sut.executeImport(importvorgangId, personpermissionsMock);
const result: Result<Buffer> = await sut.executeImport(importvorgang.id, personpermissionsMock);

expect(result).toEqual({
ok: false,
Expand All @@ -417,9 +444,10 @@ describe('ImportWorkflow', () => {
});
organisationRepoMock.findChildOrgasForIds.mockResolvedValueOnce([klasse]);

const importvorgangId: string = faker.string.uuid();
const importvorgang: ImportVorgang<true> = DoFactory.createImportVorgang(true);
importVorgangRepositoryMock.findById.mockResolvedValueOnce(importvorgang);
const importDataItem: ImportDataItem<true> = DoFactory.createImportDataItem(true, {
importvorgangId,
importvorgangId: importvorgang.id,
klasse: '1A',
});
const pks: PersonPersonenkontext = {
Expand All @@ -435,7 +463,7 @@ describe('ImportWorkflow', () => {
rolleRepoMock.findById.mockResolvedValueOnce(undefined);

sut.initialize(SELECTED_ORGANISATION_ID, SELECTED_ROLLE_ID);
const result: Result<Buffer> = await sut.executeImport(importvorgangId, personpermissionsMock);
const result: Result<Buffer> = await sut.executeImport(importvorgang.id, personpermissionsMock);

expect(result).toEqual({
ok: false,
Expand All @@ -454,9 +482,10 @@ describe('ImportWorkflow', () => {
});
organisationRepoMock.findChildOrgasForIds.mockResolvedValueOnce([klasse]);

const importvorgangId: string = faker.string.uuid();
const importvorgang: ImportVorgang<true> = DoFactory.createImportVorgang(true);
importVorgangRepositoryMock.findById.mockResolvedValueOnce(importvorgang);
const importDataItem: ImportDataItem<true> = DoFactory.createImportDataItem(true, {
importvorgangId,
importvorgangId: importvorgang.id,
klasse: '1A',
});
const pks: PersonPersonenkontext = {
Expand All @@ -475,7 +504,7 @@ describe('ImportWorkflow', () => {
});

sut.initialize(SELECTED_ORGANISATION_ID, SELECTED_ROLLE_ID);
const result: Result<Buffer> = await sut.executeImport(importvorgangId, personpermissionsMock);
const result: Result<Buffer> = await sut.executeImport(importvorgang.id, personpermissionsMock);

expect(result).toEqual({
ok: false,
Expand Down Expand Up @@ -508,10 +537,27 @@ describe('ImportWorkflow', () => {
});
});

it('should return EntityNotFoundError if a ImportVorgang does not exist', async () => {
personpermissionsMock.hasSystemrechteAtRootOrganisation.mockResolvedValueOnce(true);
importVorgangRepositoryMock.findById.mockResolvedValueOnce(null);
const importVorgangId: string = faker.string.uuid();

const result: Result<void> = await sut.cancelImport(importVorgangId, personpermissionsMock);

expect(result).toEqual({
ok: false,
error: new EntityNotFoundError('ImportVorgang', importVorgangId),
});
expect(importDataRepositoryMock.deleteByImportVorgangId).not.toHaveBeenCalled();
});

it('should call the importDataRepository if the admin has permissions to cancel the import transaction', async () => {
personpermissionsMock.hasSystemrechteAtRootOrganisation.mockResolvedValueOnce(true);
const importvorgang: ImportVorgang<true> = DoFactory.createImportVorgang(true);
importVorgangRepositoryMock.findById.mockResolvedValueOnce(importvorgang);

const result: Result<void> = await sut.cancelImport(importvorgang.id, personpermissionsMock);

const result: Result<void> = await sut.cancelImport(faker.string.uuid(), personpermissionsMock);
expect(importDataRepositoryMock.deleteByImportVorgangId).toHaveBeenCalled();
expect(result).toEqual({ ok: true, value: undefined });
});
Expand Down

0 comments on commit 4f9ccae

Please sign in to comment.