Skip to content

Commit

Permalink
SPSH-1553: Fixed test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
phaelcg committed Dec 12, 2024
1 parent 426bdfc commit 95db629
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/modules/import/api/dbiam-import.error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum ImportErrorI18nTypes {
IMPORT_NUR_LERN_AN_SCHULE_ERROR = 'IMPORT_NUR_LERN_AN_SCHULE_ERROR',
CSV_FILE_INVALID_HEADER_ERROR = 'CSV_FILE_INVALID_HEADER_ERROR',
IMPORT_MAX_USERS_LIMIT_ERROR = 'IMPORT_MAX_USERS_LIMIT_ERROR',
CSV_FILE_NO_USERS_ERROR = 'CSV_FILE_NO_USERS_ERROR',
}

export type DbiamImportErrorProps = DbiamErrorProps & {
Expand Down
8 changes: 8 additions & 0 deletions src/modules/import/api/import-exception-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ImportCSVFileEmptyError } from '../domain/import-csv-file-empty.error.j
import { ImportNurLernAnSchuleUndKlasseError } from '../domain/import-nur-lern-an-schule-und-klasse.error.js';
import { ImportCSVFileInvalidHeaderError } from '../domain/import-csv-file-invalid-header.error.js';
import { ImportCSVFileMaxUsersError } from '../domain/import-csv-file-max-users.error.js';
import { ImportCSVFileContainsNoUsersError } from '../domain/import-csv-file-contains-no-users.error.js';

@Catch(ImportDomainError)
export class ImportExceptionFilter implements ExceptionFilter<ImportDomainError> {
Expand Down Expand Up @@ -55,6 +56,13 @@ export class ImportExceptionFilter implements ExceptionFilter<ImportDomainError>
i18nKey: ImportErrorI18nTypes.IMPORT_MAX_USERS_LIMIT_ERROR,
}),
],
[
ImportCSVFileContainsNoUsersError.name,
new DbiamImportError({
code: 400,
i18nKey: ImportErrorI18nTypes.CSV_FILE_NO_USERS_ERROR,
}),
],
]);

public catch(exception: ImportDomainError, host: ArgumentsHost): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ImportDomainError } from './import-domain.error.js';

export class ImportCSVFileContainsNoUsersError extends ImportDomainError {
public constructor(details?: unknown[] | Record<string, undefined>) {
super(`CSV File is invalid because the file contains no users`, undefined, details);
}
}
25 changes: 25 additions & 0 deletions src/modules/import/domain/import-workflow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ImportStatus } from './import.enums.js';
import { EventModule } from '../../../core/eventbus/event.module.js';
import { ConfigTestModule } from '../../../../test/utils/config-test.module.js';
import { ImportCSVFileMaxUsersError } from './import-csv-file-max-users.error.js';
import { ImportCSVFileContainsNoUsersError } from './import-csv-file-contains-no-users.error.js';

describe('ImportWorkflow', () => {
let module: TestingModule;
Expand Down Expand Up @@ -250,6 +251,30 @@ describe('ImportWorkflow', () => {
expect(importDataRepositoryMock.save).not.toHaveBeenCalled();
});

it('should return ImportCSVFileContainsNoUsersError if the csv file contains no data items', async () => {
const file: Express.Multer.File = createMock<Express.Multer.File>();
file.buffer = Buffer.from('Nachname;Vorname;Klasse');

const rolleMock: DeepMocked<Rolle<true>> = createMock<Rolle<true>>();
rolleMock.rollenart = RollenArt.LERN;
rolleMock.canBeAssignedToOrga.mockResolvedValueOnce(true);
organisationRepoMock.findById.mockResolvedValueOnce(
DoFactory.createOrganisation(true, { typ: OrganisationsTyp.SCHULE }),
);
rolleRepoMock.findById.mockResolvedValueOnce(rolleMock);

personpermissionsMock.hasSystemrechteAtRootOrganisation.mockResolvedValue(true);

const result: DomainError | ImportUploadResultFields = await sut.validateImport(
file,
SELECTED_ORGANISATION_ID,
SELECTED_ROLLE_ID,
personpermissionsMock,
);
expect(result).toBeInstanceOf(ImportCSVFileContainsNoUsersError);
expect(importDataRepositoryMock.save).not.toHaveBeenCalled();
});

it('should return ImportCSVFileParsingError if the parser cannot parse', async () => {
const rolleMock: DeepMocked<Rolle<true>> = createMock<Rolle<true>>();
rolleMock.rollenart = RollenArt.LERN;
Expand Down
5 changes: 5 additions & 0 deletions src/modules/import/domain/import-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { ConfigService } from '@nestjs/config';
import { ServerConfig } from '../../../shared/config/server.config.js';
import { ImportConfig } from '../../../shared/config/import.config.js';
import { ImportCSVFileMaxUsersError } from './import-csv-file-max-users.error.js';
import { ImportCSVFileContainsNoUsersError } from './import-csv-file-contains-no-users.error.js';

export type ImportUploadResultFields = {
importVorgangId: string;
Expand Down Expand Up @@ -141,6 +142,10 @@ export class ImportWorkflow {
return new ImportCSVFileParsingError(parsedData.errors);
}

if (parsedData.data.length === 0) {
return new ImportCSVFileContainsNoUsersError();
}

parsedDataItems = plainToInstance(CSVImportDataItemDTO, parsedData.data);
} catch (error) {
if (error instanceof ImportCSVFileInvalidHeaderError) {
Expand Down

0 comments on commit 95db629

Please sign in to comment.