From ad967b8434406f35626b6613b0c7a2708393ccad Mon Sep 17 00:00:00 2001 From: Kristoff Kiefer Date: Wed, 18 Oct 2023 09:18:47 +0200 Subject: [PATCH] Adjusted tests --- .../frontend/api/frontend.controller.spec.ts | 2 +- src/modules/frontend/backend.service.ts | 35 +++++++++++++++++++ src/modules/health/health.controller.spec.ts | 1 + src/shared/config/BackendConfig.ts | 11 ++++++ src/shared/config/config.loader.spec.ts | 2 +- 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/modules/frontend/backend.service.ts create mode 100644 src/shared/config/BackendConfig.ts diff --git a/src/modules/frontend/api/frontend.controller.spec.ts b/src/modules/frontend/api/frontend.controller.spec.ts index ec8649993..a06dd3041 100644 --- a/src/modules/frontend/api/frontend.controller.spec.ts +++ b/src/modules/frontend/api/frontend.controller.spec.ts @@ -25,7 +25,7 @@ describe('FrontendController', () => { describe('Login', () => { it('should not throw', () => { - const loginResponse: string = frontendController.login(); + const loginResponse: Promise = frontendController.login(); expect(loginResponse).toBe('Login!'); }); diff --git a/src/modules/frontend/backend.service.ts b/src/modules/frontend/backend.service.ts new file mode 100644 index 000000000..e375cd2d7 --- /dev/null +++ b/src/modules/frontend/backend.service.ts @@ -0,0 +1,35 @@ +import { BackendConfig } from '../../shared/config/BackendConfig.js'; +import { ConfigService } from '@nestjs/config'; +import { HttpService } from '@nestjs/axios'; +import { UserParams } from '../ui-backend/api/user.params.js'; +import { catchError, firstValueFrom, map } from 'rxjs'; +import { AxiosError, AxiosResponse } from 'axios'; +import { Injectable, UnauthorizedException } from '@nestjs/common'; + +@Injectable() +export class BackendService { + private backendConfig: BackendConfig; + + private backendServerBasePath: string; + + public constructor(configService: ConfigService, private httpService: HttpService) { + this.backendConfig = configService.getOrThrow('BACKEND'); + this.backendServerBasePath = `http://${this.backendConfig.SERVER}:${this.backendConfig.PORT}`; + } + + public login(userParams: UserParams): Promise { + return firstValueFrom( + this.httpService + .post(`${this.backendServerBasePath}/api/login`, userParams) + .pipe( + catchError((e: AxiosError) => { + if ((e.status == 404)) { + throw new UnauthorizedException(); + } + throw e; + }), + ) + .pipe(map((v: AxiosResponse) => v.data)), + ); + } +} diff --git a/src/modules/health/health.controller.spec.ts b/src/modules/health/health.controller.spec.ts index 659c9e46c..71970d4f6 100644 --- a/src/modules/health/health.controller.spec.ts +++ b/src/modules/health/health.controller.spec.ts @@ -26,6 +26,7 @@ describe('HealthController', () => { BASE_URL: 'http://keycloak.test', REALM_NAME: '', CLIENT_ID: '', + SECRET: '', }; let configService: DeepMocked; diff --git a/src/shared/config/BackendConfig.ts b/src/shared/config/BackendConfig.ts new file mode 100644 index 000000000..b3753dc9c --- /dev/null +++ b/src/shared/config/BackendConfig.ts @@ -0,0 +1,11 @@ +import {IsInt, IsNotEmpty, IsString, Min} from 'class-validator'; + +export class BackendConfig { + @IsString() + @IsNotEmpty() + public readonly SERVER!: string; + + @IsInt() + @Min(1) + public readonly PORT!: number; +} diff --git a/src/shared/config/config.loader.spec.ts b/src/shared/config/config.loader.spec.ts index 1a23d4b5d..b3b60a043 100644 --- a/src/shared/config/config.loader.spec.ts +++ b/src/shared/config/config.loader.spec.ts @@ -48,7 +48,7 @@ describe('configloader', () => { const secrets: DeepPartial = { DB: { SECRET: 'SuperSecretSecret' }, - KEYCLOAK: { ADMIN_SECRET: 'ClientSecret' }, + KEYCLOAK: { ADMIN_SECRET: 'AdminSecret', SECRET: 'ClientSecret' }, }; beforeAll(() => {