Skip to content

Commit

Permalink
Merge branch 'main' into BC-5583-display-pdf-preview
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax authored Nov 15, 2023
2 parents 44bed1a + b3b4cee commit 0e0450e
Show file tree
Hide file tree
Showing 132 changed files with 2,653 additions and 3,870 deletions.
2 changes: 1 addition & 1 deletion apps/server/src/core/logger/types/logging.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type ErrorLogMessage = {
error?: Error;
type: string; // TODO: use enum
stack?: string;
data?: { [key: string]: string | number | undefined };
data?: { [key: string]: string | number | boolean | undefined };
};

export type ValidationErrorLogMessage = {
Expand Down
1 change: 0 additions & 1 deletion apps/server/src/modules/authentication/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './brute-force.error';
export * from './ldap-connection.error';
export * from './school-in-migration.error';
export * from './unauthorized.loggable-exception';
1 change: 1 addition & 0 deletions apps/server/src/modules/authentication/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { ICurrentUser } from './interface';
export { JWT, CurrentUser, Authenticate } from './decorator';
export { AuthenticationModule } from './authentication.module';
export { AuthenticationService } from './services';
1 change: 1 addition & 0 deletions apps/server/src/modules/authentication/loggable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './school-in-migration.loggable-exception';
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { SchoolInMigrationLoggableException } from './school-in-migration.loggable-exception';

describe(SchoolInMigrationLoggableException.name, () => {
describe('getLogMessage', () => {
const setup = () => {
const exception = new SchoolInMigrationLoggableException();

return {
exception,
};
};

it('should return the correct log message', () => {
const { exception } = setup();

const message = exception.getLogMessage();

expect(message).toEqual({
type: 'SCHOOL_IN_MIGRATION',
stack: expect.any(String),
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { HttpStatus } from '@nestjs/common';
import { BusinessError } from '@shared/common';
import { ErrorLogMessage, Loggable } from '@src/core/logger';

export class SchoolInMigrationError extends BusinessError {
constructor(details?: Record<string, unknown>) {
export class SchoolInMigrationLoggableException extends BusinessError implements Loggable {
constructor() {
super(
{
type: 'SCHOOL_IN_MIGRATION',
title: 'Login failed because school is in migration',
defaultMessage: 'Login failed because creation of user is not possible during migration',
},
HttpStatus.UNAUTHORIZED,
details
HttpStatus.UNAUTHORIZED
);
}

getLogMessage(): ErrorLogMessage {
return {
type: this.type,
stack: this.stack,
};
}
}
2 changes: 2 additions & 0 deletions apps/server/src/modules/authentication/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ldap.service';
export * from './authentication.service';
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { AccountService } from '@modules/account/services/account.service';
import { AccountDto } from '@modules/account/services/dto';
import { OAuthTokenDto, OAuthService } from '@modules/oauth';
import { UnauthorizedException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { EntityId, RoleName } from '@shared/domain';
import { UserDO } from '@shared/domain/domainobject/user.do';
import { userDoFactory } from '@shared/testing';
import { AccountService } from '@modules/account/services/account.service';
import { AccountDto } from '@modules/account/services/dto';
import { OAuthTokenDto } from '@modules/oauth';
import { OAuthService } from '@modules/oauth/service/oauth.service';
import { SchoolInMigrationError } from '../errors/school-in-migration.error';
import { ICurrentUser, OauthCurrentUser } from '../interface';
import { SchoolInMigrationLoggableException } from '../loggable';
import { Oauth2Strategy } from './oauth2.strategy';

describe('Oauth2Strategy', () => {
Expand Down Expand Up @@ -68,7 +67,7 @@ describe('Oauth2Strategy', () => {
refreshToken: 'refreshToken',
})
);
oauthService.provisionUser.mockResolvedValue({ user, redirect: '' });
oauthService.provisionUser.mockResolvedValue(user);
accountService.findByUserId.mockResolvedValue(account);

return { systemId, user, account, idToken };
Expand Down Expand Up @@ -102,7 +101,7 @@ describe('Oauth2Strategy', () => {
refreshToken: 'refreshToken',
})
);
oauthService.provisionUser.mockResolvedValue({ user: undefined, redirect: '' });
oauthService.provisionUser.mockResolvedValue(null);
};

it('should throw a SchoolInMigrationError', async () => {
Expand All @@ -111,7 +110,7 @@ describe('Oauth2Strategy', () => {
const func = async () =>
strategy.validate({ body: { code: 'code', redirectUri: 'redirectUri', systemId: 'systemId' } });

await expect(func).rejects.toThrow(new SchoolInMigrationError());
await expect(func).rejects.toThrow(new SchoolInMigrationLoggableException());
});
});

Expand All @@ -126,7 +125,7 @@ describe('Oauth2Strategy', () => {
refreshToken: 'refreshToken',
})
);
oauthService.provisionUser.mockResolvedValue({ user, redirect: '' });
oauthService.provisionUser.mockResolvedValue(user);
accountService.findByUserId.mockResolvedValue(null);
};

Expand Down
17 changes: 6 additions & 11 deletions apps/server/src/modules/authentication/strategy/oauth2.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { AccountService } from '@modules/account/services/account.service';
import { AccountDto } from '@modules/account/services/dto';
import { OAuthTokenDto, OAuthService } from '@modules/oauth';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { UserDO } from '@shared/domain/domainobject/user.do';
import { AccountService } from '@modules/account/services/account.service';
import { AccountDto } from '@modules/account/services/dto';
import { OAuthTokenDto } from '@modules/oauth';
import { OAuthService } from '@modules/oauth/service/oauth.service';
import { Strategy } from 'passport-custom';
import { Oauth2AuthorizationBodyParams } from '../controllers/dto';
import { SchoolInMigrationError } from '../errors/school-in-migration.error';
import { ICurrentUser, OauthCurrentUser } from '../interface';
import { SchoolInMigrationLoggableException } from '../loggable';
import { CurrentUserMapper } from '../mapper';

@Injectable()
Expand All @@ -22,14 +21,10 @@ export class Oauth2Strategy extends PassportStrategy(Strategy, 'oauth2') {

const tokenDto: OAuthTokenDto = await this.oauthService.authenticateUser(systemId, redirectUri, code);

const { user }: { user?: UserDO; redirect: string } = await this.oauthService.provisionUser(
systemId,
tokenDto.idToken,
tokenDto.accessToken
);
const user: UserDO | null = await this.oauthService.provisionUser(systemId, tokenDto.idToken, tokenDto.accessToken);

if (!user || !user.id) {
throw new SchoolInMigrationError();
throw new SchoolInMigrationLoggableException();
}

const account: AccountDto | null = await this.accountService.findByUserId(user.id);
Expand Down
3 changes: 0 additions & 3 deletions apps/server/src/modules/legacy-school/controller/dto/index.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0e0450e

Please sign in to comment.