Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPSH-1288 #774

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cda389c
Log creation of Schule and Klasse
he-meyer Nov 14, 2024
3edd75f
Log update of Klasse name
he-meyer Nov 14, 2024
b5443a7
Add logger
he-meyer Nov 14, 2024
5b2b6d3
Log deletion of Klasse
he-meyer Nov 14, 2024
3131ac3
Log update of Schule and Klasse
he-meyer Nov 14, 2024
7339c6c
Log creation, update and deletion of Rolle
he-meyer Nov 14, 2024
93753d3
Add logger to tests
he-meyer Nov 14, 2024
a783823
Add logger to test
he-meyer Nov 14, 2024
8b69ca7
Adapt test
he-meyer Nov 14, 2024
775c1f0
Adapt test
he-meyer Nov 18, 2024
0962b43
Adapt test
he-meyer Nov 19, 2024
c9c601d
Adapt test
he-meyer Nov 19, 2024
c41858b
Adapt helper function and log statements
he-meyer Nov 19, 2024
2297f3d
Adapt tests
he-meyer Nov 19, 2024
491e369
Fix tests
he-meyer Nov 20, 2024
f1a9c6d
Adapt tests to increase coverage
he-meyer Nov 20, 2024
eda234a
Add and adapt tests to increase coverage
he-meyer Nov 20, 2024
7b4d908
Add tests for coverage
he-meyer Nov 20, 2024
8ab0fb6
Adapt for coverage
he-meyer Nov 20, 2024
f7edf92
Log username
he-meyer Nov 22, 2024
829975a
Use empty mock in tests
he-meyer Nov 25, 2024
1e02e15
Use empty mocks in tests
he-meyer Nov 25, 2024
68f0bad
Avoid undefined checks and adapt tests
he-meyer Nov 25, 2024
514829a
Don't log Organisation name
he-meyer Nov 26, 2024
2964f72
Merge branch 'main' of https://github.com/dBildungsplattform/dbildung…
he-meyer Nov 26, 2024
8dd1055
Fix duplicate
he-meyer Nov 26, 2024
1751955
Add log for a new error case
he-meyer Nov 26, 2024
ea40302
Fix tests
he-meyer Nov 26, 2024
00ee643
Avoid undefined checks
he-meyer Nov 26, 2024
ac8c1df
Use correct log level
he-meyer Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions src/modules/organisation/api/organisation.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('OrganisationController', () => {
});

describe('createOrganisation', () => {
const permissionsMock: PersonPermissions = createMock<PersonPermissions>();
describe('when usecase returns a DTO', () => {
it('should not throw an error', async () => {
const params: CreateOrganisationBodyParams = {
Expand All @@ -101,7 +102,7 @@ describe('OrganisationController', () => {
const returnedValue: Organisation<true> = DoFactory.createOrganisation(true);

organisationServiceMock.createOrganisation.mockResolvedValueOnce({ ok: true, value: returnedValue });
await expect(organisationController.createOrganisation(params)).resolves.not.toThrow();
await expect(organisationController.createOrganisation(permissionsMock, params)).resolves.not.toThrow();
expect(organisationServiceMock.createOrganisation).toHaveBeenCalledTimes(1);
});
});
Expand Down Expand Up @@ -153,7 +154,7 @@ describe('OrganisationController', () => {
organisationRepositoryMock.findRootDirectChildren.mockResolvedValue(mockedRepoResponse);

try {
await organisationController.createOrganisation(params);
await organisationController.createOrganisation(permissionsMock, params);

fail('Expected error was not thrown');
} catch (error) {
Expand Down Expand Up @@ -202,7 +203,7 @@ describe('OrganisationController', () => {
error: new OrganisationSpecificationError('error', undefined),
});
await expect(
organisationController.createOrganisation({} as CreateOrganisationBodyParams),
organisationController.createOrganisation(permissionsMock, {} as CreateOrganisationBodyParams),
).rejects.toThrow(OrganisationSpecificationError);
expect(organisationServiceMock.createOrganisation).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -249,14 +250,15 @@ describe('OrganisationController', () => {
error: {} as EntityNotFoundError,
});
await expect(
organisationController.createOrganisation({} as CreateOrganisationBodyParams),
organisationController.createOrganisation(permissionsMock, {} as CreateOrganisationBodyParams),
).rejects.toThrow(HttpException);
expect(organisationServiceMock.createOrganisation).toHaveBeenCalledTimes(1);
});
});
});

describe('updateOrganisation', () => {
const permissionsMock: DeepMocked<PersonPermissions> = createMock<PersonPermissions>();
describe('when usecase returns a DTO', () => {
it('should not throw an error', async () => {
const params: OrganisationByIdParams = {
Expand All @@ -276,7 +278,9 @@ describe('OrganisationController', () => {
const returnedValue: Organisation<true> = DoFactory.createOrganisation(true);

organisationServiceMock.updateOrganisation.mockResolvedValue({ ok: true, value: returnedValue });
await expect(organisationController.updateOrganisation(params, body)).resolves.not.toThrow();
await expect(
organisationController.updateOrganisation(params, body, permissionsMock),
).resolves.not.toThrow();
expect(organisationServiceMock.updateOrganisation).toHaveBeenCalledTimes(1);
});
});
Expand All @@ -291,6 +295,7 @@ describe('OrganisationController', () => {
organisationController.updateOrganisation(
{ organisationId: faker.string.uuid() } as OrganisationByIdParams,
{} as UpdateOrganisationBodyParams,
permissionsMock,
),
).rejects.toThrow(OrganisationSpecificationError);
expect(organisationServiceMock.updateOrganisation).toHaveBeenCalledTimes(1);
Expand All @@ -307,6 +312,7 @@ describe('OrganisationController', () => {
organisationController.updateOrganisation(
{ organisationId: faker.string.uuid() } as OrganisationByIdParams,
{} as UpdateOrganisationBodyParams,
permissionsMock,
),
).rejects.toThrow(HttpException);
expect(organisationServiceMock.updateOrganisation).toHaveBeenCalledTimes(1);
Expand All @@ -320,6 +326,7 @@ describe('OrganisationController', () => {
organisationController.updateOrganisation(
{ organisationId: organisationId } as OrganisationByIdParams,
{} as UpdateOrganisationBodyParams,
permissionsMock,
),
).rejects.toThrow(new NotFoundException(`Organisation with ID ${organisationId} not found`));
expect(organisationRepositoryMock.findById).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -719,6 +726,7 @@ describe('OrganisationController', () => {
});

describe('updateOrganisationName', () => {
const permissionsMock: PersonPermissions = createMock<PersonPermissions>();
describe('when usecase succeeds', () => {
it('should not throw an error', async () => {
const oeffentlich: Organisation<true> = Organisation.construct(
Expand All @@ -745,7 +753,9 @@ describe('OrganisationController', () => {

organisationRepositoryMock.updateKlassenname.mockResolvedValueOnce(oeffentlich);

await expect(organisationController.updateOrganisationName(params, body)).resolves.not.toThrow();
await expect(
organisationController.updateOrganisationName(params, body, permissionsMock),
).resolves.not.toThrow();
});
});

Expand All @@ -760,9 +770,9 @@ describe('OrganisationController', () => {
};
organisationRepositoryMock.updateKlassenname.mockResolvedValueOnce(new NameRequiredForKlasseError());

await expect(organisationController.updateOrganisationName(params, body)).rejects.toThrow(
NameRequiredForKlasseError,
);
await expect(
organisationController.updateOrganisationName(params, body, permissionsMock),
).rejects.toThrow(NameRequiredForKlasseError);
});
});

Expand All @@ -778,14 +788,15 @@ describe('OrganisationController', () => {

organisationRepositoryMock.updateKlassenname.mockResolvedValueOnce(new EntityNotFoundError());

await expect(organisationController.updateOrganisationName(params, body)).rejects.toThrow(
HttpException,
);
await expect(
organisationController.updateOrganisationName(params, body, permissionsMock),
).rejects.toThrow(HttpException);
});
});
});

describe('updateOrganisationName', () => {
const permissionsMock: PersonPermissions = createMock<PersonPermissions>();
describe('when usecase succeeds', () => {
it('should not throw an error', async () => {
const oeffentlich: Organisation<true> = Organisation.construct(
Expand All @@ -812,7 +823,9 @@ describe('OrganisationController', () => {

organisationRepositoryMock.updateKlassenname.mockResolvedValueOnce(oeffentlich);

await expect(organisationController.updateOrganisationName(params, body)).resolves.not.toThrow();
await expect(
organisationController.updateOrganisationName(params, body, permissionsMock),
).resolves.not.toThrow();
});
});

Expand All @@ -827,9 +840,9 @@ describe('OrganisationController', () => {
};
organisationRepositoryMock.updateKlassenname.mockResolvedValueOnce(new NameRequiredForKlasseError());

await expect(organisationController.updateOrganisationName(params, body)).rejects.toThrow(
NameRequiredForKlasseError,
);
await expect(
organisationController.updateOrganisationName(params, body, permissionsMock),
).rejects.toThrow(NameRequiredForKlasseError);
});
});

Expand All @@ -845,9 +858,9 @@ describe('OrganisationController', () => {

organisationRepositoryMock.updateKlassenname.mockResolvedValueOnce(new EntityNotFoundError());

await expect(organisationController.updateOrganisationName(params, body)).rejects.toThrow(
HttpException,
);
await expect(
organisationController.updateOrganisationName(params, body, permissionsMock),
).rejects.toThrow(HttpException);
});
});
});
Expand Down
21 changes: 17 additions & 4 deletions src/modules/organisation/api/organisation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export class OrganisationController {
@ApiUnauthorizedResponse({ description: 'Not authorized to create the organisation.' })
@ApiForbiddenResponse({ description: 'Not permitted to create the organisation.' })
@ApiInternalServerErrorResponse({ description: 'Internal server error while creating the organisation.' })
public async createOrganisation(@Body() params: CreateOrganisationBodyParams): Promise<OrganisationResponse> {
public async createOrganisation(
@Permissions() permissions: PersonPermissions,
@Body() params: CreateOrganisationBodyParams,
): Promise<OrganisationResponse> {
const [oeffentlich]: [Organisation<true> | undefined, Organisation<true> | undefined] =
await this.organisationRepository.findRootDirectChildren();

Expand All @@ -106,6 +109,7 @@ export class OrganisationController {
}
const result: Result<Organisation<true>, DomainError> = await this.organisationService.createOrganisation(
organisation,
permissions,
);
if (!result.ok) {
if (result.error instanceof OrganisationSpecificationError) {
Expand Down Expand Up @@ -133,6 +137,7 @@ export class OrganisationController {
public async updateOrganisation(
@Param() params: OrganisationByIdParams,
@Body() body: UpdateOrganisationBodyParams,
@Permissions() permissions: PersonPermissions,
): Promise<OrganisationResponse> {
const existingOrganisation: Option<Organisation<true>> = await this.organisationRepository.findById(
params.organisationId,
Expand All @@ -156,6 +161,7 @@ export class OrganisationController {

const result: Result<Organisation<true>, DomainError> = await this.organisationService.updateOrganisation(
existingOrganisation,
permissions,
);

if (result.ok) {
Expand Down Expand Up @@ -423,12 +429,18 @@ export class OrganisationController {
@ApiBadRequestResponse({ description: 'The input was not valid.', type: DbiamOrganisationError })
@ApiNotFoundResponse({ description: 'The organisation that should be deleted does not exist.' })
@ApiUnauthorizedResponse({ description: 'Not authorized to delete the organisation.' })
public async deleteKlasse(@Param() params: OrganisationByIdParams): Promise<void> {
public async deleteKlasse(
@Param() params: OrganisationByIdParams,
@Permissions() permissions: PersonPermissions,
): Promise<void> {
if (await this.dBiamPersonenkontextRepo.isOrganisationAlreadyAssigned(params.organisationId)) {
throw new OrganisationIstBereitsZugewiesenError();
}

const result: Option<DomainError> = await this.organisationRepository.deleteKlasse(params.organisationId);
const result: Option<DomainError> = await this.organisationRepository.deleteKlasse(
params.organisationId,
permissions,
);
if (result instanceof DomainError) {
throw SchulConnexErrorMapper.mapSchulConnexErrorToHttpException(
SchulConnexErrorMapper.mapDomainErrorToSchulConnexError(result),
Expand All @@ -450,18 +462,19 @@ export class OrganisationController {
public async updateOrganisationName(
@Param() params: OrganisationByIdParams,
@Body() body: OrganisationByNameBodyParams,
@Permissions() permissions: PersonPermissions,
): Promise<OrganisationResponse | DomainError> {
const result: DomainError | Organisation<true> = await this.organisationRepository.updateKlassenname(
params.organisationId,
body.name,
body.version,
permissions,
);

if (result instanceof DomainError) {
if (result instanceof OrganisationSpecificationError) {
throw result;
}

throw SchulConnexErrorMapper.mapSchulConnexErrorToHttpException(
SchulConnexErrorMapper.mapDomainErrorToSchulConnexError(result),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { EventModule } from '../../../core/eventbus/index.js';
import { Organisation } from './organisation.js';
import { OrganisationsTyp } from './organisation.enums.js';
import { OrganisationRepository } from '../persistence/organisation.repository.js';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { PersonPermissions } from '../../authentication/domain/person-permissions.js';

describe('OrganisationServiceSpecificationTest', () => {
let module: TestingModule;
Expand All @@ -35,6 +37,7 @@ describe('OrganisationServiceSpecificationTest', () => {
DatabaseTestModule.forRoot({ isDatabaseRequired: true }),
MapperTestModule,
EventModule,
LoggingTestModule,
],
providers: [OrganisationService, OrganisationRepository, OrganisationPersistenceMapperProfile],
}).compile();
Expand Down Expand Up @@ -74,6 +77,7 @@ describe('OrganisationServiceSpecificationTest', () => {
});

describe('create', () => {
const permissionsMock: DeepMocked<PersonPermissions> = createMock<PersonPermissions>();
it('should return DomainError, when KlasseNurVonSchuleAdministriert specificaton is not satisfied and type is KLASSE', async () => {
const klasseDo: Organisation<boolean> = DoFactory.createOrganisation(false, {
name: 'Klasse',
Expand All @@ -84,6 +88,7 @@ describe('OrganisationServiceSpecificationTest', () => {

const result: Result<Organisation<true>, DomainError> = await organisationService.createOrganisation(
klasseDo,
permissionsMock,
);

expect(result).toEqual<Result<Organisation<true>>>({
Expand Down Expand Up @@ -113,7 +118,10 @@ describe('OrganisationServiceSpecificationTest', () => {
zugehoerigZu: schule.id,
typ: OrganisationsTyp.KLASSE,
});
const result: Result<Organisation<true>> = await organisationService.createOrganisation(weitereKlasseDo);
const result: Result<Organisation<true>> = await organisationService.createOrganisation(
weitereKlasseDo,
permissionsMock,
);

expect(result).toEqual<Result<Organisation<true>>>({
ok: false,
Expand All @@ -122,6 +130,7 @@ describe('OrganisationServiceSpecificationTest', () => {
});
});
describe('update', () => {
const permissionsMock: DeepMocked<PersonPermissions> = createMock<PersonPermissions>();
it('should return DomainError, when klasse specifications are not satisfied and type is klasse', async () => {
const klasse: Organisation<boolean> = DoFactory.createOrganisation(false, {
name: 'klasse',
Expand All @@ -133,6 +142,7 @@ describe('OrganisationServiceSpecificationTest', () => {

const result: Result<Organisation<true>, DomainError> = await organisationService.updateOrganisation(
klassePersisted,
permissionsMock,
);

expect(result).toEqual<Result<Organisation<true>>>({
Expand Down
Loading
Loading