From cce86849c072243e57bd3a32e77474e6326c3cc9 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 8 Nov 2023 12:34:48 +0100 Subject: [PATCH 01/96] endpoints preparation --- .../deletionExecutions.controller.ts | 26 ++++++++ .../controller/deletionRequests.controller.ts | 60 +++++++++++++++++++ .../dto/deletion-execution.params.ts | 9 +++ .../dto/deletion-request-log.response.ts | 21 +++++++ .../dto/deletion-request.body.params.ts | 19 ++++++ .../dto/deletion-request.response.ts | 14 +++++ .../modules/deletion/controller/dto/index.ts | 4 ++ .../deletion-request-log.builder.spec.ts | 6 +- .../builder/deletion-request-log.builder.ts | 7 ++- .../deletion/uc/deletion-request.uc.spec.ts | 12 ++-- .../deletion/uc/deletion-request.uc.ts | 18 +++--- .../deletion/uc/interface/interfaces.ts | 16 ----- 12 files changed, 173 insertions(+), 39 deletions(-) create mode 100644 apps/server/src/modules/deletion/controller/deletionExecutions.controller.ts create mode 100644 apps/server/src/modules/deletion/controller/deletionRequests.controller.ts create mode 100644 apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts create mode 100644 apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts create mode 100644 apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts create mode 100644 apps/server/src/modules/deletion/controller/dto/deletion-request.response.ts create mode 100644 apps/server/src/modules/deletion/controller/dto/index.ts diff --git a/apps/server/src/modules/deletion/controller/deletionExecutions.controller.ts b/apps/server/src/modules/deletion/controller/deletionExecutions.controller.ts new file mode 100644 index 00000000000..4a6898dc1ea --- /dev/null +++ b/apps/server/src/modules/deletion/controller/deletionExecutions.controller.ts @@ -0,0 +1,26 @@ +import { Controller, Post, Query } from '@nestjs/common'; +import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; +import { Authenticate } from '@src/modules/authentication/decorator/auth.decorator'; +import { ForbiddenOperationError, ValidationError } from '@shared/common'; +import { DeletionRequestUc } from '../uc/deletion-request.uc'; +import { DeletionExecutionParams } from './dto'; + +@ApiTags('DeletionExecutions') +@Authenticate('jwt') +@Controller('deletionExecutions') +export class AccountController { + constructor(private readonly deletionRequestUc: DeletionRequestUc) {} + + @Post() + @ApiOperation({ + summary: '"Queueing" a deletion request', + }) + @ApiResponse({ + status: 204, + }) + @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) + @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'User is not a superhero or administrator.' }) + async executeDeletions(@Query() deletionExecutionQuery: DeletionExecutionParams) { + return this.deletionRequestUc.executeDeletionRequests(deletionExecutionQuery.limit); + } +} diff --git a/apps/server/src/modules/deletion/controller/deletionRequests.controller.ts b/apps/server/src/modules/deletion/controller/deletionRequests.controller.ts new file mode 100644 index 00000000000..a32eca60aac --- /dev/null +++ b/apps/server/src/modules/deletion/controller/deletionRequests.controller.ts @@ -0,0 +1,60 @@ +import { Body, Controller, Get, Param, Post } from '@nestjs/common'; +import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; +import { Authenticate } from '@src/modules/authentication/decorator/auth.decorator'; +import { ForbiddenOperationError, ValidationError } from '@shared/common'; +import { DeletionRequestUc } from '../uc/deletion-request.uc'; +import { DeletionRequestResponse } from './dto/deletion-request.response'; +import { DeletionRequestBodyProps } from './dto/deletion-request.body.params'; +import { DeletionRequestLogResponse } from './dto'; + +@ApiTags('DeletionRequests') +@Authenticate('jwt') +@Controller('deletionRequests') +export class DeletionRequestsController { + constructor(private readonly deletionRequestUc: DeletionRequestUc) {} + + @Post() + @ApiOperation({ + summary: '"Queueing" a deletion request', + }) + @ApiResponse({ + status: 202, + type: DeletionRequestResponse, + description: 'Returns identifier of the deletion request and when deletion is planned at', + }) + @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) + @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'User is not a superhero or administrator.' }) + async createDeletionRequests( + @Body() deletionRequestBody: DeletionRequestBodyProps + ): Promise { + return this.deletionRequestUc.createDeletionRequest(deletionRequestBody); + } + + @Get(':requestId') + @ApiOperation({ + summary: 'Retrieving details of performed or planned deletion', + }) + @ApiResponse({ + status: 202, + type: DeletionRequestLogResponse, + description: 'Return details of performed or planned deletion', + }) + @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) + @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'User is not a superhero or administrator.' }) + async getPerformedDeletionDetails(@Param('requestId') requestId: string): Promise { + return this.deletionRequestUc.findById(requestId); + } + + @Get(':requestId') + @ApiOperation({ + summary: 'Canceling a deletion request', + }) + @ApiResponse({ + status: 204, + }) + @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) + @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'User is not a superhero or administrator.' }) + async cancelDeletionRequest(@Param('requestId') requestId: string) { + return this.deletionRequestUc.deleteDeletionRequestById(requestId); + } +} diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts new file mode 100644 index 00000000000..f7c027bc2ad --- /dev/null +++ b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts @@ -0,0 +1,9 @@ +import { ApiPropertyOptional } from '@nestjs/swagger'; +import { IsInt, Min } from 'class-validator'; + +export class DeletionExecutionParams { + @IsInt() + @Min(0) + @ApiPropertyOptional({ description: 'Page limit, defaults to 100.', minimum: 0 }) + limit?: number = 100; +} diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts new file mode 100644 index 00000000000..f8eb5ce10e3 --- /dev/null +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts @@ -0,0 +1,21 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsOptional } from 'class-validator/types/decorator/decorators'; +import { DeletionLogStatistic, DeletionTargetRef } from '../../uc/interface'; + +export class DeletionRequestLogResponse { + @ApiProperty() + targetRef: DeletionTargetRef; + + @ApiProperty() + deletionPlannedAt: Date; + + @ApiProperty() + @IsOptional() + statistics?: DeletionLogStatistic[]; + + constructor(response: DeletionRequestLogResponse) { + this.targetRef = response.targetRef; + this.deletionPlannedAt = response.deletionPlannedAt; + this.statistics = response.statistics; + } +} diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts new file mode 100644 index 00000000000..15b5ade1715 --- /dev/null +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts @@ -0,0 +1,19 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNumber } from 'class-validator'; +import { EntityId } from '@shared/domain'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; + +export class DeletionRequestBodyProps { + @ApiProperty({ + required: true, + nullable: false, + }) + targetRef!: { targetRefDomain: DeletionDomainModel; targetRefId: EntityId }; + + @IsNumber() + @ApiProperty({ + required: true, + nullable: false, + }) + deleteInMinutes?: number; +} diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.response.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.ts new file mode 100644 index 00000000000..8de61f52552 --- /dev/null +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.ts @@ -0,0 +1,14 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class DeletionRequestResponse { + @ApiProperty() + requestId: string; + + @ApiProperty() + deletionPlannedAt: Date; + + constructor(response: DeletionRequestResponse) { + this.requestId = response.requestId; + this.deletionPlannedAt = response.deletionPlannedAt; + } +} diff --git a/apps/server/src/modules/deletion/controller/dto/index.ts b/apps/server/src/modules/deletion/controller/dto/index.ts new file mode 100644 index 00000000000..5f8cd514d35 --- /dev/null +++ b/apps/server/src/modules/deletion/controller/dto/index.ts @@ -0,0 +1,4 @@ +export * from './deletion-request.response'; +export * from './deletion-request.body.params'; +export * from './deletion-request-log.response'; +export * from './deletion-execution.params'; diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.spec.ts index b317a4b2221..84eb4de314f 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.spec.ts @@ -1,9 +1,9 @@ import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionLogStatisticBuilder } from './deletion-log-statistic.builder'; -import { DeletionRequestLogBuilder } from './deletion-request-log.builder'; +import { DeletionRequestLogResponseBuilder } from './deletion-request-log.builder'; import { DeletionTargetRefBuilder } from './deletion-target-ref.builder'; -describe(DeletionRequestLogBuilder.name, () => { +describe(DeletionRequestLogResponseBuilder.name, () => { afterAll(() => { jest.clearAllMocks(); }); @@ -18,7 +18,7 @@ describe(DeletionRequestLogBuilder.name, () => { const deletedCount = 2; const statistics = [DeletionLogStatisticBuilder.build(targetRefDomain, modifiedCount, deletedCount)]; - const result = DeletionRequestLogBuilder.build(targetRef, deletionPlannedAt, statistics); + const result = DeletionRequestLogResponseBuilder.build(targetRef, deletionPlannedAt, statistics); // Assert expect(result.targetRef).toEqual(targetRef); diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.ts index 8247acf6776..9818793543e 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.ts @@ -1,11 +1,12 @@ -import { DeletionLogStatistic, DeletionRequestLog, DeletionTargetRef } from '../interface'; +import { DeletionRequestLogResponse } from '../../controller/dto'; +import { DeletionLogStatistic, DeletionTargetRef } from '../interface'; -export class DeletionRequestLogBuilder { +export class DeletionRequestLogResponseBuilder { static build( targetRef: DeletionTargetRef, deletionPlannedAt: Date, statistics?: DeletionLogStatistic[] - ): DeletionRequestLog { + ): DeletionRequestLogResponse { const deletionRequestLog = { targetRef, deletionPlannedAt, statistics }; return deletionRequestLog; diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index 063f3d46b48..c39564b752e 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -13,10 +13,10 @@ import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum' import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequestService } from '../services'; import { DeletionRequestUc } from './deletion-request.uc'; -import { DeletionRequestLog, DeletionRequestProps } from './interface/interfaces'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory'; +import { DeletionRequestBodyProps, DeletionRequestLogResponse } from '../controller/dto'; describe(DeletionRequestUc.name, () => { let module: TestingModule; @@ -103,9 +103,9 @@ describe(DeletionRequestUc.name, () => { describe('when creating a deletionRequest', () => { const setup = () => { jest.clearAllMocks(); - const deletionRequestToCreate: DeletionRequestProps = { + const deletionRequestToCreate: DeletionRequestBodyProps = { targetRef: { - targetRefDoamin: DeletionDomainModel.USER, + targetRefDomain: DeletionDomainModel.USER, targetRefId: '653e4833cc39e5907a1e18d2', }, deleteInMinutes: 1440, @@ -125,7 +125,7 @@ describe(DeletionRequestUc.name, () => { expect(deletionRequestService.createDeletionRequest).toHaveBeenCalledWith( deletionRequestToCreate.targetRef.targetRefId, - deletionRequestToCreate.targetRef.targetRefDoamin, + deletionRequestToCreate.targetRef.targetRefDomain, deletionRequestToCreate.deleteInMinutes ); }); @@ -343,7 +343,7 @@ describe(DeletionRequestUc.name, () => { deletedCount: 1, }); - const executedDeletionRequestSummary: DeletionRequestLog = { + const executedDeletionRequestSummary: DeletionRequestLogResponse = { targetRef: { targetRefDomain: deletionRequestExecuted.targetRefDomain, targetRefId: deletionRequestExecuted.targetRefId, @@ -399,7 +399,7 @@ describe(DeletionRequestUc.name, () => { const setup = () => { jest.clearAllMocks(); const deletionRequest = deletionRequestFactory.build(); - const notExecutedDeletionRequestSummary: DeletionRequestLog = { + const notExecutedDeletionRequestSummary: DeletionRequestLogResponse = { targetRef: { targetRefDomain: deletionRequest.targetRefDomain, targetRefId: deletionRequest.targetRefId, diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index d94a129310f..cef961e60e2 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -15,15 +15,11 @@ import { DeletionRequest } from '../domain/deletion-request.do'; import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; import { DeletionLog } from '../domain/deletion-log.do'; -import { - DeletionRequestProps, - DeletionRequestLog, - DeletionLogStatistic, - DeletionRequestCreateAnswer, -} from './interface/interfaces'; +import { DeletionLogStatistic } from './interface/interfaces'; import { DeletionLogStatisticBuilder } from './builder/deletion-log-statistic.builder'; -import { DeletionRequestLogBuilder } from './builder/deletion-request-log.builder'; +import { DeletionRequestLogResponseBuilder } from './builder/deletion-request-log.builder'; import { DeletionTargetRefBuilder } from './builder/deletion-target-ref.builder'; +import { DeletionRequestBodyProps, DeletionRequestLogResponse, DeletionRequestResponse } from '../controller/dto'; @Injectable() export class DeletionRequestUc { @@ -41,10 +37,10 @@ export class DeletionRequestUc { private readonly userService: UserService ) {} - async createDeletionRequest(deletionRequest: DeletionRequestProps): Promise { + async createDeletionRequest(deletionRequest: DeletionRequestBodyProps): Promise { const result = await this.deletionRequestService.createDeletionRequest( deletionRequest.targetRef.targetRefId, - deletionRequest.targetRef.targetRefDoamin, + deletionRequest.targetRef.targetRefDomain, deletionRequest.deleteInMinutes ); @@ -62,9 +58,9 @@ export class DeletionRequestUc { } } - async findById(deletionRequestId: EntityId): Promise { + async findById(deletionRequestId: EntityId): Promise { const deletionRequest: DeletionRequest = await this.deletionRequestService.findById(deletionRequestId); - let response: DeletionRequestLog = DeletionRequestLogBuilder.build( + let response: DeletionRequestLogResponse = DeletionRequestLogResponseBuilder.build( DeletionTargetRefBuilder.build(deletionRequest.targetRefDomain, deletionRequest.targetRefId), deletionRequest.deleteAfter ); diff --git a/apps/server/src/modules/deletion/uc/interface/interfaces.ts b/apps/server/src/modules/deletion/uc/interface/interfaces.ts index 47f4d887735..db783553541 100644 --- a/apps/server/src/modules/deletion/uc/interface/interfaces.ts +++ b/apps/server/src/modules/deletion/uc/interface/interfaces.ts @@ -6,24 +6,8 @@ export interface DeletionTargetRef { targetRefId: EntityId; } -export interface DeletionRequestLog { - targetRef: DeletionTargetRef; - deletionPlannedAt: Date; - statistics?: DeletionLogStatistic[]; -} - export interface DeletionLogStatistic { domain: DeletionDomainModel; modifiedCount?: number; deletedCount?: number; } - -export interface DeletionRequestProps { - targetRef: { targetRefDoamin: DeletionDomainModel; targetRefId: EntityId }; - deleteInMinutes?: number; -} - -export interface DeletionRequestCreateAnswer { - requestId: EntityId; - deletionPlannedAt: Date; -} From 175e495b2afb82d5db983185838e40a933457052 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 9 Nov 2023 18:18:41 +0100 Subject: [PATCH 02/96] auth startegy impl --- .../authentication/authentication.module.ts | 2 ++ .../authentication/config/x-api-key.config.ts | 3 +++ .../strategy/x-api-key.strategy.ts | 27 +++++++++++++++++++ ...r.ts => deletion-executions.controller.ts} | 8 +++--- ...ler.ts => deletion-requests.controller.ts} | 6 ++--- .../src/modules/deletion/deletion.module.ts | 10 +++++-- .../src/modules/server/server.config.ts | 7 ++++- config/default.schema.json | 20 ++++++++++++++ package-lock.json | 19 +++++++++++++ package.json | 1 + 10 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 apps/server/src/modules/authentication/config/x-api-key.config.ts create mode 100644 apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts rename apps/server/src/modules/deletion/controller/{deletionExecutions.controller.ts => deletion-executions.controller.ts} (82%) rename apps/server/src/modules/deletion/controller/{deletionRequests.controller.ts => deletion-requests.controller.ts} (93%) diff --git a/apps/server/src/modules/authentication/authentication.module.ts b/apps/server/src/modules/authentication/authentication.module.ts index 8f2bdcd3b0d..61092ff5e16 100644 --- a/apps/server/src/modules/authentication/authentication.module.ts +++ b/apps/server/src/modules/authentication/authentication.module.ts @@ -18,6 +18,7 @@ import { JwtStrategy } from './strategy/jwt.strategy'; import { LdapStrategy } from './strategy/ldap.strategy'; import { LocalStrategy } from './strategy/local.strategy'; import { Oauth2Strategy } from './strategy/oauth2.strategy'; +import { XApiKeyStrategy } from './strategy/x-api-key.strategy'; // values copied from Algorithm definition. Type does not exist at runtime and can't be checked anymore otherwise const algorithms = [ @@ -76,6 +77,7 @@ const jwtModuleOptions: JwtModuleOptions = { LdapService, LdapStrategy, Oauth2Strategy, + XApiKeyStrategy, ], exports: [AuthenticationService], }) diff --git a/apps/server/src/modules/authentication/config/x-api-key.config.ts b/apps/server/src/modules/authentication/config/x-api-key.config.ts new file mode 100644 index 00000000000..79288c0dd82 --- /dev/null +++ b/apps/server/src/modules/authentication/config/x-api-key.config.ts @@ -0,0 +1,3 @@ +export interface IXApiKeyConfig { + ADMIN_API__ALLOWED_API_KEYS: string[]; +} diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts new file mode 100644 index 00000000000..b3dff6c9533 --- /dev/null +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts @@ -0,0 +1,27 @@ +import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { PassportStrategy } from '@nestjs/passport'; +import { ConfigService } from '@nestjs/config'; +import Strategy from 'passport-headerapikey'; +import { IXApiKeyConfig } from '../config/x-api-key.config'; + +@Injectable() +export class XApiKeyStrategy extends PassportStrategy(Strategy, 'api-key') { + private readonly allowedApiKeys: string[]; + + constructor(private readonly configService: ConfigService) { + super( + { header: 'X-API-KEY', prefix: '' }, + true, + // eslint-disable-next-line @typescript-eslint/require-await + async (apiKey: string, done: (error: Error | null, data: boolean | null) => void) => this.validate(apiKey, done) + ); + this.allowedApiKeys = this.configService.get('ADMIN_API__ALLOWED_API_KEYS'); + } + + public validate = (apiKey: string, done: (error: Error | null, data: boolean | null) => void) => { + if (this.allowedApiKeys.includes(apiKey)) { + done(null, true); + } + done(new UnauthorizedException(), null); + }; +} diff --git a/apps/server/src/modules/deletion/controller/deletionExecutions.controller.ts b/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts similarity index 82% rename from apps/server/src/modules/deletion/controller/deletionExecutions.controller.ts rename to apps/server/src/modules/deletion/controller/deletion-executions.controller.ts index 4a6898dc1ea..f6d1754690d 100644 --- a/apps/server/src/modules/deletion/controller/deletionExecutions.controller.ts +++ b/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts @@ -1,14 +1,14 @@ -import { Controller, Post, Query } from '@nestjs/common'; +import { Controller, Post, Query, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; -import { Authenticate } from '@src/modules/authentication/decorator/auth.decorator'; import { ForbiddenOperationError, ValidationError } from '@shared/common'; +import { AuthGuard } from '@nestjs/passport'; import { DeletionRequestUc } from '../uc/deletion-request.uc'; import { DeletionExecutionParams } from './dto'; @ApiTags('DeletionExecutions') -@Authenticate('jwt') +@UseGuards(AuthGuard('api-key')) @Controller('deletionExecutions') -export class AccountController { +export class DeletionExecutionsController { constructor(private readonly deletionRequestUc: DeletionRequestUc) {} @Post() diff --git a/apps/server/src/modules/deletion/controller/deletionRequests.controller.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts similarity index 93% rename from apps/server/src/modules/deletion/controller/deletionRequests.controller.ts rename to apps/server/src/modules/deletion/controller/deletion-requests.controller.ts index a32eca60aac..6f6823acbc2 100644 --- a/apps/server/src/modules/deletion/controller/deletionRequests.controller.ts +++ b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts @@ -1,14 +1,14 @@ -import { Body, Controller, Get, Param, Post } from '@nestjs/common'; +import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; -import { Authenticate } from '@src/modules/authentication/decorator/auth.decorator'; import { ForbiddenOperationError, ValidationError } from '@shared/common'; +import { AuthGuard } from '@nestjs/passport'; import { DeletionRequestUc } from '../uc/deletion-request.uc'; import { DeletionRequestResponse } from './dto/deletion-request.response'; import { DeletionRequestBodyProps } from './dto/deletion-request.body.params'; import { DeletionRequestLogResponse } from './dto'; @ApiTags('DeletionRequests') -@Authenticate('jwt') +@UseGuards(AuthGuard('api-key')) @Controller('deletionRequests') export class DeletionRequestsController { constructor(private readonly deletionRequestUc: DeletionRequestUc) {} diff --git a/apps/server/src/modules/deletion/deletion.module.ts b/apps/server/src/modules/deletion/deletion.module.ts index 440a9418d70..5c8143e61d0 100644 --- a/apps/server/src/modules/deletion/deletion.module.ts +++ b/apps/server/src/modules/deletion/deletion.module.ts @@ -1,11 +1,17 @@ import { Module } from '@nestjs/common'; import { LoggerModule } from '@src/core/logger'; +import { ConfigService } from '@nestjs/config'; import { DeletionRequestService } from './services/deletion-request.service'; import { DeletionRequestRepo } from './repo/deletion-request.repo'; +import { AuthenticationModule } from '../authentication'; +import { DeletionRequestsController } from './controller/deletion-requests.controller'; +import { DeletionExecutionsController } from './controller/deletion-executions.controller'; +import { IXApiKeyConfig } from '../authentication/config/x-api-key.config'; @Module({ - imports: [LoggerModule], - providers: [DeletionRequestService, DeletionRequestRepo], + imports: [LoggerModule, AuthenticationModule], + controllers: [DeletionRequestsController, DeletionExecutionsController], + providers: [DeletionRequestService, DeletionRequestRepo, ConfigService], exports: [DeletionRequestService], }) export class DeletionModule {} diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 5d1ea95cc3b..4d1784da4ca 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -6,6 +6,7 @@ import type { IFilesStorageClientConfig } from '@modules/files-storage-client'; import type { IUserConfig } from '@modules/user'; import type { ICommonCartridgeConfig } from '@modules/learnroom/common-cartridge'; import { IMailConfig } from '@src/infra/mail/interfaces/mail-config'; +import { IXApiKeyConfig } from '../authentication/config/x-api-key.config'; export enum NodeEnvType { TEST = 'test', @@ -21,7 +22,8 @@ export interface IServerConfig IAccountConfig, IIdentityManagementConfig, ICommonCartridgeConfig, - IMailConfig { + IMailConfig, + IXApiKeyConfig { NODE_ENV: string; SC_DOMAIN: string; } @@ -44,6 +46,9 @@ const config: IServerConfig = { ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS: (Configuration.get('ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS') as string) .split(',') .map((domain) => domain.trim()), + ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string) + .split(',') + .map((apiKey) => apiKey.trim()), }; export const serverConfig = () => config; diff --git a/config/default.schema.json b/config/default.schema.json index bd637b719e9..dcbdf9dff1e 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -1410,6 +1410,26 @@ "SECRET_ES_MERLIN_COUNTIES_CREDENTIALS" ] } + }, + "ADMIN_API": { + "type": "object", + "description": "Configuration of the schulcloud-server's admin API.", + "properties": { + "ENABLED": { + "type": "boolean", + "default": true, + "description": "Flag to turn on/off the Admin API." + }, + "PORT": { + "type": "number", + "default": 4030, + "description": "Port of the exposed Admin API server." + }, + "ALLOWED_API_KEYS": { + "type": "string", + "description": "Allowed Admin API keys (for accessing the Admin API)." + } + } } } } diff --git a/package-lock.json b/package-lock.json index 5f895871520..c78e284fc4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -107,6 +107,7 @@ "papaparse": "^5.1.1", "passport": "^0.6.0", "passport-custom": "^1.1.1", + "passport-headerapikey": "^1.2.2", "passport-jwt": "^4.0.1", "passport-local": "^1.0.0", "prom-client": "^13.1.0", @@ -19179,6 +19180,15 @@ "node": ">= 0.10.0" } }, + "node_modules/passport-headerapikey": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/passport-headerapikey/-/passport-headerapikey-1.2.2.tgz", + "integrity": "sha512-4BvVJRrWsNJPrd3UoZfcnnl4zvUWYKEtfYkoDsaOKBsrWHYmzTApCjs7qUbncOLexE9ul0IRiYBFfBG0y9IVQA==", + "dependencies": { + "lodash": "^4.17.15", + "passport-strategy": "^1.0.0" + } + }, "node_modules/passport-jwt": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/passport-jwt/-/passport-jwt-4.0.1.tgz", @@ -39366,6 +39376,15 @@ "passport-strategy": "1.x.x" } }, + "passport-headerapikey": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/passport-headerapikey/-/passport-headerapikey-1.2.2.tgz", + "integrity": "sha512-4BvVJRrWsNJPrd3UoZfcnnl4zvUWYKEtfYkoDsaOKBsrWHYmzTApCjs7qUbncOLexE9ul0IRiYBFfBG0y9IVQA==", + "requires": { + "lodash": "^4.17.15", + "passport-strategy": "^1.0.0" + } + }, "passport-jwt": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/passport-jwt/-/passport-jwt-4.0.1.tgz", diff --git a/package.json b/package.json index 6afd927c17c..4426340c022 100644 --- a/package.json +++ b/package.json @@ -190,6 +190,7 @@ "papaparse": "^5.1.1", "passport": "^0.6.0", "passport-custom": "^1.1.1", + "passport-headerapikey": "^1.2.2", "passport-jwt": "^4.0.1", "passport-local": "^1.0.0", "prom-client": "^13.1.0", From fd2b9b819637974bc9c08aa44fee4b8141f48c9f Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Fri, 10 Nov 2023 09:26:36 +0100 Subject: [PATCH 03/96] api impl --- .../apps/helpers/admin-api-setup-helper.ts | 28 ++++++++++ apps/server/src/apps/server.app.ts | 6 +++ .../deletion-requests.controller.ts | 4 +- .../dto/deletion-request-log.response.ts | 2 +- .../src/modules/deletion/deletion.module.ts | 52 ++++++++++++++++++- .../modules/server/admin-api.server.module.ts | 48 +++++++++++++++++ .../src/modules/server/server.config.ts | 7 +-- .../src/modules/server/server.module.ts | 6 ++- config/default.json | 3 ++ config/default.schema.json | 41 ++++++++------- 10 files changed, 168 insertions(+), 29 deletions(-) create mode 100644 apps/server/src/apps/helpers/admin-api-setup-helper.ts create mode 100644 apps/server/src/modules/server/admin-api.server.module.ts diff --git a/apps/server/src/apps/helpers/admin-api-setup-helper.ts b/apps/server/src/apps/helpers/admin-api-setup-helper.ts new file mode 100644 index 00000000000..efda8e1de35 --- /dev/null +++ b/apps/server/src/apps/helpers/admin-api-setup-helper.ts @@ -0,0 +1,28 @@ +import express from 'express'; +import { Configuration } from '@hpi-schul-cloud/commons/lib'; +import { Logger } from '@src/core/logger'; +import { AdminApiServerModule } from '@src/modules/server/admin-api.server.module'; +import { ExpressAdapter } from '@nestjs/platform-express'; +import { NestFactory } from '@nestjs/core'; +import { AppStartLoggable } from './app-start-loggable'; + +export const createAndStartAdminApiServer = async (logger: Logger) => { + const nestAdminServerExpress = express(); + const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); + const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); + + nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); + await nestAdminServerApp.init(); + + const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; + + nestAdminServerExpress.listen(adminApiServerPort, () => { + logger.info( + new AppStartLoggable({ + appName: 'Admin Api server app', + port: adminApiServerPort, + mountsDescription: `/admin/api/v1 --> Admin Api Server`, + }) + ); + }); +}; diff --git a/apps/server/src/apps/server.app.ts b/apps/server/src/apps/server.app.ts index 9538d6c8515..79fd0747754 100644 --- a/apps/server/src/apps/server.app.ts +++ b/apps/server/src/apps/server.app.ts @@ -21,6 +21,7 @@ import { join } from 'path'; // register source-map-support for debugging import { install as sourceMapInstall } from 'source-map-support'; import { FeathersRosterService } from '@modules/pseudonym'; +import { Configuration } from '@hpi-schul-cloud/commons/lib'; import legacyAppPromise = require('../../../../src/app'); import { AppStartLoggable } from './helpers/app-start-loggable'; @@ -28,6 +29,7 @@ import { addPrometheusMetricsMiddlewaresIfEnabled, createAndStartPrometheusMetricsAppIfEnabled, } from './helpers/prometheus-metrics'; +import { createAndStartAdminApiServer } from './helpers/admin-api-setup-helper'; async function bootstrap() { sourceMapInstall(); @@ -127,6 +129,10 @@ async function bootstrap() { createAndStartPrometheusMetricsAppIfEnabled(logger); }); + if (Configuration.get('ADMIN_API__ENABLED') === true) { + await createAndStartAdminApiServer(logger); + } + console.log('#################################'); console.log(`### Start Server ###`); console.log(`### Port: ${port} ###`); diff --git a/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts index 6f6823acbc2..847e5b76ad0 100644 --- a/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts +++ b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common'; +import { Body, Controller, Delete, Get, Param, Post, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { ForbiddenOperationError, ValidationError } from '@shared/common'; import { AuthGuard } from '@nestjs/passport'; @@ -45,7 +45,7 @@ export class DeletionRequestsController { return this.deletionRequestUc.findById(requestId); } - @Get(':requestId') + @Delete(':requestId') @ApiOperation({ summary: 'Canceling a deletion request', }) diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts index f8eb5ce10e3..b2c34032e89 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts @@ -1,5 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsOptional } from 'class-validator/types/decorator/decorators'; +import { IsOptional } from 'class-validator'; import { DeletionLogStatistic, DeletionTargetRef } from '../../uc/interface'; export class DeletionRequestLogResponse { diff --git a/apps/server/src/modules/deletion/deletion.module.ts b/apps/server/src/modules/deletion/deletion.module.ts index 5c8143e61d0..ee0cd973c57 100644 --- a/apps/server/src/modules/deletion/deletion.module.ts +++ b/apps/server/src/modules/deletion/deletion.module.ts @@ -1,17 +1,65 @@ import { Module } from '@nestjs/common'; import { LoggerModule } from '@src/core/logger'; import { ConfigService } from '@nestjs/config'; +import { CourseGroupRepo } from '@shared/repo'; +import { Configuration } from '@hpi-schul-cloud/commons'; import { DeletionRequestService } from './services/deletion-request.service'; import { DeletionRequestRepo } from './repo/deletion-request.repo'; import { AuthenticationModule } from '../authentication'; import { DeletionRequestsController } from './controller/deletion-requests.controller'; import { DeletionExecutionsController } from './controller/deletion-executions.controller'; import { IXApiKeyConfig } from '../authentication/config/x-api-key.config'; +import { DeletionRequestUc } from './uc/deletion-request.uc'; +import { DeletionLogService } from './services/deletion-log.service'; +import { AccountModule } from '../account'; +import { ClassModule } from '../class'; +import { CourseGroupService } from '../learnroom/service'; +import { LearnroomModule } from '../learnroom'; +import { FilesService } from '../files/service'; +import { LessonModule } from '../lesson'; +import { PseudonymModule } from '../pseudonym'; +import { TeamsModule } from '../teams'; +import { UserModule } from '../user'; +import { RocketChatUserService } from '../rocketchat-user'; +import { RocketChatModule } from '../rocketchat'; +import { DeletionLogRepo } from './repo'; +import { FilesRepo } from '../files/repo'; +import { RocketChatUserRepo } from '../rocketchat-user/repo'; @Module({ - imports: [LoggerModule, AuthenticationModule], + imports: [ + LoggerModule, + AuthenticationModule, + AccountModule, + ClassModule, + LearnroomModule, + LessonModule, + PseudonymModule, + TeamsModule, + UserModule, + RocketChatModule.forRoot({ + uri: Configuration.get('ROCKET_CHAT_URI') as string, + adminId: Configuration.get('ROCKET_CHAT_ADMIN_ID') as string, + adminToken: Configuration.get('ROCKET_CHAT_ADMIN_TOKEN') as string, + adminUser: Configuration.get('ROCKET_CHAT_ADMIN_USER') as string, + adminPassword: Configuration.get('ROCKET_CHAT_ADMIN_PASSWORD') as string, + }), + ], controllers: [DeletionRequestsController, DeletionExecutionsController], - providers: [DeletionRequestService, DeletionRequestRepo, ConfigService], + providers: [ + DeletionRequestService, + DeletionRequestRepo, + ConfigService, + DeletionRequestUc, + DeletionLogService, + CourseGroupService, + FilesService, + DeletionLogRepo, + CourseGroupRepo, + FilesRepo, + RocketChatUserService, + RocketChatUserRepo, + ], exports: [DeletionRequestService], }) export class DeletionModule {} diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts new file mode 100644 index 00000000000..db756275e02 --- /dev/null +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -0,0 +1,48 @@ +import { MikroOrmModule } from '@mikro-orm/nestjs'; +import { Inject, MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; +import { ALL_ENTITIES } from '@shared/domain'; +import { RabbitMQWrapperModule } from '@infra/rabbitmq'; +import { RedisModule, REDIS_CLIENT } from '@infra/redis'; +import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config'; +import { LegacyLogger, LoggerModule } from '@src/core/logger'; +import { RedisClient } from 'redis'; +import { ConfigModule } from '@nestjs/config'; +import { ServerController } from './controller/server.controller'; +import { DeletionModule } from '../deletion'; +import { defaultMikroOrmOptions, setupSessions } from './server.module'; +import { serverConfig } from './server.config'; + +const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConfig)),DeletionModule]; + +@Module({ + imports: [ + RabbitMQWrapperModule, + ...serverModules, + MikroOrmModule.forRoot({ + ...defaultMikroOrmOptions, + type: 'mongo', + // TODO add mongoose options as mongo options (see database.js) + clientUrl: DB_URL, + password: DB_PASSWORD, + user: DB_USERNAME, + entities: ALL_ENTITIES, + + // debug: true, // use it for locally debugging of queries + }), + LoggerModule, + RedisModule, + ], + controllers: [ServerController], +}) +export class AdminApiServerModule implements NestModule { + constructor( + @Inject(REDIS_CLIENT) private readonly redisClient: RedisClient | undefined, + private readonly logger: LegacyLogger + ) { + logger.setContext(AdminApiServerModule.name); + } + + configure(consumer: MiddlewareConsumer) { + setupSessions(consumer, this.redisClient, this.logger); + } +} diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 4d1784da4ca..0cf2c4bae9a 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -43,12 +43,13 @@ const config: IServerConfig = { FEATURE_IDENTITY_MANAGEMENT_ENABLED: Configuration.get('FEATURE_IDENTITY_MANAGEMENT_ENABLED') as boolean, FEATURE_IDENTITY_MANAGEMENT_STORE_ENABLED: Configuration.get('FEATURE_IDENTITY_MANAGEMENT_STORE_ENABLED') as boolean, FEATURE_IDENTITY_MANAGEMENT_LOGIN_ENABLED: Configuration.get('FEATURE_IDENTITY_MANAGEMENT_LOGIN_ENABLED') as boolean, - ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS: (Configuration.get('ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS') as string) - .split(',') - .map((domain) => domain.trim()), ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string) .split(',') .map((apiKey) => apiKey.trim()), + ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS: (Configuration.get('ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS') as string) + .split(',') + .map((domain) => domain.trim()), + }; export const serverConfig = () => config; diff --git a/apps/server/src/modules/server/server.module.ts b/apps/server/src/modules/server/server.module.ts index a812ff773b2..42829caefa8 100644 --- a/apps/server/src/modules/server/server.module.ts +++ b/apps/server/src/modules/server/server.module.ts @@ -87,7 +87,11 @@ export const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { new NotFoundException(`The requested ${entityName}: ${where} has not been found.`), }; -const setupSessions = (consumer: MiddlewareConsumer, redisClient: RedisClient | undefined, logger: LegacyLogger) => { +export const setupSessions = ( + consumer: MiddlewareConsumer, + redisClient: RedisClient | undefined, + logger: LegacyLogger +) => { const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number; let store: connectRedis.RedisStore | undefined; diff --git a/config/default.json b/config/default.json index 7dd1802a037..f4ebccc4627 100644 --- a/config/default.json +++ b/config/default.json @@ -41,5 +41,8 @@ }, "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 + }, + "ADMIN_API": { + "ENABLED": true } } diff --git a/config/default.schema.json b/config/default.schema.json index dcbdf9dff1e..575d7f38a22 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -1267,6 +1267,27 @@ "default": false, "description": "Enables feature that allows the insecure LDAP URL (with ldap:// protocol)." }, + "ADMIN_API": { + "type": "object", + "description": "Configuration of the schulcloud-server's admin API.", + "properties": { + "ENABLED": { + "type": "boolean", + "default": true, + "description": "Flag to turn on/off the Admin API." + }, + "PORT": { + "type": "number", + "default": 4030, + "description": "Port of the exposed Admin API server." + }, + "ALLOWED_API_KEYS": { + "type": "string", + "default": "", + "description": "Allowed Admin API keys (for accessing the Admin API)." + } + } + }, "FEATURE_NEW_SCHOOL_ADMINISTRATION_PAGE_AS_DEFAULT_ENABLED": { "type": "boolean", "default": false, @@ -1410,26 +1431,6 @@ "SECRET_ES_MERLIN_COUNTIES_CREDENTIALS" ] } - }, - "ADMIN_API": { - "type": "object", - "description": "Configuration of the schulcloud-server's admin API.", - "properties": { - "ENABLED": { - "type": "boolean", - "default": true, - "description": "Flag to turn on/off the Admin API." - }, - "PORT": { - "type": "number", - "default": 4030, - "description": "Port of the exposed Admin API server." - }, - "ALLOWED_API_KEYS": { - "type": "string", - "description": "Allowed Admin API keys (for accessing the Admin API)." - } - } } } } From 7d3b62672933afff8a61dce325bcc31ee638a539 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Fri, 10 Nov 2023 12:57:32 +0100 Subject: [PATCH 04/96] some changes --- .../dto/deletion-request.body.params.ts | 5 ++--- .../deletion/entity/deletion-log.entity.ts | 5 +++-- .../builder/deletion-target-ref.builder.spec.ts | 4 ++-- .../uc/builder/deletion-target-ref.builder.ts | 4 ++-- .../deletion/uc/deletion-request.uc.spec.ts | 16 ++++++++-------- .../modules/deletion/uc/deletion-request.uc.ts | 4 ++-- .../modules/deletion/uc/interface/interfaces.ts | 4 ++-- .../modules/server/admin-api.server.module.ts | 2 +- apps/server/src/modules/server/server.config.ts | 1 - .../src/shared/domain/entity/all-entities.ts | 3 +++ 10 files changed, 25 insertions(+), 23 deletions(-) diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts index 15b5ade1715..90d1a789ddd 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts @@ -1,14 +1,13 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsNumber } from 'class-validator'; -import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionTargetRef } from '../../uc/interface'; export class DeletionRequestBodyProps { @ApiProperty({ required: true, nullable: false, }) - targetRef!: { targetRefDomain: DeletionDomainModel; targetRefId: EntityId }; + targetRef!: DeletionTargetRef; @IsNumber() @ApiProperty({ diff --git a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts index 8a9d2bab025..969214b4209 100644 --- a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts @@ -1,6 +1,7 @@ import { Entity, Property } from '@mikro-orm/core'; -import { BaseEntityWithTimestamps, EntityId } from '@shared/domain'; -import { ObjectId } from '@mikro-orm/mongodb'; +import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; +import { EntityId } from '@shared/domain'; +import { ObjectId } from 'bson'; import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts index 2fb4ae440a7..1c6e7782ce1 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts @@ -14,7 +14,7 @@ describe(DeletionTargetRefBuilder.name, () => { const result = DeletionTargetRefBuilder.build(domain, refId); // Assert - expect(result.targetRefDomain).toEqual(domain); - expect(result.targetRefId).toEqual(refId); + expect(result.domain).toEqual(domain); + expect(result.id).toEqual(refId); }); }); diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts index 91f3385a9aa..3e827d6db7f 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts @@ -3,8 +3,8 @@ import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.en import { DeletionTargetRef } from '../interface'; export class DeletionTargetRefBuilder { - static build(targetRefDomain: DeletionDomainModel, targetRefId: EntityId): DeletionTargetRef { - const deletionTargetRef = { targetRefDomain, targetRefId }; + static build(domain: DeletionDomainModel, id: EntityId): DeletionTargetRef { + const deletionTargetRef = { domain, id }; return deletionTargetRef; } diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index 538013f3170..8ead4d36e30 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -120,8 +120,8 @@ describe(DeletionRequestUc.name, () => { jest.clearAllMocks(); const deletionRequestToCreate: DeletionRequestBodyProps = { targetRef: { - targetRefDomain: DeletionDomainModel.USER, - targetRefId: '653e4833cc39e5907a1e18d2', + domain: DeletionDomainModel.USER, + id: '653e4833cc39e5907a1e18d2', }, deleteInMinutes: 1440, }; @@ -139,8 +139,8 @@ describe(DeletionRequestUc.name, () => { await uc.createDeletionRequest(deletionRequestToCreate); expect(deletionRequestService.createDeletionRequest).toHaveBeenCalledWith( - deletionRequestToCreate.targetRef.targetRefId, - deletionRequestToCreate.targetRef.targetRefDomain, + deletionRequestToCreate.targetRef.id, + deletionRequestToCreate.targetRef.domain, deletionRequestToCreate.deleteInMinutes ); }); @@ -397,8 +397,8 @@ describe(DeletionRequestUc.name, () => { const executedDeletionRequestSummary: DeletionRequestLogResponse = { targetRef: { - targetRefDomain: deletionRequestExecuted.targetRefDomain, - targetRefId: deletionRequestExecuted.targetRefId, + domain: deletionRequestExecuted.targetRefDomain, + id: deletionRequestExecuted.targetRefId, }, deletionPlannedAt: deletionRequestExecuted.deleteAfter, statistics: [ @@ -453,8 +453,8 @@ describe(DeletionRequestUc.name, () => { const deletionRequest = deletionRequestFactory.build(); const notExecutedDeletionRequestSummary: DeletionRequestLogResponse = { targetRef: { - targetRefDomain: deletionRequest.targetRefDomain, - targetRefId: deletionRequest.targetRefId, + domain: deletionRequest.targetRefDomain, + id: deletionRequest.targetRefId, }, deletionPlannedAt: deletionRequest.deleteAfter, }; diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index bc0ef602fab..89a6e878eea 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -43,8 +43,8 @@ export class DeletionRequestUc { async createDeletionRequest(deletionRequest: DeletionRequestBodyProps): Promise { const result = await this.deletionRequestService.createDeletionRequest( - deletionRequest.targetRef.targetRefId, - deletionRequest.targetRef.targetRefDomain, + deletionRequest.targetRef.id, + deletionRequest.targetRef.domain, deletionRequest.deleteInMinutes ); diff --git a/apps/server/src/modules/deletion/uc/interface/interfaces.ts b/apps/server/src/modules/deletion/uc/interface/interfaces.ts index db783553541..4ee470609ea 100644 --- a/apps/server/src/modules/deletion/uc/interface/interfaces.ts +++ b/apps/server/src/modules/deletion/uc/interface/interfaces.ts @@ -2,8 +2,8 @@ import { EntityId } from '@shared/domain'; import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; export interface DeletionTargetRef { - targetRefDomain: DeletionDomainModel; - targetRefId: EntityId; + domain: DeletionDomainModel; + id: EntityId; } export interface DeletionLogStatistic { diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index db756275e02..6934b7b7755 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -12,7 +12,7 @@ import { DeletionModule } from '../deletion'; import { defaultMikroOrmOptions, setupSessions } from './server.module'; import { serverConfig } from './server.config'; -const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConfig)),DeletionModule]; +const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), DeletionModule]; @Module({ imports: [ diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 0cf2c4bae9a..f144e26b636 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -49,7 +49,6 @@ const config: IServerConfig = { ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS: (Configuration.get('ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS') as string) .split(',') .map((domain) => domain.trim()), - }; export const serverConfig = () => config; diff --git a/apps/server/src/shared/domain/entity/all-entities.ts b/apps/server/src/shared/domain/entity/all-entities.ts index 9dc33c55b78..7433ac0ea1d 100644 --- a/apps/server/src/shared/domain/entity/all-entities.ts +++ b/apps/server/src/shared/domain/entity/all-entities.ts @@ -5,6 +5,7 @@ import { ShareToken } from '@modules/sharing/entity/share-token.entity'; import { ContextExternalToolEntity } from '@modules/tool/context-external-tool/entity'; import { ExternalToolEntity } from '@modules/tool/external-tool/entity'; import { SchoolExternalToolEntity } from '@modules/tool/school-external-tool/entity'; +import { DeletionLogEntity, DeletionRequestEntity } from '@src/modules/deletion/entity'; import { Account } from './account.entity'; import { BoardNode, @@ -58,6 +59,8 @@ export const ALL_ENTITIES = [ ColumnBoardTarget, ColumnNode, ClassEntity, + DeletionRequestEntity, + DeletionLogEntity, FileElementNode, LinkElementNode, RichTextElementNode, From a954f6b501fbe811dd634451d2495eaf73f8c40d Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Sat, 11 Nov 2023 17:08:45 +0100 Subject: [PATCH 05/96] some fixes need in KNL module --- .../modules/deletion/domain/deletion-log.do.spec.ts | 2 ++ .../src/modules/deletion/domain/deletion-log.do.ts | 5 +++++ .../domain/testing/factory/deletion-log.factory.ts | 1 + .../deletion/entity/deletion-log.entity.spec.ts | 2 ++ .../modules/deletion/entity/deletion-log.entity.ts | 11 ++++++++++- .../deletion/entity/deletion-request.entity.ts | 8 ++++---- .../modules/deletion/repo/deletion-log.repo.spec.ts | 4 ++++ .../deletion/repo/mapper/deletion-log.mapper.spec.ts | 4 ++++ .../deletion/repo/mapper/deletion-log.mapper.ts | 2 ++ .../deletion/services/deletion-log.service.spec.ts | 1 + .../modules/deletion/services/deletion-log.service.ts | 1 + .../modules/deletion/uc/deletion-request.uc.spec.ts | 10 ++++------ apps/server/src/modules/server/server.module.ts | 1 + config/default.json | 3 ++- 14 files changed, 43 insertions(+), 12 deletions(-) diff --git a/apps/server/src/modules/deletion/domain/deletion-log.do.spec.ts b/apps/server/src/modules/deletion/domain/deletion-log.do.spec.ts index 9117ded29c5..b0eab14470a 100644 --- a/apps/server/src/modules/deletion/domain/deletion-log.do.spec.ts +++ b/apps/server/src/modules/deletion/domain/deletion-log.do.spec.ts @@ -41,6 +41,7 @@ describe(DeletionLog.name, () => { modifiedCount: 0, deletedCount: 1, deletionRequestId: new ObjectId().toHexString(), + performedAt: new Date(), createdAt: new Date(), updatedAt: new Date(), }; @@ -59,6 +60,7 @@ describe(DeletionLog.name, () => { modifiedCount: deletionLogDo.modifiedCount, deletedCount: deletionLogDo.deletedCount, deletionRequestId: deletionLogDo.deletionRequestId, + performedAt: deletionLogDo.performedAt, createdAt: deletionLogDo.createdAt, updatedAt: deletionLogDo.updatedAt, }; diff --git a/apps/server/src/modules/deletion/domain/deletion-log.do.ts b/apps/server/src/modules/deletion/domain/deletion-log.do.ts index 73e62b46055..11f81efa817 100644 --- a/apps/server/src/modules/deletion/domain/deletion-log.do.ts +++ b/apps/server/src/modules/deletion/domain/deletion-log.do.ts @@ -11,6 +11,7 @@ export interface DeletionLogProps extends AuthorizableObject { modifiedCount?: number; deletedCount?: number; deletionRequestId?: EntityId; + performedAt?: Date; } export class DeletionLog extends DomainObject { @@ -41,4 +42,8 @@ export class DeletionLog extends DomainObject { get deletionRequestId(): EntityId | undefined { return this.props.deletionRequestId; } + + get performedAt(): Date | undefined { + return this.props.performedAt; + } } diff --git a/apps/server/src/modules/deletion/domain/testing/factory/deletion-log.factory.ts b/apps/server/src/modules/deletion/domain/testing/factory/deletion-log.factory.ts index d83b2f44c8a..fbeeba78730 100644 --- a/apps/server/src/modules/deletion/domain/testing/factory/deletion-log.factory.ts +++ b/apps/server/src/modules/deletion/domain/testing/factory/deletion-log.factory.ts @@ -12,6 +12,7 @@ export const deletionLogFactory = DoBaseFactory.define { modifiedCount: 0, deletedCount: 1, deletionRequestId: new ObjectId(), + performedAt: new Date(), createdAt: new Date(), updatedAt: new Date(), }; @@ -49,6 +50,7 @@ describe(DeletionLogEntity.name, () => { modifiedCount: entity.modifiedCount, deletedCount: entity.deletedCount, deletionRequestId: entity.deletionRequestId, + performedAt: entity.performedAt, createdAt: entity.createdAt, updatedAt: entity.updatedAt, }; diff --git a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts index 969214b4209..27208833f08 100644 --- a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts @@ -1,4 +1,4 @@ -import { Entity, Property } from '@mikro-orm/core'; +import { Entity, Index, Property } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain'; import { ObjectId } from 'bson'; @@ -12,6 +12,7 @@ export interface DeletionLogEntityProps { modifiedCount?: number; deletedCount?: number; deletionRequestId?: ObjectId; + performedAt?: Date; createdAt?: Date; updatedAt?: Date; } @@ -33,6 +34,10 @@ export class DeletionLogEntity extends BaseEntityWithTimestamps { @Property({ nullable: true }) deletionRequestId?: ObjectId; + @Property({ nullable: true }) + @Index({ options: { expireAfterSeconds: 7776000 } }) + performedAt?: Date; + constructor(props: DeletionLogEntityProps) { super(); if (props.id !== undefined) { @@ -64,5 +69,9 @@ export class DeletionLogEntity extends BaseEntityWithTimestamps { if (props.updatedAt !== undefined) { this.updatedAt = props.updatedAt; } + + if (props.performedAt !== undefined) { + this.performedAt = props.performedAt; + } } } diff --git a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts index 150fed4d91e..315c249f809 100644 --- a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts @@ -1,4 +1,4 @@ -import { Entity, Index, Property } from '@mikro-orm/core'; +import { Entity, Index, Property, Unique } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain'; import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; @@ -15,19 +15,19 @@ export interface DeletionRequestEntityProps { } @Entity({ tableName: 'deletionrequests' }) -@Index({ properties: ['targetRefId', 'targetRefDomain'] }) +@Unique({ properties: ['targetRefId', 'targetRefDomain'] }) export class DeletionRequestEntity extends BaseEntityWithTimestamps { @Property() + @Index({ options: { expireAfterSeconds: 7776000 } }) deleteAfter: Date; @Property() - targetRefId: EntityId; + targetRefId!: EntityId; @Property() targetRefDomain: DeletionDomainModel; @Property() - @Index() status: DeletionStatusModel; constructor(props: DeletionRequestEntityProps) { diff --git a/apps/server/src/modules/deletion/repo/deletion-log.repo.spec.ts b/apps/server/src/modules/deletion/repo/deletion-log.repo.spec.ts index bba32408e84..1ab1e515acc 100644 --- a/apps/server/src/modules/deletion/repo/deletion-log.repo.spec.ts +++ b/apps/server/src/modules/deletion/repo/deletion-log.repo.spec.ts @@ -64,6 +64,7 @@ describe(DeletionLogRepo.name, () => { modifiedCount: domainObject.modifiedCount, deletedCount: domainObject.deletedCount, deletionRequestId: domainObject.deletionRequestId, + performedAt: domainObject.performedAt, createdAt: domainObject.createdAt, updatedAt: domainObject.updatedAt, }; @@ -95,6 +96,7 @@ describe(DeletionLogRepo.name, () => { modifiedCount: entity.modifiedCount, deletedCount: entity.deletedCount, deletionRequestId: entity.deletionRequestId?.toHexString(), + performedAt: entity.performedAt, createdAt: entity.createdAt, updatedAt: entity.updatedAt, }; @@ -149,6 +151,7 @@ describe(DeletionLogRepo.name, () => { domain: deletionLogEntity1.domain, operation: deletionLogEntity1.operation, deletionRequestId: deletionLogEntity1.deletionRequestId?.toHexString(), + performedAt: deletionLogEntity1.performedAt, modifiedCount: deletionLogEntity1.modifiedCount, deletedCount: deletionLogEntity1.deletedCount, createdAt: deletionLogEntity1.createdAt, @@ -159,6 +162,7 @@ describe(DeletionLogRepo.name, () => { domain: deletionLogEntity2.domain, operation: deletionLogEntity2.operation, deletionRequestId: deletionLogEntity2.deletionRequestId?.toHexString(), + performedAt: deletionLogEntity2.performedAt, modifiedCount: deletionLogEntity2.modifiedCount, deletedCount: deletionLogEntity2.deletedCount, createdAt: deletionLogEntity2.createdAt, diff --git a/apps/server/src/modules/deletion/repo/mapper/deletion-log.mapper.spec.ts b/apps/server/src/modules/deletion/repo/mapper/deletion-log.mapper.spec.ts index a5823f5ce32..3937710336a 100644 --- a/apps/server/src/modules/deletion/repo/mapper/deletion-log.mapper.spec.ts +++ b/apps/server/src/modules/deletion/repo/mapper/deletion-log.mapper.spec.ts @@ -16,6 +16,7 @@ describe(DeletionLogMapper.name, () => { domain: entity.domain, operation: entity.operation, deletionRequestId: entity.deletionRequestId?.toHexString(), + performedAt: entity.performedAt, modifiedCount: entity.modifiedCount, deletedCount: entity.deletedCount, createdAt: entity.createdAt, @@ -54,6 +55,7 @@ describe(DeletionLogMapper.name, () => { domain: entity.domain, operation: entity.operation, deletionRequestId: entity.deletionRequestId?.toHexString(), + performedAt: entity.performedAt, modifiedCount: entity.modifiedCount, deletedCount: entity.deletedCount, createdAt: entity.createdAt, @@ -92,6 +94,7 @@ describe(DeletionLogMapper.name, () => { domain: domainObject.domain, operation: domainObject.operation, deletionRequestId: new ObjectId(domainObject.deletionRequestId), + performedAt: domainObject.performedAt, modifiedCount: domainObject.modifiedCount, deletedCount: domainObject.deletedCount, createdAt: domainObject.createdAt, @@ -140,6 +143,7 @@ describe(DeletionLogMapper.name, () => { domain: domainObject.domain, operation: domainObject.operation, deletionRequestId: new ObjectId(domainObject.deletionRequestId), + performedAt: domainObject.performedAt, modifiedCount: domainObject.modifiedCount, deletedCount: domainObject.deletedCount, createdAt: domainObject.createdAt, diff --git a/apps/server/src/modules/deletion/repo/mapper/deletion-log.mapper.ts b/apps/server/src/modules/deletion/repo/mapper/deletion-log.mapper.ts index 820cd9d87c0..bb10fe0ac93 100644 --- a/apps/server/src/modules/deletion/repo/mapper/deletion-log.mapper.ts +++ b/apps/server/src/modules/deletion/repo/mapper/deletion-log.mapper.ts @@ -13,6 +13,7 @@ export class DeletionLogMapper { modifiedCount: entity.modifiedCount, deletedCount: entity.deletedCount, deletionRequestId: entity.deletionRequestId?.toHexString(), + performedAt: entity.performedAt, }); } @@ -26,6 +27,7 @@ export class DeletionLogMapper { modifiedCount: domainObject.modifiedCount, deletedCount: domainObject.deletedCount, deletionRequestId: new ObjectId(domainObject.deletionRequestId), + performedAt: domainObject.performedAt, }); } diff --git a/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts b/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts index 21522e5e924..449e7bda322 100644 --- a/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts +++ b/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts @@ -64,6 +64,7 @@ describe(DeletionLogService.name, () => { expect(deletionLogRepo.create).toHaveBeenCalledWith( expect.objectContaining({ id: expect.any(String), + performedAt: expect.any(Date), deletionRequestId, domain, operation, diff --git a/apps/server/src/modules/deletion/services/deletion-log.service.ts b/apps/server/src/modules/deletion/services/deletion-log.service.ts index 937d422ebb3..94f5d082530 100644 --- a/apps/server/src/modules/deletion/services/deletion-log.service.ts +++ b/apps/server/src/modules/deletion/services/deletion-log.service.ts @@ -19,6 +19,7 @@ export class DeletionLogService { ): Promise { const newDeletionLog = new DeletionLog({ id: new ObjectId().toHexString(), + performedAt: new Date(), domain, deletionRequestId, operation, diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index 8ead4d36e30..ceac1bc6d2a 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -114,10 +114,13 @@ describe(DeletionRequestUc.name, () => { await setupEntities(); }); + beforeEach(() => { + jest.clearAllMocks(); + }); + describe('createDeletionRequest', () => { describe('when creating a deletionRequest', () => { const setup = () => { - jest.clearAllMocks(); const deletionRequestToCreate: DeletionRequestBodyProps = { targetRef: { domain: DeletionDomainModel.USER, @@ -166,7 +169,6 @@ describe(DeletionRequestUc.name, () => { describe('executeDeletionRequests', () => { describe('when executing deletionRequests', () => { const setup = () => { - jest.clearAllMocks(); const deletionRequestToExecute = deletionRequestFactory.build({ deleteAfter: new Date('2023-01-01') }); const rocketChatUser: RocketChatUser = rocketChatUserFactory.build({ userId: deletionRequestToExecute.targetRefId, @@ -352,7 +354,6 @@ describe(DeletionRequestUc.name, () => { describe('when an error occurred', () => { const setup = () => { - jest.clearAllMocks(); const deletionRequestToExecute = deletionRequestFactory.build({ deleteAfter: new Date('2023-01-01') }); classService.deleteUserDataFromClasses.mockResolvedValueOnce(1); @@ -385,7 +386,6 @@ describe(DeletionRequestUc.name, () => { describe('findById', () => { describe('when searching for logs for deletionRequest which was executed', () => { const setup = () => { - jest.clearAllMocks(); const deletionRequestExecuted = deletionRequestFactory.build({ status: DeletionStatusModel.SUCCESS }); const deletionLogExecuted1 = deletionLogFactory.build({ deletionRequestId: deletionRequestExecuted.id }); const deletionLogExecuted2 = deletionLogFactory.build({ @@ -449,7 +449,6 @@ describe(DeletionRequestUc.name, () => { describe('when searching for logs for deletionRequest which was not executed', () => { const setup = () => { - jest.clearAllMocks(); const deletionRequest = deletionRequestFactory.build(); const notExecutedDeletionRequestSummary: DeletionRequestLogResponse = { targetRef: { @@ -491,7 +490,6 @@ describe(DeletionRequestUc.name, () => { describe('deleteDeletionRequestById', () => { describe('when deleting a deletionRequestId', () => { const setup = () => { - jest.clearAllMocks(); const deletionRequest = deletionRequestFactory.build(); return { diff --git a/apps/server/src/modules/server/server.module.ts b/apps/server/src/modules/server/server.module.ts index 42829caefa8..62e37d285ae 100644 --- a/apps/server/src/modules/server/server.module.ts +++ b/apps/server/src/modules/server/server.module.ts @@ -142,6 +142,7 @@ export const setupSessions = ( password: DB_PASSWORD, user: DB_USERNAME, entities: ALL_ENTITIES, + ensureIndexes: true, // debug: true, // use it for locally debugging of queries }), diff --git a/config/default.json b/config/default.json index f4ebccc4627..c3f1ade0738 100644 --- a/config/default.json +++ b/config/default.json @@ -43,6 +43,7 @@ "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 }, "ADMIN_API": { - "ENABLED": true + "ENABLED": true, + "ALLOWED_API_KEYS": "1ab2c3d4e5f61ab2c3d4e5f6" } } From 60aa0ac4fff232013455ab8c682a213af0e53ee1 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Sun, 12 Nov 2023 12:14:38 +0100 Subject: [PATCH 06/96] changes in uc and register entities --- .../src/modules/deletion/uc/deletion-request.uc.ts | 11 ++++++++--- .../rocketchat-user/entity/rocket-chat-user.entity.ts | 2 +- apps/server/src/shared/domain/entity/all-entities.ts | 4 ++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index 89a6e878eea..d7830d2b38a 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -208,14 +208,19 @@ export class DeletionRequestUc { await this.logDeletion(deletionRequest, DeletionDomainModel.USER, DeletionOperationModel.DELETE, 0, userDeleted); } - private async removeUserFromRocketChat(deletionRequest: DeletionRequest): Promise { + private async removeUserFromRocketChat(deletionRequest: DeletionRequest) { const rocketChatUser = await this.rocketChatUserService.findByUserId(deletionRequest.targetRefId); const [, rocketChatUserDeleted] = await Promise.all([ this.rocketChatService.deleteUser(rocketChatUser.username), this.rocketChatUserService.deleteByUserId(rocketChatUser.userId), ]); - - return rocketChatUserDeleted; + await this.logDeletion( + deletionRequest, + DeletionDomainModel.ROCKETCHATUSER, + DeletionOperationModel.DELETE, + 0, + rocketChatUserDeleted + ); } } diff --git a/apps/server/src/modules/rocketchat-user/entity/rocket-chat-user.entity.ts b/apps/server/src/modules/rocketchat-user/entity/rocket-chat-user.entity.ts index 6df469e0ddb..23a30f66d5e 100644 --- a/apps/server/src/modules/rocketchat-user/entity/rocket-chat-user.entity.ts +++ b/apps/server/src/modules/rocketchat-user/entity/rocket-chat-user.entity.ts @@ -13,7 +13,7 @@ export interface RocketChatUserEntityProps { updatedAt?: Date; } -@Entity({ tableName: 'rocketchatuser' }) +@Entity({ tableName: 'rocketchatusers' }) export class RocketChatUserEntity extends BaseEntityWithTimestamps { @Property() @Unique() diff --git a/apps/server/src/shared/domain/entity/all-entities.ts b/apps/server/src/shared/domain/entity/all-entities.ts index 7433ac0ea1d..d34710282dd 100644 --- a/apps/server/src/shared/domain/entity/all-entities.ts +++ b/apps/server/src/shared/domain/entity/all-entities.ts @@ -6,6 +6,8 @@ import { ContextExternalToolEntity } from '@modules/tool/context-external-tool/e import { ExternalToolEntity } from '@modules/tool/external-tool/entity'; import { SchoolExternalToolEntity } from '@modules/tool/school-external-tool/entity'; import { DeletionLogEntity, DeletionRequestEntity } from '@src/modules/deletion/entity'; +import { RocketChatUserEntity } from '@src/modules/rocketchat-user/entity'; +import { FileEntity } from '@src/modules/files/entity'; import { Account } from './account.entity'; import { BoardNode, @@ -75,6 +77,7 @@ export const ALL_ENTITIES = [ DashboardModelEntity, ExternalToolEntity, FederalStateEntity, + FileEntity, ImportUser, LessonEntity, LessonBoardElement, @@ -83,6 +86,7 @@ export const ALL_ENTITIES = [ News, PseudonymEntity, ExternalToolPseudonymEntity, + RocketChatUserEntity, Role, SchoolEntity, SchoolExternalToolEntity, From c65367b7144efa0a238e83a72df6926beb94ded4 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Sun, 12 Nov 2023 12:52:37 +0100 Subject: [PATCH 07/96] Pr fixes and minor changes --- .../src/apps/helpers/admin-api-setup-helper.ts | 1 + .../deletion-executions.controller.ts | 9 +++++---- .../controller/deletion-requests.controller.ts | 18 +++++++++--------- .../dto/deletion-execution.params.ts | 4 ++-- .../dto/deletion-request.body.params.ts | 6 +++--- apps/server/src/modules/deletion/uc/index.ts | 1 + .../modules/server/admin-api.server.module.ts | 16 ++++------------ 7 files changed, 25 insertions(+), 30 deletions(-) create mode 100644 apps/server/src/modules/deletion/uc/index.ts diff --git a/apps/server/src/apps/helpers/admin-api-setup-helper.ts b/apps/server/src/apps/helpers/admin-api-setup-helper.ts index efda8e1de35..cdae853a125 100644 --- a/apps/server/src/apps/helpers/admin-api-setup-helper.ts +++ b/apps/server/src/apps/helpers/admin-api-setup-helper.ts @@ -8,6 +8,7 @@ import { AppStartLoggable } from './app-start-loggable'; export const createAndStartAdminApiServer = async (logger: Logger) => { const nestAdminServerExpress = express(); + nestAdminServerExpress.disable('x-powered-by'); const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); diff --git a/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts b/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts index f6d1754690d..c37c7046ae4 100644 --- a/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts +++ b/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts @@ -1,8 +1,8 @@ -import { Controller, Post, Query, UseGuards } from '@nestjs/common'; +import { Controller, HttpCode, Post, Query, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { ForbiddenOperationError, ValidationError } from '@shared/common'; import { AuthGuard } from '@nestjs/passport'; -import { DeletionRequestUc } from '../uc/deletion-request.uc'; +import { DeletionRequestUc } from '../uc'; import { DeletionExecutionParams } from './dto'; @ApiTags('DeletionExecutions') @@ -12,14 +12,15 @@ export class DeletionExecutionsController { constructor(private readonly deletionRequestUc: DeletionRequestUc) {} @Post() + @HttpCode(204) @ApiOperation({ - summary: '"Queueing" a deletion request', + summary: 'Execute the deletion process', }) @ApiResponse({ status: 204, }) @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) - @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'User is not a superhero or administrator.' }) + @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'Wrong authentication' }) async executeDeletions(@Query() deletionExecutionQuery: DeletionExecutionParams) { return this.deletionRequestUc.executeDeletionRequests(deletionExecutionQuery.limit); } diff --git a/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts index 847e5b76ad0..d50f006b110 100644 --- a/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts +++ b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts @@ -1,11 +1,9 @@ -import { Body, Controller, Delete, Get, Param, Post, UseGuards } from '@nestjs/common'; +import { Body, Controller, Delete, Get, HttpCode, Param, Post, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { ForbiddenOperationError, ValidationError } from '@shared/common'; import { AuthGuard } from '@nestjs/passport'; -import { DeletionRequestUc } from '../uc/deletion-request.uc'; -import { DeletionRequestResponse } from './dto/deletion-request.response'; -import { DeletionRequestBodyProps } from './dto/deletion-request.body.params'; -import { DeletionRequestLogResponse } from './dto'; +import { DeletionRequestUc } from '../uc'; +import { DeletionRequestLogResponse, DeletionRequestBodyProps, DeletionRequestResponse } from './dto'; @ApiTags('DeletionRequests') @UseGuards(AuthGuard('api-key')) @@ -14,6 +12,7 @@ export class DeletionRequestsController { constructor(private readonly deletionRequestUc: DeletionRequestUc) {} @Post() + @HttpCode(202) @ApiOperation({ summary: '"Queueing" a deletion request', }) @@ -23,7 +22,7 @@ export class DeletionRequestsController { description: 'Returns identifier of the deletion request and when deletion is planned at', }) @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) - @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'User is not a superhero or administrator.' }) + @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'Wrong authentication' }) async createDeletionRequests( @Body() deletionRequestBody: DeletionRequestBodyProps ): Promise { @@ -35,17 +34,18 @@ export class DeletionRequestsController { summary: 'Retrieving details of performed or planned deletion', }) @ApiResponse({ - status: 202, + status: 200, type: DeletionRequestLogResponse, description: 'Return details of performed or planned deletion', }) @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) - @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'User is not a superhero or administrator.' }) + @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'Wrong authentication' }) async getPerformedDeletionDetails(@Param('requestId') requestId: string): Promise { return this.deletionRequestUc.findById(requestId); } @Delete(':requestId') + @HttpCode(204) @ApiOperation({ summary: 'Canceling a deletion request', }) @@ -53,7 +53,7 @@ export class DeletionRequestsController { status: 204, }) @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) - @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'User is not a superhero or administrator.' }) + @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'Wrong authentication' }) async cancelDeletionRequest(@Param('requestId') requestId: string) { return this.deletionRequestUc.deleteDeletionRequestById(requestId); } diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts index f7c027bc2ad..1e878745746 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts @@ -3,7 +3,7 @@ import { IsInt, Min } from 'class-validator'; export class DeletionExecutionParams { @IsInt() - @Min(0) - @ApiPropertyOptional({ description: 'Page limit, defaults to 100.', minimum: 0 }) + @Min(1) + @ApiPropertyOptional({ description: 'Page limit, defaults to 100.', minimum: 1 }) limit?: number = 100; } diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts index 90d1a789ddd..b5ac8a65869 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts @@ -1,4 +1,4 @@ -import { ApiProperty } from '@nestjs/swagger'; +import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; import { IsNumber } from 'class-validator'; import { DeletionTargetRef } from '../../uc/interface'; @@ -10,9 +10,9 @@ export class DeletionRequestBodyProps { targetRef!: DeletionTargetRef; @IsNumber() - @ApiProperty({ + @ApiPropertyOptional({ required: true, nullable: false, }) - deleteInMinutes?: number; + deleteInMinutes?: number = 43200; } diff --git a/apps/server/src/modules/deletion/uc/index.ts b/apps/server/src/modules/deletion/uc/index.ts new file mode 100644 index 00000000000..d9f93f4f8ad --- /dev/null +++ b/apps/server/src/modules/deletion/uc/index.ts @@ -0,0 +1 @@ +export * from './deletion-request.uc'; diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 6934b7b7755..76eb878c091 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -1,13 +1,10 @@ import { MikroOrmModule } from '@mikro-orm/nestjs'; -import { Inject, MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; +import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { ALL_ENTITIES } from '@shared/domain'; -import { RabbitMQWrapperModule } from '@infra/rabbitmq'; -import { RedisModule, REDIS_CLIENT } from '@infra/redis'; import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config'; import { LegacyLogger, LoggerModule } from '@src/core/logger'; -import { RedisClient } from 'redis'; import { ConfigModule } from '@nestjs/config'; -import { ServerController } from './controller/server.controller'; +import { RabbitMQWrapperModule } from '@src/infra/rabbitmq'; import { DeletionModule } from '../deletion'; import { defaultMikroOrmOptions, setupSessions } from './server.module'; import { serverConfig } from './server.config'; @@ -30,19 +27,14 @@ const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConf // debug: true, // use it for locally debugging of queries }), LoggerModule, - RedisModule, ], - controllers: [ServerController], }) export class AdminApiServerModule implements NestModule { - constructor( - @Inject(REDIS_CLIENT) private readonly redisClient: RedisClient | undefined, - private readonly logger: LegacyLogger - ) { + constructor(private readonly logger: LegacyLogger) { logger.setContext(AdminApiServerModule.name); } configure(consumer: MiddlewareConsumer) { - setupSessions(consumer, this.redisClient, this.logger); + setupSessions(consumer, undefined, this.logger); } } From bf27fd81beb70982c344177f800f4413f84229b0 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Sun, 12 Nov 2023 13:05:39 +0100 Subject: [PATCH 08/96] rename file --- ...er.spec.ts => deletion-request-log-response.builder.spec.ts} | 2 +- ...-log.builder.ts => deletion-request-log-response.builder.ts} | 0 apps/server/src/modules/deletion/uc/deletion-request.uc.ts | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename apps/server/src/modules/deletion/uc/builder/{deletion-request-log.builder.spec.ts => deletion-request-log-response.builder.spec.ts} (97%) rename apps/server/src/modules/deletion/uc/builder/{deletion-request-log.builder.ts => deletion-request-log-response.builder.ts} (100%) diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts similarity index 97% rename from apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.spec.ts rename to apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts index 84eb4de314f..347c80baa7a 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts @@ -1,6 +1,6 @@ import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionLogStatisticBuilder } from './deletion-log-statistic.builder'; -import { DeletionRequestLogResponseBuilder } from './deletion-request-log.builder'; +import { DeletionRequestLogResponseBuilder } from './deletion-request-log-response.builder'; import { DeletionTargetRefBuilder } from './deletion-target-ref.builder'; describe(DeletionRequestLogResponseBuilder.name, () => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.ts similarity index 100% rename from apps/server/src/modules/deletion/uc/builder/deletion-request-log.builder.ts rename to apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.ts diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index d7830d2b38a..9bd7a3263fc 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -19,7 +19,7 @@ import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum' import { DeletionLog } from '../domain/deletion-log.do'; import { DeletionLogStatistic } from './interface/interfaces'; import { DeletionLogStatisticBuilder } from './builder/deletion-log-statistic.builder'; -import { DeletionRequestLogResponseBuilder } from './builder/deletion-request-log.builder'; +import { DeletionRequestLogResponseBuilder } from './builder/deletion-request-log-response.builder'; import { DeletionTargetRefBuilder } from './builder/deletion-target-ref.builder'; import { DeletionRequestBodyProps, DeletionRequestLogResponse, DeletionRequestResponse } from '../controller/dto'; From d477ba79de680b949f0c539153ff42b45995507c Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Mon, 13 Nov 2023 12:49:25 +0100 Subject: [PATCH 09/96] some fixes --- .../modules/deletion/uc/deletion-request.uc.spec.ts | 13 ++++++------- apps/server/src/modules/files/index.ts | 1 + apps/server/src/modules/learnroom/index.ts | 2 +- apps/server/src/modules/lesson/index.ts | 1 + apps/server/src/modules/rocketchat-user/index.ts | 1 + config/default.json | 3 +-- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index ceac1bc6d2a..1cbe39f34a1 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -1,17 +1,16 @@ import { Test, TestingModule } from '@nestjs/testing'; import { DeepMocked, createMock } from '@golevelup/ts-jest'; import { setupEntities } from '@shared/testing'; -import { AccountService } from '@modules/account/services'; +import { AccountService } from '@modules/account'; import { ClassService } from '@modules/class'; -import { CourseGroupService, CourseService } from '@modules/learnroom/service'; -import { FilesService } from '@modules/files/service'; -import { LessonService } from '@modules/lesson/service'; +import { CourseGroupService, CourseService } from '@modules/learnroom'; +import { FilesService } from '@modules/files'; +import { LessonService } from '@modules/lesson'; import { PseudonymService } from '@modules/pseudonym'; import { TeamService } from '@modules/teams'; import { UserService } from '@modules/user'; import { RocketChatService } from '@modules/rocketchat'; -import { rocketChatUserFactory } from '@modules/rocketchat-user/domain/testing'; -import { RocketChatUser, RocketChatUserService } from '@modules/rocketchat-user'; +import { RocketChatUser, RocketChatUserService, rocketChatUserFactory } from '@modules/rocketchat-user'; import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequestService } from '../services'; @@ -348,7 +347,7 @@ describe(DeletionRequestUc.name, () => { await uc.executeDeletionRequests(); - expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(9); + expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(10); }); }); diff --git a/apps/server/src/modules/files/index.ts b/apps/server/src/modules/files/index.ts index 51eaedc8548..158749cfd9d 100644 --- a/apps/server/src/modules/files/index.ts +++ b/apps/server/src/modules/files/index.ts @@ -1 +1,2 @@ export * from './files.module'; +export * from './service'; diff --git a/apps/server/src/modules/learnroom/index.ts b/apps/server/src/modules/learnroom/index.ts index e4d907784d5..9cdde3bd511 100644 --- a/apps/server/src/modules/learnroom/index.ts +++ b/apps/server/src/modules/learnroom/index.ts @@ -1,3 +1,3 @@ export * from './learnroom.module'; export * from './service/course-copy.service'; -export { CourseService } from './service'; +export { CourseService, CourseGroupService } from './service'; diff --git a/apps/server/src/modules/lesson/index.ts b/apps/server/src/modules/lesson/index.ts index 61e512d84b7..318800cad8b 100644 --- a/apps/server/src/modules/lesson/index.ts +++ b/apps/server/src/modules/lesson/index.ts @@ -1,3 +1,4 @@ export * from './lesson.module'; export * from './types/lesson-copy-parent.params'; export * from './types/lesson-copy.params'; +export * from './service'; diff --git a/apps/server/src/modules/rocketchat-user/index.ts b/apps/server/src/modules/rocketchat-user/index.ts index 34ae0f25f87..a190609dd5e 100644 --- a/apps/server/src/modules/rocketchat-user/index.ts +++ b/apps/server/src/modules/rocketchat-user/index.ts @@ -1,3 +1,4 @@ export * from './rocketchat-user.module'; export * from './service'; export * from './domain'; +export * from './domain/testing/rocket-chat-user.factory'; diff --git a/config/default.json b/config/default.json index c3f1ade0738..f4ebccc4627 100644 --- a/config/default.json +++ b/config/default.json @@ -43,7 +43,6 @@ "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 }, "ADMIN_API": { - "ENABLED": true, - "ALLOWED_API_KEYS": "1ab2c3d4e5f61ab2c3d4e5f6" + "ENABLED": true } } From 80430f5455d0afc14fc60205653c8d536e862dde Mon Sep 17 00:00:00 2001 From: Bartosz Nowicki <116367402+bn-pass@users.noreply.github.com> Date: Mon, 13 Nov 2023 15:05:12 +0100 Subject: [PATCH 10/96] add exposing admin API port from the api-svc --- ansible/roles/schulcloud-server-core/templates/svc.yml.j2 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 b/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 index 99a1e9df8ac..0ef85ec904d 100644 --- a/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 @@ -12,6 +12,10 @@ spec: targetPort: 3030 protocol: TCP name: api + - port: 4030 + targetPort: 4030 + protocol: TCP + name: api-admin - port: {{ PORT_METRICS_SERVER }} targetPort: 9090 protocol: TCP From 24b823c89ed5bbb6bb29e43af33da8356102297f Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Mon, 13 Nov 2023 15:45:49 +0100 Subject: [PATCH 11/96] add server test module + delete FileEntity from allEntities --- .../modules/server/admin-api.server.module.ts | 37 ++++++++++++++++++- .../src/shared/domain/entity/all-entities.ts | 2 - 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 76eb878c091..90512795071 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -1,13 +1,15 @@ import { MikroOrmModule } from '@mikro-orm/nestjs'; -import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; +import { DynamicModule, MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { ALL_ENTITIES } from '@shared/domain'; import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config'; import { LegacyLogger, LoggerModule } from '@src/core/logger'; import { ConfigModule } from '@nestjs/config'; -import { RabbitMQWrapperModule } from '@src/infra/rabbitmq'; +import { RabbitMQWrapperModule, RabbitMQWrapperTestModule } from '@src/infra/rabbitmq'; +import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@src/infra/database'; import { DeletionModule } from '../deletion'; import { defaultMikroOrmOptions, setupSessions } from './server.module'; import { serverConfig } from './server.config'; +import { ServerController } from './controller/server.controller'; const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), DeletionModule]; @@ -38,3 +40,34 @@ export class AdminApiServerModule implements NestModule { setupSessions(consumer, undefined, this.logger); } } + +@Module({ + imports: [ + ...serverModules, + MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions }), + RabbitMQWrapperTestModule, + LoggerModule, + ], + controllers: [ServerController], +}) +export class AdminApiServerTestModule implements NestModule { + constructor(private readonly logger: LegacyLogger) { + logger.setContext(AdminApiServerTestModule.name); + } + + configure(consumer: MiddlewareConsumer) { + setupSessions(consumer, undefined, this.logger); + } + + static forRoot(options?: MongoDatabaseModuleOptions): DynamicModule { + return { + module: AdminApiServerModule, + imports: [ + ...serverModules, + MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions, ...options }), + RabbitMQWrapperTestModule, + ], + controllers: [ServerController], + }; + } +} diff --git a/apps/server/src/shared/domain/entity/all-entities.ts b/apps/server/src/shared/domain/entity/all-entities.ts index d34710282dd..8bfca7728b2 100644 --- a/apps/server/src/shared/domain/entity/all-entities.ts +++ b/apps/server/src/shared/domain/entity/all-entities.ts @@ -7,7 +7,6 @@ import { ExternalToolEntity } from '@modules/tool/external-tool/entity'; import { SchoolExternalToolEntity } from '@modules/tool/school-external-tool/entity'; import { DeletionLogEntity, DeletionRequestEntity } from '@src/modules/deletion/entity'; import { RocketChatUserEntity } from '@src/modules/rocketchat-user/entity'; -import { FileEntity } from '@src/modules/files/entity'; import { Account } from './account.entity'; import { BoardNode, @@ -77,7 +76,6 @@ export const ALL_ENTITIES = [ DashboardModelEntity, ExternalToolEntity, FederalStateEntity, - FileEntity, ImportUser, LessonEntity, LessonBoardElement, From f4e10ffdf81245e249c418808c5262ce1dd2f9dd Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Mon, 13 Nov 2023 15:51:45 +0100 Subject: [PATCH 12/96] change module --- apps/server/src/modules/server/admin-api.server.module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 90512795071..6a609c16d9c 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -61,7 +61,7 @@ export class AdminApiServerTestModule implements NestModule { static forRoot(options?: MongoDatabaseModuleOptions): DynamicModule { return { - module: AdminApiServerModule, + module: AdminApiServerTestModule, imports: [ ...serverModules, MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions, ...options }), From f2f2a5d6273d0220cba07d54b53c3f90b7436d80 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Mon, 13 Nov 2023 17:53:56 +0100 Subject: [PATCH 13/96] admin api server module test impl --- .../modules/server/admin-api.server.module.ts | 7 +++--- .../admin-api-server.controller.spec.ts | 25 +++++++++++++++++++ .../controller/admin-api-server.controller.ts | 9 +++++++ .../api-test/admin-api.server.spec.ts | 22 ++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 apps/server/src/modules/server/controller/admin-api-server.controller.spec.ts create mode 100644 apps/server/src/modules/server/controller/admin-api-server.controller.ts create mode 100644 apps/server/src/modules/server/controller/api-test/admin-api.server.spec.ts diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 6a609c16d9c..94e7a77531f 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -9,7 +9,7 @@ import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@src/infr import { DeletionModule } from '../deletion'; import { defaultMikroOrmOptions, setupSessions } from './server.module'; import { serverConfig } from './server.config'; -import { ServerController } from './controller/server.controller'; +import { AdminApiServerController } from './controller/admin-api-server.controller'; const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), DeletionModule]; @@ -30,6 +30,7 @@ const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConf }), LoggerModule, ], + controllers: [AdminApiServerController], }) export class AdminApiServerModule implements NestModule { constructor(private readonly logger: LegacyLogger) { @@ -48,7 +49,7 @@ export class AdminApiServerModule implements NestModule { RabbitMQWrapperTestModule, LoggerModule, ], - controllers: [ServerController], + controllers: [AdminApiServerController], }) export class AdminApiServerTestModule implements NestModule { constructor(private readonly logger: LegacyLogger) { @@ -67,7 +68,7 @@ export class AdminApiServerTestModule implements NestModule { MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions, ...options }), RabbitMQWrapperTestModule, ], - controllers: [ServerController], + controllers: [AdminApiServerController], }; } } diff --git a/apps/server/src/modules/server/controller/admin-api-server.controller.spec.ts b/apps/server/src/modules/server/controller/admin-api-server.controller.spec.ts new file mode 100644 index 00000000000..256b20126ea --- /dev/null +++ b/apps/server/src/modules/server/controller/admin-api-server.controller.spec.ts @@ -0,0 +1,25 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { AdminApiServerController } from './admin-api-server.controller'; + +describe('AdminApiServerController', () => { + let module: TestingModule; + let serverController: AdminApiServerController; + + beforeAll(async () => { + module = await Test.createTestingModule({ + controllers: [AdminApiServerController], + }).compile(); + + serverController = module.get(AdminApiServerController); + }); + + afterAll(async () => { + await module.close(); + }); + + describe('root', () => { + it('should return "Admin Server API"', () => { + expect(serverController.getHello()).toBe('Admin Server API'); + }); + }); +}); diff --git a/apps/server/src/modules/server/controller/admin-api-server.controller.ts b/apps/server/src/modules/server/controller/admin-api-server.controller.ts new file mode 100644 index 00000000000..6393d4b6a05 --- /dev/null +++ b/apps/server/src/modules/server/controller/admin-api-server.controller.ts @@ -0,0 +1,9 @@ +import { Controller, Get } from '@nestjs/common'; + +@Controller() +export class AdminApiServerController { + @Get() + getHello(): string { + return 'Admin Server API'; + } +} diff --git a/apps/server/src/modules/server/controller/api-test/admin-api.server.spec.ts b/apps/server/src/modules/server/controller/api-test/admin-api.server.spec.ts new file mode 100644 index 00000000000..05a9e7dbd91 --- /dev/null +++ b/apps/server/src/modules/server/controller/api-test/admin-api.server.spec.ts @@ -0,0 +1,22 @@ +import { INestApplication } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import request from 'supertest'; +import { AdminApiServerTestModule } from '../../admin-api.server.module'; + +describe('Admin Api Server Controller (API)', () => { + let app: INestApplication; + beforeAll(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [AdminApiServerTestModule], + }).compile(); + + app = moduleFixture.createNestApplication(); + await app.init(); + }); + + afterAll(async () => { + await app.close(); + }); + + it('/ (GET)', () => request(app.getHttpServer()).get('/').expect(200).expect('Admin Server API')); +}); From 9c4be9eb26543e607fd93cf9ea6a04f6f820b830 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Tue, 14 Nov 2023 08:57:36 +0100 Subject: [PATCH 14/96] add some tests --- .../deletion-request-create.api.spec.ts | 109 ++++++++++++++++++ .../modules/server/admin-api.server.module.ts | 2 +- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts new file mode 100644 index 00000000000..54f196df415 --- /dev/null +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -0,0 +1,109 @@ +import { ExecutionContext, INestApplication } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { ApiValidationError } from '@shared/common'; +import { Request } from 'express'; +import request from 'supertest'; +import { AuthGuard } from '@nestjs/passport'; +import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; +import { DeletionRequestBodyProps, DeletionRequestResponse } from '../dto'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; + +const baseRouteName = '/deletionRequests'; + +class API { + app: INestApplication; + + constructor(app: INestApplication) { + this.app = app; + } + + async post(requestBody: object) { + const response = await request(this.app.getHttpServer()) + .post(`${baseRouteName}`) + .set('Accept', 'application/json') + .send(requestBody); + + return { + result: response.body as DeletionRequestResponse, + error: response.body as ApiValidationError, + status: response.status, + }; + } +} + +describe(`deletionRequest create (api)`, () => { + let app: INestApplication; + let api: API; + const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [AdminApiServerTestModule], + }) + .overrideGuard(AuthGuard('api-key')) + .useValue({ + canActivate(context: ExecutionContext) { + const req: Request = context.switchToHttp().getRequest(); + req.headers['X-API-KEY'] = API_KEY; + return true; + }, + }) + .compile(); + + app = module.createNestApplication(); + await app.init(); + api = new API(app); + }); + + afterAll(async () => { + await app.close(); + }); + + const setup = () => { + const deletionRequestToCreate: DeletionRequestBodyProps = { + targetRef: { + domain: DeletionDomainModel.USER, + id: '653e4833cc39e5907a1e18d2', + }, + deleteInMinutes: 1440, + }; + + const deletionRequestToCreateDefected = { + targetRef: { + domein: DeletionDomainModel.USER, + id: '653e4833cc39e5907a1e18d3', + }, + deleteInMinutes: 1440, + }; + return { deletionRequestToCreate, deletionRequestToCreateDefected }; + }; + + describe('when create deeltionRequest with object good formatted', () => { + it('should return status 202', async () => { + const { deletionRequestToCreate } = setup(); + + const response = await api.post(deletionRequestToCreate); + + expect(response.status).toEqual(202); + }); + + it('should return the created deletionRequest', async () => { + const { deletionRequestToCreate } = setup(); + + const { result } = await api.post(deletionRequestToCreate); + + expect(result.requestId).toBeDefined(); + }); + }); + + // For improvement. Server throws error 500 + // describe('when create deeltionRequest with object wrong formatted', () => { + // it('should return status 403', async () => { + // const { deletionRequestToCreateDefected } = setup(); + + // const response = await api.post(deletionRequestToCreateDefected); + + // expect(response.status).toEqual(403); + // }); + // }); +}); diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 90512795071..6a609c16d9c 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -61,7 +61,7 @@ export class AdminApiServerTestModule implements NestModule { static forRoot(options?: MongoDatabaseModuleOptions): DynamicModule { return { - module: AdminApiServerModule, + module: AdminApiServerTestModule, imports: [ ...serverModules, MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions, ...options }), From b5e2a01be6a257f143a783cb23f54aaf074113c1 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Tue, 14 Nov 2023 14:24:47 +0100 Subject: [PATCH 15/96] add some test for API --- .../api-test/deletion-executions.api.spec.ts | 67 ++++++++++++++ .../deletion-request-create.api.spec.ts | 22 +---- .../deletion-request-delete.api.spec.ts | 91 ++++++++++++++++++ .../deletion-request-find.api.spec.ts | 92 +++++++++++++++++++ .../deletion-executions.controller.ts | 3 - .../deletion-requests.controller.ts | 7 -- 6 files changed, 252 insertions(+), 30 deletions(-) create mode 100644 apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts create mode 100644 apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts create mode 100644 apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts new file mode 100644 index 00000000000..60587d68132 --- /dev/null +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts @@ -0,0 +1,67 @@ +import { ExecutionContext, INestApplication } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { ApiValidationError } from '@shared/common'; +import { Request } from 'express'; +import request from 'supertest'; +import { AuthGuard } from '@nestjs/passport'; +import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; +import { DeletionExecutionParams } from '../dto'; + +const baseRouteName = '/deletionExecutions'; + +class API { + app: INestApplication; + + constructor(app: INestApplication) { + this.app = app; + } + + async post(query?: DeletionExecutionParams) { + const response = await request(this.app.getHttpServer()) + .post(`${baseRouteName}`) + .set('Accept', 'application/json') + .query(query || {}); + + return { + error: response.body as ApiValidationError, + status: response.status, + }; + } +} + +describe(`deletionExecution (api)`, () => { + let app: INestApplication; + let api: API; + const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [AdminApiServerTestModule], + }) + .overrideGuard(AuthGuard('api-key')) + .useValue({ + canActivate(context: ExecutionContext) { + const req: Request = context.switchToHttp().getRequest(); + req.headers['X-API-KEY'] = API_KEY; + return true; + }, + }) + .compile(); + + app = module.createNestApplication(); + await app.init(); + api = new API(app); + }); + + afterAll(async () => { + await app.close(); + }); + + describe('when execute deletionRequests', () => { + it('should return status 204', async () => { + const response = await api.post(); + + expect(response.status).toEqual(204); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index 54f196df415..7634ec93e42 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -68,17 +68,10 @@ describe(`deletionRequest create (api)`, () => { deleteInMinutes: 1440, }; - const deletionRequestToCreateDefected = { - targetRef: { - domein: DeletionDomainModel.USER, - id: '653e4833cc39e5907a1e18d3', - }, - deleteInMinutes: 1440, - }; - return { deletionRequestToCreate, deletionRequestToCreateDefected }; + return { deletionRequestToCreate }; }; - describe('when create deeltionRequest with object good formatted', () => { + describe('when create deletionRequest', () => { it('should return status 202', async () => { const { deletionRequestToCreate } = setup(); @@ -95,15 +88,4 @@ describe(`deletionRequest create (api)`, () => { expect(result.requestId).toBeDefined(); }); }); - - // For improvement. Server throws error 500 - // describe('when create deeltionRequest with object wrong formatted', () => { - // it('should return status 403', async () => { - // const { deletionRequestToCreateDefected } = setup(); - - // const response = await api.post(deletionRequestToCreateDefected); - - // expect(response.status).toEqual(403); - // }); - // }); }); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts new file mode 100644 index 00000000000..aa1779cb9ce --- /dev/null +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts @@ -0,0 +1,91 @@ +import { ExecutionContext, INestApplication } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { ApiValidationError } from '@shared/common'; +import { Request } from 'express'; +import request from 'supertest'; +import { AuthGuard } from '@nestjs/passport'; +import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; +import { cleanupCollections } from '@shared/testing'; +import { EntityManager } from '@mikro-orm/mongodb'; +import { deletionRequestEntityFactory } from '../../entity/testing/factory/deletion-request.entity.factory'; +import { DeletionRequestEntity } from '../../entity'; + +const baseRouteName = '/deletionRequests'; + +class API { + app: INestApplication; + + constructor(app: INestApplication) { + this.app = app; + } + + async delete(deletionRequestId: string) { + const response = await request(this.app.getHttpServer()) + .delete(`${baseRouteName}/${deletionRequestId}`) + .set('Accept', 'application/json'); + + return { + error: response.body as ApiValidationError, + status: response.status, + }; + } +} + +describe(`deletionRequest delete (api)`, () => { + let app: INestApplication; + let em: EntityManager; + let api: API; + const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [AdminApiServerTestModule], + }) + .overrideGuard(AuthGuard('api-key')) + .useValue({ + canActivate(context: ExecutionContext) { + const req: Request = context.switchToHttp().getRequest(); + req.headers['X-API-KEY'] = API_KEY; + return true; + }, + }) + .compile(); + + app = module.createNestApplication(); + await app.init(); + em = module.get(EntityManager); + api = new API(app); + }); + + afterAll(async () => { + await app.close(); + }); + + const setup = async () => { + await cleanupCollections(em); + const deletionRequest = deletionRequestEntityFactory.build(); + + await em.persistAndFlush(deletionRequest); + em.clear(); + + return { deletionRequest }; + }; + + describe('when deletiong deletionRequest', () => { + it('should return status 204', async () => { + const { deletionRequest } = await setup(); + + const response = await api.delete(deletionRequest.id); + + expect(response.status).toEqual(204); + }); + + it('should actually delete deletionRequest', async () => { + const { deletionRequest } = await setup(); + + await api.delete(deletionRequest.id); + + await expect(em.findOneOrFail(DeletionRequestEntity, deletionRequest.id)).rejects.toThrow(); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts new file mode 100644 index 00000000000..e95e624f3d8 --- /dev/null +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts @@ -0,0 +1,92 @@ +import { ExecutionContext, INestApplication } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { ApiValidationError } from '@shared/common'; +import { Request } from 'express'; +import request from 'supertest'; +import { AuthGuard } from '@nestjs/passport'; +import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; +import { EntityManager } from '@mikro-orm/mongodb'; +import { cleanupCollections } from '@shared/testing'; +import { DeletionRequestLogResponse } from '../dto'; +import { deletionRequestEntityFactory } from '../../entity/testing/factory/deletion-request.entity.factory'; + +const baseRouteName = '/deletionRequests'; + +class API { + app: INestApplication; + + constructor(app: INestApplication) { + this.app = app; + } + + async get(deletionRequestId: string) { + const response = await request(this.app.getHttpServer()) + .get(`${baseRouteName}/${deletionRequestId}`) + .set('Accept', 'application/json'); + + return { + result: response.body as DeletionRequestLogResponse, + error: response.body as ApiValidationError, + status: response.status, + }; + } +} + +describe(`deletionRequest find (api)`, () => { + let app: INestApplication; + let em: EntityManager; + let api: API; + const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [AdminApiServerTestModule], + }) + .overrideGuard(AuthGuard('api-key')) + .useValue({ + canActivate(context: ExecutionContext) { + const req: Request = context.switchToHttp().getRequest(); + req.headers['X-API-KEY'] = API_KEY; + return true; + }, + }) + .compile(); + + app = module.createNestApplication(); + await app.init(); + em = module.get(EntityManager); + api = new API(app); + }); + + afterAll(async () => { + await app.close(); + }); + + const setup = async () => { + await cleanupCollections(em); + const deletionRequest = deletionRequestEntityFactory.build(); + + await em.persistAndFlush(deletionRequest); + em.clear(); + + return { deletionRequest }; + }; + + describe('when searching for deletionRequest', () => { + it('should return status 202', async () => { + const { deletionRequest } = await setup(); + + const response = await api.get(deletionRequest.id); + + expect(response.status).toEqual(200); + }); + + it('should return the found deletionRequest', async () => { + const { deletionRequest } = await setup(); + + const { result } = await api.get(deletionRequest.id); + + expect(result.targetRef.id).toEqual(deletionRequest.targetRefId); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts b/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts index c37c7046ae4..143e69735dd 100644 --- a/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts +++ b/apps/server/src/modules/deletion/controller/deletion-executions.controller.ts @@ -1,6 +1,5 @@ import { Controller, HttpCode, Post, Query, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; -import { ForbiddenOperationError, ValidationError } from '@shared/common'; import { AuthGuard } from '@nestjs/passport'; import { DeletionRequestUc } from '../uc'; import { DeletionExecutionParams } from './dto'; @@ -19,8 +18,6 @@ export class DeletionExecutionsController { @ApiResponse({ status: 204, }) - @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) - @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'Wrong authentication' }) async executeDeletions(@Query() deletionExecutionQuery: DeletionExecutionParams) { return this.deletionRequestUc.executeDeletionRequests(deletionExecutionQuery.limit); } diff --git a/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts index d50f006b110..05fb3948841 100644 --- a/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts +++ b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts @@ -1,6 +1,5 @@ import { Body, Controller, Delete, Get, HttpCode, Param, Post, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; -import { ForbiddenOperationError, ValidationError } from '@shared/common'; import { AuthGuard } from '@nestjs/passport'; import { DeletionRequestUc } from '../uc'; import { DeletionRequestLogResponse, DeletionRequestBodyProps, DeletionRequestResponse } from './dto'; @@ -21,8 +20,6 @@ export class DeletionRequestsController { type: DeletionRequestResponse, description: 'Returns identifier of the deletion request and when deletion is planned at', }) - @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) - @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'Wrong authentication' }) async createDeletionRequests( @Body() deletionRequestBody: DeletionRequestBodyProps ): Promise { @@ -38,8 +35,6 @@ export class DeletionRequestsController { type: DeletionRequestLogResponse, description: 'Return details of performed or planned deletion', }) - @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) - @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'Wrong authentication' }) async getPerformedDeletionDetails(@Param('requestId') requestId: string): Promise { return this.deletionRequestUc.findById(requestId); } @@ -52,8 +47,6 @@ export class DeletionRequestsController { @ApiResponse({ status: 204, }) - @ApiResponse({ status: 400, type: ValidationError, description: 'Request data has invalid format.' }) - @ApiResponse({ status: 403, type: ForbiddenOperationError, description: 'Wrong authentication' }) async cancelDeletionRequest(@Param('requestId') requestId: string) { return this.deletionRequestUc.deleteDeletionRequestById(requestId); } From bd430e98a5f695412692b7e5a293720ca28371ab Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Tue, 14 Nov 2023 17:01:49 +0100 Subject: [PATCH 16/96] add some test --- .../api-test/deletion-executions.api.spec.ts | 2 +- .../deletion-request-create.api.spec.ts | 42 ++++++++++++++++++- .../dto/deletion-execution.params.ts | 3 +- .../dto/deletion-request.body.params.ts | 4 +- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts index 60587d68132..83a784fb4cf 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts @@ -57,7 +57,7 @@ describe(`deletionExecution (api)`, () => { await app.close(); }); - describe('when execute deletionRequests', () => { + describe('when execute deletionRequests with default limit', () => { it('should return status 204', async () => { const response = await api.post(); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index 7634ec93e42..432356fc83c 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -5,8 +5,10 @@ import { Request } from 'express'; import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; +import { EntityManager } from '@mikro-orm/mongodb'; import { DeletionRequestBodyProps, DeletionRequestResponse } from '../dto'; import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionRequestEntity } from '../../entity'; const baseRouteName = '/deletionRequests'; @@ -33,6 +35,7 @@ class API { describe(`deletionRequest create (api)`, () => { let app: INestApplication; + let em: EntityManager; let api: API; const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; @@ -52,6 +55,7 @@ describe(`deletionRequest create (api)`, () => { app = module.createNestApplication(); await app.init(); + em = module.get(EntityManager); api = new API(app); }); @@ -65,10 +69,17 @@ describe(`deletionRequest create (api)`, () => { domain: DeletionDomainModel.USER, id: '653e4833cc39e5907a1e18d2', }, - deleteInMinutes: 1440, }; - return { deletionRequestToCreate }; + const deletionRequestToImmediateRemoval: DeletionRequestBodyProps = { + targetRef: { + domain: DeletionDomainModel.USER, + id: '653e4833cc39e5907a1e18d2', + }, + deleteInMinutes: 0, + }; + + return { deletionRequestToCreate, deletionRequestToImmediateRemoval }; }; describe('when create deletionRequest', () => { @@ -87,5 +98,32 @@ describe(`deletionRequest create (api)`, () => { expect(result.requestId).toBeDefined(); }); + + it('should create deletionRequest with default deletion time (add 43200 minutes to current time) ', async () => { + const { deletionRequestToCreate } = setup(); + + const { result } = await api.post(deletionRequestToCreate); + + const createdDeletionRequestId = result.requestId; + + const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId); + + const deletionPlannedAt = createdItem.createdAt; + deletionPlannedAt.setMinutes(deletionPlannedAt.getMinutes() + 43200); + + expect(createdItem.deleteAfter).toEqual(deletionPlannedAt); + }); + + it('should create deletionRequest with deletion time (0 minutes to current time) ', async () => { + const { deletionRequestToImmediateRemoval } = setup(); + + const { result } = await api.post(deletionRequestToImmediateRemoval); + + const createdDeletionRequestId = result.requestId; + + const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId); + + expect(createdItem.createdAt).toEqual(createdItem.deleteAfter); + }); }); }); diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts index 1e878745746..9ca4a18bd71 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.ts @@ -1,9 +1,10 @@ import { ApiPropertyOptional } from '@nestjs/swagger'; -import { IsInt, Min } from 'class-validator'; +import { IsInt, IsOptional, Min } from 'class-validator'; export class DeletionExecutionParams { @IsInt() @Min(1) + @IsOptional() @ApiPropertyOptional({ description: 'Page limit, defaults to 100.', minimum: 1 }) limit?: number = 100; } diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts index b5ac8a65869..95f3705510c 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts @@ -1,5 +1,5 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; -import { IsNumber } from 'class-validator'; +import { IsNumber, IsOptional, Min } from 'class-validator'; import { DeletionTargetRef } from '../../uc/interface'; export class DeletionRequestBodyProps { @@ -10,6 +10,8 @@ export class DeletionRequestBodyProps { targetRef!: DeletionTargetRef; @IsNumber() + @Min(0) + @IsOptional() @ApiPropertyOptional({ required: true, nullable: false, From caf07ab3f800aa786c25037cb051e4a48fda1b24 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 14 Nov 2023 22:06:05 +0100 Subject: [PATCH 17/96] x-api-key.strategy tests --- .../strategy/x-api-key.strategy.spec.ts | 67 +++++++++++++++++++ config/default.json | 3 +- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts new file mode 100644 index 00000000000..c6c526c8ed0 --- /dev/null +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts @@ -0,0 +1,67 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import { OAuthTokenDto } from '@modules/oauth'; +import { UnauthorizedException } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { UserDO } from '@shared/domain/domainobject/user.do'; +import { userDoFactory } from '@shared/testing'; +import { Configuration } from '@hpi-schul-cloud/commons/lib'; +import { ConfigService } from '@nestjs/config'; +import { createMock } from '@golevelup/ts-jest'; +import { SchoolInMigrationLoggableException } from '../loggable'; +import { XApiKeyStrategy } from './x-api-key.strategy'; +import { IXApiKeyConfig } from '../config/x-api-key.config'; + +describe('XApiKeyStrategy', () => { + let module: TestingModule; + let strategy: XApiKeyStrategy; + Configuration.set('ADMIN_API__ALLOWED_API_KEYS', '1ab2c3d4e5f61ab2c3d4e5f6'); + + beforeAll(async () => { + module = await Test.createTestingModule({ + imports: [], + providers: [ + XApiKeyStrategy, + { + provide: ConfigService, + useValue: createMock>({ get: () => ['1ab2c3d4e5f61ab2c3d4e5f6'] }), + }, + ], + }).compile(); + + strategy = module.get(XApiKeyStrategy); + }); + + afterAll(async () => { + await module.close(); + }); + + beforeEach(() => { + jest.resetAllMocks(); + }); + + describe('validate', () => { + const setup = () => { + const CORRECT_API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; + const INVALID_API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6778173'; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const done = jest.fn((error: Error | null, data: boolean | null) => {}); + + return { CORRECT_API_KEY, INVALID_API_KEY, done }; + }; + describe('when a valid api key is provided', () => { + it('should do nothing', () => { + const { CORRECT_API_KEY, done } = setup(); + const result = strategy.validate(CORRECT_API_KEY, done); + expect(done).toBeCalledWith(null, true); + }); + }); + + describe('when a invalid api key is provided', () => { + it('should throw error', () => { + const { INVALID_API_KEY, done } = setup(); + const result = strategy.validate(INVALID_API_KEY, done); + expect(done).toBeCalledWith(new UnauthorizedException(), null); + }); + }); + }); +}); diff --git a/config/default.json b/config/default.json index f4ebccc4627..c3f1ade0738 100644 --- a/config/default.json +++ b/config/default.json @@ -43,6 +43,7 @@ "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 }, "ADMIN_API": { - "ENABLED": true + "ENABLED": true, + "ALLOWED_API_KEYS": "1ab2c3d4e5f61ab2c3d4e5f6" } } From b0408761fbf67785a7757d6639d90cd1979781f0 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 14 Nov 2023 23:09:55 +0100 Subject: [PATCH 18/96] x-api-key-strategy-tests impl --- .../helpers/admin-api-setup-helper.spec.ts | 160 ++++++++++++++++++ .../strategy/x-api-key.strategy.spec.ts | 10 ++ 2 files changed, 170 insertions(+) create mode 100644 apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts diff --git a/apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts b/apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts new file mode 100644 index 00000000000..e306a7cddd3 --- /dev/null +++ b/apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts @@ -0,0 +1,160 @@ +import { createMock } from '@golevelup/ts-jest'; +import { Configuration } from '@hpi-schul-cloud/commons'; +import { IConfig } from '@hpi-schul-cloud/commons/lib/interfaces/IConfig'; +import { + PrometheusMetricsConfig, + createAPIResponseTimeMetricMiddleware, + createPrometheusMetricsApp, +} from '@infra/metrics'; +import { Logger } from '@src/core/logger'; +import express, { Express, NextFunction, Request, RequestHandler, Response } from 'express'; +import { + PrometheusMetricsSetupState, + PrometheusMetricsSetupStateLoggable, + addPrometheusMetricsMiddlewaresIfEnabled, + createAndStartPrometheusMetricsAppIfEnabled, +} from './prometheus-metrics'; + +jest.mock('@infra/metrics', () => { + const moduleMock: unknown = { + ...jest.requireActual('@infra/metrics'), + createAPIResponseTimeMetricMiddleware: jest.fn(), + createPrometheusMetricsApp: jest.fn(), + }; + + return moduleMock; +}); + +const testLogger = createMock(); + +let configBefore: IConfig; + +beforeAll(() => { + configBefore = Configuration.toObject({ plainSecrets: true }); +}); + +beforeEach(() => { + Configuration.reset(configBefore); + + const middlewareMock: RequestHandler = (_req: Request, _res: Response, next: NextFunction) => { + next(); + }; + + (createAPIResponseTimeMetricMiddleware as jest.Mock).mockClear(); + (createAPIResponseTimeMetricMiddleware as jest.Mock).mockReturnValue(middlewareMock); + + const appMock = { listen: jest.fn() }; + + (createPrometheusMetricsApp as jest.Mock).mockClear(); + (createPrometheusMetricsApp as jest.Mock).mockReturnValue(appMock); +}); + +afterAll(() => { + Configuration.reset(configBefore); +}); + +describe('PrometheusMetricsSetupStateLoggable', () => { + describe('getLogMessage', () => { + describe('should return a log message with proper content', () => { + const expectedMessage = 'Setting up Prometheus metrics...'; + + it.each([ + [PrometheusMetricsSetupState.FEATURE_DISABLED_MIDDLEWARES_WILL_NOT_BE_CREATED], + [PrometheusMetricsSetupState.API_RESPONSE_TIME_METRIC_MIDDLEWARE_SUCCESSFULLY_ADDED], + [PrometheusMetricsSetupState.FEATURE_DISABLED_APP_WILL_NOT_BE_CREATED], + [PrometheusMetricsSetupState.COLLECTING_DEFAULT_METRICS_DISABLED], + [PrometheusMetricsSetupState.COLLECTING_METRICS_ROUTE_METRICS_DISABLED], + ])("for the '%s' state", (state: PrometheusMetricsSetupState) => { + const testLogMessage = new PrometheusMetricsSetupStateLoggable(state).getLogMessage(); + + expect(testLogMessage).toHaveProperty('message', expectedMessage); + expect(testLogMessage).toHaveProperty('data', { state }); + }); + }); + }); +}); + +describe('addPrometheusMetricsMiddlewaresIfEnabled', () => { + let testApp: Express; + let testAppUseSpy: jest.SpyInstance; + + beforeEach(() => { + testApp = express(); + testAppUseSpy = jest.spyOn(testApp, 'use'); + }); + + it('should create the API response time metric middleware and should add it to the given app', () => { + // To not create setters in the PrometheusMetricsConfig just for the unit tests + // purpose, we will enable the Prometheus metrics feature the way it should be + // enabled in a real app which is via the app configuration. + Configuration.set('FEATURE_PROMETHEUS_METRICS_ENABLED', true); + PrometheusMetricsConfig.reload(); + + addPrometheusMetricsMiddlewaresIfEnabled(testLogger, testApp); + + expect(createAPIResponseTimeMetricMiddleware).toBeCalled(); + expect(testAppUseSpy).toBeCalled(); + }); + + it('should not create the API response time metric middleware and should not add it to the given app', () => { + Configuration.set('FEATURE_PROMETHEUS_METRICS_ENABLED', false); + PrometheusMetricsConfig.reload(); + + addPrometheusMetricsMiddlewaresIfEnabled(testLogger, testApp); + + expect(createAPIResponseTimeMetricMiddleware).not.toBeCalled(); + expect(testAppUseSpy).not.toBeCalled(); + }); +}); + +describe('createAndStartAdminApiServer', () => { + describe('should create Prometheus metrics app and run it', () => { + const testPort = 9000; + const testLoggerSpy = jest.spyOn(testLogger, 'info'); + + let appMockListenFn: jest.Mock; + + beforeEach(() => { + testLoggerSpy.mockClear(); + appMockListenFn = jest.fn(); + + (createPrometheusMetricsApp as jest.Mock).mockClear(); + (createPrometheusMetricsApp as jest.Mock).mockReturnValue({ listen: appMockListenFn }); + }); + + it.each([ + [true, true], + [false, false], + [true, false], + [false, true], + ])( + "with collecting default metrics set to '%s' and collecting metrics route metrics set to '%s'", + (collectDefaultMetrics: boolean, collectMetricsRouteMetrics: boolean) => { + Configuration.set('FEATURE_PROMETHEUS_METRICS_ENABLED', true); + Configuration.set('PROMETHEUS_METRICS_PORT', testPort); + Configuration.set('PROMETHEUS_METRICS_COLLECT_DEFAULT_METRICS', collectDefaultMetrics); + Configuration.set('PROMETHEUS_METRICS_COLLECT_METRICS_ROUTE_METRICS', collectMetricsRouteMetrics); + PrometheusMetricsConfig.reload(); + + createAndStartPrometheusMetricsAppIfEnabled(testLogger); + + expect(createPrometheusMetricsApp).toBeCalledTimes(1); + expect(appMockListenFn).toHaveBeenLastCalledWith(testPort, expect.any(Function)); + + // Also test logging info message about running Prometheus metrics app. + // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access + appMockListenFn.mock.lastCall[1](); + expect(testLoggerSpy).toBeCalledTimes(1); + } + ); + }); + + it('should not create Prometheus metrics app if the whole feature is not enabled', () => { + Configuration.set('FEATURE_PROMETHEUS_METRICS_ENABLED', false); + PrometheusMetricsConfig.reload(); + + createAndStartPrometheusMetricsAppIfEnabled(testLogger); + + expect(createPrometheusMetricsApp).not.toBeCalled(); + }); +}); diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts index c6c526c8ed0..9bc03ab3a18 100644 --- a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts @@ -14,6 +14,7 @@ import { IXApiKeyConfig } from '../config/x-api-key.config'; describe('XApiKeyStrategy', () => { let module: TestingModule; let strategy: XApiKeyStrategy; + let configService: ConfigService; Configuration.set('ADMIN_API__ALLOWED_API_KEYS', '1ab2c3d4e5f61ab2c3d4e5f6'); beforeAll(async () => { @@ -29,6 +30,7 @@ describe('XApiKeyStrategy', () => { }).compile(); strategy = module.get(XApiKeyStrategy); + configService = module.get(ConfigService); }); afterAll(async () => { @@ -64,4 +66,12 @@ describe('XApiKeyStrategy', () => { }); }); }); + + describe('constructor', () => { + it('should create strategy', () => { + const ApiKeyStrategy = new XApiKeyStrategy(configService); + expect(ApiKeyStrategy).toBeDefined(); + expect(ApiKeyStrategy).toBeInstanceOf(XApiKeyStrategy); + }); + }); }); From 0d3074e284b9b5714d5184db41f4df3cb6c73dbb Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 14 Nov 2023 23:14:19 +0100 Subject: [PATCH 19/96] remove test for api-setup-helper, remove unused imports --- .../helpers/admin-api-setup-helper.spec.ts | 160 ------------------ .../strategy/x-api-key.strategy.spec.ts | 9 +- 2 files changed, 2 insertions(+), 167 deletions(-) diff --git a/apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts b/apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts index e306a7cddd3..e69de29bb2d 100644 --- a/apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts +++ b/apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts @@ -1,160 +0,0 @@ -import { createMock } from '@golevelup/ts-jest'; -import { Configuration } from '@hpi-schul-cloud/commons'; -import { IConfig } from '@hpi-schul-cloud/commons/lib/interfaces/IConfig'; -import { - PrometheusMetricsConfig, - createAPIResponseTimeMetricMiddleware, - createPrometheusMetricsApp, -} from '@infra/metrics'; -import { Logger } from '@src/core/logger'; -import express, { Express, NextFunction, Request, RequestHandler, Response } from 'express'; -import { - PrometheusMetricsSetupState, - PrometheusMetricsSetupStateLoggable, - addPrometheusMetricsMiddlewaresIfEnabled, - createAndStartPrometheusMetricsAppIfEnabled, -} from './prometheus-metrics'; - -jest.mock('@infra/metrics', () => { - const moduleMock: unknown = { - ...jest.requireActual('@infra/metrics'), - createAPIResponseTimeMetricMiddleware: jest.fn(), - createPrometheusMetricsApp: jest.fn(), - }; - - return moduleMock; -}); - -const testLogger = createMock(); - -let configBefore: IConfig; - -beforeAll(() => { - configBefore = Configuration.toObject({ plainSecrets: true }); -}); - -beforeEach(() => { - Configuration.reset(configBefore); - - const middlewareMock: RequestHandler = (_req: Request, _res: Response, next: NextFunction) => { - next(); - }; - - (createAPIResponseTimeMetricMiddleware as jest.Mock).mockClear(); - (createAPIResponseTimeMetricMiddleware as jest.Mock).mockReturnValue(middlewareMock); - - const appMock = { listen: jest.fn() }; - - (createPrometheusMetricsApp as jest.Mock).mockClear(); - (createPrometheusMetricsApp as jest.Mock).mockReturnValue(appMock); -}); - -afterAll(() => { - Configuration.reset(configBefore); -}); - -describe('PrometheusMetricsSetupStateLoggable', () => { - describe('getLogMessage', () => { - describe('should return a log message with proper content', () => { - const expectedMessage = 'Setting up Prometheus metrics...'; - - it.each([ - [PrometheusMetricsSetupState.FEATURE_DISABLED_MIDDLEWARES_WILL_NOT_BE_CREATED], - [PrometheusMetricsSetupState.API_RESPONSE_TIME_METRIC_MIDDLEWARE_SUCCESSFULLY_ADDED], - [PrometheusMetricsSetupState.FEATURE_DISABLED_APP_WILL_NOT_BE_CREATED], - [PrometheusMetricsSetupState.COLLECTING_DEFAULT_METRICS_DISABLED], - [PrometheusMetricsSetupState.COLLECTING_METRICS_ROUTE_METRICS_DISABLED], - ])("for the '%s' state", (state: PrometheusMetricsSetupState) => { - const testLogMessage = new PrometheusMetricsSetupStateLoggable(state).getLogMessage(); - - expect(testLogMessage).toHaveProperty('message', expectedMessage); - expect(testLogMessage).toHaveProperty('data', { state }); - }); - }); - }); -}); - -describe('addPrometheusMetricsMiddlewaresIfEnabled', () => { - let testApp: Express; - let testAppUseSpy: jest.SpyInstance; - - beforeEach(() => { - testApp = express(); - testAppUseSpy = jest.spyOn(testApp, 'use'); - }); - - it('should create the API response time metric middleware and should add it to the given app', () => { - // To not create setters in the PrometheusMetricsConfig just for the unit tests - // purpose, we will enable the Prometheus metrics feature the way it should be - // enabled in a real app which is via the app configuration. - Configuration.set('FEATURE_PROMETHEUS_METRICS_ENABLED', true); - PrometheusMetricsConfig.reload(); - - addPrometheusMetricsMiddlewaresIfEnabled(testLogger, testApp); - - expect(createAPIResponseTimeMetricMiddleware).toBeCalled(); - expect(testAppUseSpy).toBeCalled(); - }); - - it('should not create the API response time metric middleware and should not add it to the given app', () => { - Configuration.set('FEATURE_PROMETHEUS_METRICS_ENABLED', false); - PrometheusMetricsConfig.reload(); - - addPrometheusMetricsMiddlewaresIfEnabled(testLogger, testApp); - - expect(createAPIResponseTimeMetricMiddleware).not.toBeCalled(); - expect(testAppUseSpy).not.toBeCalled(); - }); -}); - -describe('createAndStartAdminApiServer', () => { - describe('should create Prometheus metrics app and run it', () => { - const testPort = 9000; - const testLoggerSpy = jest.spyOn(testLogger, 'info'); - - let appMockListenFn: jest.Mock; - - beforeEach(() => { - testLoggerSpy.mockClear(); - appMockListenFn = jest.fn(); - - (createPrometheusMetricsApp as jest.Mock).mockClear(); - (createPrometheusMetricsApp as jest.Mock).mockReturnValue({ listen: appMockListenFn }); - }); - - it.each([ - [true, true], - [false, false], - [true, false], - [false, true], - ])( - "with collecting default metrics set to '%s' and collecting metrics route metrics set to '%s'", - (collectDefaultMetrics: boolean, collectMetricsRouteMetrics: boolean) => { - Configuration.set('FEATURE_PROMETHEUS_METRICS_ENABLED', true); - Configuration.set('PROMETHEUS_METRICS_PORT', testPort); - Configuration.set('PROMETHEUS_METRICS_COLLECT_DEFAULT_METRICS', collectDefaultMetrics); - Configuration.set('PROMETHEUS_METRICS_COLLECT_METRICS_ROUTE_METRICS', collectMetricsRouteMetrics); - PrometheusMetricsConfig.reload(); - - createAndStartPrometheusMetricsAppIfEnabled(testLogger); - - expect(createPrometheusMetricsApp).toBeCalledTimes(1); - expect(appMockListenFn).toHaveBeenLastCalledWith(testPort, expect.any(Function)); - - // Also test logging info message about running Prometheus metrics app. - // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access - appMockListenFn.mock.lastCall[1](); - expect(testLoggerSpy).toBeCalledTimes(1); - } - ); - }); - - it('should not create Prometheus metrics app if the whole feature is not enabled', () => { - Configuration.set('FEATURE_PROMETHEUS_METRICS_ENABLED', false); - PrometheusMetricsConfig.reload(); - - createAndStartPrometheusMetricsAppIfEnabled(testLogger); - - expect(createPrometheusMetricsApp).not.toBeCalled(); - }); -}); diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts index 9bc03ab3a18..7dd9f45f2f8 100644 --- a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts @@ -1,13 +1,8 @@ -/* eslint-disable @typescript-eslint/no-unused-vars */ -import { OAuthTokenDto } from '@modules/oauth'; import { UnauthorizedException } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { UserDO } from '@shared/domain/domainobject/user.do'; -import { userDoFactory } from '@shared/testing'; import { Configuration } from '@hpi-schul-cloud/commons/lib'; import { ConfigService } from '@nestjs/config'; import { createMock } from '@golevelup/ts-jest'; -import { SchoolInMigrationLoggableException } from '../loggable'; import { XApiKeyStrategy } from './x-api-key.strategy'; import { IXApiKeyConfig } from '../config/x-api-key.config'; @@ -53,7 +48,7 @@ describe('XApiKeyStrategy', () => { describe('when a valid api key is provided', () => { it('should do nothing', () => { const { CORRECT_API_KEY, done } = setup(); - const result = strategy.validate(CORRECT_API_KEY, done); + strategy.validate(CORRECT_API_KEY, done); expect(done).toBeCalledWith(null, true); }); }); @@ -61,7 +56,7 @@ describe('XApiKeyStrategy', () => { describe('when a invalid api key is provided', () => { it('should throw error', () => { const { INVALID_API_KEY, done } = setup(); - const result = strategy.validate(INVALID_API_KEY, done); + strategy.validate(INVALID_API_KEY, done); expect(done).toBeCalledWith(new UnauthorizedException(), null); }); }); From d935d13451f56d4e139a54804ec5f9df05610156 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 14 Nov 2023 23:25:08 +0100 Subject: [PATCH 20/96] remove test file --- apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts diff --git a/apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts b/apps/server/src/apps/helpers/admin-api-setup-helper.spec.ts deleted file mode 100644 index e69de29bb2d..00000000000 From 01340fb9ba4237c001b29f3c4362d13f46084951 Mon Sep 17 00:00:00 2001 From: Bartosz Nowicki <116367402+bn-pass@users.noreply.github.com> Date: Wed, 15 Nov 2023 01:33:07 +0100 Subject: [PATCH 21/96] add info for the Admin API port --- .../roles/schulcloud-server-core/templates/deployment.yml.j2 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ansible/roles/schulcloud-server-core/templates/deployment.yml.j2 b/ansible/roles/schulcloud-server-core/templates/deployment.yml.j2 index 0750bf91495..965408c0910 100644 --- a/ansible/roles/schulcloud-server-core/templates/deployment.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/deployment.yml.j2 @@ -35,6 +35,9 @@ spec: - containerPort: 3030 name: api protocol: TCP + - containerPort: 4030 + name: api-admin + protocol: TCP - containerPort: 9090 name: api-metrics protocol: TCP From c866efe3e45a75216e7ec8fc7e8ffa3f9d19578a Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 15 Nov 2023 08:26:03 +0100 Subject: [PATCH 22/96] add test for controller --- .../deletion-executon.controller.spec.ts | 49 ++++++++ .../deletion-requests.controller.spec.ts | 114 ++++++++++++++++++ ...eletion-request-body-props.builder.spec.ts | 22 ++++ .../deletion-request-body-props.builder.ts | 14 +++ 4 files changed, 199 insertions(+) create mode 100644 apps/server/src/modules/deletion/controller/deletion-executon.controller.spec.ts create mode 100644 apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts create mode 100644 apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts create mode 100644 apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts diff --git a/apps/server/src/modules/deletion/controller/deletion-executon.controller.spec.ts b/apps/server/src/modules/deletion/controller/deletion-executon.controller.spec.ts new file mode 100644 index 00000000000..d76758789fb --- /dev/null +++ b/apps/server/src/modules/deletion/controller/deletion-executon.controller.spec.ts @@ -0,0 +1,49 @@ +import { createMock } from '@golevelup/ts-jest'; +import { Test, TestingModule } from '@nestjs/testing'; +import { DeletionRequestUc } from '../uc'; + +import { DeletionExecutionsController } from './deletion-executions.controller'; + +describe(DeletionExecutionsController.name, () => { + let module: TestingModule; + let controller: DeletionExecutionsController; + let uc: DeletionRequestUc; + + beforeAll(async () => { + module = await Test.createTestingModule({ + providers: [ + { + provide: DeletionRequestUc, + useValue: createMock(), + }, + ], + controllers: [DeletionExecutionsController], + }).compile(); + + controller = module.get(DeletionExecutionsController); + uc = module.get(DeletionRequestUc); + }); + + afterAll(async () => { + await module.close(); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); + + describe('deletionRequestUc.executeDeletionRequests', () => { + describe('when executing deletionRequest', () => { + const setup = () => { + const limit = 10; + const ucSpy = jest.spyOn(uc, 'executeDeletionRequests').mockImplementation(); + return { limit, ucSpy }; + }; + it('should call deletionRequestUc.executeDeletionRequests with limit parameter', async () => { + const { limit, ucSpy } = setup(); + await controller.executeDeletions({ limit }); + expect(ucSpy).toHaveBeenCalledWith(limit); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts new file mode 100644 index 00000000000..51d2b2759fb --- /dev/null +++ b/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts @@ -0,0 +1,114 @@ +import { createMock } from '@golevelup/ts-jest'; +import { Test, TestingModule } from '@nestjs/testing'; +import { DeletionRequestUc } from '../uc'; +import { DeletionRequestsController } from './deletion-requests.controller'; +import { DeletionRequestBodyPropsBuilder } from '../uc/builder/deletion-request-body-props.builder'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; +import { DeletionRequestLogResponseBuilder } from '../uc/builder/deletion-request-log-response.builder'; +import { DeletionTargetRefBuilder } from '../uc/builder/deletion-target-ref.builder'; + +describe(DeletionRequestsController.name, () => { + let module: TestingModule; + let controller: DeletionRequestsController; + let uc: DeletionRequestUc; + + beforeAll(async () => { + module = await Test.createTestingModule({ + providers: [ + { + provide: DeletionRequestUc, + useValue: createMock(), + }, + ], + controllers: [DeletionRequestsController], + }).compile(); + + controller = module.get(DeletionRequestsController); + uc = module.get(DeletionRequestUc); + }); + + afterAll(async () => { + await module.close(); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); + + describe('createDeletionRequests', () => { + describe('when creating deletionRequest', () => { + const setup = () => { + const domain = DeletionDomainModel.PSEUDONYMS; + const refId = '653e4833cc39e5907a1e18d2'; + const deleteInMinutes = 1000; + const deletionRequestBody = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); + const deletionRequest = deletionRequestFactory.build(); + const ucResult = { + requestId: deletionRequest.id, + deletionPlannedAt: deletionRequest.deleteAfter, + }; + const ucSpy = jest.spyOn(uc, 'createDeletionRequest').mockImplementation(() => Promise.resolve(ucResult)); + return { deletionRequestBody, ucResult, ucSpy }; + }; + it('should call deletionRequestUc.createDeletionRequest with deletionRequestBody', async () => { + const { deletionRequestBody, ucSpy } = setup(); + await controller.createDeletionRequests(deletionRequestBody); + + expect(ucSpy).toHaveBeenCalledWith(deletionRequestBody); + }); + + it('should return deletionRequestResponse', async () => { + const { deletionRequestBody, ucResult } = setup(); + const result = await controller.createDeletionRequests(deletionRequestBody); + + expect(result).toEqual(ucResult); + }); + }); + }); + + describe('getPerformedDeletionDetail', () => { + describe('when finding deletionRequests', () => { + const setup = () => { + const domain = DeletionDomainModel.PSEUDONYMS; + const refId = '653e4833cc39e5907a1e18d2'; + const deletionRequest = deletionRequestFactory.build(); + const deletionTargetRef = DeletionTargetRefBuilder.build(domain, refId); + const ucResult = DeletionRequestLogResponseBuilder.build(deletionTargetRef, deletionRequest.deleteAfter); + const ucSpy = jest.spyOn(uc, 'findById').mockImplementation(() => Promise.resolve(ucResult)); + + return { deletionRequest, ucResult, ucSpy }; + }; + it('should call deletionRequestUc.findById with deletionRequestId', async () => { + const { deletionRequest, ucSpy } = setup(); + await controller.getPerformedDeletionDetails(deletionRequest.id); + + expect(ucSpy).toHaveBeenCalledWith(deletionRequest.id); + }); + + it('should return deletionRequestLogResponse', async () => { + const { deletionRequest, ucResult } = setup(); + const result = await controller.getPerformedDeletionDetails(deletionRequest.id); + + expect(result).toEqual(ucResult); + }); + }); + }); + + describe('cancelDeletionRequest', () => { + describe('when cancel deletionRequests', () => { + const setup = () => { + const deletionRequest = deletionRequestFactory.build(); + const ucSpy = jest.spyOn(uc, 'deleteDeletionRequestById').mockImplementation(); + + return { deletionRequest, ucSpy }; + }; + it('should call deletionRequestUc.deleteDeletionRequestById with deletionRequestId', async () => { + const { deletionRequest, ucSpy } = setup(); + await controller.cancelDeletionRequest(deletionRequest.id); + + expect(ucSpy).toHaveBeenCalledWith(deletionRequest.id); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts new file mode 100644 index 00000000000..62568468d69 --- /dev/null +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts @@ -0,0 +1,22 @@ +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionRequestBodyPropsBuilder } from './deletion-request-body-props.builder'; + +describe(DeletionRequestBodyPropsBuilder.name, () => { + afterAll(() => { + jest.clearAllMocks(); + }); + + it('should build generic deletionRequestBodyParams with all attributes', () => { + // Arrange + const domain = DeletionDomainModel.PSEUDONYMS; + const refId = '653e4833cc39e5907a1e18d2'; + const deleteInMinutes = 1000; + + const result = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); + + // Assert + expect(result.targetRef.domain).toEqual(domain); + expect(result.targetRef.id).toEqual(refId); + expect(result.deleteInMinutes).toEqual(deleteInMinutes); + }); +}); diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts new file mode 100644 index 00000000000..8d5e60bb19d --- /dev/null +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts @@ -0,0 +1,14 @@ +import { EntityId } from '@shared/domain'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionRequestBodyProps } from '../../controller/dto'; + +export class DeletionRequestBodyPropsBuilder { + static build(domain: DeletionDomainModel, id: EntityId, deleteInMinutes: number): DeletionRequestBodyProps { + const deletionRequest = { + targetRef: { domain, id }, + deleteInMinutes, + }; + + return deletionRequest; + } +} From adb77c97e434ace4843e8bee1838bd893915fa9d Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 15 Nov 2023 09:30:59 +0100 Subject: [PATCH 23/96] add test for deletionRequestResponse --- ...=> deletion-executions.controller.spec.ts} | 0 .../dto/deletion-request.response.spec.ts | 26 +++++++++++++++++++ 2 files changed, 26 insertions(+) rename apps/server/src/modules/deletion/controller/{deletion-executon.controller.spec.ts => deletion-executions.controller.spec.ts} (100%) create mode 100644 apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts diff --git a/apps/server/src/modules/deletion/controller/deletion-executon.controller.spec.ts b/apps/server/src/modules/deletion/controller/deletion-executions.controller.spec.ts similarity index 100% rename from apps/server/src/modules/deletion/controller/deletion-executon.controller.spec.ts rename to apps/server/src/modules/deletion/controller/deletion-executions.controller.spec.ts diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts new file mode 100644 index 00000000000..4356579cbdc --- /dev/null +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts @@ -0,0 +1,26 @@ +import { deletionRequestFactory } from '../../domain/testing/factory/deletion-request.factory'; +import { DeletionRequestResponse } from './deletion-request.response'; + +describe(DeletionRequestResponse.name, () => { + describe('constructor', () => { + describe('when passed properties', () => { + const setup = () => { + const deletionRequest = deletionRequestFactory.build(); + const deletionRequestResponse: DeletionRequestResponse = { + requestId: deletionRequest.id, + deletionPlannedAt: deletionRequest.deleteAfter, + }; + + return { deletionRequestResponse }; + }; + + it('should set the id', () => { + const { deletionRequestResponse } = setup(); + + const deletionRequest = new DeletionRequestResponse(deletionRequestResponse); + + expect(deletionRequest.requestId).toEqual(deletionRequestResponse.requestId); + }); + }); + }); +}); From 3f046f5dc572ea2abbc7522641967045c9a0611d Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 10:27:55 +0100 Subject: [PATCH 24/96] remove not needed parameter from constructor --- .../modules/authentication/strategy/x-api-key.strategy.ts | 7 +------ apps/server/src/modules/server/admin-api.server.module.ts | 3 --- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts index b3dff6c9533..85ac6474ea0 100644 --- a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts @@ -9,12 +9,7 @@ export class XApiKeyStrategy extends PassportStrategy(Strategy, 'api-key') { private readonly allowedApiKeys: string[]; constructor(private readonly configService: ConfigService) { - super( - { header: 'X-API-KEY', prefix: '' }, - true, - // eslint-disable-next-line @typescript-eslint/require-await - async (apiKey: string, done: (error: Error | null, data: boolean | null) => void) => this.validate(apiKey, done) - ); + super({ header: 'X-API-KEY', prefix: '' }, true); this.allowedApiKeys = this.configService.get('ADMIN_API__ALLOWED_API_KEYS'); } diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 94e7a77531f..e22d0cc604a 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -20,13 +20,10 @@ const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConf MikroOrmModule.forRoot({ ...defaultMikroOrmOptions, type: 'mongo', - // TODO add mongoose options as mongo options (see database.js) clientUrl: DB_URL, password: DB_PASSWORD, user: DB_USERNAME, entities: ALL_ENTITIES, - - // debug: true, // use it for locally debugging of queries }), LoggerModule, ], From 9a04a4a5321d8d40bc7563fcfec513a6b41ef19c Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 15 Nov 2023 11:53:27 +0100 Subject: [PATCH 25/96] add test for deletionRequestLogResponse --- .../dto/deletion-request-log.response.spec.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts new file mode 100644 index 00000000000..3c511ec7b7b --- /dev/null +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts @@ -0,0 +1,32 @@ +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionLogStatisticBuilder } from '../../uc/builder/deletion-log-statistic.builder'; +import { DeletionTargetRefBuilder } from '../../uc/builder/deletion-target-ref.builder'; +import { DeletionRequestLogResponse } from './deletion-request-log.response'; + +describe(DeletionRequestLogResponse.name, () => { + describe('constructor', () => { + describe('when passed properties', () => { + const setup = () => { + const targetRefDomain = DeletionDomainModel.PSEUDONYMS; + const targetRefId = '653e4833cc39e5907a1e18d2'; + const targetRef = DeletionTargetRefBuilder.build(targetRefDomain, targetRefId); + const deletionPlannedAt = new Date(); + const modifiedCount = 0; + const deletedCount = 2; + const statistics = [DeletionLogStatisticBuilder.build(targetRefDomain, modifiedCount, deletedCount)]; + + return { targetRef, deletionPlannedAt, statistics }; + }; + + it('should set the id', () => { + const { targetRef, deletionPlannedAt, statistics } = setup(); + + const deletionRequestLog = new DeletionRequestLogResponse({ targetRef, deletionPlannedAt, statistics }); + + expect(deletionRequestLog.targetRef).toEqual(targetRef); + expect(deletionRequestLog.deletionPlannedAt).toEqual(deletionPlannedAt); + expect(deletionRequestLog.statistics).toEqual(statistics); + }); + }); + }); +}); From 46c98b233b6daf40667279ddb228259c9f705b78 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 15 Nov 2023 12:48:26 +0100 Subject: [PATCH 26/96] add test for executionParams and requestbodyParams --- .../dto/deletion-execution.params.spec.ts | 38 +++++++++++++++++++ .../dto/deletion-request.body.params.spec.ts | 34 +++++++++++++++++ .../deletion-request-body-props.builder.ts | 2 +- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 apps/server/src/modules/deletion/controller/dto/deletion-execution.params.spec.ts create mode 100644 apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.spec.ts diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.spec.ts new file mode 100644 index 00000000000..c915276e97b --- /dev/null +++ b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.spec.ts @@ -0,0 +1,38 @@ +import { DeletionExecutionParams } from './deletion-execution.params'; + +describe(DeletionExecutionParams.name, () => { + describe('constructor', () => { + describe('when passed properties', () => { + let deletionExecutionParams: DeletionExecutionParams; + + beforeEach(() => { + deletionExecutionParams = new DeletionExecutionParams(); + }); + + it('should be defined', () => { + expect(deletionExecutionParams).toBeDefined(); + }); + + it('should have a limit property', () => { + expect(deletionExecutionParams.limit).toBeDefined(); + }); + + it('limit should be a number with a default value of 100', () => { + expect(deletionExecutionParams.limit).toBeDefined(); + expect(typeof deletionExecutionParams.limit).toBe('number'); + expect(deletionExecutionParams.limit).toBe(100); + }); + + it('limit should be optional', () => { + deletionExecutionParams.limit = undefined; + expect(deletionExecutionParams.limit).toBeUndefined(); + }); + + it('limit should be a number when provided', () => { + const customLimit = 50; + deletionExecutionParams.limit = customLimit; + expect(deletionExecutionParams.limit).toBe(customLimit); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.spec.ts new file mode 100644 index 00000000000..8aa63cc5e60 --- /dev/null +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.spec.ts @@ -0,0 +1,34 @@ +import { DeletionRequestBodyProps } from './deletion-request.body.params'; + +describe(DeletionRequestBodyProps.name, () => { + describe('constructor', () => { + describe('when passed properties', () => { + let deletionRequestBodyProps: DeletionRequestBodyProps; + + beforeEach(() => { + deletionRequestBodyProps = new DeletionRequestBodyProps(); + }); + + it('should be defined', () => { + expect(deletionRequestBodyProps).toBeDefined(); + }); + + it('deleteInMinutes should be a number with default value 43200', () => { + expect(deletionRequestBodyProps.deleteInMinutes).toBeDefined(); + expect(typeof deletionRequestBodyProps.deleteInMinutes).toBe('number'); + expect(deletionRequestBodyProps.deleteInMinutes).toBe(43200); + }); + + it('deleteInMinutes should be optional', () => { + deletionRequestBodyProps.deleteInMinutes = undefined; + expect(deletionRequestBodyProps.deleteInMinutes).toBeUndefined(); + }); + + it('deleteInMinutes should be a number when provided', () => { + const customDeleteInMinutes = 60; + deletionRequestBodyProps.deleteInMinutes = customDeleteInMinutes; + expect(deletionRequestBodyProps.deleteInMinutes).toBe(customDeleteInMinutes); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts index 8d5e60bb19d..77ade83b917 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts @@ -3,7 +3,7 @@ import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.en import { DeletionRequestBodyProps } from '../../controller/dto'; export class DeletionRequestBodyPropsBuilder { - static build(domain: DeletionDomainModel, id: EntityId, deleteInMinutes: number): DeletionRequestBodyProps { + static build(domain: DeletionDomainModel, id: EntityId, deleteInMinutes?: number): DeletionRequestBodyProps { const deletionRequest = { targetRef: { domain, id }, deleteInMinutes, From 539c173692f84650980eba1c8c01b6d988382d8f Mon Sep 17 00:00:00 2001 From: Bartosz Nowicki <116367402+bn-pass@users.noreply.github.com> Date: Wed, 15 Nov 2023 13:28:55 +0100 Subject: [PATCH 27/96] replace hard-coded Admin API server port value with the one taken from the var --- ansible/roles/schulcloud-server-core/templates/svc.yml.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 b/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 index 0ef85ec904d..445da44fde8 100644 --- a/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 @@ -12,7 +12,7 @@ spec: targetPort: 3030 protocol: TCP name: api - - port: 4030 + - port: {{ PORT_ADMIN_SERVER }} targetPort: 4030 protocol: TCP name: api-admin From c1f1f3546e3bebf8a8a08e7e08443c81c765a039 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 14:26:37 +0100 Subject: [PATCH 28/96] change creation of API method location --- .../apps/helpers/admin-api-setup-helper.ts | 29 ------------------- apps/server/src/apps/server.app.ts | 24 ++++++++++++++- 2 files changed, 23 insertions(+), 30 deletions(-) delete mode 100644 apps/server/src/apps/helpers/admin-api-setup-helper.ts diff --git a/apps/server/src/apps/helpers/admin-api-setup-helper.ts b/apps/server/src/apps/helpers/admin-api-setup-helper.ts deleted file mode 100644 index cdae853a125..00000000000 --- a/apps/server/src/apps/helpers/admin-api-setup-helper.ts +++ /dev/null @@ -1,29 +0,0 @@ -import express from 'express'; -import { Configuration } from '@hpi-schul-cloud/commons/lib'; -import { Logger } from '@src/core/logger'; -import { AdminApiServerModule } from '@src/modules/server/admin-api.server.module'; -import { ExpressAdapter } from '@nestjs/platform-express'; -import { NestFactory } from '@nestjs/core'; -import { AppStartLoggable } from './app-start-loggable'; - -export const createAndStartAdminApiServer = async (logger: Logger) => { - const nestAdminServerExpress = express(); - nestAdminServerExpress.disable('x-powered-by'); - const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); - const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); - - nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); - await nestAdminServerApp.init(); - - const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; - - nestAdminServerExpress.listen(adminApiServerPort, () => { - logger.info( - new AppStartLoggable({ - appName: 'Admin Api server app', - port: adminApiServerPort, - mountsDescription: `/admin/api/v1 --> Admin Api Server`, - }) - ); - }); -}; diff --git a/apps/server/src/apps/server.app.ts b/apps/server/src/apps/server.app.ts index 38babb24b68..890e42af6b0 100644 --- a/apps/server/src/apps/server.app.ts +++ b/apps/server/src/apps/server.app.ts @@ -22,6 +22,7 @@ import { join } from 'path'; import { install as sourceMapInstall } from 'source-map-support'; import { FeathersRosterService } from '@modules/pseudonym'; import { Configuration } from '@hpi-schul-cloud/commons/lib'; +import { AdminApiServerModule } from '@src/modules/server/admin-api.server.module'; import legacyAppPromise = require('../../../../src/app'); import { AppStartLoggable } from './helpers/app-start-loggable'; @@ -29,7 +30,28 @@ import { addPrometheusMetricsMiddlewaresIfEnabled, createAndStartPrometheusMetricsAppIfEnabled, } from './helpers/prometheus-metrics'; -import { createAndStartAdminApiServer } from './helpers/admin-api-setup-helper'; + +const createAndStartAdminApiServer = async (logger: Logger) => { + const nestAdminServerExpress = express(); + nestAdminServerExpress.disable('x-powered-by'); + const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); + const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); + + nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); + await nestAdminServerApp.init(); + + const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; + + nestAdminServerExpress.listen(adminApiServerPort, () => { + logger.info( + new AppStartLoggable({ + appName: 'Admin API server app', + port: adminApiServerPort, + mountsDescription: `/admin/api/v1 --> Admin API Server`, + }) + ); + }); +}; async function bootstrap() { sourceMapInstall(); From 9ad4206fa48f3cb0274885e7ee77d8d7faca1208 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 16:26:45 +0100 Subject: [PATCH 29/96] change default json --- .../strategy/x-api-key.strategy.spec.ts | 2 -- .../authentication/strategy/x-api-key.strategy.ts | 2 +- .../controller/deletion-requests.controller.ts | 1 + config/default.json | 4 ---- config/default.schema.json | 12 +++++++----- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts index 7dd9f45f2f8..836b4adc7e5 100644 --- a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts @@ -1,6 +1,5 @@ import { UnauthorizedException } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { Configuration } from '@hpi-schul-cloud/commons/lib'; import { ConfigService } from '@nestjs/config'; import { createMock } from '@golevelup/ts-jest'; import { XApiKeyStrategy } from './x-api-key.strategy'; @@ -10,7 +9,6 @@ describe('XApiKeyStrategy', () => { let module: TestingModule; let strategy: XApiKeyStrategy; let configService: ConfigService; - Configuration.set('ADMIN_API__ALLOWED_API_KEYS', '1ab2c3d4e5f61ab2c3d4e5f6'); beforeAll(async () => { module = await Test.createTestingModule({ diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts index 85ac6474ea0..125973de38f 100644 --- a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts @@ -9,7 +9,7 @@ export class XApiKeyStrategy extends PassportStrategy(Strategy, 'api-key') { private readonly allowedApiKeys: string[]; constructor(private readonly configService: ConfigService) { - super({ header: 'X-API-KEY', prefix: '' }, true); + super({ header: 'X-API-KEY' }, true); this.allowedApiKeys = this.configService.get('ADMIN_API__ALLOWED_API_KEYS'); } diff --git a/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts index 05fb3948841..159678f2042 100644 --- a/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts +++ b/apps/server/src/modules/deletion/controller/deletion-requests.controller.ts @@ -27,6 +27,7 @@ export class DeletionRequestsController { } @Get(':requestId') + @HttpCode(200) @ApiOperation({ summary: 'Retrieving details of performed or planned deletion', }) diff --git a/config/default.json b/config/default.json index c3f1ade0738..7dd1802a037 100644 --- a/config/default.json +++ b/config/default.json @@ -41,9 +41,5 @@ }, "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 - }, - "ADMIN_API": { - "ENABLED": true, - "ALLOWED_API_KEYS": "1ab2c3d4e5f61ab2c3d4e5f6" } } diff --git a/config/default.schema.json b/config/default.schema.json index 4a1373ad8d8..9a7c75445ee 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -177,7 +177,7 @@ }, "ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS": { "type": "string", - "default":"", + "default": "", "description": "Add custom domain to the list of blocked domains (comma separated list)." }, "FEATURE_TSP_AUTO_CONSENT_ENABLED": { @@ -1268,24 +1268,26 @@ "description": "Enables feature that allows the insecure LDAP URL (with ldap:// protocol)." }, "ADMIN_API": { - "type": "object", + "type": "object", "description": "Configuration of the schulcloud-server's admin API.", "properties": { "ENABLED": { "type": "boolean", - "default": true, "description": "Flag to turn on/off the Admin API." }, "PORT": { "type": "number", - "default": 4030, "description": "Port of the exposed Admin API server." }, "ALLOWED_API_KEYS": { "type": "string", - "default": "", "description": "Allowed Admin API keys (for accessing the Admin API)." } + }, + "default": { + "ENABLED": true, + "PORT": 4030, + "ALLOWED_API_KEYS": "" } }, "FEATURE_NEW_SCHOOL_ADMINISTRATION_PAGE_AS_DEFAULT_ENABLED": { From f422a7b03726463bb274e09ea2dc31e6baea1739 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 16:36:05 +0100 Subject: [PATCH 30/96] fix import in server module --- apps/server/src/modules/server/admin-api.server.module.ts | 2 +- apps/server/src/modules/server/controller/index.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 apps/server/src/modules/server/controller/index.ts diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index e22d0cc604a..91e6a334732 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -9,7 +9,7 @@ import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@src/infr import { DeletionModule } from '../deletion'; import { defaultMikroOrmOptions, setupSessions } from './server.module'; import { serverConfig } from './server.config'; -import { AdminApiServerController } from './controller/admin-api-server.controller'; +import { AdminApiServerController } from './controller'; const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), DeletionModule]; diff --git a/apps/server/src/modules/server/controller/index.ts b/apps/server/src/modules/server/controller/index.ts new file mode 100644 index 00000000000..73cc804558c --- /dev/null +++ b/apps/server/src/modules/server/controller/index.ts @@ -0,0 +1,2 @@ +export * from './admin-api-server.controller'; +export * from './server.controller'; From 3b58d132f77e9f05a5fafeeece1c7cc5535e80f7 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 15 Nov 2023 17:05:23 +0100 Subject: [PATCH 31/96] fixes soma issues --- .../deletion-request-create.api.spec.ts | 2 +- .../deletion-request-find.api.spec.ts | 4 ++-- .../deletion-requests.controller.spec.ts | 2 +- .../dto/deletion-request-log.response.spec.ts | 2 +- .../dto/deletion-request.response.spec.ts | 2 +- .../deletion/domain/testing/factory/index.ts | 2 ++ .../modules/deletion/domain/testing/index.ts | 1 + .../modules/deletion/domain/types/index.ts | 3 +++ .../entity/deletion-log.entity.spec.ts | 3 +-- .../deletion/entity/deletion-log.entity.ts | 3 +-- .../entity/deletion-request.entity.spec.ts | 3 +-- .../entity/deletion-request.entity.ts | 3 +-- .../src/modules/deletion/entity/index.ts | 1 + .../factory/deletion-log.entity.factory.ts | 3 +-- .../deletion-request.entity.factory.ts | 3 +-- .../deletion/entity/testing/factory/index.ts | 2 ++ .../modules/deletion/entity/testing/index.ts | 1 + .../deletion/repo/deletion-request-scope.ts | 2 +- .../repo/deletion-request.repo.spec.ts | 2 +- .../services/deletion-log.service.spec.ts | 3 +-- .../deletion/services/deletion-log.service.ts | 3 +-- .../services/deletion-request.service.spec.ts | 3 +-- .../services/deletion-request.service.ts | 3 +-- .../deletion-log-statistic.builder.spec.ts | 2 +- .../builder/deletion-log-statistic.builder.ts | 2 +- ...eletion-request-body-props.builder.spec.ts | 24 ++++++++++--------- .../deletion-request-body-props.builder.ts | 6 ++--- ...etion-request-log-response.builder.spec.ts | 2 +- .../deletion-target-ref.builder.spec.ts | 2 +- .../uc/builder/deletion-target-ref.builder.ts | 2 +- .../deletion/uc/deletion-request.uc.spec.ts | 3 +-- .../deletion/uc/deletion-request.uc.ts | 4 +--- .../deletion/uc/interface/interfaces.ts | 2 +- 33 files changed, 52 insertions(+), 53 deletions(-) create mode 100644 apps/server/src/modules/deletion/domain/testing/factory/index.ts create mode 100644 apps/server/src/modules/deletion/domain/testing/index.ts create mode 100644 apps/server/src/modules/deletion/domain/types/index.ts create mode 100644 apps/server/src/modules/deletion/entity/testing/factory/index.ts create mode 100644 apps/server/src/modules/deletion/entity/testing/index.ts diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index 432356fc83c..2f21c4ce85e 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -7,7 +7,7 @@ import { AuthGuard } from '@nestjs/passport'; import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; import { EntityManager } from '@mikro-orm/mongodb'; import { DeletionRequestBodyProps, DeletionRequestResponse } from '../dto'; -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionRequestEntity } from '../../entity'; const baseRouteName = '/deletionRequests'; diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts index e95e624f3d8..d2e4d089005 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts @@ -4,11 +4,11 @@ import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; -import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; import { EntityManager } from '@mikro-orm/mongodb'; import { cleanupCollections } from '@shared/testing'; +import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; import { DeletionRequestLogResponse } from '../dto'; -import { deletionRequestEntityFactory } from '../../entity/testing/factory/deletion-request.entity.factory'; +import { deletionRequestEntityFactory } from '../../entity/testing'; const baseRouteName = '/deletionRequests'; diff --git a/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts index 51d2b2759fb..8d525da1dbe 100644 --- a/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts +++ b/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts @@ -3,7 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { DeletionRequestUc } from '../uc'; import { DeletionRequestsController } from './deletion-requests.controller'; import { DeletionRequestBodyPropsBuilder } from '../uc/builder/deletion-request-body-props.builder'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../domain/types'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; import { DeletionRequestLogResponseBuilder } from '../uc/builder/deletion-request-log-response.builder'; import { DeletionTargetRefBuilder } from '../uc/builder/deletion-target-ref.builder'; diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts index 3c511ec7b7b..74bb08191ae 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionLogStatisticBuilder } from '../../uc/builder/deletion-log-statistic.builder'; import { DeletionTargetRefBuilder } from '../../uc/builder/deletion-target-ref.builder'; import { DeletionRequestLogResponse } from './deletion-request-log.response'; diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts index 4356579cbdc..d99dbf07c13 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts @@ -1,4 +1,4 @@ -import { deletionRequestFactory } from '../../domain/testing/factory/deletion-request.factory'; +import { deletionRequestFactory } from '../../domain/testing'; import { DeletionRequestResponse } from './deletion-request.response'; describe(DeletionRequestResponse.name, () => { diff --git a/apps/server/src/modules/deletion/domain/testing/factory/index.ts b/apps/server/src/modules/deletion/domain/testing/factory/index.ts new file mode 100644 index 00000000000..9bd93ea6254 --- /dev/null +++ b/apps/server/src/modules/deletion/domain/testing/factory/index.ts @@ -0,0 +1,2 @@ +export * from './deletion-log.factory'; +export * from './deletion-request.factory'; diff --git a/apps/server/src/modules/deletion/domain/testing/index.ts b/apps/server/src/modules/deletion/domain/testing/index.ts new file mode 100644 index 00000000000..d847d7abce6 --- /dev/null +++ b/apps/server/src/modules/deletion/domain/testing/index.ts @@ -0,0 +1 @@ +export * from './factory'; diff --git a/apps/server/src/modules/deletion/domain/types/index.ts b/apps/server/src/modules/deletion/domain/types/index.ts new file mode 100644 index 00000000000..d1f4de8eb6b --- /dev/null +++ b/apps/server/src/modules/deletion/domain/types/index.ts @@ -0,0 +1,3 @@ +export * from './deletion-domain-model.enum'; +export * from './deletion-operation-model.enum'; +export * from './deletion-status-model.enum'; diff --git a/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts b/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts index f77ee9e3013..5d4256632ce 100644 --- a/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts +++ b/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts @@ -1,8 +1,7 @@ import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLogEntity } from './deletion-log.entity'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; describe(DeletionLogEntity.name, () => { beforeAll(async () => { diff --git a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts index 27208833f08..38c13aaf78d 100644 --- a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts @@ -2,8 +2,7 @@ import { Entity, Index, Property } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain'; import { ObjectId } from 'bson'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; +import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; export interface DeletionLogEntityProps { id?: EntityId; diff --git a/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts b/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts index 6a0e416d580..c6d441669ae 100644 --- a/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts +++ b/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts @@ -1,8 +1,7 @@ import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionRequestEntity } from '@src/modules/deletion/entity/deletion-request.entity'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; describe(DeletionRequestEntity.name, () => { beforeAll(async () => { diff --git a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts index 315c249f809..ee2763ad5fe 100644 --- a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts @@ -1,8 +1,7 @@ import { Entity, Index, Property, Unique } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; export interface DeletionRequestEntityProps { id?: EntityId; diff --git a/apps/server/src/modules/deletion/entity/index.ts b/apps/server/src/modules/deletion/entity/index.ts index 7e3e31dcd19..9263946d98f 100644 --- a/apps/server/src/modules/deletion/entity/index.ts +++ b/apps/server/src/modules/deletion/entity/index.ts @@ -1,2 +1,3 @@ export * from './deletion-request.entity'; export * from './deletion-log.entity'; +export * from './testing/factory'; diff --git a/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts b/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts index 897fba6820a..93ed4198f15 100644 --- a/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts +++ b/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts @@ -1,8 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; import { DeletionLogEntity, DeletionLogEntityProps } from '../../deletion-log.entity'; -import { DeletionOperationModel } from '../../../domain/types/deletion-operation-model.enum'; -import { DeletionDomainModel } from '../../../domain/types/deletion-domain-model.enum'; +import { DeletionOperationModel, DeletionDomainModel } from '../../../domain/types'; export const deletionLogEntityFactory = BaseFactory.define( DeletionLogEntity, diff --git a/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts b/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts index 3ccba779e3e..cc65bb5f4dc 100644 --- a/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts +++ b/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts @@ -1,8 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; -import { DeletionStatusModel } from '../../../domain/types/deletion-status-model.enum'; +import { DeletionStatusModel, DeletionDomainModel } from '../../../domain/types'; import { DeletionRequestEntity, DeletionRequestEntityProps } from '../../deletion-request.entity'; -import { DeletionDomainModel } from '../../../domain/types/deletion-domain-model.enum'; export const deletionRequestEntityFactory = BaseFactory.define( DeletionRequestEntity, diff --git a/apps/server/src/modules/deletion/entity/testing/factory/index.ts b/apps/server/src/modules/deletion/entity/testing/factory/index.ts new file mode 100644 index 00000000000..7718554b849 --- /dev/null +++ b/apps/server/src/modules/deletion/entity/testing/factory/index.ts @@ -0,0 +1,2 @@ +export * from './deletion-request.entity.factory'; +export * from './deletion-log.entity.factory'; diff --git a/apps/server/src/modules/deletion/entity/testing/index.ts b/apps/server/src/modules/deletion/entity/testing/index.ts new file mode 100644 index 00000000000..d847d7abce6 --- /dev/null +++ b/apps/server/src/modules/deletion/entity/testing/index.ts @@ -0,0 +1 @@ +export * from './factory'; diff --git a/apps/server/src/modules/deletion/repo/deletion-request-scope.ts b/apps/server/src/modules/deletion/repo/deletion-request-scope.ts index 202bc09a887..6ac8351ce13 100644 --- a/apps/server/src/modules/deletion/repo/deletion-request-scope.ts +++ b/apps/server/src/modules/deletion/repo/deletion-request-scope.ts @@ -1,6 +1,6 @@ import { Scope } from '@shared/repo'; import { DeletionRequestEntity } from '../entity'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionStatusModel } from '../domain/types'; export class DeletionRequestScope extends Scope { byDeleteAfter(currentDate: Date): DeletionRequestScope { diff --git a/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts b/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts index c3018180218..4bd0c86c5cc 100644 --- a/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts +++ b/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts @@ -9,7 +9,7 @@ import { DeletionRequestEntity } from '../entity'; import { DeletionRequest } from '../domain/deletion-request.do'; import { deletionRequestEntityFactory } from '../entity/testing/factory/deletion-request.entity.factory'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionStatusModel } from '../domain/types'; describe(DeletionRequestRepo.name, () => { let module: TestingModule; diff --git a/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts b/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts index 449e7bda322..7b63e866b14 100644 --- a/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts +++ b/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts @@ -3,9 +3,8 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLogRepo } from '../repo'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; import { DeletionLogService } from './deletion-log.service'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory'; describe(DeletionLogService.name, () => { diff --git a/apps/server/src/modules/deletion/services/deletion-log.service.ts b/apps/server/src/modules/deletion/services/deletion-log.service.ts index 94f5d082530..1fce142decb 100644 --- a/apps/server/src/modules/deletion/services/deletion-log.service.ts +++ b/apps/server/src/modules/deletion/services/deletion-log.service.ts @@ -3,8 +3,7 @@ import { EntityId } from '@shared/domain'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLogRepo } from '../repo'; import { DeletionLog } from '../domain/deletion-log.do'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; +import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; @Injectable() export class DeletionLogService { diff --git a/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts b/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts index fcccfc433db..99763882064 100644 --- a/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts +++ b/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts @@ -5,8 +5,7 @@ import { setupEntities } from '@shared/testing'; import { DeletionRequestService } from './deletion-request.service'; import { DeletionRequestRepo } from '../repo'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; describe(DeletionRequestService.name, () => { let module: TestingModule; diff --git a/apps/server/src/modules/deletion/services/deletion-request.service.ts b/apps/server/src/modules/deletion/services/deletion-request.service.ts index 82b65521d68..206cb01a7a1 100644 --- a/apps/server/src/modules/deletion/services/deletion-request.service.ts +++ b/apps/server/src/modules/deletion/services/deletion-request.service.ts @@ -3,8 +3,7 @@ import { EntityId } from '@shared/domain'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionRequestRepo } from '../repo/deletion-request.repo'; import { DeletionRequest } from '../domain/deletion-request.do'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; @Injectable() export class DeletionRequestService { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts index c2952f40f59..ec2ff44da88 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionLogStatisticBuilder } from './deletion-log-statistic.builder'; describe(DeletionLogStatisticBuilder.name, () => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts index a562505b885..91ce13bc827 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionLogStatistic } from '../interface'; export class DeletionLogStatisticBuilder { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts index 62568468d69..ef11e71f812 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionRequestBodyPropsBuilder } from './deletion-request-body-props.builder'; describe(DeletionRequestBodyPropsBuilder.name, () => { @@ -6,17 +6,19 @@ describe(DeletionRequestBodyPropsBuilder.name, () => { jest.clearAllMocks(); }); - it('should build generic deletionRequestBodyParams with all attributes', () => { - // Arrange - const domain = DeletionDomainModel.PSEUDONYMS; - const refId = '653e4833cc39e5907a1e18d2'; - const deleteInMinutes = 1000; + describe('when creating deletionRequestBodyProps', () => { + it('should build deletionRequestBodyParams with all attributes', () => { + // Arrange + const domain = DeletionDomainModel.PSEUDONYMS; + const refId = '653e4833cc39e5907a1e18d2'; + const deleteInMinutes = 1000; - const result = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); + const result = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); - // Assert - expect(result.targetRef.domain).toEqual(domain); - expect(result.targetRef.id).toEqual(refId); - expect(result.deleteInMinutes).toEqual(deleteInMinutes); + // Assert + expect(result.targetRef.domain).toEqual(domain); + expect(result.targetRef.id).toEqual(refId); + expect(result.deleteInMinutes).toEqual(deleteInMinutes); + }); }); }); diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts index 77ade83b917..70227da5abc 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts @@ -1,14 +1,14 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionRequestBodyProps } from '../../controller/dto'; export class DeletionRequestBodyPropsBuilder { static build(domain: DeletionDomainModel, id: EntityId, deleteInMinutes?: number): DeletionRequestBodyProps { - const deletionRequest = { + const deletionRequestItem = { targetRef: { domain, id }, deleteInMinutes, }; - return deletionRequest; + return deletionRequestItem; } } diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts index 347c80baa7a..e2c5fd6e6e8 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionLogStatisticBuilder } from './deletion-log-statistic.builder'; import { DeletionRequestLogResponseBuilder } from './deletion-request-log-response.builder'; import { DeletionTargetRefBuilder } from './deletion-target-ref.builder'; diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts index 1c6e7782ce1..33fd7724706 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionTargetRefBuilder } from './deletion-target-ref.builder'; describe(DeletionTargetRefBuilder.name, () => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts index 3e827d6db7f..aa1420e8790 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts @@ -1,5 +1,5 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionTargetRef } from '../interface'; export class DeletionTargetRefBuilder { diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index 1cbe39f34a1..cd6692d980d 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -11,12 +11,11 @@ import { TeamService } from '@modules/teams'; import { UserService } from '@modules/user'; import { RocketChatService } from '@modules/rocketchat'; import { RocketChatUser, RocketChatUserService, rocketChatUserFactory } from '@modules/rocketchat-user'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequestService } from '../services'; import { DeletionRequestUc } from './deletion-request.uc'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory'; import { DeletionRequestBodyProps, DeletionRequestLogResponse } from '../controller/dto'; diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index 9bd7a3263fc..f17fed9b82c 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -11,11 +11,9 @@ import { AccountService } from '@modules/account/services'; import { RocketChatUserService } from '@modules/rocketchat-user'; import { RocketChatService } from '@modules/rocketchat'; import { DeletionRequestService } from '../services/deletion-request.service'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequest } from '../domain/deletion-request.do'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionOperationModel, DeletionStatusModel } from '../domain/types'; import { DeletionLog } from '../domain/deletion-log.do'; import { DeletionLogStatistic } from './interface/interfaces'; import { DeletionLogStatisticBuilder } from './builder/deletion-log-statistic.builder'; diff --git a/apps/server/src/modules/deletion/uc/interface/interfaces.ts b/apps/server/src/modules/deletion/uc/interface/interfaces.ts index 4ee470609ea..535711f4f65 100644 --- a/apps/server/src/modules/deletion/uc/interface/interfaces.ts +++ b/apps/server/src/modules/deletion/uc/interface/interfaces.ts @@ -1,5 +1,5 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; export interface DeletionTargetRef { domain: DeletionDomainModel; From 4fc4c70b0750f258d805b0d1a7d6f542d251a326 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 15 Nov 2023 17:27:45 +0100 Subject: [PATCH 32/96] fixes after review --- .../deletion/uc/deletion-request.uc.spec.ts | 67 ++++++++----------- .../modules/rocketchat-user/domain/index.ts | 1 + .../src/modules/rocketchat-user/index.ts | 1 - 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index cd6692d980d..e0fae44ce5b 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -17,7 +17,10 @@ import { DeletionRequestService } from '../services'; import { DeletionRequestUc } from './deletion-request.uc'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory'; -import { DeletionRequestBodyProps, DeletionRequestLogResponse } from '../controller/dto'; +import { DeletionRequestBodyProps } from '../controller/dto'; +import { DeletionRequestLogResponseBuilder } from './builder/deletion-request-log-response.builder'; +import { DeletionTargetRefBuilder } from './builder/deletion-target-ref.builder'; +import { DeletionLogStatisticBuilder } from './builder/deletion-log-statistic.builder'; describe(DeletionRequestUc.name, () => { let module: TestingModule; @@ -385,39 +388,28 @@ describe(DeletionRequestUc.name, () => { describe('when searching for logs for deletionRequest which was executed', () => { const setup = () => { const deletionRequestExecuted = deletionRequestFactory.build({ status: DeletionStatusModel.SUCCESS }); - const deletionLogExecuted1 = deletionLogFactory.build({ deletionRequestId: deletionRequestExecuted.id }); - const deletionLogExecuted2 = deletionLogFactory.build({ - deletionRequestId: deletionRequestExecuted.id, - domain: DeletionDomainModel.ACCOUNT, - modifiedCount: 0, - deletedCount: 1, - }); + const deletionLogExecuted = deletionLogFactory.build({ deletionRequestId: deletionRequestExecuted.id }); - const executedDeletionRequestSummary: DeletionRequestLogResponse = { - targetRef: { - domain: deletionRequestExecuted.targetRefDomain, - id: deletionRequestExecuted.targetRefId, - }, - deletionPlannedAt: deletionRequestExecuted.deleteAfter, - statistics: [ - { - domain: deletionLogExecuted1.domain, - modifiedCount: deletionLogExecuted1.modifiedCount, - deletedCount: deletionLogExecuted1.deletedCount, - }, - { - domain: deletionLogExecuted2.domain, - modifiedCount: deletionLogExecuted2.modifiedCount, - deletedCount: deletionLogExecuted2.deletedCount, - }, - ], - }; + const targetRef = DeletionTargetRefBuilder.build( + deletionRequestExecuted.targetRefDomain, + deletionRequestExecuted.targetRefId + ); + const statistics = DeletionLogStatisticBuilder.build( + deletionLogExecuted.domain, + deletionLogExecuted.modifiedCount, + deletionLogExecuted.deletedCount + ); + + const executedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build( + targetRef, + deletionRequestExecuted.deleteAfter, + [statistics] + ); return { deletionRequestExecuted, executedDeletionRequestSummary, - deletionLogExecuted1, - deletionLogExecuted2, + deletionLogExecuted, }; }; @@ -433,11 +425,10 @@ describe(DeletionRequestUc.name, () => { }); it('should return object with summary of deletionRequest', async () => { - const { deletionRequestExecuted, deletionLogExecuted1, deletionLogExecuted2, executedDeletionRequestSummary } = - setup(); + const { deletionRequestExecuted, deletionLogExecuted, executedDeletionRequestSummary } = setup(); deletionRequestService.findById.mockResolvedValueOnce(deletionRequestExecuted); - deletionLogService.findByDeletionRequestId.mockResolvedValueOnce([deletionLogExecuted1, deletionLogExecuted2]); + deletionLogService.findByDeletionRequestId.mockResolvedValueOnce([deletionLogExecuted]); const result = await uc.findById(deletionRequestExecuted.id); @@ -448,13 +439,11 @@ describe(DeletionRequestUc.name, () => { describe('when searching for logs for deletionRequest which was not executed', () => { const setup = () => { const deletionRequest = deletionRequestFactory.build(); - const notExecutedDeletionRequestSummary: DeletionRequestLogResponse = { - targetRef: { - domain: deletionRequest.targetRefDomain, - id: deletionRequest.targetRefId, - }, - deletionPlannedAt: deletionRequest.deleteAfter, - }; + const targetRef = DeletionTargetRefBuilder.build(deletionRequest.targetRefDomain, deletionRequest.targetRefId); + const notExecutedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build( + targetRef, + deletionRequest.deleteAfter + ); return { deletionRequest, diff --git a/apps/server/src/modules/rocketchat-user/domain/index.ts b/apps/server/src/modules/rocketchat-user/domain/index.ts index 0246dd0f0f9..441baa69c52 100644 --- a/apps/server/src/modules/rocketchat-user/domain/index.ts +++ b/apps/server/src/modules/rocketchat-user/domain/index.ts @@ -1 +1,2 @@ export * from './rocket-chat-user.do'; +export * from './testing'; diff --git a/apps/server/src/modules/rocketchat-user/index.ts b/apps/server/src/modules/rocketchat-user/index.ts index a190609dd5e..34ae0f25f87 100644 --- a/apps/server/src/modules/rocketchat-user/index.ts +++ b/apps/server/src/modules/rocketchat-user/index.ts @@ -1,4 +1,3 @@ export * from './rocketchat-user.module'; export * from './service'; export * from './domain'; -export * from './domain/testing/rocket-chat-user.factory'; From aba6e1aad058c834bf905ee874bc738e9b10858c Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 17:32:31 +0100 Subject: [PATCH 33/96] add admin api object --- config/default.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/default.json b/config/default.json index 7dd1802a037..3e21415f49d 100644 --- a/config/default.json +++ b/config/default.json @@ -41,5 +41,6 @@ }, "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 - } + }, + "ADMIN_API": {} } From 6764f956c22cb1ae92b6cbdfd813729c0a28243b Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 17:34:52 +0100 Subject: [PATCH 34/96] default json chnages --- config/default.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/default.json b/config/default.json index 3e21415f49d..f4ebccc4627 100644 --- a/config/default.json +++ b/config/default.json @@ -42,5 +42,7 @@ "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 }, - "ADMIN_API": {} + "ADMIN_API": { + "ENABLED": true + } } From 15ed0458fc3750de54bd032273dcfac8b41ddd74 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 17:53:29 +0100 Subject: [PATCH 35/96] Revert "default json chnages" This reverts commit 6764f956c22cb1ae92b6cbdfd813729c0a28243b. --- config/default.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/config/default.json b/config/default.json index f4ebccc4627..3e21415f49d 100644 --- a/config/default.json +++ b/config/default.json @@ -42,7 +42,5 @@ "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 }, - "ADMIN_API": { - "ENABLED": true - } + "ADMIN_API": {} } From 2a15fe3769312e4fecb455658fe361fb5484dfa1 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 17:54:16 +0100 Subject: [PATCH 36/96] Revert "add admin api object" This reverts commit aba6e1aad058c834bf905ee874bc738e9b10858c. --- config/default.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/default.json b/config/default.json index 3e21415f49d..7dd1802a037 100644 --- a/config/default.json +++ b/config/default.json @@ -41,6 +41,5 @@ }, "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 - }, - "ADMIN_API": {} + } } From 2e80ece64a4198962ac5cf4924e0ec632e3ecc8b Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 17:54:35 +0100 Subject: [PATCH 37/96] Revert "fixes after review" This reverts commit 4fc4c70b0750f258d805b0d1a7d6f542d251a326. --- .../deletion/uc/deletion-request.uc.spec.ts | 67 +++++++++++-------- .../modules/rocketchat-user/domain/index.ts | 1 - .../src/modules/rocketchat-user/index.ts | 1 + 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index e0fae44ce5b..cd6692d980d 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -17,10 +17,7 @@ import { DeletionRequestService } from '../services'; import { DeletionRequestUc } from './deletion-request.uc'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory'; -import { DeletionRequestBodyProps } from '../controller/dto'; -import { DeletionRequestLogResponseBuilder } from './builder/deletion-request-log-response.builder'; -import { DeletionTargetRefBuilder } from './builder/deletion-target-ref.builder'; -import { DeletionLogStatisticBuilder } from './builder/deletion-log-statistic.builder'; +import { DeletionRequestBodyProps, DeletionRequestLogResponse } from '../controller/dto'; describe(DeletionRequestUc.name, () => { let module: TestingModule; @@ -388,28 +385,39 @@ describe(DeletionRequestUc.name, () => { describe('when searching for logs for deletionRequest which was executed', () => { const setup = () => { const deletionRequestExecuted = deletionRequestFactory.build({ status: DeletionStatusModel.SUCCESS }); - const deletionLogExecuted = deletionLogFactory.build({ deletionRequestId: deletionRequestExecuted.id }); - - const targetRef = DeletionTargetRefBuilder.build( - deletionRequestExecuted.targetRefDomain, - deletionRequestExecuted.targetRefId - ); - const statistics = DeletionLogStatisticBuilder.build( - deletionLogExecuted.domain, - deletionLogExecuted.modifiedCount, - deletionLogExecuted.deletedCount - ); + const deletionLogExecuted1 = deletionLogFactory.build({ deletionRequestId: deletionRequestExecuted.id }); + const deletionLogExecuted2 = deletionLogFactory.build({ + deletionRequestId: deletionRequestExecuted.id, + domain: DeletionDomainModel.ACCOUNT, + modifiedCount: 0, + deletedCount: 1, + }); - const executedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build( - targetRef, - deletionRequestExecuted.deleteAfter, - [statistics] - ); + const executedDeletionRequestSummary: DeletionRequestLogResponse = { + targetRef: { + domain: deletionRequestExecuted.targetRefDomain, + id: deletionRequestExecuted.targetRefId, + }, + deletionPlannedAt: deletionRequestExecuted.deleteAfter, + statistics: [ + { + domain: deletionLogExecuted1.domain, + modifiedCount: deletionLogExecuted1.modifiedCount, + deletedCount: deletionLogExecuted1.deletedCount, + }, + { + domain: deletionLogExecuted2.domain, + modifiedCount: deletionLogExecuted2.modifiedCount, + deletedCount: deletionLogExecuted2.deletedCount, + }, + ], + }; return { deletionRequestExecuted, executedDeletionRequestSummary, - deletionLogExecuted, + deletionLogExecuted1, + deletionLogExecuted2, }; }; @@ -425,10 +433,11 @@ describe(DeletionRequestUc.name, () => { }); it('should return object with summary of deletionRequest', async () => { - const { deletionRequestExecuted, deletionLogExecuted, executedDeletionRequestSummary } = setup(); + const { deletionRequestExecuted, deletionLogExecuted1, deletionLogExecuted2, executedDeletionRequestSummary } = + setup(); deletionRequestService.findById.mockResolvedValueOnce(deletionRequestExecuted); - deletionLogService.findByDeletionRequestId.mockResolvedValueOnce([deletionLogExecuted]); + deletionLogService.findByDeletionRequestId.mockResolvedValueOnce([deletionLogExecuted1, deletionLogExecuted2]); const result = await uc.findById(deletionRequestExecuted.id); @@ -439,11 +448,13 @@ describe(DeletionRequestUc.name, () => { describe('when searching for logs for deletionRequest which was not executed', () => { const setup = () => { const deletionRequest = deletionRequestFactory.build(); - const targetRef = DeletionTargetRefBuilder.build(deletionRequest.targetRefDomain, deletionRequest.targetRefId); - const notExecutedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build( - targetRef, - deletionRequest.deleteAfter - ); + const notExecutedDeletionRequestSummary: DeletionRequestLogResponse = { + targetRef: { + domain: deletionRequest.targetRefDomain, + id: deletionRequest.targetRefId, + }, + deletionPlannedAt: deletionRequest.deleteAfter, + }; return { deletionRequest, diff --git a/apps/server/src/modules/rocketchat-user/domain/index.ts b/apps/server/src/modules/rocketchat-user/domain/index.ts index 441baa69c52..0246dd0f0f9 100644 --- a/apps/server/src/modules/rocketchat-user/domain/index.ts +++ b/apps/server/src/modules/rocketchat-user/domain/index.ts @@ -1,2 +1 @@ export * from './rocket-chat-user.do'; -export * from './testing'; diff --git a/apps/server/src/modules/rocketchat-user/index.ts b/apps/server/src/modules/rocketchat-user/index.ts index 34ae0f25f87..a190609dd5e 100644 --- a/apps/server/src/modules/rocketchat-user/index.ts +++ b/apps/server/src/modules/rocketchat-user/index.ts @@ -1,3 +1,4 @@ export * from './rocketchat-user.module'; export * from './service'; export * from './domain'; +export * from './domain/testing/rocket-chat-user.factory'; From c57995f2fa8624731d03af8a87779414989ac8f2 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 15 Nov 2023 17:56:02 +0100 Subject: [PATCH 38/96] Revert "fixes soma issues" This reverts commit 3b58d132f77e9f05a5fafeeece1c7cc5535e80f7. --- .../deletion-request-create.api.spec.ts | 2 +- .../deletion-request-find.api.spec.ts | 4 ++-- .../deletion-requests.controller.spec.ts | 2 +- .../dto/deletion-request-log.response.spec.ts | 2 +- .../dto/deletion-request.response.spec.ts | 2 +- .../deletion/domain/testing/factory/index.ts | 2 -- .../modules/deletion/domain/testing/index.ts | 1 - .../modules/deletion/domain/types/index.ts | 3 --- .../entity/deletion-log.entity.spec.ts | 3 ++- .../deletion/entity/deletion-log.entity.ts | 3 ++- .../entity/deletion-request.entity.spec.ts | 3 ++- .../entity/deletion-request.entity.ts | 3 ++- .../src/modules/deletion/entity/index.ts | 1 - .../factory/deletion-log.entity.factory.ts | 3 ++- .../deletion-request.entity.factory.ts | 3 ++- .../deletion/entity/testing/factory/index.ts | 2 -- .../modules/deletion/entity/testing/index.ts | 1 - .../deletion/repo/deletion-request-scope.ts | 2 +- .../repo/deletion-request.repo.spec.ts | 2 +- .../services/deletion-log.service.spec.ts | 3 ++- .../deletion/services/deletion-log.service.ts | 3 ++- .../services/deletion-request.service.spec.ts | 3 ++- .../services/deletion-request.service.ts | 3 ++- .../deletion-log-statistic.builder.spec.ts | 2 +- .../builder/deletion-log-statistic.builder.ts | 2 +- ...eletion-request-body-props.builder.spec.ts | 24 +++++++++---------- .../deletion-request-body-props.builder.ts | 6 ++--- ...etion-request-log-response.builder.spec.ts | 2 +- .../deletion-target-ref.builder.spec.ts | 2 +- .../uc/builder/deletion-target-ref.builder.ts | 2 +- .../deletion/uc/deletion-request.uc.spec.ts | 3 ++- .../deletion/uc/deletion-request.uc.ts | 4 +++- .../deletion/uc/interface/interfaces.ts | 2 +- 33 files changed, 53 insertions(+), 52 deletions(-) delete mode 100644 apps/server/src/modules/deletion/domain/testing/factory/index.ts delete mode 100644 apps/server/src/modules/deletion/domain/testing/index.ts delete mode 100644 apps/server/src/modules/deletion/domain/types/index.ts delete mode 100644 apps/server/src/modules/deletion/entity/testing/factory/index.ts delete mode 100644 apps/server/src/modules/deletion/entity/testing/index.ts diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index 2f21c4ce85e..432356fc83c 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -7,7 +7,7 @@ import { AuthGuard } from '@nestjs/passport'; import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; import { EntityManager } from '@mikro-orm/mongodb'; import { DeletionRequestBodyProps, DeletionRequestResponse } from '../dto'; -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionRequestEntity } from '../../entity'; const baseRouteName = '/deletionRequests'; diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts index d2e4d089005..e95e624f3d8 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts @@ -4,11 +4,11 @@ import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; +import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; import { EntityManager } from '@mikro-orm/mongodb'; import { cleanupCollections } from '@shared/testing'; -import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; import { DeletionRequestLogResponse } from '../dto'; -import { deletionRequestEntityFactory } from '../../entity/testing'; +import { deletionRequestEntityFactory } from '../../entity/testing/factory/deletion-request.entity.factory'; const baseRouteName = '/deletionRequests'; diff --git a/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts index 8d525da1dbe..51d2b2759fb 100644 --- a/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts +++ b/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts @@ -3,7 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { DeletionRequestUc } from '../uc'; import { DeletionRequestsController } from './deletion-requests.controller'; import { DeletionRequestBodyPropsBuilder } from '../uc/builder/deletion-request-body-props.builder'; -import { DeletionDomainModel } from '../domain/types'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; import { DeletionRequestLogResponseBuilder } from '../uc/builder/deletion-request-log-response.builder'; import { DeletionTargetRefBuilder } from '../uc/builder/deletion-target-ref.builder'; diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts index 74bb08191ae..3c511ec7b7b 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionLogStatisticBuilder } from '../../uc/builder/deletion-log-statistic.builder'; import { DeletionTargetRefBuilder } from '../../uc/builder/deletion-target-ref.builder'; import { DeletionRequestLogResponse } from './deletion-request-log.response'; diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts index d99dbf07c13..4356579cbdc 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts @@ -1,4 +1,4 @@ -import { deletionRequestFactory } from '../../domain/testing'; +import { deletionRequestFactory } from '../../domain/testing/factory/deletion-request.factory'; import { DeletionRequestResponse } from './deletion-request.response'; describe(DeletionRequestResponse.name, () => { diff --git a/apps/server/src/modules/deletion/domain/testing/factory/index.ts b/apps/server/src/modules/deletion/domain/testing/factory/index.ts deleted file mode 100644 index 9bd93ea6254..00000000000 --- a/apps/server/src/modules/deletion/domain/testing/factory/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './deletion-log.factory'; -export * from './deletion-request.factory'; diff --git a/apps/server/src/modules/deletion/domain/testing/index.ts b/apps/server/src/modules/deletion/domain/testing/index.ts deleted file mode 100644 index d847d7abce6..00000000000 --- a/apps/server/src/modules/deletion/domain/testing/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './factory'; diff --git a/apps/server/src/modules/deletion/domain/types/index.ts b/apps/server/src/modules/deletion/domain/types/index.ts deleted file mode 100644 index d1f4de8eb6b..00000000000 --- a/apps/server/src/modules/deletion/domain/types/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './deletion-domain-model.enum'; -export * from './deletion-operation-model.enum'; -export * from './deletion-status-model.enum'; diff --git a/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts b/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts index 5d4256632ce..f77ee9e3013 100644 --- a/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts +++ b/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts @@ -1,7 +1,8 @@ import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLogEntity } from './deletion-log.entity'; -import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; +import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; describe(DeletionLogEntity.name, () => { beforeAll(async () => { diff --git a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts index 38c13aaf78d..27208833f08 100644 --- a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts @@ -2,7 +2,8 @@ import { Entity, Index, Property } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain'; import { ObjectId } from 'bson'; -import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; export interface DeletionLogEntityProps { id?: EntityId; diff --git a/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts b/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts index c6d441669ae..6a0e416d580 100644 --- a/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts +++ b/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts @@ -1,7 +1,8 @@ import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionRequestEntity } from '@src/modules/deletion/entity/deletion-request.entity'; -import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; describe(DeletionRequestEntity.name, () => { beforeAll(async () => { diff --git a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts index ee2763ad5fe..315c249f809 100644 --- a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts @@ -1,7 +1,8 @@ import { Entity, Index, Property, Unique } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain'; -import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; export interface DeletionRequestEntityProps { id?: EntityId; diff --git a/apps/server/src/modules/deletion/entity/index.ts b/apps/server/src/modules/deletion/entity/index.ts index 9263946d98f..7e3e31dcd19 100644 --- a/apps/server/src/modules/deletion/entity/index.ts +++ b/apps/server/src/modules/deletion/entity/index.ts @@ -1,3 +1,2 @@ export * from './deletion-request.entity'; export * from './deletion-log.entity'; -export * from './testing/factory'; diff --git a/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts b/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts index 93ed4198f15..897fba6820a 100644 --- a/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts +++ b/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts @@ -1,7 +1,8 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; import { DeletionLogEntity, DeletionLogEntityProps } from '../../deletion-log.entity'; -import { DeletionOperationModel, DeletionDomainModel } from '../../../domain/types'; +import { DeletionOperationModel } from '../../../domain/types/deletion-operation-model.enum'; +import { DeletionDomainModel } from '../../../domain/types/deletion-domain-model.enum'; export const deletionLogEntityFactory = BaseFactory.define( DeletionLogEntity, diff --git a/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts b/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts index cc65bb5f4dc..3ccba779e3e 100644 --- a/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts +++ b/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts @@ -1,7 +1,8 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; -import { DeletionStatusModel, DeletionDomainModel } from '../../../domain/types'; +import { DeletionStatusModel } from '../../../domain/types/deletion-status-model.enum'; import { DeletionRequestEntity, DeletionRequestEntityProps } from '../../deletion-request.entity'; +import { DeletionDomainModel } from '../../../domain/types/deletion-domain-model.enum'; export const deletionRequestEntityFactory = BaseFactory.define( DeletionRequestEntity, diff --git a/apps/server/src/modules/deletion/entity/testing/factory/index.ts b/apps/server/src/modules/deletion/entity/testing/factory/index.ts deleted file mode 100644 index 7718554b849..00000000000 --- a/apps/server/src/modules/deletion/entity/testing/factory/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './deletion-request.entity.factory'; -export * from './deletion-log.entity.factory'; diff --git a/apps/server/src/modules/deletion/entity/testing/index.ts b/apps/server/src/modules/deletion/entity/testing/index.ts deleted file mode 100644 index d847d7abce6..00000000000 --- a/apps/server/src/modules/deletion/entity/testing/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './factory'; diff --git a/apps/server/src/modules/deletion/repo/deletion-request-scope.ts b/apps/server/src/modules/deletion/repo/deletion-request-scope.ts index 6ac8351ce13..202bc09a887 100644 --- a/apps/server/src/modules/deletion/repo/deletion-request-scope.ts +++ b/apps/server/src/modules/deletion/repo/deletion-request-scope.ts @@ -1,6 +1,6 @@ import { Scope } from '@shared/repo'; import { DeletionRequestEntity } from '../entity'; -import { DeletionStatusModel } from '../domain/types'; +import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; export class DeletionRequestScope extends Scope { byDeleteAfter(currentDate: Date): DeletionRequestScope { diff --git a/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts b/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts index 4bd0c86c5cc..c3018180218 100644 --- a/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts +++ b/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts @@ -9,7 +9,7 @@ import { DeletionRequestEntity } from '../entity'; import { DeletionRequest } from '../domain/deletion-request.do'; import { deletionRequestEntityFactory } from '../entity/testing/factory/deletion-request.entity.factory'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; -import { DeletionStatusModel } from '../domain/types'; +import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; describe(DeletionRequestRepo.name, () => { let module: TestingModule; diff --git a/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts b/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts index 7b63e866b14..449e7bda322 100644 --- a/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts +++ b/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts @@ -3,8 +3,9 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLogRepo } from '../repo'; -import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; import { DeletionLogService } from './deletion-log.service'; +import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory'; describe(DeletionLogService.name, () => { diff --git a/apps/server/src/modules/deletion/services/deletion-log.service.ts b/apps/server/src/modules/deletion/services/deletion-log.service.ts index 1fce142decb..94f5d082530 100644 --- a/apps/server/src/modules/deletion/services/deletion-log.service.ts +++ b/apps/server/src/modules/deletion/services/deletion-log.service.ts @@ -3,7 +3,8 @@ import { EntityId } from '@shared/domain'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLogRepo } from '../repo'; import { DeletionLog } from '../domain/deletion-log.do'; -import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; @Injectable() export class DeletionLogService { diff --git a/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts b/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts index 99763882064..fcccfc433db 100644 --- a/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts +++ b/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts @@ -5,7 +5,8 @@ import { setupEntities } from '@shared/testing'; import { DeletionRequestService } from './deletion-request.service'; import { DeletionRequestRepo } from '../repo'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; -import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; describe(DeletionRequestService.name, () => { let module: TestingModule; diff --git a/apps/server/src/modules/deletion/services/deletion-request.service.ts b/apps/server/src/modules/deletion/services/deletion-request.service.ts index 206cb01a7a1..82b65521d68 100644 --- a/apps/server/src/modules/deletion/services/deletion-request.service.ts +++ b/apps/server/src/modules/deletion/services/deletion-request.service.ts @@ -3,7 +3,8 @@ import { EntityId } from '@shared/domain'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionRequestRepo } from '../repo/deletion-request.repo'; import { DeletionRequest } from '../domain/deletion-request.do'; -import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; @Injectable() export class DeletionRequestService { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts index ec2ff44da88..c2952f40f59 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionLogStatisticBuilder } from './deletion-log-statistic.builder'; describe(DeletionLogStatisticBuilder.name, () => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts index 91ce13bc827..a562505b885 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionLogStatistic } from '../interface'; export class DeletionLogStatisticBuilder { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts index ef11e71f812..62568468d69 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionRequestBodyPropsBuilder } from './deletion-request-body-props.builder'; describe(DeletionRequestBodyPropsBuilder.name, () => { @@ -6,19 +6,17 @@ describe(DeletionRequestBodyPropsBuilder.name, () => { jest.clearAllMocks(); }); - describe('when creating deletionRequestBodyProps', () => { - it('should build deletionRequestBodyParams with all attributes', () => { - // Arrange - const domain = DeletionDomainModel.PSEUDONYMS; - const refId = '653e4833cc39e5907a1e18d2'; - const deleteInMinutes = 1000; + it('should build generic deletionRequestBodyParams with all attributes', () => { + // Arrange + const domain = DeletionDomainModel.PSEUDONYMS; + const refId = '653e4833cc39e5907a1e18d2'; + const deleteInMinutes = 1000; - const result = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); + const result = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); - // Assert - expect(result.targetRef.domain).toEqual(domain); - expect(result.targetRef.id).toEqual(refId); - expect(result.deleteInMinutes).toEqual(deleteInMinutes); - }); + // Assert + expect(result.targetRef.domain).toEqual(domain); + expect(result.targetRef.id).toEqual(refId); + expect(result.deleteInMinutes).toEqual(deleteInMinutes); }); }); diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts index 70227da5abc..77ade83b917 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts @@ -1,14 +1,14 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionRequestBodyProps } from '../../controller/dto'; export class DeletionRequestBodyPropsBuilder { static build(domain: DeletionDomainModel, id: EntityId, deleteInMinutes?: number): DeletionRequestBodyProps { - const deletionRequestItem = { + const deletionRequest = { targetRef: { domain, id }, deleteInMinutes, }; - return deletionRequestItem; + return deletionRequest; } } diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts index e2c5fd6e6e8..347c80baa7a 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionLogStatisticBuilder } from './deletion-log-statistic.builder'; import { DeletionRequestLogResponseBuilder } from './deletion-request-log-response.builder'; import { DeletionTargetRefBuilder } from './deletion-target-ref.builder'; diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts index 33fd7724706..1c6e7782ce1 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionTargetRefBuilder } from './deletion-target-ref.builder'; describe(DeletionTargetRefBuilder.name, () => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts index aa1420e8790..3e827d6db7f 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts @@ -1,5 +1,5 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; import { DeletionTargetRef } from '../interface'; export class DeletionTargetRefBuilder { diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index cd6692d980d..1cbe39f34a1 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -11,11 +11,12 @@ import { TeamService } from '@modules/teams'; import { UserService } from '@modules/user'; import { RocketChatService } from '@modules/rocketchat'; import { RocketChatUser, RocketChatUserService, rocketChatUserFactory } from '@modules/rocketchat-user'; -import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequestService } from '../services'; import { DeletionRequestUc } from './deletion-request.uc'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; +import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory'; import { DeletionRequestBodyProps, DeletionRequestLogResponse } from '../controller/dto'; diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index f17fed9b82c..9bd7a3263fc 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -11,9 +11,11 @@ import { AccountService } from '@modules/account/services'; import { RocketChatUserService } from '@modules/rocketchat-user'; import { RocketChatService } from '@modules/rocketchat'; import { DeletionRequestService } from '../services/deletion-request.service'; +import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequest } from '../domain/deletion-request.do'; -import { DeletionDomainModel, DeletionOperationModel, DeletionStatusModel } from '../domain/types'; +import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; +import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; import { DeletionLog } from '../domain/deletion-log.do'; import { DeletionLogStatistic } from './interface/interfaces'; import { DeletionLogStatisticBuilder } from './builder/deletion-log-statistic.builder'; diff --git a/apps/server/src/modules/deletion/uc/interface/interfaces.ts b/apps/server/src/modules/deletion/uc/interface/interfaces.ts index 535711f4f65..4ee470609ea 100644 --- a/apps/server/src/modules/deletion/uc/interface/interfaces.ts +++ b/apps/server/src/modules/deletion/uc/interface/interfaces.ts @@ -1,5 +1,5 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; export interface DeletionTargetRef { domain: DeletionDomainModel; From 011b0f787b70cab2362ec7e75cdebdf20bac1348 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Thu, 16 Nov 2023 06:52:40 +0100 Subject: [PATCH 39/96] fixes after review --- .../deletion-request-create.api.spec.ts | 2 +- .../deletion-request-find.api.spec.ts | 4 +- .../deletion-requests.controller.spec.ts | 12 ++-- .../dto/deletion-request-log.response.spec.ts | 7 +- .../dto/deletion-request.response.spec.ts | 2 +- .../deletion/domain/deletion-log.do.spec.ts | 3 +- .../deletion/domain/deletion-log.do.ts | 3 +- .../domain/deletion-request.do.spec.ts | 3 +- .../deletion/domain/deletion-request.do.ts | 3 +- .../testing/factory/deletion-log.factory.ts | 3 +- .../factory/deletion-request.factory.ts | 3 +- .../deletion/domain/testing/factory/index.ts | 2 + .../modules/deletion/domain/testing/index.ts | 1 + .../modules/deletion/domain/types/index.ts | 3 + .../entity/deletion-log.entity.spec.ts | 3 +- .../deletion/entity/deletion-log.entity.ts | 3 +- .../entity/deletion-request.entity.spec.ts | 3 +- .../entity/deletion-request.entity.ts | 3 +- .../factory/deletion-log.entity.factory.ts | 3 +- .../deletion-request.entity.factory.ts | 3 +- .../deletion/entity/testing/factory/index.ts | 2 + .../modules/deletion/entity/testing/index.ts | 1 + .../deletion/repo/deletion-request-scope.ts | 2 +- .../repo/deletion-request.repo.spec.ts | 2 +- .../services/deletion-log.service.spec.ts | 3 +- .../deletion/services/deletion-log.service.ts | 3 +- .../services/deletion-request.service.spec.ts | 3 +- .../services/deletion-request.service.ts | 3 +- .../deletion-log-statistic.builder.spec.ts | 4 +- .../builder/deletion-log-statistic.builder.ts | 2 +- ...eletion-request-body-props.builder.spec.ts | 25 +++---- .../deletion-request-body-props.builder.ts | 6 +- ...etion-request-log-response.builder.spec.ts | 6 +- .../deletion-target-ref.builder.spec.ts | 4 +- .../uc/builder/deletion-target-ref.builder.ts | 2 +- .../src/modules/deletion/uc/builder/index.ts | 4 ++ .../deletion/uc/deletion-request.uc.spec.ts | 70 ++++++++----------- .../deletion/uc/deletion-request.uc.ts | 8 +-- apps/server/src/modules/deletion/uc/index.ts | 2 + .../deletion/uc/interface/interfaces.ts | 2 +- .../modules/rocketchat-user/domain/index.ts | 1 + .../src/modules/rocketchat-user/index.ts | 1 - 42 files changed, 103 insertions(+), 122 deletions(-) create mode 100644 apps/server/src/modules/deletion/domain/testing/factory/index.ts create mode 100644 apps/server/src/modules/deletion/domain/testing/index.ts create mode 100644 apps/server/src/modules/deletion/domain/types/index.ts create mode 100644 apps/server/src/modules/deletion/entity/testing/factory/index.ts create mode 100644 apps/server/src/modules/deletion/entity/testing/index.ts create mode 100644 apps/server/src/modules/deletion/uc/builder/index.ts diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index 432356fc83c..2f21c4ce85e 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -7,7 +7,7 @@ import { AuthGuard } from '@nestjs/passport'; import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; import { EntityManager } from '@mikro-orm/mongodb'; import { DeletionRequestBodyProps, DeletionRequestResponse } from '../dto'; -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionRequestEntity } from '../../entity'; const baseRouteName = '/deletionRequests'; diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts index e95e624f3d8..d2e4d089005 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts @@ -4,11 +4,11 @@ import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; -import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; import { EntityManager } from '@mikro-orm/mongodb'; import { cleanupCollections } from '@shared/testing'; +import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; import { DeletionRequestLogResponse } from '../dto'; -import { deletionRequestEntityFactory } from '../../entity/testing/factory/deletion-request.entity.factory'; +import { deletionRequestEntityFactory } from '../../entity/testing'; const baseRouteName = '/deletionRequests'; diff --git a/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts index 51d2b2759fb..09e13275e93 100644 --- a/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts +++ b/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts @@ -2,11 +2,13 @@ import { createMock } from '@golevelup/ts-jest'; import { Test, TestingModule } from '@nestjs/testing'; import { DeletionRequestUc } from '../uc'; import { DeletionRequestsController } from './deletion-requests.controller'; -import { DeletionRequestBodyPropsBuilder } from '../uc/builder/deletion-request-body-props.builder'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; -import { DeletionRequestLogResponseBuilder } from '../uc/builder/deletion-request-log-response.builder'; -import { DeletionTargetRefBuilder } from '../uc/builder/deletion-target-ref.builder'; +import { + DeletionRequestBodyPropsBuilder, + DeletionRequestLogResponseBuilder, + DeletionTargetRefBuilder, +} from '../uc/builder'; +import { DeletionDomainModel } from '../domain/types'; +import { deletionRequestFactory } from '../domain/testing'; describe(DeletionRequestsController.name, () => { let module: TestingModule; diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts index 3c511ec7b7b..19591dda92a 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts @@ -1,7 +1,6 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; -import { DeletionLogStatisticBuilder } from '../../uc/builder/deletion-log-statistic.builder'; -import { DeletionTargetRefBuilder } from '../../uc/builder/deletion-target-ref.builder'; -import { DeletionRequestLogResponse } from './deletion-request-log.response'; +import { DeletionDomainModel } from '../../domain/types'; +import { DeletionLogStatisticBuilder, DeletionTargetRefBuilder } from '../../uc/builder'; +import { DeletionRequestLogResponse } from './index'; describe(DeletionRequestLogResponse.name, () => { describe('constructor', () => { diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts index 4356579cbdc..d99dbf07c13 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.response.spec.ts @@ -1,4 +1,4 @@ -import { deletionRequestFactory } from '../../domain/testing/factory/deletion-request.factory'; +import { deletionRequestFactory } from '../../domain/testing'; import { DeletionRequestResponse } from './deletion-request.response'; describe(DeletionRequestResponse.name, () => { diff --git a/apps/server/src/modules/deletion/domain/deletion-log.do.spec.ts b/apps/server/src/modules/deletion/domain/deletion-log.do.spec.ts index b0eab14470a..fd320ff79cb 100644 --- a/apps/server/src/modules/deletion/domain/deletion-log.do.spec.ts +++ b/apps/server/src/modules/deletion/domain/deletion-log.do.spec.ts @@ -1,8 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { deletionLogFactory } from './testing/factory/deletion-log.factory'; import { DeletionLog } from './deletion-log.do'; -import { DeletionOperationModel } from './types/deletion-operation-model.enum'; -import { DeletionDomainModel } from './types/deletion-domain-model.enum'; +import { DeletionOperationModel, DeletionDomainModel } from './types'; describe(DeletionLog.name, () => { describe('constructor', () => { diff --git a/apps/server/src/modules/deletion/domain/deletion-log.do.ts b/apps/server/src/modules/deletion/domain/deletion-log.do.ts index 11f81efa817..c5ca2b652d1 100644 --- a/apps/server/src/modules/deletion/domain/deletion-log.do.ts +++ b/apps/server/src/modules/deletion/domain/deletion-log.do.ts @@ -1,7 +1,6 @@ import { EntityId } from '@shared/domain/types'; import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object'; -import { DeletionDomainModel } from './types/deletion-domain-model.enum'; -import { DeletionOperationModel } from './types/deletion-operation-model.enum'; +import { DeletionDomainModel, DeletionOperationModel } from './types'; export interface DeletionLogProps extends AuthorizableObject { createdAt?: Date; diff --git a/apps/server/src/modules/deletion/domain/deletion-request.do.spec.ts b/apps/server/src/modules/deletion/domain/deletion-request.do.spec.ts index 3c0eb608c87..1ffb7d3f906 100644 --- a/apps/server/src/modules/deletion/domain/deletion-request.do.spec.ts +++ b/apps/server/src/modules/deletion/domain/deletion-request.do.spec.ts @@ -1,8 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionRequest } from './deletion-request.do'; -import { DeletionDomainModel } from './types/deletion-domain-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from './types'; import { deletionRequestFactory } from './testing/factory/deletion-request.factory'; -import { DeletionStatusModel } from './types/deletion-status-model.enum'; describe(DeletionRequest.name, () => { describe('constructor', () => { diff --git a/apps/server/src/modules/deletion/domain/deletion-request.do.ts b/apps/server/src/modules/deletion/domain/deletion-request.do.ts index e1a8b289ef0..76b7f0371ba 100644 --- a/apps/server/src/modules/deletion/domain/deletion-request.do.ts +++ b/apps/server/src/modules/deletion/domain/deletion-request.do.ts @@ -1,7 +1,6 @@ import { EntityId } from '@shared/domain/types'; import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object'; -import { DeletionDomainModel } from './types/deletion-domain-model.enum'; -import { DeletionStatusModel } from './types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from './types'; export interface DeletionRequestProps extends AuthorizableObject { createdAt?: Date; diff --git a/apps/server/src/modules/deletion/domain/testing/factory/deletion-log.factory.ts b/apps/server/src/modules/deletion/domain/testing/factory/deletion-log.factory.ts index fbeeba78730..2a3d0529866 100644 --- a/apps/server/src/modules/deletion/domain/testing/factory/deletion-log.factory.ts +++ b/apps/server/src/modules/deletion/domain/testing/factory/deletion-log.factory.ts @@ -1,8 +1,7 @@ import { DoBaseFactory } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLog, DeletionLogProps } from '../../deletion-log.do'; -import { DeletionOperationModel } from '../../types/deletion-operation-model.enum'; -import { DeletionDomainModel } from '../../types/deletion-domain-model.enum'; +import { DeletionOperationModel, DeletionDomainModel } from '../../types'; export const deletionLogFactory = DoBaseFactory.define(DeletionLog, () => { return { diff --git a/apps/server/src/modules/deletion/domain/testing/factory/deletion-request.factory.ts b/apps/server/src/modules/deletion/domain/testing/factory/deletion-request.factory.ts index 9f87bbc1cbf..cf1f64daaec 100644 --- a/apps/server/src/modules/deletion/domain/testing/factory/deletion-request.factory.ts +++ b/apps/server/src/modules/deletion/domain/testing/factory/deletion-request.factory.ts @@ -2,8 +2,7 @@ import { DoBaseFactory } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeepPartial } from 'fishery'; import { DeletionRequest, DeletionRequestProps } from '../../deletion-request.do'; -import { DeletionDomainModel } from '../../types/deletion-domain-model.enum'; -import { DeletionStatusModel } from '../../types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../../types'; class DeletionRequestFactory extends DoBaseFactory { withUserIds(id: string): this { diff --git a/apps/server/src/modules/deletion/domain/testing/factory/index.ts b/apps/server/src/modules/deletion/domain/testing/factory/index.ts new file mode 100644 index 00000000000..9bd93ea6254 --- /dev/null +++ b/apps/server/src/modules/deletion/domain/testing/factory/index.ts @@ -0,0 +1,2 @@ +export * from './deletion-log.factory'; +export * from './deletion-request.factory'; diff --git a/apps/server/src/modules/deletion/domain/testing/index.ts b/apps/server/src/modules/deletion/domain/testing/index.ts new file mode 100644 index 00000000000..d847d7abce6 --- /dev/null +++ b/apps/server/src/modules/deletion/domain/testing/index.ts @@ -0,0 +1 @@ +export * from './factory'; diff --git a/apps/server/src/modules/deletion/domain/types/index.ts b/apps/server/src/modules/deletion/domain/types/index.ts new file mode 100644 index 00000000000..d1f4de8eb6b --- /dev/null +++ b/apps/server/src/modules/deletion/domain/types/index.ts @@ -0,0 +1,3 @@ +export * from './deletion-domain-model.enum'; +export * from './deletion-operation-model.enum'; +export * from './deletion-status-model.enum'; diff --git a/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts b/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts index f77ee9e3013..c1b5f5f7184 100644 --- a/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts +++ b/apps/server/src/modules/deletion/entity/deletion-log.entity.spec.ts @@ -1,8 +1,7 @@ import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLogEntity } from './deletion-log.entity'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionOperationModel, DeletionDomainModel } from '../domain/types'; describe(DeletionLogEntity.name, () => { beforeAll(async () => { diff --git a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts index 27208833f08..38c13aaf78d 100644 --- a/apps/server/src/modules/deletion/entity/deletion-log.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-log.entity.ts @@ -2,8 +2,7 @@ import { Entity, Index, Property } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain'; import { ObjectId } from 'bson'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; +import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; export interface DeletionLogEntityProps { id?: EntityId; diff --git a/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts b/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts index 6a0e416d580..c6d441669ae 100644 --- a/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts +++ b/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts @@ -1,8 +1,7 @@ import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionRequestEntity } from '@src/modules/deletion/entity/deletion-request.entity'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; describe(DeletionRequestEntity.name, () => { beforeAll(async () => { diff --git a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts index 315c249f809..ee2763ad5fe 100644 --- a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts @@ -1,8 +1,7 @@ import { Entity, Index, Property, Unique } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; export interface DeletionRequestEntityProps { id?: EntityId; diff --git a/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts b/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts index 897fba6820a..93ed4198f15 100644 --- a/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts +++ b/apps/server/src/modules/deletion/entity/testing/factory/deletion-log.entity.factory.ts @@ -1,8 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; import { DeletionLogEntity, DeletionLogEntityProps } from '../../deletion-log.entity'; -import { DeletionOperationModel } from '../../../domain/types/deletion-operation-model.enum'; -import { DeletionDomainModel } from '../../../domain/types/deletion-domain-model.enum'; +import { DeletionOperationModel, DeletionDomainModel } from '../../../domain/types'; export const deletionLogEntityFactory = BaseFactory.define( DeletionLogEntity, diff --git a/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts b/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts index 3ccba779e3e..cc65bb5f4dc 100644 --- a/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts +++ b/apps/server/src/modules/deletion/entity/testing/factory/deletion-request.entity.factory.ts @@ -1,8 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; -import { DeletionStatusModel } from '../../../domain/types/deletion-status-model.enum'; +import { DeletionStatusModel, DeletionDomainModel } from '../../../domain/types'; import { DeletionRequestEntity, DeletionRequestEntityProps } from '../../deletion-request.entity'; -import { DeletionDomainModel } from '../../../domain/types/deletion-domain-model.enum'; export const deletionRequestEntityFactory = BaseFactory.define( DeletionRequestEntity, diff --git a/apps/server/src/modules/deletion/entity/testing/factory/index.ts b/apps/server/src/modules/deletion/entity/testing/factory/index.ts new file mode 100644 index 00000000000..6d9e8115468 --- /dev/null +++ b/apps/server/src/modules/deletion/entity/testing/factory/index.ts @@ -0,0 +1,2 @@ +export * from './deletion-log.entity.factory'; +export * from './deletion-request.entity.factory'; diff --git a/apps/server/src/modules/deletion/entity/testing/index.ts b/apps/server/src/modules/deletion/entity/testing/index.ts new file mode 100644 index 00000000000..d847d7abce6 --- /dev/null +++ b/apps/server/src/modules/deletion/entity/testing/index.ts @@ -0,0 +1 @@ +export * from './factory'; diff --git a/apps/server/src/modules/deletion/repo/deletion-request-scope.ts b/apps/server/src/modules/deletion/repo/deletion-request-scope.ts index 202bc09a887..6ac8351ce13 100644 --- a/apps/server/src/modules/deletion/repo/deletion-request-scope.ts +++ b/apps/server/src/modules/deletion/repo/deletion-request-scope.ts @@ -1,6 +1,6 @@ import { Scope } from '@shared/repo'; import { DeletionRequestEntity } from '../entity'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionStatusModel } from '../domain/types'; export class DeletionRequestScope extends Scope { byDeleteAfter(currentDate: Date): DeletionRequestScope { diff --git a/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts b/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts index c3018180218..4bd0c86c5cc 100644 --- a/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts +++ b/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts @@ -9,7 +9,7 @@ import { DeletionRequestEntity } from '../entity'; import { DeletionRequest } from '../domain/deletion-request.do'; import { deletionRequestEntityFactory } from '../entity/testing/factory/deletion-request.entity.factory'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionStatusModel } from '../domain/types'; describe(DeletionRequestRepo.name, () => { let module: TestingModule; diff --git a/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts b/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts index 449e7bda322..7b63e866b14 100644 --- a/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts +++ b/apps/server/src/modules/deletion/services/deletion-log.service.spec.ts @@ -3,9 +3,8 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLogRepo } from '../repo'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; import { DeletionLogService } from './deletion-log.service'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory'; describe(DeletionLogService.name, () => { diff --git a/apps/server/src/modules/deletion/services/deletion-log.service.ts b/apps/server/src/modules/deletion/services/deletion-log.service.ts index 94f5d082530..1fce142decb 100644 --- a/apps/server/src/modules/deletion/services/deletion-log.service.ts +++ b/apps/server/src/modules/deletion/services/deletion-log.service.ts @@ -3,8 +3,7 @@ import { EntityId } from '@shared/domain'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionLogRepo } from '../repo'; import { DeletionLog } from '../domain/deletion-log.do'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; +import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; @Injectable() export class DeletionLogService { diff --git a/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts b/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts index fcccfc433db..99763882064 100644 --- a/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts +++ b/apps/server/src/modules/deletion/services/deletion-request.service.spec.ts @@ -5,8 +5,7 @@ import { setupEntities } from '@shared/testing'; import { DeletionRequestService } from './deletion-request.service'; import { DeletionRequestRepo } from '../repo'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; describe(DeletionRequestService.name, () => { let module: TestingModule; diff --git a/apps/server/src/modules/deletion/services/deletion-request.service.ts b/apps/server/src/modules/deletion/services/deletion-request.service.ts index 82b65521d68..206cb01a7a1 100644 --- a/apps/server/src/modules/deletion/services/deletion-request.service.ts +++ b/apps/server/src/modules/deletion/services/deletion-request.service.ts @@ -3,8 +3,7 @@ import { EntityId } from '@shared/domain'; import { ObjectId } from '@mikro-orm/mongodb'; import { DeletionRequestRepo } from '../repo/deletion-request.repo'; import { DeletionRequest } from '../domain/deletion-request.do'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; @Injectable() export class DeletionRequestService { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts index c2952f40f59..b92be19216a 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts @@ -1,5 +1,5 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; -import { DeletionLogStatisticBuilder } from './deletion-log-statistic.builder'; +import { DeletionDomainModel } from '../../domain/types'; +import { DeletionLogStatisticBuilder } from './index'; describe(DeletionLogStatisticBuilder.name, () => { afterAll(() => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts index a562505b885..91ce13bc827 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionLogStatistic } from '../interface'; export class DeletionLogStatisticBuilder { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts index 62568468d69..dcc2aa057e6 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts @@ -1,22 +1,23 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionRequestBodyPropsBuilder } from './deletion-request-body-props.builder'; describe(DeletionRequestBodyPropsBuilder.name, () => { afterAll(() => { jest.clearAllMocks(); }); + describe('when create deletionRequestBodyParams', () => { + it('should build deletionRequestBodyParams with all attributes', () => { + // Arrange + const domain = DeletionDomainModel.PSEUDONYMS; + const refId = '653e4833cc39e5907a1e18d2'; + const deleteInMinutes = 1000; - it('should build generic deletionRequestBodyParams with all attributes', () => { - // Arrange - const domain = DeletionDomainModel.PSEUDONYMS; - const refId = '653e4833cc39e5907a1e18d2'; - const deleteInMinutes = 1000; + const result = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); - const result = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); - - // Assert - expect(result.targetRef.domain).toEqual(domain); - expect(result.targetRef.id).toEqual(refId); - expect(result.deleteInMinutes).toEqual(deleteInMinutes); + // Assert + expect(result.targetRef.domain).toEqual(domain); + expect(result.targetRef.id).toEqual(refId); + expect(result.deleteInMinutes).toEqual(deleteInMinutes); + }); }); }); diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts index 77ade83b917..70227da5abc 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts @@ -1,14 +1,14 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionRequestBodyProps } from '../../controller/dto'; export class DeletionRequestBodyPropsBuilder { static build(domain: DeletionDomainModel, id: EntityId, deleteInMinutes?: number): DeletionRequestBodyProps { - const deletionRequest = { + const deletionRequestItem = { targetRef: { domain, id }, deleteInMinutes, }; - return deletionRequest; + return deletionRequestItem; } } diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts index 347c80baa7a..86f9991c881 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts @@ -1,7 +1,5 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; -import { DeletionLogStatisticBuilder } from './deletion-log-statistic.builder'; -import { DeletionRequestLogResponseBuilder } from './deletion-request-log-response.builder'; -import { DeletionTargetRefBuilder } from './deletion-target-ref.builder'; +import { DeletionDomainModel } from '../../domain/types'; +import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from './index'; describe(DeletionRequestLogResponseBuilder.name, () => { afterAll(() => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts index 1c6e7782ce1..ed72b67f904 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts @@ -1,5 +1,5 @@ -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; -import { DeletionTargetRefBuilder } from './deletion-target-ref.builder'; +import { DeletionDomainModel } from '../../domain/types'; +import { DeletionTargetRefBuilder } from './index'; describe(DeletionTargetRefBuilder.name, () => { afterAll(() => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts index 3e827d6db7f..aa1420e8790 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts @@ -1,5 +1,5 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; import { DeletionTargetRef } from '../interface'; export class DeletionTargetRefBuilder { diff --git a/apps/server/src/modules/deletion/uc/builder/index.ts b/apps/server/src/modules/deletion/uc/builder/index.ts new file mode 100644 index 00000000000..fb8a2a5e7ec --- /dev/null +++ b/apps/server/src/modules/deletion/uc/builder/index.ts @@ -0,0 +1,4 @@ +export * from './deletion-log-statistic.builder'; +export * from './deletion-request-body-props.builder'; +export * from './deletion-request-log-response.builder'; +export * from './deletion-target-ref.builder'; diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index 1cbe39f34a1..5bb38137e38 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -11,14 +11,14 @@ import { TeamService } from '@modules/teams'; import { UserService } from '@modules/user'; import { RocketChatService } from '@modules/rocketchat'; import { RocketChatUser, RocketChatUserService, rocketChatUserFactory } from '@modules/rocketchat-user'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequestService } from '../services'; import { DeletionRequestUc } from './deletion-request.uc'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; -import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory'; -import { DeletionRequestBodyProps, DeletionRequestLogResponse } from '../controller/dto'; +import { deletionLogFactory } from '../domain/testing'; +import { DeletionRequestBodyProps } from '../controller/dto'; +import { DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder, DeletionLogStatisticBuilder } from './builder'; describe(DeletionRequestUc.name, () => { let module: TestingModule; @@ -386,39 +386,28 @@ describe(DeletionRequestUc.name, () => { describe('when searching for logs for deletionRequest which was executed', () => { const setup = () => { const deletionRequestExecuted = deletionRequestFactory.build({ status: DeletionStatusModel.SUCCESS }); - const deletionLogExecuted1 = deletionLogFactory.build({ deletionRequestId: deletionRequestExecuted.id }); - const deletionLogExecuted2 = deletionLogFactory.build({ - deletionRequestId: deletionRequestExecuted.id, - domain: DeletionDomainModel.ACCOUNT, - modifiedCount: 0, - deletedCount: 1, - }); + const deletionLogExecuted = deletionLogFactory.build({ deletionRequestId: deletionRequestExecuted.id }); - const executedDeletionRequestSummary: DeletionRequestLogResponse = { - targetRef: { - domain: deletionRequestExecuted.targetRefDomain, - id: deletionRequestExecuted.targetRefId, - }, - deletionPlannedAt: deletionRequestExecuted.deleteAfter, - statistics: [ - { - domain: deletionLogExecuted1.domain, - modifiedCount: deletionLogExecuted1.modifiedCount, - deletedCount: deletionLogExecuted1.deletedCount, - }, - { - domain: deletionLogExecuted2.domain, - modifiedCount: deletionLogExecuted2.modifiedCount, - deletedCount: deletionLogExecuted2.deletedCount, - }, - ], - }; + const targetRef = DeletionTargetRefBuilder.build( + deletionRequestExecuted.targetRefDomain, + deletionRequestExecuted.targetRefId + ); + const statistics = DeletionLogStatisticBuilder.build( + deletionLogExecuted.domain, + deletionLogExecuted.modifiedCount, + deletionLogExecuted.deletedCount + ); + + const executedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build( + targetRef, + deletionRequestExecuted.deleteAfter, + [statistics] + ); return { deletionRequestExecuted, executedDeletionRequestSummary, - deletionLogExecuted1, - deletionLogExecuted2, + deletionLogExecuted, }; }; @@ -434,11 +423,10 @@ describe(DeletionRequestUc.name, () => { }); it('should return object with summary of deletionRequest', async () => { - const { deletionRequestExecuted, deletionLogExecuted1, deletionLogExecuted2, executedDeletionRequestSummary } = - setup(); + const { deletionRequestExecuted, deletionLogExecuted, executedDeletionRequestSummary } = setup(); deletionRequestService.findById.mockResolvedValueOnce(deletionRequestExecuted); - deletionLogService.findByDeletionRequestId.mockResolvedValueOnce([deletionLogExecuted1, deletionLogExecuted2]); + deletionLogService.findByDeletionRequestId.mockResolvedValueOnce([deletionLogExecuted]); const result = await uc.findById(deletionRequestExecuted.id); @@ -449,13 +437,11 @@ describe(DeletionRequestUc.name, () => { describe('when searching for logs for deletionRequest which was not executed', () => { const setup = () => { const deletionRequest = deletionRequestFactory.build(); - const notExecutedDeletionRequestSummary: DeletionRequestLogResponse = { - targetRef: { - domain: deletionRequest.targetRefDomain, - id: deletionRequest.targetRefId, - }, - deletionPlannedAt: deletionRequest.deleteAfter, - }; + const targetRef = DeletionTargetRefBuilder.build(deletionRequest.targetRefDomain, deletionRequest.targetRefId); + const notExecutedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build( + targetRef, + deletionRequest.deleteAfter + ); return { deletionRequest, diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index 9bd7a3263fc..51a79aaab48 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -11,16 +11,12 @@ import { AccountService } from '@modules/account/services'; import { RocketChatUserService } from '@modules/rocketchat-user'; import { RocketChatService } from '@modules/rocketchat'; import { DeletionRequestService } from '../services/deletion-request.service'; -import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel, DeletionOperationModel, DeletionStatusModel } from '../domain/types'; import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequest } from '../domain/deletion-request.do'; -import { DeletionOperationModel } from '../domain/types/deletion-operation-model.enum'; -import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum'; import { DeletionLog } from '../domain/deletion-log.do'; import { DeletionLogStatistic } from './interface/interfaces'; -import { DeletionLogStatisticBuilder } from './builder/deletion-log-statistic.builder'; -import { DeletionRequestLogResponseBuilder } from './builder/deletion-request-log-response.builder'; -import { DeletionTargetRefBuilder } from './builder/deletion-target-ref.builder'; +import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from './builder'; import { DeletionRequestBodyProps, DeletionRequestLogResponse, DeletionRequestResponse } from '../controller/dto'; @Injectable() diff --git a/apps/server/src/modules/deletion/uc/index.ts b/apps/server/src/modules/deletion/uc/index.ts index d9f93f4f8ad..f83bf1549c8 100644 --- a/apps/server/src/modules/deletion/uc/index.ts +++ b/apps/server/src/modules/deletion/uc/index.ts @@ -1 +1,3 @@ export * from './deletion-request.uc'; +export * from './builder'; +export * from './interface'; diff --git a/apps/server/src/modules/deletion/uc/interface/interfaces.ts b/apps/server/src/modules/deletion/uc/interface/interfaces.ts index 4ee470609ea..535711f4f65 100644 --- a/apps/server/src/modules/deletion/uc/interface/interfaces.ts +++ b/apps/server/src/modules/deletion/uc/interface/interfaces.ts @@ -1,5 +1,5 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types/deletion-domain-model.enum'; +import { DeletionDomainModel } from '../../domain/types'; export interface DeletionTargetRef { domain: DeletionDomainModel; diff --git a/apps/server/src/modules/rocketchat-user/domain/index.ts b/apps/server/src/modules/rocketchat-user/domain/index.ts index 0246dd0f0f9..441baa69c52 100644 --- a/apps/server/src/modules/rocketchat-user/domain/index.ts +++ b/apps/server/src/modules/rocketchat-user/domain/index.ts @@ -1 +1,2 @@ export * from './rocket-chat-user.do'; +export * from './testing'; diff --git a/apps/server/src/modules/rocketchat-user/index.ts b/apps/server/src/modules/rocketchat-user/index.ts index a190609dd5e..34ae0f25f87 100644 --- a/apps/server/src/modules/rocketchat-user/index.ts +++ b/apps/server/src/modules/rocketchat-user/index.ts @@ -1,4 +1,3 @@ export * from './rocketchat-user.module'; export * from './service'; export * from './domain'; -export * from './domain/testing/rocket-chat-user.factory'; From 30ab3f4eeb6a44fe6263612b651b04a27b817dc7 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 16 Nov 2023 10:06:12 +0100 Subject: [PATCH 40/96] Revert "small fixes" This reverts commit ccf0aef7579c3903dee99302b45e244ffc046018, reversing changes made to 011b0f787b70cab2362ec7e75cdebdf20bac1348. --- apps/server/src/apps/deletion-console.app.ts | 31 --- .../deletion-request-input.builder.spec.ts | 33 --- .../builder/deletion-request-input.builder.ts | 11 - .../deletion-request-output.builder.spec.ts | 29 --- .../deletion-request-output.builder.ts | 10 - ...n-request-target-ref-input.builder.spec.ts | 26 --- ...letion-request-target-ref-input.builder.ts | 7 - .../modules/deletion/client/builder/index.ts | 3 - .../client/deletion-client.config.spec.ts | 41 ---- .../deletion/client/deletion-client.config.ts | 9 - .../deletion/client/deletion.client.spec.ts | 154 -------------- .../deletion/client/deletion.client.ts | 66 ------ .../src/modules/deletion/client/index.ts | 3 - .../deletion-client-config.interface.ts | 4 - .../deletion-request-input.interface.ts | 6 - .../deletion-request-output.interface.ts | 4 - ...tion-request-target-ref-input.interface.ts | 4 - .../deletion/client/interface/index.ts | 4 - .../modules/deletion/console/builder/index.ts | 1 - ...sh-delete-requests-options.builder.spec.ts | 43 ---- .../push-delete-requests-options.builder.ts | 17 -- .../console/deletion-console.module.ts | 22 -- .../console/deletion-queue.console.spec.ts | 79 ------- .../console/deletion-queue.console.ts | 46 ----- .../src/modules/deletion/console/index.ts | 1 - .../deletion/console/interface/index.ts | 1 - .../push-delete-requests-options.interface.ts | 6 - apps/server/src/modules/deletion/index.ts | 2 - .../services/batch-deletion.service.spec.ts | 97 --------- .../services/batch-deletion.service.ts | 55 ----- .../deletion/services/builder/index.ts | 2 - ...eue-deletion-request-input.builder.spec.ts | 27 --- .../queue-deletion-request-input.builder.ts | 7 - ...ue-deletion-request-output.builder.spec.ts | 46 ----- .../queue-deletion-request-output.builder.ts | 29 --- .../src/modules/deletion/services/index.ts | 4 - .../deletion/services/interface/index.ts | 2 - .../queue-deletion-request-input.interface.ts | 5 - ...queue-deletion-request-output.interface.ts | 5 - .../services/references.service.spec.ts | 74 ------- .../deletion/services/references.service.ts | 27 --- .../deletion/uc/batch-deletion.uc.spec.ts | 195 ------------------ .../modules/deletion/uc/batch-deletion.uc.ts | 71 ------- ...ch-deletion-summary-detail.builder.spec.ts | 69 ------- .../batch-deletion-summary-detail.builder.ts | 8 - .../batch-deletion-summary.builder.spec.ts | 30 --- .../builder/batch-deletion-summary.builder.ts | 13 -- .../src/modules/deletion/uc/builder/index.ts | 2 - apps/server/src/modules/deletion/uc/index.ts | 1 - ...batch-deletion-summary-detail.interface.ts | 6 - ...ch-deletion-summary-overall-status.enum.ts | 4 - .../batch-deletion-summary.interface.ts | 9 - .../modules/deletion/uc/interface/index.ts | 3 - config/default.schema.json | 17 -- nest-cli.json | 9 - package.json | 3 - 56 files changed, 1483 deletions(-) delete mode 100644 apps/server/src/apps/deletion-console.app.ts delete mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.spec.ts delete mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.ts delete mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.spec.ts delete mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.ts delete mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.spec.ts delete mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.ts delete mode 100644 apps/server/src/modules/deletion/client/builder/index.ts delete mode 100644 apps/server/src/modules/deletion/client/deletion-client.config.spec.ts delete mode 100644 apps/server/src/modules/deletion/client/deletion-client.config.ts delete mode 100644 apps/server/src/modules/deletion/client/deletion.client.spec.ts delete mode 100644 apps/server/src/modules/deletion/client/deletion.client.ts delete mode 100644 apps/server/src/modules/deletion/client/index.ts delete mode 100644 apps/server/src/modules/deletion/client/interface/deletion-client-config.interface.ts delete mode 100644 apps/server/src/modules/deletion/client/interface/deletion-request-input.interface.ts delete mode 100644 apps/server/src/modules/deletion/client/interface/deletion-request-output.interface.ts delete mode 100644 apps/server/src/modules/deletion/client/interface/deletion-request-target-ref-input.interface.ts delete mode 100644 apps/server/src/modules/deletion/client/interface/index.ts delete mode 100644 apps/server/src/modules/deletion/console/builder/index.ts delete mode 100644 apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.spec.ts delete mode 100644 apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.ts delete mode 100644 apps/server/src/modules/deletion/console/deletion-console.module.ts delete mode 100644 apps/server/src/modules/deletion/console/deletion-queue.console.spec.ts delete mode 100644 apps/server/src/modules/deletion/console/deletion-queue.console.ts delete mode 100644 apps/server/src/modules/deletion/console/index.ts delete mode 100644 apps/server/src/modules/deletion/console/interface/index.ts delete mode 100644 apps/server/src/modules/deletion/console/interface/push-delete-requests-options.interface.ts delete mode 100644 apps/server/src/modules/deletion/services/batch-deletion.service.spec.ts delete mode 100644 apps/server/src/modules/deletion/services/batch-deletion.service.ts delete mode 100644 apps/server/src/modules/deletion/services/builder/index.ts delete mode 100644 apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.spec.ts delete mode 100644 apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.ts delete mode 100644 apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.spec.ts delete mode 100644 apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.ts delete mode 100644 apps/server/src/modules/deletion/services/interface/index.ts delete mode 100644 apps/server/src/modules/deletion/services/interface/queue-deletion-request-input.interface.ts delete mode 100644 apps/server/src/modules/deletion/services/interface/queue-deletion-request-output.interface.ts delete mode 100644 apps/server/src/modules/deletion/services/references.service.spec.ts delete mode 100644 apps/server/src/modules/deletion/services/references.service.ts delete mode 100644 apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts delete mode 100644 apps/server/src/modules/deletion/uc/batch-deletion.uc.ts delete mode 100644 apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts delete mode 100644 apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts delete mode 100644 apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts delete mode 100644 apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts delete mode 100644 apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts delete mode 100644 apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts delete mode 100644 apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts diff --git a/apps/server/src/apps/deletion-console.app.ts b/apps/server/src/apps/deletion-console.app.ts deleted file mode 100644 index cafb137e160..00000000000 --- a/apps/server/src/apps/deletion-console.app.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* istanbul ignore file */ -import { BootstrapConsole } from 'nestjs-console'; -import { DeletionConsoleModule } from '@modules/deletion'; - -async function run() { - const bootstrap = new BootstrapConsole({ - module: DeletionConsoleModule, - useDecorators: true, - }); - - const app = await bootstrap.init(); - - try { - await app.init(); - - // Execute console application with provided arguments. - await bootstrap.boot(); - } catch (err) { - // eslint-disable-next-line no-console, @typescript-eslint/no-unsafe-call - console.error(err); - - // Set the exit code to 1 to indicate a console app failure. - process.exitCode = 1; - } - - // Always close the app, even if some exception - // has been thrown from the console app. - await app.close(); -} - -void run(); diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.spec.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.spec.ts deleted file mode 100644 index bd49bb841e6..00000000000 --- a/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.spec.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { ObjectId } from 'bson'; -import { DeletionRequestInput } from '../interface'; -import { DeletionRequestInputBuilder } from './deletion-request-input.builder'; - -describe(DeletionRequestInputBuilder.name, () => { - describe(DeletionRequestInputBuilder.build.name, () => { - describe('when called with proper arguments', () => { - const setup = () => { - const targetRefDomain = 'school'; - const targetRefId = new ObjectId().toHexString(); - const deleteInMinutes = 43200; - - const expectedOutput: DeletionRequestInput = { - targetRef: { - domain: targetRefDomain, - id: targetRefId, - }, - deleteInMinutes, - }; - - return { targetRefDomain, targetRefId, deleteInMinutes, expectedOutput }; - }; - - it('should return valid object with expected values', () => { - const { targetRefDomain, targetRefId, deleteInMinutes, expectedOutput } = setup(); - - const output = DeletionRequestInputBuilder.build(targetRefDomain, targetRefId, deleteInMinutes); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.ts deleted file mode 100644 index 28091418065..00000000000 --- a/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { DeletionRequestInput } from '../interface'; -import { DeletionRequestTargetRefInputBuilder } from './deletion-request-target-ref-input.builder'; - -export class DeletionRequestInputBuilder { - static build(targetRefDomain: string, targetRefId: string, deleteInMinutes?: number): DeletionRequestInput { - return { - targetRef: DeletionRequestTargetRefInputBuilder.build(targetRefDomain, targetRefId), - deleteInMinutes, - }; - } -} diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.spec.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.spec.ts deleted file mode 100644 index 399821f33ff..00000000000 --- a/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { ObjectId } from 'bson'; -import { DeletionRequestOutput } from '../interface'; -import { DeletionRequestOutputBuilder } from './deletion-request-output.builder'; - -describe(DeletionRequestOutputBuilder.name, () => { - describe(DeletionRequestOutputBuilder.build.name, () => { - describe('when called with proper arguments', () => { - const setup = () => { - const requestId = new ObjectId().toHexString(); - const deletionPlannedAt = new Date(); - - const expectedOutput: DeletionRequestOutput = { - requestId, - deletionPlannedAt, - }; - - return { requestId, deletionPlannedAt, expectedOutput }; - }; - - it('should return valid object with expected values', () => { - const { requestId, deletionPlannedAt, expectedOutput } = setup(); - - const output = DeletionRequestOutputBuilder.build(requestId, deletionPlannedAt); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.ts deleted file mode 100644 index 9192c1a47ce..00000000000 --- a/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { DeletionRequestOutput } from '../interface'; - -export class DeletionRequestOutputBuilder { - static build(requestId: string, deletionPlannedAt: Date): DeletionRequestOutput { - return { - requestId, - deletionPlannedAt, - }; - } -} diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.spec.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.spec.ts deleted file mode 100644 index 74b0631e49d..00000000000 --- a/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.spec.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { ObjectId } from 'bson'; -import { DeletionRequestTargetRefInput } from '../interface'; -import { DeletionRequestTargetRefInputBuilder } from './deletion-request-target-ref-input.builder'; - -describe(DeletionRequestTargetRefInputBuilder.name, () => { - describe(DeletionRequestTargetRefInputBuilder.build.name, () => { - describe('when called with proper arguments', () => { - const setup = () => { - const domain = 'user'; - const id = new ObjectId().toHexString(); - - const expectedOutput: DeletionRequestTargetRefInput = { domain, id }; - - return { domain, id, expectedOutput }; - }; - - it('should return valid object with expected values', () => { - const { domain, id, expectedOutput } = setup(); - - const output = DeletionRequestTargetRefInputBuilder.build(domain, id); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.ts deleted file mode 100644 index ed3c0219993..00000000000 --- a/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { DeletionRequestTargetRefInput } from '../interface'; - -export class DeletionRequestTargetRefInputBuilder { - static build(domain: string, id: string): DeletionRequestTargetRefInput { - return { domain, id }; - } -} diff --git a/apps/server/src/modules/deletion/client/builder/index.ts b/apps/server/src/modules/deletion/client/builder/index.ts deleted file mode 100644 index 85644a6b2ee..00000000000 --- a/apps/server/src/modules/deletion/client/builder/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './deletion-request-target-ref-input.builder'; -export * from './deletion-request-input.builder'; -export * from './deletion-request-output.builder'; diff --git a/apps/server/src/modules/deletion/client/deletion-client.config.spec.ts b/apps/server/src/modules/deletion/client/deletion-client.config.spec.ts deleted file mode 100644 index a3cae21e425..00000000000 --- a/apps/server/src/modules/deletion/client/deletion-client.config.spec.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { IConfig } from '@hpi-schul-cloud/commons/lib/interfaces/IConfig'; -import { Configuration } from '@hpi-schul-cloud/commons/lib'; -import { DeletionClientConfig } from './interface'; -import { getDeletionClientConfig } from './deletion-client.config'; - -describe(getDeletionClientConfig.name, () => { - let configBefore: IConfig; - - beforeAll(() => { - configBefore = Configuration.toObject({ plainSecrets: true }); - }); - - afterEach(() => { - Configuration.reset(configBefore); - }); - - describe('when called', () => { - const setup = () => { - const baseUrl = 'http://api-admin:4030'; - const apiKey = '652559c2-93da-42ad-94e1-640e3afbaca0'; - - Configuration.set('ADMIN_API_CLIENT__BASE_URL', baseUrl); - Configuration.set('ADMIN_API_CLIENT__API_KEY', apiKey); - - const expectedConfig: DeletionClientConfig = { - ADMIN_API_CLIENT_BASE_URL: baseUrl, - ADMIN_API_CLIENT_API_KEY: apiKey, - }; - - return { expectedConfig }; - }; - - it('should return config with proper values', () => { - const { expectedConfig } = setup(); - - const config = getDeletionClientConfig(); - - expect(config).toEqual(expectedConfig); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/client/deletion-client.config.ts b/apps/server/src/modules/deletion/client/deletion-client.config.ts deleted file mode 100644 index db5bf7ff226..00000000000 --- a/apps/server/src/modules/deletion/client/deletion-client.config.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Configuration } from '@hpi-schul-cloud/commons/lib'; -import { DeletionClientConfig } from './interface'; - -export const getDeletionClientConfig = (): DeletionClientConfig => { - return { - ADMIN_API_CLIENT_BASE_URL: Configuration.get('ADMIN_API_CLIENT__BASE_URL') as string, - ADMIN_API_CLIENT_API_KEY: Configuration.get('ADMIN_API_CLIENT__API_KEY') as string, - }; -}; diff --git a/apps/server/src/modules/deletion/client/deletion.client.spec.ts b/apps/server/src/modules/deletion/client/deletion.client.spec.ts deleted file mode 100644 index 096b1f9b082..00000000000 --- a/apps/server/src/modules/deletion/client/deletion.client.spec.ts +++ /dev/null @@ -1,154 +0,0 @@ -import { of } from 'rxjs'; -import { AxiosResponse } from 'axios'; -import { HttpService } from '@nestjs/axios'; -import { ConfigService } from '@nestjs/config'; -import { Test, TestingModule } from '@nestjs/testing'; -import { createMock, DeepMocked } from '@golevelup/ts-jest'; -import { axiosResponseFactory } from '@shared/testing'; -import { DeletionRequestInputBuilder, DeletionRequestOutputBuilder } from '.'; -import { DeletionRequestOutput } from './interface'; -import { DeletionClient } from './deletion.client'; - -describe(DeletionClient.name, () => { - let module: TestingModule; - let client: DeletionClient; - let httpService: DeepMocked; - - beforeEach(async () => { - module = await Test.createTestingModule({ - providers: [ - DeletionClient, - { - provide: ConfigService, - useValue: createMock({ - get: jest.fn((key: string) => { - if (key === 'ADMIN_API_CLIENT_BASE_URL') { - return 'http://localhost:4030'; - } - - // Default is for the Admin APIs API Key. - return '6b3df003-61e9-467c-9e6b-579634801896'; - }), - }), - }, - { - provide: HttpService, - useValue: createMock(), - }, - ], - }).compile(); - - client = module.get(DeletionClient); - httpService = module.get(HttpService); - }); - - afterEach(() => { - jest.resetAllMocks(); - }); - - afterAll(async () => { - await module.close(); - }); - - describe('queueDeletionRequest', () => { - describe('when received valid response with expected HTTP status code', () => { - const setup = () => { - const input = DeletionRequestInputBuilder.build('user', '652f1625e9bc1a13bdaae48b'); - - const output: DeletionRequestOutput = DeletionRequestOutputBuilder.build( - '6536ce29b595d7c8e5faf200', - new Date('2024-10-15T12:42:50.521Z') - ); - - const response: AxiosResponse = axiosResponseFactory.build({ - data: output, - status: 202, - }); - - httpService.post.mockReturnValueOnce(of(response)); - - return { input, output }; - }; - - it('should return proper output', async () => { - const { input, output } = setup(); - - const result = await client.queueDeletionRequest(input); - - expect(result).toEqual(output); - }); - }); - - describe('when received invalid HTTP status code in a response', () => { - const setup = () => { - const input = DeletionRequestInputBuilder.build('user', '652f1625e9bc1a13bdaae48b'); - - const output: DeletionRequestOutput = DeletionRequestOutputBuilder.build('', new Date()); - - const response: AxiosResponse = axiosResponseFactory.build({ - data: output, - status: 200, - }); - - httpService.post.mockReturnValueOnce(of(response)); - - return { input }; - }; - - it('should throw an exception', async () => { - const { input } = setup(); - - await expect(client.queueDeletionRequest(input)).rejects.toThrow(Error); - }); - }); - - describe('when received no requestId in a response', () => { - const setup = () => { - const input = DeletionRequestInputBuilder.build('user', '652f1625e9bc1a13bdaae48b'); - - const output: DeletionRequestOutput = DeletionRequestOutputBuilder.build( - '', - new Date('2024-10-15T12:42:50.521Z') - ); - - const response: AxiosResponse = axiosResponseFactory.build({ - data: output, - status: 202, - }); - - httpService.post.mockReturnValueOnce(of(response)); - - return { input }; - }; - - it('should throw an exception', async () => { - const { input } = setup(); - - await expect(client.queueDeletionRequest(input)).rejects.toThrow(Error); - }); - }); - - describe('when received no deletionPlannedAt in a response', () => { - const setup = () => { - const input = DeletionRequestInputBuilder.build('user', '652f1625e9bc1a13bdaae48b'); - - const response: AxiosResponse = axiosResponseFactory.build({ - data: { - requestId: '6536ce29b595d7c8e5faf200', - }, - status: 202, - }); - - httpService.post.mockReturnValueOnce(of(response)); - - return { input }; - }; - - it('should throw an exception', async () => { - const { input } = setup(); - - await expect(client.queueDeletionRequest(input)).rejects.toThrow(Error); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/client/deletion.client.ts b/apps/server/src/modules/deletion/client/deletion.client.ts deleted file mode 100644 index 66bb267d070..00000000000 --- a/apps/server/src/modules/deletion/client/deletion.client.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { firstValueFrom } from 'rxjs'; -import { AxiosResponse } from 'axios'; -import { Injectable } from '@nestjs/common'; -import { HttpService } from '@nestjs/axios'; -import { ConfigService } from '@nestjs/config'; -import { DeletionRequestInput, DeletionRequestOutput, DeletionClientConfig } from './interface'; - -@Injectable() -export class DeletionClient { - private readonly baseUrl: string; - - private readonly apiKey: string; - - private readonly postDeletionRequestsEndpoint: string; - - constructor( - private readonly httpService: HttpService, - private readonly configService: ConfigService - ) { - this.baseUrl = this.configService.get('ADMIN_API_CLIENT_BASE_URL'); - this.apiKey = this.configService.get('ADMIN_API_CLIENT_API_KEY'); - - // Prepare the POST /deletionRequests endpoint beforehand to not do it on every client call. - this.postDeletionRequestsEndpoint = new URL('/admin/api/v1/deletionRequests', this.baseUrl).toString(); - } - - async queueDeletionRequest(input: DeletionRequestInput): Promise { - const request = this.httpService.post(this.postDeletionRequestsEndpoint, input, this.defaultHeaders()); - - return firstValueFrom(request) - .then((resp: AxiosResponse) => { - // Throw an error if any other status code (other than expected "202 Accepted" is returned). - if (resp.status !== 202) { - throw new Error(`invalid HTTP status code in a response from the server - ${resp.status} instead of 202`); - } - - // Throw an error if server didn't return a requestId in a response (and it is - // required as it gives client the reference to the created deletion request). - if (!resp.data.requestId) { - throw new Error('no valid requestId returned from the server'); - } - - // Throw an error if server didn't return a deletionPlannedAt timestamp so the user - // will not be aware after which date the deletion request's execution will begin. - if (!resp.data.deletionPlannedAt) { - throw new Error('no valid deletionPlannedAt returned from the server'); - } - - return resp.data; - }) - .catch((err: Error) => { - // Throw an error if sending/processing deletion request by the client failed in any way. - throw new Error(`failed to send/process a deletion request: ${err.toString()}`); - }); - } - - private apiKeyHeader() { - return { 'X-Api-Key': this.apiKey }; - } - - private defaultHeaders() { - return { - headers: this.apiKeyHeader(), - }; - } -} diff --git a/apps/server/src/modules/deletion/client/index.ts b/apps/server/src/modules/deletion/client/index.ts deleted file mode 100644 index fde3db98f3b..00000000000 --- a/apps/server/src/modules/deletion/client/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './interface'; -export * from './builder'; -export * from './deletion.client'; diff --git a/apps/server/src/modules/deletion/client/interface/deletion-client-config.interface.ts b/apps/server/src/modules/deletion/client/interface/deletion-client-config.interface.ts deleted file mode 100644 index 8178515b6d4..00000000000 --- a/apps/server/src/modules/deletion/client/interface/deletion-client-config.interface.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface DeletionClientConfig { - ADMIN_API_CLIENT_BASE_URL: string; - ADMIN_API_CLIENT_API_KEY: string; -} diff --git a/apps/server/src/modules/deletion/client/interface/deletion-request-input.interface.ts b/apps/server/src/modules/deletion/client/interface/deletion-request-input.interface.ts deleted file mode 100644 index 4879ce4d972..00000000000 --- a/apps/server/src/modules/deletion/client/interface/deletion-request-input.interface.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { DeletionRequestTargetRefInput } from './deletion-request-target-ref-input.interface'; - -export interface DeletionRequestInput { - targetRef: DeletionRequestTargetRefInput; - deleteInMinutes?: number; -} diff --git a/apps/server/src/modules/deletion/client/interface/deletion-request-output.interface.ts b/apps/server/src/modules/deletion/client/interface/deletion-request-output.interface.ts deleted file mode 100644 index b61a372d5a5..00000000000 --- a/apps/server/src/modules/deletion/client/interface/deletion-request-output.interface.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface DeletionRequestOutput { - requestId: string; - deletionPlannedAt: Date; -} diff --git a/apps/server/src/modules/deletion/client/interface/deletion-request-target-ref-input.interface.ts b/apps/server/src/modules/deletion/client/interface/deletion-request-target-ref-input.interface.ts deleted file mode 100644 index 603bb0c13ec..00000000000 --- a/apps/server/src/modules/deletion/client/interface/deletion-request-target-ref-input.interface.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface DeletionRequestTargetRefInput { - domain: string; - id: string; -} diff --git a/apps/server/src/modules/deletion/client/interface/index.ts b/apps/server/src/modules/deletion/client/interface/index.ts deleted file mode 100644 index 38f0f639731..00000000000 --- a/apps/server/src/modules/deletion/client/interface/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './deletion-client-config.interface'; -export * from './deletion-request-target-ref-input.interface'; -export * from './deletion-request-input.interface'; -export * from './deletion-request-output.interface'; diff --git a/apps/server/src/modules/deletion/console/builder/index.ts b/apps/server/src/modules/deletion/console/builder/index.ts deleted file mode 100644 index 12fd0997ebe..00000000000 --- a/apps/server/src/modules/deletion/console/builder/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './push-delete-requests-options.builder'; diff --git a/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.spec.ts b/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.spec.ts deleted file mode 100644 index 5c83defdd1e..00000000000 --- a/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.spec.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { PushDeletionRequestsOptions } from '../interface'; -import { PushDeleteRequestsOptionsBuilder } from './push-delete-requests-options.builder'; - -describe(PushDeleteRequestsOptionsBuilder.name, () => { - describe(PushDeleteRequestsOptionsBuilder.build.name, () => { - describe('when called with proper arguments', () => { - const setup = () => { - const refsFilePath = '/tmp/ids.txt'; - const targetRefDomain = 'school'; - const deleteInMinutes = 43200; - const callsDelayMs = 100; - - const expectedOutput: PushDeletionRequestsOptions = { - refsFilePath, - targetRefDomain, - deleteInMinutes, - callsDelayMs, - }; - - return { - refsFilePath, - targetRefDomain, - deleteInMinutes, - callsDelayMs, - expectedOutput, - }; - }; - - it('should return valid object with expected values', () => { - const { refsFilePath, targetRefDomain, deleteInMinutes, callsDelayMs, expectedOutput } = setup(); - - const output = PushDeleteRequestsOptionsBuilder.build( - refsFilePath, - targetRefDomain, - deleteInMinutes, - callsDelayMs - ); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.ts b/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.ts deleted file mode 100644 index f8ceae9263d..00000000000 --- a/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { PushDeletionRequestsOptions } from '../interface'; - -export class PushDeleteRequestsOptionsBuilder { - static build( - refsFilePath: string, - targetRefDomain: string, - deleteInMinutes: number, - callsDelayMs: number - ): PushDeletionRequestsOptions { - return { - refsFilePath, - targetRefDomain, - deleteInMinutes, - callsDelayMs, - }; - } -} diff --git a/apps/server/src/modules/deletion/console/deletion-console.module.ts b/apps/server/src/modules/deletion/console/deletion-console.module.ts deleted file mode 100644 index 0585b3631da..00000000000 --- a/apps/server/src/modules/deletion/console/deletion-console.module.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Module } from '@nestjs/common'; -import { HttpModule } from '@nestjs/axios'; -import { ConfigModule } from '@nestjs/config'; -import { ConsoleModule } from 'nestjs-console'; -import { ConsoleWriterModule } from '@infra/console'; -import { createConfigModuleOptions } from '@src/config'; -import { DeletionClient } from '../client'; -import { getDeletionClientConfig } from '../client/deletion-client.config'; -import { BatchDeletionService } from '../services'; -import { BatchDeletionUc } from '../uc'; -import { DeletionQueueConsole } from './deletion-queue.console'; - -@Module({ - imports: [ - ConsoleModule, - ConsoleWriterModule, - HttpModule, - ConfigModule.forRoot(createConfigModuleOptions(getDeletionClientConfig)), - ], - providers: [DeletionClient, BatchDeletionService, BatchDeletionUc, DeletionQueueConsole], -}) -export class DeletionConsoleModule {} diff --git a/apps/server/src/modules/deletion/console/deletion-queue.console.spec.ts b/apps/server/src/modules/deletion/console/deletion-queue.console.spec.ts deleted file mode 100644 index 61f9cf0ff53..00000000000 --- a/apps/server/src/modules/deletion/console/deletion-queue.console.spec.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { ConsoleWriterService } from '@infra/console'; -import { createMock } from '@golevelup/ts-jest'; -import { BatchDeletionUc } from '../uc'; -import { DeletionQueueConsole } from './deletion-queue.console'; -import { PushDeleteRequestsOptionsBuilder } from './builder'; - -describe(DeletionQueueConsole.name, () => { - let module: TestingModule; - let console: DeletionQueueConsole; - let batchDeletionUc: BatchDeletionUc; - - beforeAll(async () => { - module = await Test.createTestingModule({ - providers: [ - DeletionQueueConsole, - { - provide: ConsoleWriterService, - useValue: createMock(), - }, - { - provide: BatchDeletionUc, - useValue: createMock(), - }, - ], - }).compile(); - - console = module.get(DeletionQueueConsole); - batchDeletionUc = module.get(BatchDeletionUc); - }); - - beforeEach(() => { - jest.clearAllMocks(); - }); - - afterAll(async () => { - await module.close(); - }); - - it('console should be defined', () => { - expect(console).toBeDefined(); - }); - - describe('pushDeletionRequests', () => { - describe('when called with valid options', () => { - const setup = () => { - const refsFilePath = '/tmp/ids.txt'; - const targetRefDomain = 'school'; - const deleteInMinutes = 43200; - const callsDelayMs = 100; - - const options = PushDeleteRequestsOptionsBuilder.build( - refsFilePath, - targetRefDomain, - deleteInMinutes, - callsDelayMs - ); - - return { - refsFilePath, - targetRefDomain, - deleteInMinutes, - callsDelayMs, - options, - }; - }; - - it(`should call ${BatchDeletionUc.name} with proper arguments`, async () => { - const { refsFilePath, targetRefDomain, deleteInMinutes, callsDelayMs, options } = setup(); - - const spy = jest.spyOn(batchDeletionUc, 'deleteRefsFromTxtFile'); - - await console.pushDeletionRequests(options); - - expect(spy).toBeCalledWith(refsFilePath, targetRefDomain, deleteInMinutes, callsDelayMs); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/console/deletion-queue.console.ts b/apps/server/src/modules/deletion/console/deletion-queue.console.ts deleted file mode 100644 index c8b133dba84..00000000000 --- a/apps/server/src/modules/deletion/console/deletion-queue.console.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { Console, Command } from 'nestjs-console'; -import { ConsoleWriterService } from '@infra/console'; -import { BatchDeletionUc } from '../uc'; -import { PushDeletionRequestsOptions } from './interface'; - -@Console({ command: 'queue', description: 'Console providing an access to the deletion queue.' }) -export class DeletionQueueConsole { - constructor(private consoleWriter: ConsoleWriterService, private batchDeletionUc: BatchDeletionUc) {} - - @Command({ - command: 'push', - description: 'Push new deletion requests to the deletion queue.', - options: [ - { - flags: '-rfp, --refsFilePath ', - description: 'Path of the file containing all the references to the data that should be deleted.', - required: true, - }, - { - flags: '-trd, --targetRefDomain ', - description: 'Name of the target ref domain.', - required: false, - }, - { - flags: '-dim, --deleteInMinutes ', - description: 'Number of minutes after which the data deletion process should begin.', - required: false, - }, - { - flags: '-cdm, --callsDelayMs ', - description: 'Delay between all the performed client calls, in milliseconds.', - required: false, - }, - ], - }) - async pushDeletionRequests(options: PushDeletionRequestsOptions): Promise { - const summary = await this.batchDeletionUc.deleteRefsFromTxtFile( - options.refsFilePath, - options.targetRefDomain, - options.deleteInMinutes ? Number(options.deleteInMinutes) : undefined, - options.callsDelayMs ? Number(options.callsDelayMs) : undefined - ); - - this.consoleWriter.info(JSON.stringify(summary)); - } -} diff --git a/apps/server/src/modules/deletion/console/index.ts b/apps/server/src/modules/deletion/console/index.ts deleted file mode 100644 index db47bd8c99f..00000000000 --- a/apps/server/src/modules/deletion/console/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './deletion-console.module'; diff --git a/apps/server/src/modules/deletion/console/interface/index.ts b/apps/server/src/modules/deletion/console/interface/index.ts deleted file mode 100644 index 2fcb281430f..00000000000 --- a/apps/server/src/modules/deletion/console/interface/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './push-delete-requests-options.interface'; diff --git a/apps/server/src/modules/deletion/console/interface/push-delete-requests-options.interface.ts b/apps/server/src/modules/deletion/console/interface/push-delete-requests-options.interface.ts deleted file mode 100644 index a54f652cf94..00000000000 --- a/apps/server/src/modules/deletion/console/interface/push-delete-requests-options.interface.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface PushDeletionRequestsOptions { - refsFilePath: string; - targetRefDomain: string; - deleteInMinutes: number; - callsDelayMs: number; -} diff --git a/apps/server/src/modules/deletion/index.ts b/apps/server/src/modules/deletion/index.ts index 793f306e7a0..bd89c1e8d84 100644 --- a/apps/server/src/modules/deletion/index.ts +++ b/apps/server/src/modules/deletion/index.ts @@ -1,4 +1,2 @@ export * from './deletion.module'; export * from './services'; -export * from './client'; -export * from './console'; diff --git a/apps/server/src/modules/deletion/services/batch-deletion.service.spec.ts b/apps/server/src/modules/deletion/services/batch-deletion.service.spec.ts deleted file mode 100644 index 640e290af1a..00000000000 --- a/apps/server/src/modules/deletion/services/batch-deletion.service.spec.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { ObjectId } from 'bson'; -import { Test, TestingModule } from '@nestjs/testing'; -import { createMock, DeepMocked } from '@golevelup/ts-jest'; -import { DeletionClient, DeletionRequestOutput, DeletionRequestOutputBuilder } from '../client'; -import { QueueDeletionRequestInputBuilder, QueueDeletionRequestOutputBuilder } from './builder'; -import { BatchDeletionService } from './batch-deletion.service'; - -describe(BatchDeletionService.name, () => { - let module: TestingModule; - let service: BatchDeletionService; - let deletionClient: DeepMocked; - - beforeAll(async () => { - module = await Test.createTestingModule({ - providers: [ - BatchDeletionService, - { - provide: DeletionClient, - useValue: createMock(), - }, - ], - }).compile(); - - service = module.get(BatchDeletionService); - deletionClient = module.get(DeletionClient); - }); - - beforeEach(() => { - jest.clearAllMocks(); - }); - - afterAll(async () => { - await module.close(); - }); - - it('service should be defined', () => { - expect(service).toBeDefined(); - }); - - describe('queueDeletionRequests', () => { - describe('when called with valid inputs array and a requested delay between the client calls', () => { - describe("when client doesn't throw any error", () => { - const setup = () => { - const inputs = [QueueDeletionRequestInputBuilder.build('user', new ObjectId().toHexString(), 60)]; - - const requestId = new ObjectId().toHexString(); - const deletionPlannedAt = new Date(); - - const queueDeletionRequestOutput: DeletionRequestOutput = DeletionRequestOutputBuilder.build( - requestId, - deletionPlannedAt - ); - - deletionClient.queueDeletionRequest.mockResolvedValueOnce(queueDeletionRequestOutput); - - const expectedOutput = QueueDeletionRequestOutputBuilder.buildSuccess(requestId, deletionPlannedAt); - - const expectedOutputs = [expectedOutput]; - - return { inputs, expectedOutputs }; - }; - - it('should return an output object with successful status info', async () => { - const { inputs, expectedOutputs } = setup(); - - const outputs = await service.queueDeletionRequests(inputs, 1); - - expect(outputs).toStrictEqual(expectedOutputs); - }); - }); - - describe('when client throws an error', () => { - const setup = () => { - const inputs = [QueueDeletionRequestInputBuilder.build('user', new ObjectId().toHexString(), 60)]; - - const error = new Error('connection error'); - - deletionClient.queueDeletionRequest.mockRejectedValueOnce(error); - - const expectedOutput = QueueDeletionRequestOutputBuilder.buildError(error); - - const expectedOutputs = [expectedOutput]; - - return { inputs, expectedOutputs }; - }; - - it('should return an output object with failure status info', async () => { - const { inputs, expectedOutputs } = setup(); - - const outputs = await service.queueDeletionRequests(inputs); - - expect(outputs).toStrictEqual(expectedOutputs); - }); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/services/batch-deletion.service.ts b/apps/server/src/modules/deletion/services/batch-deletion.service.ts deleted file mode 100644 index d8c14f315a0..00000000000 --- a/apps/server/src/modules/deletion/services/batch-deletion.service.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { QueueDeletionRequestOutputBuilder } from './builder'; -import { DeletionClient, DeletionRequestInputBuilder } from '../client'; -import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from './interface'; - -@Injectable() -export class BatchDeletionService { - constructor(private readonly deletionClient: DeletionClient) {} - - async queueDeletionRequests( - inputs: QueueDeletionRequestInput[], - callsDelayMilliseconds?: number - ): Promise { - const outputs: QueueDeletionRequestOutput[] = []; - - // For every provided deletion request input, try to queue it via deletion client. - // In any case, add the result of the trial to the outputs - it will be either a valid - // response in a form of a requestId + deletionPlannedAt values pair or some error - // returned from the client. In any case, every input should be processed. - for (const input of inputs) { - const deletionRequestInput = DeletionRequestInputBuilder.build( - input.targetRefDomain, - input.targetRefId, - input.deleteInMinutes - ); - - try { - // eslint-disable-next-line no-await-in-loop - const deletionRequestOutput = await this.deletionClient.queueDeletionRequest(deletionRequestInput); - - // In case of a successful client response, add the - // requestId + deletionPlannedAt values pair to the outputs. - outputs.push( - QueueDeletionRequestOutputBuilder.buildSuccess( - deletionRequestOutput.requestId, - deletionRequestOutput.deletionPlannedAt - ) - ); - } catch (err) { - // In case of a failure client response, add the full error message to the outputs. - outputs.push(QueueDeletionRequestOutputBuilder.buildError(err as Error)); - } - - // If any delay between the client calls has been requested, "sleep" for the specified amount of time. - if (callsDelayMilliseconds && callsDelayMilliseconds > 0) { - // eslint-disable-next-line no-await-in-loop - await new Promise((resolve) => { - setTimeout(resolve, callsDelayMilliseconds); - }); - } - } - - return outputs; - } -} diff --git a/apps/server/src/modules/deletion/services/builder/index.ts b/apps/server/src/modules/deletion/services/builder/index.ts deleted file mode 100644 index acd85a37989..00000000000 --- a/apps/server/src/modules/deletion/services/builder/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './queue-deletion-request-input.builder'; -export * from './queue-deletion-request-output.builder'; diff --git a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.spec.ts b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.spec.ts deleted file mode 100644 index e5d87858156..00000000000 --- a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.spec.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { ObjectId } from 'bson'; -import { QueueDeletionRequestInput } from '../interface'; -import { QueueDeletionRequestInputBuilder } from './queue-deletion-request-input.builder'; - -describe(QueueDeletionRequestInputBuilder.name, () => { - describe(QueueDeletionRequestInputBuilder.build.name, () => { - describe('when called with proper arguments', () => { - const setup = () => { - const targetRefDomain = 'user'; - const targetRefId = new ObjectId().toHexString(); - const deleteInMinutes = 60; - - const expectedOutput: QueueDeletionRequestInput = { targetRefDomain, targetRefId, deleteInMinutes }; - - return { targetRefDomain, targetRefId, deleteInMinutes, expectedOutput }; - }; - - it('should return valid object with expected values', () => { - const { targetRefDomain, targetRefId, deleteInMinutes, expectedOutput } = setup(); - - const output = QueueDeletionRequestInputBuilder.build(targetRefDomain, targetRefId, deleteInMinutes); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.ts b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.ts deleted file mode 100644 index a7fff2152b9..00000000000 --- a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { QueueDeletionRequestInput } from '../interface'; - -export class QueueDeletionRequestInputBuilder { - static build(targetRefDomain: string, targetRefId: string, deleteInMinutes: number): QueueDeletionRequestInput { - return { targetRefDomain, targetRefId, deleteInMinutes }; - } -} diff --git a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.spec.ts b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.spec.ts deleted file mode 100644 index cd835a9cf4a..00000000000 --- a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.spec.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { ObjectId } from 'bson'; -import { QueueDeletionRequestOutput } from '../interface'; -import { QueueDeletionRequestOutputBuilder } from './queue-deletion-request-output.builder'; - -describe(QueueDeletionRequestOutputBuilder.name, () => { - describe(QueueDeletionRequestOutputBuilder.buildSuccess.name, () => { - describe('when called with proper arguments', () => { - const setup = () => { - const requestId = new ObjectId().toHexString(); - const deletionPlannedAt = new Date(); - - const expectedOutput: QueueDeletionRequestOutput = { requestId, deletionPlannedAt }; - - return { requestId, deletionPlannedAt, expectedOutput }; - }; - - it('should return valid object with expected values', () => { - const { requestId, deletionPlannedAt, expectedOutput } = setup(); - - const output = QueueDeletionRequestOutputBuilder.buildSuccess(requestId, deletionPlannedAt); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - }); - - describe(QueueDeletionRequestOutputBuilder.buildError.name, () => { - describe('when called with proper arguments', () => { - const setup = () => { - const error = new Error('test error message'); - - const expectedOutput: QueueDeletionRequestOutput = { error: error.toString() }; - - return { error, expectedOutput }; - }; - - it('should return valid object with expected values', () => { - const { error, expectedOutput } = setup(); - - const output = QueueDeletionRequestOutputBuilder.buildError(error); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.ts b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.ts deleted file mode 100644 index c44e503cbaf..00000000000 --- a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { QueueDeletionRequestOutput } from '../interface'; - -export class QueueDeletionRequestOutputBuilder { - private static build(requestId?: string, deletionPlannedAt?: Date, error?: string): QueueDeletionRequestOutput { - const output: QueueDeletionRequestOutput = {}; - - if (requestId) { - output.requestId = requestId; - } - - if (deletionPlannedAt) { - output.deletionPlannedAt = deletionPlannedAt; - } - - if (error) { - output.error = error.toString(); - } - - return output; - } - - static buildSuccess(requestId: string, deletionPlannedAt: Date): QueueDeletionRequestOutput { - return this.build(requestId, deletionPlannedAt); - } - - static buildError(err: Error): QueueDeletionRequestOutput { - return this.build(undefined, undefined, err.toString()); - } -} diff --git a/apps/server/src/modules/deletion/services/index.ts b/apps/server/src/modules/deletion/services/index.ts index 52e0b6ba22d..9661354718c 100644 --- a/apps/server/src/modules/deletion/services/index.ts +++ b/apps/server/src/modules/deletion/services/index.ts @@ -1,5 +1 @@ -export * from './interface'; -export * from './builder'; -export * from './references.service'; -export * from './batch-deletion.service'; export * from './deletion-request.service'; diff --git a/apps/server/src/modules/deletion/services/interface/index.ts b/apps/server/src/modules/deletion/services/interface/index.ts deleted file mode 100644 index 8a455440798..00000000000 --- a/apps/server/src/modules/deletion/services/interface/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './queue-deletion-request-input.interface'; -export * from './queue-deletion-request-output.interface'; diff --git a/apps/server/src/modules/deletion/services/interface/queue-deletion-request-input.interface.ts b/apps/server/src/modules/deletion/services/interface/queue-deletion-request-input.interface.ts deleted file mode 100644 index b421943bce9..00000000000 --- a/apps/server/src/modules/deletion/services/interface/queue-deletion-request-input.interface.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface QueueDeletionRequestInput { - targetRefDomain: string; - targetRefId: string; - deleteInMinutes: number; -} diff --git a/apps/server/src/modules/deletion/services/interface/queue-deletion-request-output.interface.ts b/apps/server/src/modules/deletion/services/interface/queue-deletion-request-output.interface.ts deleted file mode 100644 index 375ff811857..00000000000 --- a/apps/server/src/modules/deletion/services/interface/queue-deletion-request-output.interface.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface QueueDeletionRequestOutput { - requestId?: string; - deletionPlannedAt?: Date; - error?: string; -} diff --git a/apps/server/src/modules/deletion/services/references.service.spec.ts b/apps/server/src/modules/deletion/services/references.service.spec.ts deleted file mode 100644 index 26eb6e0c8d7..00000000000 --- a/apps/server/src/modules/deletion/services/references.service.spec.ts +++ /dev/null @@ -1,74 +0,0 @@ -import fs from 'fs'; -import { ReferencesService } from './references.service'; - -describe(ReferencesService.name, () => { - describe(ReferencesService.loadFromTxtFile.name, () => { - const setup = (mockedFileContent: string) => { - jest.spyOn(fs, 'readFileSync').mockReturnValueOnce(mockedFileContent); - }; - - describe('when passed a completely empty file (without any content)', () => { - it('should return an empty references array', () => { - setup(''); - - const references = ReferencesService.loadFromTxtFile('references.txt'); - - expect(references).toEqual([]); - }); - }); - - describe('when passed a file without any references (just some empty lines)', () => { - it('should return an empty references array', () => { - setup('\n\n \n \n\n\n \n\n\n'); - - const references = ReferencesService.loadFromTxtFile('references.txt'); - - expect(references).toEqual([]); - }); - }); - - describe('when passed a file with 3 references on a few separate lines', () => { - describe('split with CRs', () => { - it('should return an array with all the references present in a file', () => { - setup('653fd3b784ca851b17e98579\r653fd3b784ca851b17e9857a\r653fd3b784ca851b17e9857b\n\n\n'); - - const references = ReferencesService.loadFromTxtFile('references.txt'); - - expect(references).toEqual([ - '653fd3b784ca851b17e98579', - '653fd3b784ca851b17e9857a', - '653fd3b784ca851b17e9857b', - ]); - }); - }); - - describe('split with LFs', () => { - it('should return an array with all the references present in a file', () => { - setup('653fd3b784ca851b17e98579\n653fd3b784ca851b17e9857a\n653fd3b784ca851b17e9857b\n\n\n'); - - const references = ReferencesService.loadFromTxtFile('references.txt'); - - expect(references).toEqual([ - '653fd3b784ca851b17e98579', - '653fd3b784ca851b17e9857a', - '653fd3b784ca851b17e9857b', - ]); - }); - }); - - describe('split with CRLFs', () => { - it('should return an array with all the references present in a file', () => { - setup('653fd3b784ca851b17e98579\r\n653fd3b784ca851b17e9857a\r\n653fd3b784ca851b17e9857b\r\n\r\n\r\n'); - - const references = ReferencesService.loadFromTxtFile('references.txt'); - - expect(references).toEqual([ - '653fd3b784ca851b17e98579', - '653fd3b784ca851b17e9857a', - '653fd3b784ca851b17e9857b', - ]); - }); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/services/references.service.ts b/apps/server/src/modules/deletion/services/references.service.ts deleted file mode 100644 index 991c36c02af..00000000000 --- a/apps/server/src/modules/deletion/services/references.service.ts +++ /dev/null @@ -1,27 +0,0 @@ -import fs from 'fs'; - -export class ReferencesService { - static loadFromTxtFile(filePath: string): string[] { - let fileContent = fs.readFileSync(filePath).toString(); - - // Replace all the CRLF occurrences with just a LF. - fileContent = fileContent.replace(/\r\n?/g, '\n'); - - // Split the whole file content by a line feed (LF) char (\n). - const fileLines = fileContent.split('\n'); - - const references: string[] = []; - - // Iterate over all the file lines and if it contains a valid id (which is - // basically any non-empty string), add it to the loaded references array. - fileLines.forEach((fileLine) => { - const reference = fileLine.trim(); - - if (reference && reference.length > 0) { - references.push(reference); - } - }); - - return references; - } -} diff --git a/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts b/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts deleted file mode 100644 index 7292f36efb0..00000000000 --- a/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts +++ /dev/null @@ -1,195 +0,0 @@ -import { ObjectId } from 'bson'; -import { Test, TestingModule } from '@nestjs/testing'; -import { createMock, DeepMocked } from '@golevelup/ts-jest'; -import { - BatchDeletionService, - QueueDeletionRequestInputBuilder, - QueueDeletionRequestOutput, - QueueDeletionRequestOutputBuilder, - ReferencesService, -} from '../services'; -import { BatchDeletionSummaryDetail, BatchDeletionSummaryOverallStatus } from './interface'; -import { BatchDeletionSummaryDetailBuilder } from './builder'; -import { BatchDeletionUc } from './batch-deletion.uc'; - -describe(BatchDeletionUc.name, () => { - let module: TestingModule; - let uc: BatchDeletionUc; - let batchDeletionService: DeepMocked; - - beforeAll(async () => { - module = await Test.createTestingModule({ - providers: [ - BatchDeletionUc, - { - provide: BatchDeletionService, - useValue: createMock(), - }, - ], - }).compile(); - - uc = module.get(BatchDeletionUc); - batchDeletionService = module.get(BatchDeletionService); - }); - - beforeEach(() => { - jest.clearAllMocks(); - }); - - afterAll(async () => { - await module.close(); - }); - - it('uc should be defined', () => { - expect(uc).toBeDefined(); - }); - - describe('deleteRefsFromTxtFile', () => { - describe('when called with valid arguments', () => { - describe('when batch deletion service returns an expected amount of outputs', () => { - describe('when only successful executions took place', () => { - const setup = () => { - const targetRefsCount = 3; - - const targetRefIds: string[] = []; - const outputs: QueueDeletionRequestOutput[] = []; - - for (let i = 0; i < targetRefsCount; i += 1) { - targetRefIds.push(new ObjectId().toHexString()); - outputs.push(QueueDeletionRequestOutputBuilder.buildSuccess(new ObjectId().toHexString(), new Date())); - } - - ReferencesService.loadFromTxtFile = jest.fn().mockReturnValueOnce(targetRefIds); - - batchDeletionService.queueDeletionRequests.mockResolvedValueOnce([outputs[0], outputs[1], outputs[2]]); - - const targetRefDomain = 'school'; - const deleteInMinutes = 60; - - const expectedSummaryFieldsDetails: BatchDeletionSummaryDetail[] = []; - - for (let i = 0; i < targetRefIds.length; i += 1) { - expectedSummaryFieldsDetails.push( - BatchDeletionSummaryDetailBuilder.build( - QueueDeletionRequestInputBuilder.build(targetRefDomain, targetRefIds[i], deleteInMinutes), - outputs[i] - ) - ); - } - - const expectedSummaryFields = { - overallStatus: BatchDeletionSummaryOverallStatus.SUCCESS, - successCount: 3, - failureCount: 0, - details: expectedSummaryFieldsDetails, - }; - - const refsFilePath = '/tmp/ids.txt'; - - return { refsFilePath, targetRefDomain, deleteInMinutes, expectedSummaryFields }; - }; - - it('should return proper summary with all the successes and a successful overall status', async () => { - const { refsFilePath, targetRefDomain, deleteInMinutes, expectedSummaryFields } = setup(); - - const summary = await uc.deleteRefsFromTxtFile(refsFilePath, targetRefDomain, deleteInMinutes); - - expect(summary.executionTimeMilliseconds).toBeGreaterThan(0); - expect(summary).toMatchObject(expectedSummaryFields); - }); - }); - - describe('when both successful and failed executions took place', () => { - const setup = () => { - const targetRefsCount = 3; - - const targetRefIds: string[] = []; - - for (let i = 0; i < targetRefsCount; i += 1) { - targetRefIds.push(new ObjectId().toHexString()); - } - - const targetRefDomain = 'school'; - const deleteInMinutes = 60; - - ReferencesService.loadFromTxtFile = jest.fn().mockReturnValueOnce(targetRefIds); - - const outputs = [ - QueueDeletionRequestOutputBuilder.buildSuccess(new ObjectId().toHexString(), new Date()), - QueueDeletionRequestOutputBuilder.buildError(new Error('some error occurred...')), - QueueDeletionRequestOutputBuilder.buildSuccess(new ObjectId().toHexString(), new Date()), - ]; - - batchDeletionService.queueDeletionRequests.mockResolvedValueOnce([outputs[0], outputs[1], outputs[2]]); - - const expectedSummaryFieldsDetails: BatchDeletionSummaryDetail[] = []; - - for (let i = 0; i < targetRefIds.length; i += 1) { - expectedSummaryFieldsDetails.push( - BatchDeletionSummaryDetailBuilder.build( - QueueDeletionRequestInputBuilder.build(targetRefDomain, targetRefIds[i], deleteInMinutes), - outputs[i] - ) - ); - } - - const expectedSummaryFields = { - overallStatus: BatchDeletionSummaryOverallStatus.FAILURE, - successCount: 2, - failureCount: 1, - details: expectedSummaryFieldsDetails, - }; - - const refsFilePath = '/tmp/ids.txt'; - - return { refsFilePath, targetRefDomain, deleteInMinutes, expectedSummaryFields }; - }; - - it('should return proper summary with all the successes and failures', async () => { - const { refsFilePath, targetRefDomain, deleteInMinutes, expectedSummaryFields } = setup(); - - const summary = await uc.deleteRefsFromTxtFile(refsFilePath, targetRefDomain, deleteInMinutes); - - expect(summary.executionTimeMilliseconds).toBeGreaterThan(0); - expect(summary).toMatchObject(expectedSummaryFields); - }); - }); - }); - - describe('when batch deletion service returns an invalid amount of outputs', () => { - const setup = () => { - const targetRefsCount = 3; - - const targetRefIds: string[] = []; - - for (let i = 0; i < targetRefsCount; i += 1) { - targetRefIds.push(new ObjectId().toHexString()); - } - - ReferencesService.loadFromTxtFile = jest.fn().mockReturnValueOnce(targetRefIds); - - const outputs: QueueDeletionRequestOutput[] = []; - - for (let i = 0; i < targetRefsCount - 1; i += 1) { - targetRefIds.push(new ObjectId().toHexString()); - outputs.push(QueueDeletionRequestOutputBuilder.buildSuccess(new ObjectId().toHexString(), new Date())); - } - - batchDeletionService.queueDeletionRequests.mockResolvedValueOnce(outputs); - - const refsFilePath = '/tmp/ids.txt'; - - return { refsFilePath }; - }; - - it('should throw an error', async () => { - const { refsFilePath } = setup(); - - const func = () => uc.deleteRefsFromTxtFile(refsFilePath); - - await expect(func()).rejects.toThrow(); - }); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts b/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts deleted file mode 100644 index 258b1b53f65..00000000000 --- a/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { BatchDeletionSummaryBuilder, BatchDeletionSummaryDetailBuilder } from './builder'; -import { - ReferencesService, - BatchDeletionService, - QueueDeletionRequestInput, - QueueDeletionRequestInputBuilder, -} from '../services'; -import { BatchDeletionSummary, BatchDeletionSummaryOverallStatus } from './interface'; - -@Injectable() -export class BatchDeletionUc { - constructor(private readonly batchDeletionService: BatchDeletionService) {} - - async deleteRefsFromTxtFile( - refsFilePath: string, - targetRefDomain = 'user', - deleteInMinutes = 43200, // 43200 minutes = 720 hours = 30 days - callsDelayMilliseconds?: number - ): Promise { - // First, load all the references from the provided text file (with given path). - const refsFromTxtFile = ReferencesService.loadFromTxtFile(refsFilePath); - - const inputs: QueueDeletionRequestInput[] = []; - - // For each reference found in a given file, add it to the inputs - // array (with added targetRefDomain and deleteInMinutes fields). - refsFromTxtFile.forEach((ref) => - inputs.push(QueueDeletionRequestInputBuilder.build(targetRefDomain, ref, deleteInMinutes)) - ); - - // Measure the overall queueing execution time by setting the start... - const startTime = performance.now(); - - const outputs = await this.batchDeletionService.queueDeletionRequests(inputs, callsDelayMilliseconds); - - // ...and end timestamps before and after the batch deletion service method execution. - const endTime = performance.now(); - - // Throw an error if the returned outputs number doesn't match the returned inputs number. - if (outputs.length !== inputs.length) { - throw new Error( - 'invalid result from the batch deletion service - expected to ' + - 'receive the same amount of outputs as the provided inputs, ' + - `instead received ${outputs.length} outputs for ${inputs.length} inputs` - ); - } - - const summary: BatchDeletionSummary = BatchDeletionSummaryBuilder.build(endTime - startTime); - - // Go through every received output and, in case of an error presence increase - // a failure count or, in case of no error, increase a success count. - for (let i = 0; i < outputs.length; i += 1) { - if (outputs[i].error) { - summary.failureCount += 1; - } else { - summary.successCount += 1; - } - - // Also add all the processed inputs and outputs details to the overall summary. - summary.details.push(BatchDeletionSummaryDetailBuilder.build(inputs[i], outputs[i])); - } - - // If no failure has been spotted, assume an overall success. - if (summary.failureCount === 0) { - summary.overallStatus = BatchDeletionSummaryOverallStatus.SUCCESS; - } - - return summary; - } -} diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts deleted file mode 100644 index 43ea82e86d5..00000000000 --- a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { ObjectId } from '@mikro-orm/mongodb'; -import { BatchDeletionSummaryDetail } from '..'; -import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../../services'; -import { BatchDeletionSummaryDetailBuilder } from './batch-deletion-summary-detail.builder'; - -describe(BatchDeletionSummaryDetailBuilder.name, () => { - describe(BatchDeletionSummaryDetailBuilder.build.name, () => { - describe('when called with proper arguments for', () => { - describe('a successful output case', () => { - const setup = () => { - const deletionRequestInput: QueueDeletionRequestInput = { - targetRefDomain: 'user', - targetRefId: new ObjectId().toHexString(), - deleteInMinutes: 1440, - }; - - const deletionRequestOutput: QueueDeletionRequestOutput = { - requestId: new ObjectId().toHexString(), - deletionPlannedAt: new Date(), - }; - - const expectedOutput: BatchDeletionSummaryDetail = { - input: deletionRequestInput, - output: deletionRequestOutput, - }; - - return { deletionRequestInput, deletionRequestOutput, expectedOutput }; - }; - - it('should return valid object with expected values', () => { - const { deletionRequestInput, deletionRequestOutput, expectedOutput } = setup(); - - const output = BatchDeletionSummaryDetailBuilder.build(deletionRequestInput, deletionRequestOutput); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - - describe('an error output case', () => { - const setup = () => { - const deletionRequestInput: QueueDeletionRequestInput = { - targetRefDomain: 'user', - targetRefId: new ObjectId().toHexString(), - deleteInMinutes: 1440, - }; - - const deletionRequestOutput: QueueDeletionRequestOutput = { - error: 'some error occurred...', - }; - - const expectedOutput: BatchDeletionSummaryDetail = { - input: deletionRequestInput, - output: deletionRequestOutput, - }; - - return { deletionRequestInput, deletionRequestOutput, expectedOutput }; - }; - - it('should return valid object with expected values', () => { - const { deletionRequestInput, deletionRequestOutput, expectedOutput } = setup(); - - const output = BatchDeletionSummaryDetailBuilder.build(deletionRequestInput, deletionRequestOutput); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts deleted file mode 100644 index 9ebbce66171..00000000000 --- a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../../services'; -import { BatchDeletionSummaryDetail } from '../interface'; - -export class BatchDeletionSummaryDetailBuilder { - static build(input: QueueDeletionRequestInput, output: QueueDeletionRequestOutput): BatchDeletionSummaryDetail { - return { input, output }; - } -} diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts deleted file mode 100644 index a2b534602d4..00000000000 --- a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { BatchDeletionSummary, BatchDeletionSummaryOverallStatus } from '../interface'; -import { BatchDeletionSummaryBuilder } from './batch-deletion-summary.builder'; - -describe(BatchDeletionSummaryBuilder.name, () => { - describe(BatchDeletionSummaryBuilder.build.name, () => { - describe('when called with proper arguments', () => { - const setup = () => { - const executionTimeMilliseconds = 142; - - const expectedOutput: BatchDeletionSummary = { - executionTimeMilliseconds: 142, - overallStatus: BatchDeletionSummaryOverallStatus.FAILURE, - successCount: 0, - failureCount: 0, - details: [], - }; - - return { executionTimeMilliseconds, expectedOutput }; - }; - - it('should return valid object with expected values', () => { - const { executionTimeMilliseconds, expectedOutput } = setup(); - - const output = BatchDeletionSummaryBuilder.build(executionTimeMilliseconds); - - expect(output).toStrictEqual(expectedOutput); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts deleted file mode 100644 index 57fa2bcccd9..00000000000 --- a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { BatchDeletionSummary, BatchDeletionSummaryOverallStatus } from '../interface'; - -export class BatchDeletionSummaryBuilder { - static build(executionTimeMilliseconds: number): BatchDeletionSummary { - return { - executionTimeMilliseconds, - overallStatus: BatchDeletionSummaryOverallStatus.FAILURE, - successCount: 0, - failureCount: 0, - details: [], - }; - } -} diff --git a/apps/server/src/modules/deletion/uc/builder/index.ts b/apps/server/src/modules/deletion/uc/builder/index.ts index c7ac430fa63..fb8a2a5e7ec 100644 --- a/apps/server/src/modules/deletion/uc/builder/index.ts +++ b/apps/server/src/modules/deletion/uc/builder/index.ts @@ -2,5 +2,3 @@ export * from './deletion-log-statistic.builder'; export * from './deletion-request-body-props.builder'; export * from './deletion-request-log-response.builder'; export * from './deletion-target-ref.builder'; -export * from './batch-deletion-summary-detail.builder'; -export * from './batch-deletion-summary.builder'; diff --git a/apps/server/src/modules/deletion/uc/index.ts b/apps/server/src/modules/deletion/uc/index.ts index 3260e898ae6..f83bf1549c8 100644 --- a/apps/server/src/modules/deletion/uc/index.ts +++ b/apps/server/src/modules/deletion/uc/index.ts @@ -1,4 +1,3 @@ export * from './deletion-request.uc'; export * from './builder'; export * from './interface'; -export * from './batch-deletion.uc'; diff --git a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts b/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts deleted file mode 100644 index 4fe99c13fad..00000000000 --- a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../../services'; - -export interface BatchDeletionSummaryDetail { - input: QueueDeletionRequestInput; - output: QueueDeletionRequestOutput; -} diff --git a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts b/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts deleted file mode 100644 index 4ae91bdf70e..00000000000 --- a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts +++ /dev/null @@ -1,4 +0,0 @@ -export const enum BatchDeletionSummaryOverallStatus { - SUCCESS = 'success', - FAILURE = 'failure', -} diff --git a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts b/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts deleted file mode 100644 index ce633e164f1..00000000000 --- a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { BatchDeletionSummaryDetail } from './batch-deletion-summary-detail.interface'; - -export interface BatchDeletionSummary { - executionTimeMilliseconds: number; - overallStatus: string; - successCount: number; - failureCount: number; - details: BatchDeletionSummaryDetail[]; -} diff --git a/apps/server/src/modules/deletion/uc/interface/index.ts b/apps/server/src/modules/deletion/uc/interface/index.ts index 9dcf644f410..95786098275 100644 --- a/apps/server/src/modules/deletion/uc/interface/index.ts +++ b/apps/server/src/modules/deletion/uc/interface/index.ts @@ -1,4 +1 @@ -export * from './batch-deletion-summary-overall-status.enum'; -export * from './batch-deletion-summary-detail.interface'; -export * from './batch-deletion-summary.interface'; export * from './interfaces'; diff --git a/config/default.schema.json b/config/default.schema.json index 903aac324ca..9a7c75445ee 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -1366,23 +1366,6 @@ "description": "Number of simultaneously synchronized students, teachers and classes" } } - }, - "ADMIN_API_CLIENT": { - "type": "object", - "description": "Configuration of the schulcloud-server's admin API client.", - "properties": { - "BASE_URL": { - "type": "string", - "description": "Base URL of the Admin API." - }, - "API_KEY": { - "type": "string", - "description": "API key for accessing the Admin API." - } - }, - "default": { - "BASE_URL": "http://localhost:4030" - } } }, "required": [], diff --git a/nest-cli.json b/nest-cli.json index 8ce5461bb6f..73dea03c093 100644 --- a/nest-cli.json +++ b/nest-cli.json @@ -45,15 +45,6 @@ "tsConfigPath": "apps/server/tsconfig.app.json" } }, - "deletion-console": { - "type": "application", - "root": "apps/server", - "entryFile": "apps/deletion-console.app", - "sourceRoot": "apps/server/src", - "compilerOptions": { - "tsConfigPath": "apps/server/tsconfig.app.json" - } - }, "files-storage": { "type": "application", "root": "apps/server", diff --git a/package.json b/package.json index 7b85512165b..057125e9756 100644 --- a/package.json +++ b/package.json @@ -76,9 +76,6 @@ "nest:start:console": "nest start console --", "nest:start:console:dev": "nest start console --watch --", "nest:start:console:debug": "nest start console --debug --watch --", - "nest:start:deletion-console": "nest start deletion-console --", - "nest:start:deletion-console:dev": "nest start deletion-console --watch --", - "nest:start:deletion-console:debug": "nest start deletion-console --debug --watch --", "nest:test": "npm run nest:test:cov && npm run nest:lint", "nest:test:all": "jest", "nest:test:unit": "jest \"^((?!\\.api\\.spec\\.ts).)*\\.spec\\.ts$\"", From 6feb0a841516a11afa744d9e3cec94cdea709cfb Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 16 Nov 2023 10:12:28 +0100 Subject: [PATCH 41/96] Revert "Revert "small fixes"" This reverts commit 30ab3f4eeb6a44fe6263612b651b04a27b817dc7. --- apps/server/src/apps/deletion-console.app.ts | 31 +++ .../deletion-request-input.builder.spec.ts | 33 +++ .../builder/deletion-request-input.builder.ts | 11 + .../deletion-request-output.builder.spec.ts | 29 +++ .../deletion-request-output.builder.ts | 10 + ...n-request-target-ref-input.builder.spec.ts | 26 +++ ...letion-request-target-ref-input.builder.ts | 7 + .../modules/deletion/client/builder/index.ts | 3 + .../client/deletion-client.config.spec.ts | 41 ++++ .../deletion/client/deletion-client.config.ts | 9 + .../deletion/client/deletion.client.spec.ts | 154 ++++++++++++++ .../deletion/client/deletion.client.ts | 66 ++++++ .../src/modules/deletion/client/index.ts | 3 + .../deletion-client-config.interface.ts | 4 + .../deletion-request-input.interface.ts | 6 + .../deletion-request-output.interface.ts | 4 + ...tion-request-target-ref-input.interface.ts | 4 + .../deletion/client/interface/index.ts | 4 + .../modules/deletion/console/builder/index.ts | 1 + ...sh-delete-requests-options.builder.spec.ts | 43 ++++ .../push-delete-requests-options.builder.ts | 17 ++ .../console/deletion-console.module.ts | 22 ++ .../console/deletion-queue.console.spec.ts | 79 +++++++ .../console/deletion-queue.console.ts | 46 +++++ .../src/modules/deletion/console/index.ts | 1 + .../deletion/console/interface/index.ts | 1 + .../push-delete-requests-options.interface.ts | 6 + apps/server/src/modules/deletion/index.ts | 2 + .../services/batch-deletion.service.spec.ts | 97 +++++++++ .../services/batch-deletion.service.ts | 55 +++++ .../deletion/services/builder/index.ts | 2 + ...eue-deletion-request-input.builder.spec.ts | 27 +++ .../queue-deletion-request-input.builder.ts | 7 + ...ue-deletion-request-output.builder.spec.ts | 46 +++++ .../queue-deletion-request-output.builder.ts | 29 +++ .../src/modules/deletion/services/index.ts | 4 + .../deletion/services/interface/index.ts | 2 + .../queue-deletion-request-input.interface.ts | 5 + ...queue-deletion-request-output.interface.ts | 5 + .../services/references.service.spec.ts | 74 +++++++ .../deletion/services/references.service.ts | 27 +++ .../deletion/uc/batch-deletion.uc.spec.ts | 195 ++++++++++++++++++ .../modules/deletion/uc/batch-deletion.uc.ts | 71 +++++++ ...ch-deletion-summary-detail.builder.spec.ts | 69 +++++++ .../batch-deletion-summary-detail.builder.ts | 8 + .../batch-deletion-summary.builder.spec.ts | 30 +++ .../builder/batch-deletion-summary.builder.ts | 13 ++ .../src/modules/deletion/uc/builder/index.ts | 2 + apps/server/src/modules/deletion/uc/index.ts | 1 + ...batch-deletion-summary-detail.interface.ts | 6 + ...ch-deletion-summary-overall-status.enum.ts | 4 + .../batch-deletion-summary.interface.ts | 9 + .../modules/deletion/uc/interface/index.ts | 3 + config/default.schema.json | 17 ++ nest-cli.json | 9 + package.json | 3 + 56 files changed, 1483 insertions(+) create mode 100644 apps/server/src/apps/deletion-console.app.ts create mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.spec.ts create mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.ts create mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.spec.ts create mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.ts create mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.spec.ts create mode 100644 apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.ts create mode 100644 apps/server/src/modules/deletion/client/builder/index.ts create mode 100644 apps/server/src/modules/deletion/client/deletion-client.config.spec.ts create mode 100644 apps/server/src/modules/deletion/client/deletion-client.config.ts create mode 100644 apps/server/src/modules/deletion/client/deletion.client.spec.ts create mode 100644 apps/server/src/modules/deletion/client/deletion.client.ts create mode 100644 apps/server/src/modules/deletion/client/index.ts create mode 100644 apps/server/src/modules/deletion/client/interface/deletion-client-config.interface.ts create mode 100644 apps/server/src/modules/deletion/client/interface/deletion-request-input.interface.ts create mode 100644 apps/server/src/modules/deletion/client/interface/deletion-request-output.interface.ts create mode 100644 apps/server/src/modules/deletion/client/interface/deletion-request-target-ref-input.interface.ts create mode 100644 apps/server/src/modules/deletion/client/interface/index.ts create mode 100644 apps/server/src/modules/deletion/console/builder/index.ts create mode 100644 apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.spec.ts create mode 100644 apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.ts create mode 100644 apps/server/src/modules/deletion/console/deletion-console.module.ts create mode 100644 apps/server/src/modules/deletion/console/deletion-queue.console.spec.ts create mode 100644 apps/server/src/modules/deletion/console/deletion-queue.console.ts create mode 100644 apps/server/src/modules/deletion/console/index.ts create mode 100644 apps/server/src/modules/deletion/console/interface/index.ts create mode 100644 apps/server/src/modules/deletion/console/interface/push-delete-requests-options.interface.ts create mode 100644 apps/server/src/modules/deletion/services/batch-deletion.service.spec.ts create mode 100644 apps/server/src/modules/deletion/services/batch-deletion.service.ts create mode 100644 apps/server/src/modules/deletion/services/builder/index.ts create mode 100644 apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.spec.ts create mode 100644 apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.ts create mode 100644 apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.spec.ts create mode 100644 apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.ts create mode 100644 apps/server/src/modules/deletion/services/interface/index.ts create mode 100644 apps/server/src/modules/deletion/services/interface/queue-deletion-request-input.interface.ts create mode 100644 apps/server/src/modules/deletion/services/interface/queue-deletion-request-output.interface.ts create mode 100644 apps/server/src/modules/deletion/services/references.service.spec.ts create mode 100644 apps/server/src/modules/deletion/services/references.service.ts create mode 100644 apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts create mode 100644 apps/server/src/modules/deletion/uc/batch-deletion.uc.ts create mode 100644 apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts create mode 100644 apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts create mode 100644 apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts create mode 100644 apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts create mode 100644 apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts create mode 100644 apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts create mode 100644 apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts diff --git a/apps/server/src/apps/deletion-console.app.ts b/apps/server/src/apps/deletion-console.app.ts new file mode 100644 index 00000000000..cafb137e160 --- /dev/null +++ b/apps/server/src/apps/deletion-console.app.ts @@ -0,0 +1,31 @@ +/* istanbul ignore file */ +import { BootstrapConsole } from 'nestjs-console'; +import { DeletionConsoleModule } from '@modules/deletion'; + +async function run() { + const bootstrap = new BootstrapConsole({ + module: DeletionConsoleModule, + useDecorators: true, + }); + + const app = await bootstrap.init(); + + try { + await app.init(); + + // Execute console application with provided arguments. + await bootstrap.boot(); + } catch (err) { + // eslint-disable-next-line no-console, @typescript-eslint/no-unsafe-call + console.error(err); + + // Set the exit code to 1 to indicate a console app failure. + process.exitCode = 1; + } + + // Always close the app, even if some exception + // has been thrown from the console app. + await app.close(); +} + +void run(); diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.spec.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.spec.ts new file mode 100644 index 00000000000..bd49bb841e6 --- /dev/null +++ b/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.spec.ts @@ -0,0 +1,33 @@ +import { ObjectId } from 'bson'; +import { DeletionRequestInput } from '../interface'; +import { DeletionRequestInputBuilder } from './deletion-request-input.builder'; + +describe(DeletionRequestInputBuilder.name, () => { + describe(DeletionRequestInputBuilder.build.name, () => { + describe('when called with proper arguments', () => { + const setup = () => { + const targetRefDomain = 'school'; + const targetRefId = new ObjectId().toHexString(); + const deleteInMinutes = 43200; + + const expectedOutput: DeletionRequestInput = { + targetRef: { + domain: targetRefDomain, + id: targetRefId, + }, + deleteInMinutes, + }; + + return { targetRefDomain, targetRefId, deleteInMinutes, expectedOutput }; + }; + + it('should return valid object with expected values', () => { + const { targetRefDomain, targetRefId, deleteInMinutes, expectedOutput } = setup(); + + const output = DeletionRequestInputBuilder.build(targetRefDomain, targetRefId, deleteInMinutes); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.ts new file mode 100644 index 00000000000..28091418065 --- /dev/null +++ b/apps/server/src/modules/deletion/client/builder/deletion-request-input.builder.ts @@ -0,0 +1,11 @@ +import { DeletionRequestInput } from '../interface'; +import { DeletionRequestTargetRefInputBuilder } from './deletion-request-target-ref-input.builder'; + +export class DeletionRequestInputBuilder { + static build(targetRefDomain: string, targetRefId: string, deleteInMinutes?: number): DeletionRequestInput { + return { + targetRef: DeletionRequestTargetRefInputBuilder.build(targetRefDomain, targetRefId), + deleteInMinutes, + }; + } +} diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.spec.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.spec.ts new file mode 100644 index 00000000000..399821f33ff --- /dev/null +++ b/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.spec.ts @@ -0,0 +1,29 @@ +import { ObjectId } from 'bson'; +import { DeletionRequestOutput } from '../interface'; +import { DeletionRequestOutputBuilder } from './deletion-request-output.builder'; + +describe(DeletionRequestOutputBuilder.name, () => { + describe(DeletionRequestOutputBuilder.build.name, () => { + describe('when called with proper arguments', () => { + const setup = () => { + const requestId = new ObjectId().toHexString(); + const deletionPlannedAt = new Date(); + + const expectedOutput: DeletionRequestOutput = { + requestId, + deletionPlannedAt, + }; + + return { requestId, deletionPlannedAt, expectedOutput }; + }; + + it('should return valid object with expected values', () => { + const { requestId, deletionPlannedAt, expectedOutput } = setup(); + + const output = DeletionRequestOutputBuilder.build(requestId, deletionPlannedAt); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.ts new file mode 100644 index 00000000000..9192c1a47ce --- /dev/null +++ b/apps/server/src/modules/deletion/client/builder/deletion-request-output.builder.ts @@ -0,0 +1,10 @@ +import { DeletionRequestOutput } from '../interface'; + +export class DeletionRequestOutputBuilder { + static build(requestId: string, deletionPlannedAt: Date): DeletionRequestOutput { + return { + requestId, + deletionPlannedAt, + }; + } +} diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.spec.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.spec.ts new file mode 100644 index 00000000000..74b0631e49d --- /dev/null +++ b/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.spec.ts @@ -0,0 +1,26 @@ +import { ObjectId } from 'bson'; +import { DeletionRequestTargetRefInput } from '../interface'; +import { DeletionRequestTargetRefInputBuilder } from './deletion-request-target-ref-input.builder'; + +describe(DeletionRequestTargetRefInputBuilder.name, () => { + describe(DeletionRequestTargetRefInputBuilder.build.name, () => { + describe('when called with proper arguments', () => { + const setup = () => { + const domain = 'user'; + const id = new ObjectId().toHexString(); + + const expectedOutput: DeletionRequestTargetRefInput = { domain, id }; + + return { domain, id, expectedOutput }; + }; + + it('should return valid object with expected values', () => { + const { domain, id, expectedOutput } = setup(); + + const output = DeletionRequestTargetRefInputBuilder.build(domain, id); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.ts b/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.ts new file mode 100644 index 00000000000..ed3c0219993 --- /dev/null +++ b/apps/server/src/modules/deletion/client/builder/deletion-request-target-ref-input.builder.ts @@ -0,0 +1,7 @@ +import { DeletionRequestTargetRefInput } from '../interface'; + +export class DeletionRequestTargetRefInputBuilder { + static build(domain: string, id: string): DeletionRequestTargetRefInput { + return { domain, id }; + } +} diff --git a/apps/server/src/modules/deletion/client/builder/index.ts b/apps/server/src/modules/deletion/client/builder/index.ts new file mode 100644 index 00000000000..85644a6b2ee --- /dev/null +++ b/apps/server/src/modules/deletion/client/builder/index.ts @@ -0,0 +1,3 @@ +export * from './deletion-request-target-ref-input.builder'; +export * from './deletion-request-input.builder'; +export * from './deletion-request-output.builder'; diff --git a/apps/server/src/modules/deletion/client/deletion-client.config.spec.ts b/apps/server/src/modules/deletion/client/deletion-client.config.spec.ts new file mode 100644 index 00000000000..a3cae21e425 --- /dev/null +++ b/apps/server/src/modules/deletion/client/deletion-client.config.spec.ts @@ -0,0 +1,41 @@ +import { IConfig } from '@hpi-schul-cloud/commons/lib/interfaces/IConfig'; +import { Configuration } from '@hpi-schul-cloud/commons/lib'; +import { DeletionClientConfig } from './interface'; +import { getDeletionClientConfig } from './deletion-client.config'; + +describe(getDeletionClientConfig.name, () => { + let configBefore: IConfig; + + beforeAll(() => { + configBefore = Configuration.toObject({ plainSecrets: true }); + }); + + afterEach(() => { + Configuration.reset(configBefore); + }); + + describe('when called', () => { + const setup = () => { + const baseUrl = 'http://api-admin:4030'; + const apiKey = '652559c2-93da-42ad-94e1-640e3afbaca0'; + + Configuration.set('ADMIN_API_CLIENT__BASE_URL', baseUrl); + Configuration.set('ADMIN_API_CLIENT__API_KEY', apiKey); + + const expectedConfig: DeletionClientConfig = { + ADMIN_API_CLIENT_BASE_URL: baseUrl, + ADMIN_API_CLIENT_API_KEY: apiKey, + }; + + return { expectedConfig }; + }; + + it('should return config with proper values', () => { + const { expectedConfig } = setup(); + + const config = getDeletionClientConfig(); + + expect(config).toEqual(expectedConfig); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/client/deletion-client.config.ts b/apps/server/src/modules/deletion/client/deletion-client.config.ts new file mode 100644 index 00000000000..db5bf7ff226 --- /dev/null +++ b/apps/server/src/modules/deletion/client/deletion-client.config.ts @@ -0,0 +1,9 @@ +import { Configuration } from '@hpi-schul-cloud/commons/lib'; +import { DeletionClientConfig } from './interface'; + +export const getDeletionClientConfig = (): DeletionClientConfig => { + return { + ADMIN_API_CLIENT_BASE_URL: Configuration.get('ADMIN_API_CLIENT__BASE_URL') as string, + ADMIN_API_CLIENT_API_KEY: Configuration.get('ADMIN_API_CLIENT__API_KEY') as string, + }; +}; diff --git a/apps/server/src/modules/deletion/client/deletion.client.spec.ts b/apps/server/src/modules/deletion/client/deletion.client.spec.ts new file mode 100644 index 00000000000..096b1f9b082 --- /dev/null +++ b/apps/server/src/modules/deletion/client/deletion.client.spec.ts @@ -0,0 +1,154 @@ +import { of } from 'rxjs'; +import { AxiosResponse } from 'axios'; +import { HttpService } from '@nestjs/axios'; +import { ConfigService } from '@nestjs/config'; +import { Test, TestingModule } from '@nestjs/testing'; +import { createMock, DeepMocked } from '@golevelup/ts-jest'; +import { axiosResponseFactory } from '@shared/testing'; +import { DeletionRequestInputBuilder, DeletionRequestOutputBuilder } from '.'; +import { DeletionRequestOutput } from './interface'; +import { DeletionClient } from './deletion.client'; + +describe(DeletionClient.name, () => { + let module: TestingModule; + let client: DeletionClient; + let httpService: DeepMocked; + + beforeEach(async () => { + module = await Test.createTestingModule({ + providers: [ + DeletionClient, + { + provide: ConfigService, + useValue: createMock({ + get: jest.fn((key: string) => { + if (key === 'ADMIN_API_CLIENT_BASE_URL') { + return 'http://localhost:4030'; + } + + // Default is for the Admin APIs API Key. + return '6b3df003-61e9-467c-9e6b-579634801896'; + }), + }), + }, + { + provide: HttpService, + useValue: createMock(), + }, + ], + }).compile(); + + client = module.get(DeletionClient); + httpService = module.get(HttpService); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + afterAll(async () => { + await module.close(); + }); + + describe('queueDeletionRequest', () => { + describe('when received valid response with expected HTTP status code', () => { + const setup = () => { + const input = DeletionRequestInputBuilder.build('user', '652f1625e9bc1a13bdaae48b'); + + const output: DeletionRequestOutput = DeletionRequestOutputBuilder.build( + '6536ce29b595d7c8e5faf200', + new Date('2024-10-15T12:42:50.521Z') + ); + + const response: AxiosResponse = axiosResponseFactory.build({ + data: output, + status: 202, + }); + + httpService.post.mockReturnValueOnce(of(response)); + + return { input, output }; + }; + + it('should return proper output', async () => { + const { input, output } = setup(); + + const result = await client.queueDeletionRequest(input); + + expect(result).toEqual(output); + }); + }); + + describe('when received invalid HTTP status code in a response', () => { + const setup = () => { + const input = DeletionRequestInputBuilder.build('user', '652f1625e9bc1a13bdaae48b'); + + const output: DeletionRequestOutput = DeletionRequestOutputBuilder.build('', new Date()); + + const response: AxiosResponse = axiosResponseFactory.build({ + data: output, + status: 200, + }); + + httpService.post.mockReturnValueOnce(of(response)); + + return { input }; + }; + + it('should throw an exception', async () => { + const { input } = setup(); + + await expect(client.queueDeletionRequest(input)).rejects.toThrow(Error); + }); + }); + + describe('when received no requestId in a response', () => { + const setup = () => { + const input = DeletionRequestInputBuilder.build('user', '652f1625e9bc1a13bdaae48b'); + + const output: DeletionRequestOutput = DeletionRequestOutputBuilder.build( + '', + new Date('2024-10-15T12:42:50.521Z') + ); + + const response: AxiosResponse = axiosResponseFactory.build({ + data: output, + status: 202, + }); + + httpService.post.mockReturnValueOnce(of(response)); + + return { input }; + }; + + it('should throw an exception', async () => { + const { input } = setup(); + + await expect(client.queueDeletionRequest(input)).rejects.toThrow(Error); + }); + }); + + describe('when received no deletionPlannedAt in a response', () => { + const setup = () => { + const input = DeletionRequestInputBuilder.build('user', '652f1625e9bc1a13bdaae48b'); + + const response: AxiosResponse = axiosResponseFactory.build({ + data: { + requestId: '6536ce29b595d7c8e5faf200', + }, + status: 202, + }); + + httpService.post.mockReturnValueOnce(of(response)); + + return { input }; + }; + + it('should throw an exception', async () => { + const { input } = setup(); + + await expect(client.queueDeletionRequest(input)).rejects.toThrow(Error); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/client/deletion.client.ts b/apps/server/src/modules/deletion/client/deletion.client.ts new file mode 100644 index 00000000000..66bb267d070 --- /dev/null +++ b/apps/server/src/modules/deletion/client/deletion.client.ts @@ -0,0 +1,66 @@ +import { firstValueFrom } from 'rxjs'; +import { AxiosResponse } from 'axios'; +import { Injectable } from '@nestjs/common'; +import { HttpService } from '@nestjs/axios'; +import { ConfigService } from '@nestjs/config'; +import { DeletionRequestInput, DeletionRequestOutput, DeletionClientConfig } from './interface'; + +@Injectable() +export class DeletionClient { + private readonly baseUrl: string; + + private readonly apiKey: string; + + private readonly postDeletionRequestsEndpoint: string; + + constructor( + private readonly httpService: HttpService, + private readonly configService: ConfigService + ) { + this.baseUrl = this.configService.get('ADMIN_API_CLIENT_BASE_URL'); + this.apiKey = this.configService.get('ADMIN_API_CLIENT_API_KEY'); + + // Prepare the POST /deletionRequests endpoint beforehand to not do it on every client call. + this.postDeletionRequestsEndpoint = new URL('/admin/api/v1/deletionRequests', this.baseUrl).toString(); + } + + async queueDeletionRequest(input: DeletionRequestInput): Promise { + const request = this.httpService.post(this.postDeletionRequestsEndpoint, input, this.defaultHeaders()); + + return firstValueFrom(request) + .then((resp: AxiosResponse) => { + // Throw an error if any other status code (other than expected "202 Accepted" is returned). + if (resp.status !== 202) { + throw new Error(`invalid HTTP status code in a response from the server - ${resp.status} instead of 202`); + } + + // Throw an error if server didn't return a requestId in a response (and it is + // required as it gives client the reference to the created deletion request). + if (!resp.data.requestId) { + throw new Error('no valid requestId returned from the server'); + } + + // Throw an error if server didn't return a deletionPlannedAt timestamp so the user + // will not be aware after which date the deletion request's execution will begin. + if (!resp.data.deletionPlannedAt) { + throw new Error('no valid deletionPlannedAt returned from the server'); + } + + return resp.data; + }) + .catch((err: Error) => { + // Throw an error if sending/processing deletion request by the client failed in any way. + throw new Error(`failed to send/process a deletion request: ${err.toString()}`); + }); + } + + private apiKeyHeader() { + return { 'X-Api-Key': this.apiKey }; + } + + private defaultHeaders() { + return { + headers: this.apiKeyHeader(), + }; + } +} diff --git a/apps/server/src/modules/deletion/client/index.ts b/apps/server/src/modules/deletion/client/index.ts new file mode 100644 index 00000000000..fde3db98f3b --- /dev/null +++ b/apps/server/src/modules/deletion/client/index.ts @@ -0,0 +1,3 @@ +export * from './interface'; +export * from './builder'; +export * from './deletion.client'; diff --git a/apps/server/src/modules/deletion/client/interface/deletion-client-config.interface.ts b/apps/server/src/modules/deletion/client/interface/deletion-client-config.interface.ts new file mode 100644 index 00000000000..8178515b6d4 --- /dev/null +++ b/apps/server/src/modules/deletion/client/interface/deletion-client-config.interface.ts @@ -0,0 +1,4 @@ +export interface DeletionClientConfig { + ADMIN_API_CLIENT_BASE_URL: string; + ADMIN_API_CLIENT_API_KEY: string; +} diff --git a/apps/server/src/modules/deletion/client/interface/deletion-request-input.interface.ts b/apps/server/src/modules/deletion/client/interface/deletion-request-input.interface.ts new file mode 100644 index 00000000000..4879ce4d972 --- /dev/null +++ b/apps/server/src/modules/deletion/client/interface/deletion-request-input.interface.ts @@ -0,0 +1,6 @@ +import { DeletionRequestTargetRefInput } from './deletion-request-target-ref-input.interface'; + +export interface DeletionRequestInput { + targetRef: DeletionRequestTargetRefInput; + deleteInMinutes?: number; +} diff --git a/apps/server/src/modules/deletion/client/interface/deletion-request-output.interface.ts b/apps/server/src/modules/deletion/client/interface/deletion-request-output.interface.ts new file mode 100644 index 00000000000..b61a372d5a5 --- /dev/null +++ b/apps/server/src/modules/deletion/client/interface/deletion-request-output.interface.ts @@ -0,0 +1,4 @@ +export interface DeletionRequestOutput { + requestId: string; + deletionPlannedAt: Date; +} diff --git a/apps/server/src/modules/deletion/client/interface/deletion-request-target-ref-input.interface.ts b/apps/server/src/modules/deletion/client/interface/deletion-request-target-ref-input.interface.ts new file mode 100644 index 00000000000..603bb0c13ec --- /dev/null +++ b/apps/server/src/modules/deletion/client/interface/deletion-request-target-ref-input.interface.ts @@ -0,0 +1,4 @@ +export interface DeletionRequestTargetRefInput { + domain: string; + id: string; +} diff --git a/apps/server/src/modules/deletion/client/interface/index.ts b/apps/server/src/modules/deletion/client/interface/index.ts new file mode 100644 index 00000000000..38f0f639731 --- /dev/null +++ b/apps/server/src/modules/deletion/client/interface/index.ts @@ -0,0 +1,4 @@ +export * from './deletion-client-config.interface'; +export * from './deletion-request-target-ref-input.interface'; +export * from './deletion-request-input.interface'; +export * from './deletion-request-output.interface'; diff --git a/apps/server/src/modules/deletion/console/builder/index.ts b/apps/server/src/modules/deletion/console/builder/index.ts new file mode 100644 index 00000000000..12fd0997ebe --- /dev/null +++ b/apps/server/src/modules/deletion/console/builder/index.ts @@ -0,0 +1 @@ +export * from './push-delete-requests-options.builder'; diff --git a/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.spec.ts b/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.spec.ts new file mode 100644 index 00000000000..5c83defdd1e --- /dev/null +++ b/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.spec.ts @@ -0,0 +1,43 @@ +import { PushDeletionRequestsOptions } from '../interface'; +import { PushDeleteRequestsOptionsBuilder } from './push-delete-requests-options.builder'; + +describe(PushDeleteRequestsOptionsBuilder.name, () => { + describe(PushDeleteRequestsOptionsBuilder.build.name, () => { + describe('when called with proper arguments', () => { + const setup = () => { + const refsFilePath = '/tmp/ids.txt'; + const targetRefDomain = 'school'; + const deleteInMinutes = 43200; + const callsDelayMs = 100; + + const expectedOutput: PushDeletionRequestsOptions = { + refsFilePath, + targetRefDomain, + deleteInMinutes, + callsDelayMs, + }; + + return { + refsFilePath, + targetRefDomain, + deleteInMinutes, + callsDelayMs, + expectedOutput, + }; + }; + + it('should return valid object with expected values', () => { + const { refsFilePath, targetRefDomain, deleteInMinutes, callsDelayMs, expectedOutput } = setup(); + + const output = PushDeleteRequestsOptionsBuilder.build( + refsFilePath, + targetRefDomain, + deleteInMinutes, + callsDelayMs + ); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.ts b/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.ts new file mode 100644 index 00000000000..f8ceae9263d --- /dev/null +++ b/apps/server/src/modules/deletion/console/builder/push-delete-requests-options.builder.ts @@ -0,0 +1,17 @@ +import { PushDeletionRequestsOptions } from '../interface'; + +export class PushDeleteRequestsOptionsBuilder { + static build( + refsFilePath: string, + targetRefDomain: string, + deleteInMinutes: number, + callsDelayMs: number + ): PushDeletionRequestsOptions { + return { + refsFilePath, + targetRefDomain, + deleteInMinutes, + callsDelayMs, + }; + } +} diff --git a/apps/server/src/modules/deletion/console/deletion-console.module.ts b/apps/server/src/modules/deletion/console/deletion-console.module.ts new file mode 100644 index 00000000000..0585b3631da --- /dev/null +++ b/apps/server/src/modules/deletion/console/deletion-console.module.ts @@ -0,0 +1,22 @@ +import { Module } from '@nestjs/common'; +import { HttpModule } from '@nestjs/axios'; +import { ConfigModule } from '@nestjs/config'; +import { ConsoleModule } from 'nestjs-console'; +import { ConsoleWriterModule } from '@infra/console'; +import { createConfigModuleOptions } from '@src/config'; +import { DeletionClient } from '../client'; +import { getDeletionClientConfig } from '../client/deletion-client.config'; +import { BatchDeletionService } from '../services'; +import { BatchDeletionUc } from '../uc'; +import { DeletionQueueConsole } from './deletion-queue.console'; + +@Module({ + imports: [ + ConsoleModule, + ConsoleWriterModule, + HttpModule, + ConfigModule.forRoot(createConfigModuleOptions(getDeletionClientConfig)), + ], + providers: [DeletionClient, BatchDeletionService, BatchDeletionUc, DeletionQueueConsole], +}) +export class DeletionConsoleModule {} diff --git a/apps/server/src/modules/deletion/console/deletion-queue.console.spec.ts b/apps/server/src/modules/deletion/console/deletion-queue.console.spec.ts new file mode 100644 index 00000000000..61f9cf0ff53 --- /dev/null +++ b/apps/server/src/modules/deletion/console/deletion-queue.console.spec.ts @@ -0,0 +1,79 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { ConsoleWriterService } from '@infra/console'; +import { createMock } from '@golevelup/ts-jest'; +import { BatchDeletionUc } from '../uc'; +import { DeletionQueueConsole } from './deletion-queue.console'; +import { PushDeleteRequestsOptionsBuilder } from './builder'; + +describe(DeletionQueueConsole.name, () => { + let module: TestingModule; + let console: DeletionQueueConsole; + let batchDeletionUc: BatchDeletionUc; + + beforeAll(async () => { + module = await Test.createTestingModule({ + providers: [ + DeletionQueueConsole, + { + provide: ConsoleWriterService, + useValue: createMock(), + }, + { + provide: BatchDeletionUc, + useValue: createMock(), + }, + ], + }).compile(); + + console = module.get(DeletionQueueConsole); + batchDeletionUc = module.get(BatchDeletionUc); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + afterAll(async () => { + await module.close(); + }); + + it('console should be defined', () => { + expect(console).toBeDefined(); + }); + + describe('pushDeletionRequests', () => { + describe('when called with valid options', () => { + const setup = () => { + const refsFilePath = '/tmp/ids.txt'; + const targetRefDomain = 'school'; + const deleteInMinutes = 43200; + const callsDelayMs = 100; + + const options = PushDeleteRequestsOptionsBuilder.build( + refsFilePath, + targetRefDomain, + deleteInMinutes, + callsDelayMs + ); + + return { + refsFilePath, + targetRefDomain, + deleteInMinutes, + callsDelayMs, + options, + }; + }; + + it(`should call ${BatchDeletionUc.name} with proper arguments`, async () => { + const { refsFilePath, targetRefDomain, deleteInMinutes, callsDelayMs, options } = setup(); + + const spy = jest.spyOn(batchDeletionUc, 'deleteRefsFromTxtFile'); + + await console.pushDeletionRequests(options); + + expect(spy).toBeCalledWith(refsFilePath, targetRefDomain, deleteInMinutes, callsDelayMs); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/console/deletion-queue.console.ts b/apps/server/src/modules/deletion/console/deletion-queue.console.ts new file mode 100644 index 00000000000..c8b133dba84 --- /dev/null +++ b/apps/server/src/modules/deletion/console/deletion-queue.console.ts @@ -0,0 +1,46 @@ +import { Console, Command } from 'nestjs-console'; +import { ConsoleWriterService } from '@infra/console'; +import { BatchDeletionUc } from '../uc'; +import { PushDeletionRequestsOptions } from './interface'; + +@Console({ command: 'queue', description: 'Console providing an access to the deletion queue.' }) +export class DeletionQueueConsole { + constructor(private consoleWriter: ConsoleWriterService, private batchDeletionUc: BatchDeletionUc) {} + + @Command({ + command: 'push', + description: 'Push new deletion requests to the deletion queue.', + options: [ + { + flags: '-rfp, --refsFilePath ', + description: 'Path of the file containing all the references to the data that should be deleted.', + required: true, + }, + { + flags: '-trd, --targetRefDomain ', + description: 'Name of the target ref domain.', + required: false, + }, + { + flags: '-dim, --deleteInMinutes ', + description: 'Number of minutes after which the data deletion process should begin.', + required: false, + }, + { + flags: '-cdm, --callsDelayMs ', + description: 'Delay between all the performed client calls, in milliseconds.', + required: false, + }, + ], + }) + async pushDeletionRequests(options: PushDeletionRequestsOptions): Promise { + const summary = await this.batchDeletionUc.deleteRefsFromTxtFile( + options.refsFilePath, + options.targetRefDomain, + options.deleteInMinutes ? Number(options.deleteInMinutes) : undefined, + options.callsDelayMs ? Number(options.callsDelayMs) : undefined + ); + + this.consoleWriter.info(JSON.stringify(summary)); + } +} diff --git a/apps/server/src/modules/deletion/console/index.ts b/apps/server/src/modules/deletion/console/index.ts new file mode 100644 index 00000000000..db47bd8c99f --- /dev/null +++ b/apps/server/src/modules/deletion/console/index.ts @@ -0,0 +1 @@ +export * from './deletion-console.module'; diff --git a/apps/server/src/modules/deletion/console/interface/index.ts b/apps/server/src/modules/deletion/console/interface/index.ts new file mode 100644 index 00000000000..2fcb281430f --- /dev/null +++ b/apps/server/src/modules/deletion/console/interface/index.ts @@ -0,0 +1 @@ +export * from './push-delete-requests-options.interface'; diff --git a/apps/server/src/modules/deletion/console/interface/push-delete-requests-options.interface.ts b/apps/server/src/modules/deletion/console/interface/push-delete-requests-options.interface.ts new file mode 100644 index 00000000000..a54f652cf94 --- /dev/null +++ b/apps/server/src/modules/deletion/console/interface/push-delete-requests-options.interface.ts @@ -0,0 +1,6 @@ +export interface PushDeletionRequestsOptions { + refsFilePath: string; + targetRefDomain: string; + deleteInMinutes: number; + callsDelayMs: number; +} diff --git a/apps/server/src/modules/deletion/index.ts b/apps/server/src/modules/deletion/index.ts index bd89c1e8d84..793f306e7a0 100644 --- a/apps/server/src/modules/deletion/index.ts +++ b/apps/server/src/modules/deletion/index.ts @@ -1,2 +1,4 @@ export * from './deletion.module'; export * from './services'; +export * from './client'; +export * from './console'; diff --git a/apps/server/src/modules/deletion/services/batch-deletion.service.spec.ts b/apps/server/src/modules/deletion/services/batch-deletion.service.spec.ts new file mode 100644 index 00000000000..640e290af1a --- /dev/null +++ b/apps/server/src/modules/deletion/services/batch-deletion.service.spec.ts @@ -0,0 +1,97 @@ +import { ObjectId } from 'bson'; +import { Test, TestingModule } from '@nestjs/testing'; +import { createMock, DeepMocked } from '@golevelup/ts-jest'; +import { DeletionClient, DeletionRequestOutput, DeletionRequestOutputBuilder } from '../client'; +import { QueueDeletionRequestInputBuilder, QueueDeletionRequestOutputBuilder } from './builder'; +import { BatchDeletionService } from './batch-deletion.service'; + +describe(BatchDeletionService.name, () => { + let module: TestingModule; + let service: BatchDeletionService; + let deletionClient: DeepMocked; + + beforeAll(async () => { + module = await Test.createTestingModule({ + providers: [ + BatchDeletionService, + { + provide: DeletionClient, + useValue: createMock(), + }, + ], + }).compile(); + + service = module.get(BatchDeletionService); + deletionClient = module.get(DeletionClient); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + afterAll(async () => { + await module.close(); + }); + + it('service should be defined', () => { + expect(service).toBeDefined(); + }); + + describe('queueDeletionRequests', () => { + describe('when called with valid inputs array and a requested delay between the client calls', () => { + describe("when client doesn't throw any error", () => { + const setup = () => { + const inputs = [QueueDeletionRequestInputBuilder.build('user', new ObjectId().toHexString(), 60)]; + + const requestId = new ObjectId().toHexString(); + const deletionPlannedAt = new Date(); + + const queueDeletionRequestOutput: DeletionRequestOutput = DeletionRequestOutputBuilder.build( + requestId, + deletionPlannedAt + ); + + deletionClient.queueDeletionRequest.mockResolvedValueOnce(queueDeletionRequestOutput); + + const expectedOutput = QueueDeletionRequestOutputBuilder.buildSuccess(requestId, deletionPlannedAt); + + const expectedOutputs = [expectedOutput]; + + return { inputs, expectedOutputs }; + }; + + it('should return an output object with successful status info', async () => { + const { inputs, expectedOutputs } = setup(); + + const outputs = await service.queueDeletionRequests(inputs, 1); + + expect(outputs).toStrictEqual(expectedOutputs); + }); + }); + + describe('when client throws an error', () => { + const setup = () => { + const inputs = [QueueDeletionRequestInputBuilder.build('user', new ObjectId().toHexString(), 60)]; + + const error = new Error('connection error'); + + deletionClient.queueDeletionRequest.mockRejectedValueOnce(error); + + const expectedOutput = QueueDeletionRequestOutputBuilder.buildError(error); + + const expectedOutputs = [expectedOutput]; + + return { inputs, expectedOutputs }; + }; + + it('should return an output object with failure status info', async () => { + const { inputs, expectedOutputs } = setup(); + + const outputs = await service.queueDeletionRequests(inputs); + + expect(outputs).toStrictEqual(expectedOutputs); + }); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/services/batch-deletion.service.ts b/apps/server/src/modules/deletion/services/batch-deletion.service.ts new file mode 100644 index 00000000000..d8c14f315a0 --- /dev/null +++ b/apps/server/src/modules/deletion/services/batch-deletion.service.ts @@ -0,0 +1,55 @@ +import { Injectable } from '@nestjs/common'; +import { QueueDeletionRequestOutputBuilder } from './builder'; +import { DeletionClient, DeletionRequestInputBuilder } from '../client'; +import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from './interface'; + +@Injectable() +export class BatchDeletionService { + constructor(private readonly deletionClient: DeletionClient) {} + + async queueDeletionRequests( + inputs: QueueDeletionRequestInput[], + callsDelayMilliseconds?: number + ): Promise { + const outputs: QueueDeletionRequestOutput[] = []; + + // For every provided deletion request input, try to queue it via deletion client. + // In any case, add the result of the trial to the outputs - it will be either a valid + // response in a form of a requestId + deletionPlannedAt values pair or some error + // returned from the client. In any case, every input should be processed. + for (const input of inputs) { + const deletionRequestInput = DeletionRequestInputBuilder.build( + input.targetRefDomain, + input.targetRefId, + input.deleteInMinutes + ); + + try { + // eslint-disable-next-line no-await-in-loop + const deletionRequestOutput = await this.deletionClient.queueDeletionRequest(deletionRequestInput); + + // In case of a successful client response, add the + // requestId + deletionPlannedAt values pair to the outputs. + outputs.push( + QueueDeletionRequestOutputBuilder.buildSuccess( + deletionRequestOutput.requestId, + deletionRequestOutput.deletionPlannedAt + ) + ); + } catch (err) { + // In case of a failure client response, add the full error message to the outputs. + outputs.push(QueueDeletionRequestOutputBuilder.buildError(err as Error)); + } + + // If any delay between the client calls has been requested, "sleep" for the specified amount of time. + if (callsDelayMilliseconds && callsDelayMilliseconds > 0) { + // eslint-disable-next-line no-await-in-loop + await new Promise((resolve) => { + setTimeout(resolve, callsDelayMilliseconds); + }); + } + } + + return outputs; + } +} diff --git a/apps/server/src/modules/deletion/services/builder/index.ts b/apps/server/src/modules/deletion/services/builder/index.ts new file mode 100644 index 00000000000..acd85a37989 --- /dev/null +++ b/apps/server/src/modules/deletion/services/builder/index.ts @@ -0,0 +1,2 @@ +export * from './queue-deletion-request-input.builder'; +export * from './queue-deletion-request-output.builder'; diff --git a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.spec.ts b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.spec.ts new file mode 100644 index 00000000000..e5d87858156 --- /dev/null +++ b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.spec.ts @@ -0,0 +1,27 @@ +import { ObjectId } from 'bson'; +import { QueueDeletionRequestInput } from '../interface'; +import { QueueDeletionRequestInputBuilder } from './queue-deletion-request-input.builder'; + +describe(QueueDeletionRequestInputBuilder.name, () => { + describe(QueueDeletionRequestInputBuilder.build.name, () => { + describe('when called with proper arguments', () => { + const setup = () => { + const targetRefDomain = 'user'; + const targetRefId = new ObjectId().toHexString(); + const deleteInMinutes = 60; + + const expectedOutput: QueueDeletionRequestInput = { targetRefDomain, targetRefId, deleteInMinutes }; + + return { targetRefDomain, targetRefId, deleteInMinutes, expectedOutput }; + }; + + it('should return valid object with expected values', () => { + const { targetRefDomain, targetRefId, deleteInMinutes, expectedOutput } = setup(); + + const output = QueueDeletionRequestInputBuilder.build(targetRefDomain, targetRefId, deleteInMinutes); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.ts b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.ts new file mode 100644 index 00000000000..a7fff2152b9 --- /dev/null +++ b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-input.builder.ts @@ -0,0 +1,7 @@ +import { QueueDeletionRequestInput } from '../interface'; + +export class QueueDeletionRequestInputBuilder { + static build(targetRefDomain: string, targetRefId: string, deleteInMinutes: number): QueueDeletionRequestInput { + return { targetRefDomain, targetRefId, deleteInMinutes }; + } +} diff --git a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.spec.ts b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.spec.ts new file mode 100644 index 00000000000..cd835a9cf4a --- /dev/null +++ b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.spec.ts @@ -0,0 +1,46 @@ +import { ObjectId } from 'bson'; +import { QueueDeletionRequestOutput } from '../interface'; +import { QueueDeletionRequestOutputBuilder } from './queue-deletion-request-output.builder'; + +describe(QueueDeletionRequestOutputBuilder.name, () => { + describe(QueueDeletionRequestOutputBuilder.buildSuccess.name, () => { + describe('when called with proper arguments', () => { + const setup = () => { + const requestId = new ObjectId().toHexString(); + const deletionPlannedAt = new Date(); + + const expectedOutput: QueueDeletionRequestOutput = { requestId, deletionPlannedAt }; + + return { requestId, deletionPlannedAt, expectedOutput }; + }; + + it('should return valid object with expected values', () => { + const { requestId, deletionPlannedAt, expectedOutput } = setup(); + + const output = QueueDeletionRequestOutputBuilder.buildSuccess(requestId, deletionPlannedAt); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + }); + + describe(QueueDeletionRequestOutputBuilder.buildError.name, () => { + describe('when called with proper arguments', () => { + const setup = () => { + const error = new Error('test error message'); + + const expectedOutput: QueueDeletionRequestOutput = { error: error.toString() }; + + return { error, expectedOutput }; + }; + + it('should return valid object with expected values', () => { + const { error, expectedOutput } = setup(); + + const output = QueueDeletionRequestOutputBuilder.buildError(error); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.ts b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.ts new file mode 100644 index 00000000000..c44e503cbaf --- /dev/null +++ b/apps/server/src/modules/deletion/services/builder/queue-deletion-request-output.builder.ts @@ -0,0 +1,29 @@ +import { QueueDeletionRequestOutput } from '../interface'; + +export class QueueDeletionRequestOutputBuilder { + private static build(requestId?: string, deletionPlannedAt?: Date, error?: string): QueueDeletionRequestOutput { + const output: QueueDeletionRequestOutput = {}; + + if (requestId) { + output.requestId = requestId; + } + + if (deletionPlannedAt) { + output.deletionPlannedAt = deletionPlannedAt; + } + + if (error) { + output.error = error.toString(); + } + + return output; + } + + static buildSuccess(requestId: string, deletionPlannedAt: Date): QueueDeletionRequestOutput { + return this.build(requestId, deletionPlannedAt); + } + + static buildError(err: Error): QueueDeletionRequestOutput { + return this.build(undefined, undefined, err.toString()); + } +} diff --git a/apps/server/src/modules/deletion/services/index.ts b/apps/server/src/modules/deletion/services/index.ts index 9661354718c..52e0b6ba22d 100644 --- a/apps/server/src/modules/deletion/services/index.ts +++ b/apps/server/src/modules/deletion/services/index.ts @@ -1 +1,5 @@ +export * from './interface'; +export * from './builder'; +export * from './references.service'; +export * from './batch-deletion.service'; export * from './deletion-request.service'; diff --git a/apps/server/src/modules/deletion/services/interface/index.ts b/apps/server/src/modules/deletion/services/interface/index.ts new file mode 100644 index 00000000000..8a455440798 --- /dev/null +++ b/apps/server/src/modules/deletion/services/interface/index.ts @@ -0,0 +1,2 @@ +export * from './queue-deletion-request-input.interface'; +export * from './queue-deletion-request-output.interface'; diff --git a/apps/server/src/modules/deletion/services/interface/queue-deletion-request-input.interface.ts b/apps/server/src/modules/deletion/services/interface/queue-deletion-request-input.interface.ts new file mode 100644 index 00000000000..b421943bce9 --- /dev/null +++ b/apps/server/src/modules/deletion/services/interface/queue-deletion-request-input.interface.ts @@ -0,0 +1,5 @@ +export interface QueueDeletionRequestInput { + targetRefDomain: string; + targetRefId: string; + deleteInMinutes: number; +} diff --git a/apps/server/src/modules/deletion/services/interface/queue-deletion-request-output.interface.ts b/apps/server/src/modules/deletion/services/interface/queue-deletion-request-output.interface.ts new file mode 100644 index 00000000000..375ff811857 --- /dev/null +++ b/apps/server/src/modules/deletion/services/interface/queue-deletion-request-output.interface.ts @@ -0,0 +1,5 @@ +export interface QueueDeletionRequestOutput { + requestId?: string; + deletionPlannedAt?: Date; + error?: string; +} diff --git a/apps/server/src/modules/deletion/services/references.service.spec.ts b/apps/server/src/modules/deletion/services/references.service.spec.ts new file mode 100644 index 00000000000..26eb6e0c8d7 --- /dev/null +++ b/apps/server/src/modules/deletion/services/references.service.spec.ts @@ -0,0 +1,74 @@ +import fs from 'fs'; +import { ReferencesService } from './references.service'; + +describe(ReferencesService.name, () => { + describe(ReferencesService.loadFromTxtFile.name, () => { + const setup = (mockedFileContent: string) => { + jest.spyOn(fs, 'readFileSync').mockReturnValueOnce(mockedFileContent); + }; + + describe('when passed a completely empty file (without any content)', () => { + it('should return an empty references array', () => { + setup(''); + + const references = ReferencesService.loadFromTxtFile('references.txt'); + + expect(references).toEqual([]); + }); + }); + + describe('when passed a file without any references (just some empty lines)', () => { + it('should return an empty references array', () => { + setup('\n\n \n \n\n\n \n\n\n'); + + const references = ReferencesService.loadFromTxtFile('references.txt'); + + expect(references).toEqual([]); + }); + }); + + describe('when passed a file with 3 references on a few separate lines', () => { + describe('split with CRs', () => { + it('should return an array with all the references present in a file', () => { + setup('653fd3b784ca851b17e98579\r653fd3b784ca851b17e9857a\r653fd3b784ca851b17e9857b\n\n\n'); + + const references = ReferencesService.loadFromTxtFile('references.txt'); + + expect(references).toEqual([ + '653fd3b784ca851b17e98579', + '653fd3b784ca851b17e9857a', + '653fd3b784ca851b17e9857b', + ]); + }); + }); + + describe('split with LFs', () => { + it('should return an array with all the references present in a file', () => { + setup('653fd3b784ca851b17e98579\n653fd3b784ca851b17e9857a\n653fd3b784ca851b17e9857b\n\n\n'); + + const references = ReferencesService.loadFromTxtFile('references.txt'); + + expect(references).toEqual([ + '653fd3b784ca851b17e98579', + '653fd3b784ca851b17e9857a', + '653fd3b784ca851b17e9857b', + ]); + }); + }); + + describe('split with CRLFs', () => { + it('should return an array with all the references present in a file', () => { + setup('653fd3b784ca851b17e98579\r\n653fd3b784ca851b17e9857a\r\n653fd3b784ca851b17e9857b\r\n\r\n\r\n'); + + const references = ReferencesService.loadFromTxtFile('references.txt'); + + expect(references).toEqual([ + '653fd3b784ca851b17e98579', + '653fd3b784ca851b17e9857a', + '653fd3b784ca851b17e9857b', + ]); + }); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/services/references.service.ts b/apps/server/src/modules/deletion/services/references.service.ts new file mode 100644 index 00000000000..991c36c02af --- /dev/null +++ b/apps/server/src/modules/deletion/services/references.service.ts @@ -0,0 +1,27 @@ +import fs from 'fs'; + +export class ReferencesService { + static loadFromTxtFile(filePath: string): string[] { + let fileContent = fs.readFileSync(filePath).toString(); + + // Replace all the CRLF occurrences with just a LF. + fileContent = fileContent.replace(/\r\n?/g, '\n'); + + // Split the whole file content by a line feed (LF) char (\n). + const fileLines = fileContent.split('\n'); + + const references: string[] = []; + + // Iterate over all the file lines and if it contains a valid id (which is + // basically any non-empty string), add it to the loaded references array. + fileLines.forEach((fileLine) => { + const reference = fileLine.trim(); + + if (reference && reference.length > 0) { + references.push(reference); + } + }); + + return references; + } +} diff --git a/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts b/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts new file mode 100644 index 00000000000..7292f36efb0 --- /dev/null +++ b/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts @@ -0,0 +1,195 @@ +import { ObjectId } from 'bson'; +import { Test, TestingModule } from '@nestjs/testing'; +import { createMock, DeepMocked } from '@golevelup/ts-jest'; +import { + BatchDeletionService, + QueueDeletionRequestInputBuilder, + QueueDeletionRequestOutput, + QueueDeletionRequestOutputBuilder, + ReferencesService, +} from '../services'; +import { BatchDeletionSummaryDetail, BatchDeletionSummaryOverallStatus } from './interface'; +import { BatchDeletionSummaryDetailBuilder } from './builder'; +import { BatchDeletionUc } from './batch-deletion.uc'; + +describe(BatchDeletionUc.name, () => { + let module: TestingModule; + let uc: BatchDeletionUc; + let batchDeletionService: DeepMocked; + + beforeAll(async () => { + module = await Test.createTestingModule({ + providers: [ + BatchDeletionUc, + { + provide: BatchDeletionService, + useValue: createMock(), + }, + ], + }).compile(); + + uc = module.get(BatchDeletionUc); + batchDeletionService = module.get(BatchDeletionService); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + afterAll(async () => { + await module.close(); + }); + + it('uc should be defined', () => { + expect(uc).toBeDefined(); + }); + + describe('deleteRefsFromTxtFile', () => { + describe('when called with valid arguments', () => { + describe('when batch deletion service returns an expected amount of outputs', () => { + describe('when only successful executions took place', () => { + const setup = () => { + const targetRefsCount = 3; + + const targetRefIds: string[] = []; + const outputs: QueueDeletionRequestOutput[] = []; + + for (let i = 0; i < targetRefsCount; i += 1) { + targetRefIds.push(new ObjectId().toHexString()); + outputs.push(QueueDeletionRequestOutputBuilder.buildSuccess(new ObjectId().toHexString(), new Date())); + } + + ReferencesService.loadFromTxtFile = jest.fn().mockReturnValueOnce(targetRefIds); + + batchDeletionService.queueDeletionRequests.mockResolvedValueOnce([outputs[0], outputs[1], outputs[2]]); + + const targetRefDomain = 'school'; + const deleteInMinutes = 60; + + const expectedSummaryFieldsDetails: BatchDeletionSummaryDetail[] = []; + + for (let i = 0; i < targetRefIds.length; i += 1) { + expectedSummaryFieldsDetails.push( + BatchDeletionSummaryDetailBuilder.build( + QueueDeletionRequestInputBuilder.build(targetRefDomain, targetRefIds[i], deleteInMinutes), + outputs[i] + ) + ); + } + + const expectedSummaryFields = { + overallStatus: BatchDeletionSummaryOverallStatus.SUCCESS, + successCount: 3, + failureCount: 0, + details: expectedSummaryFieldsDetails, + }; + + const refsFilePath = '/tmp/ids.txt'; + + return { refsFilePath, targetRefDomain, deleteInMinutes, expectedSummaryFields }; + }; + + it('should return proper summary with all the successes and a successful overall status', async () => { + const { refsFilePath, targetRefDomain, deleteInMinutes, expectedSummaryFields } = setup(); + + const summary = await uc.deleteRefsFromTxtFile(refsFilePath, targetRefDomain, deleteInMinutes); + + expect(summary.executionTimeMilliseconds).toBeGreaterThan(0); + expect(summary).toMatchObject(expectedSummaryFields); + }); + }); + + describe('when both successful and failed executions took place', () => { + const setup = () => { + const targetRefsCount = 3; + + const targetRefIds: string[] = []; + + for (let i = 0; i < targetRefsCount; i += 1) { + targetRefIds.push(new ObjectId().toHexString()); + } + + const targetRefDomain = 'school'; + const deleteInMinutes = 60; + + ReferencesService.loadFromTxtFile = jest.fn().mockReturnValueOnce(targetRefIds); + + const outputs = [ + QueueDeletionRequestOutputBuilder.buildSuccess(new ObjectId().toHexString(), new Date()), + QueueDeletionRequestOutputBuilder.buildError(new Error('some error occurred...')), + QueueDeletionRequestOutputBuilder.buildSuccess(new ObjectId().toHexString(), new Date()), + ]; + + batchDeletionService.queueDeletionRequests.mockResolvedValueOnce([outputs[0], outputs[1], outputs[2]]); + + const expectedSummaryFieldsDetails: BatchDeletionSummaryDetail[] = []; + + for (let i = 0; i < targetRefIds.length; i += 1) { + expectedSummaryFieldsDetails.push( + BatchDeletionSummaryDetailBuilder.build( + QueueDeletionRequestInputBuilder.build(targetRefDomain, targetRefIds[i], deleteInMinutes), + outputs[i] + ) + ); + } + + const expectedSummaryFields = { + overallStatus: BatchDeletionSummaryOverallStatus.FAILURE, + successCount: 2, + failureCount: 1, + details: expectedSummaryFieldsDetails, + }; + + const refsFilePath = '/tmp/ids.txt'; + + return { refsFilePath, targetRefDomain, deleteInMinutes, expectedSummaryFields }; + }; + + it('should return proper summary with all the successes and failures', async () => { + const { refsFilePath, targetRefDomain, deleteInMinutes, expectedSummaryFields } = setup(); + + const summary = await uc.deleteRefsFromTxtFile(refsFilePath, targetRefDomain, deleteInMinutes); + + expect(summary.executionTimeMilliseconds).toBeGreaterThan(0); + expect(summary).toMatchObject(expectedSummaryFields); + }); + }); + }); + + describe('when batch deletion service returns an invalid amount of outputs', () => { + const setup = () => { + const targetRefsCount = 3; + + const targetRefIds: string[] = []; + + for (let i = 0; i < targetRefsCount; i += 1) { + targetRefIds.push(new ObjectId().toHexString()); + } + + ReferencesService.loadFromTxtFile = jest.fn().mockReturnValueOnce(targetRefIds); + + const outputs: QueueDeletionRequestOutput[] = []; + + for (let i = 0; i < targetRefsCount - 1; i += 1) { + targetRefIds.push(new ObjectId().toHexString()); + outputs.push(QueueDeletionRequestOutputBuilder.buildSuccess(new ObjectId().toHexString(), new Date())); + } + + batchDeletionService.queueDeletionRequests.mockResolvedValueOnce(outputs); + + const refsFilePath = '/tmp/ids.txt'; + + return { refsFilePath }; + }; + + it('should throw an error', async () => { + const { refsFilePath } = setup(); + + const func = () => uc.deleteRefsFromTxtFile(refsFilePath); + + await expect(func()).rejects.toThrow(); + }); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts b/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts new file mode 100644 index 00000000000..258b1b53f65 --- /dev/null +++ b/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts @@ -0,0 +1,71 @@ +import { Injectable } from '@nestjs/common'; +import { BatchDeletionSummaryBuilder, BatchDeletionSummaryDetailBuilder } from './builder'; +import { + ReferencesService, + BatchDeletionService, + QueueDeletionRequestInput, + QueueDeletionRequestInputBuilder, +} from '../services'; +import { BatchDeletionSummary, BatchDeletionSummaryOverallStatus } from './interface'; + +@Injectable() +export class BatchDeletionUc { + constructor(private readonly batchDeletionService: BatchDeletionService) {} + + async deleteRefsFromTxtFile( + refsFilePath: string, + targetRefDomain = 'user', + deleteInMinutes = 43200, // 43200 minutes = 720 hours = 30 days + callsDelayMilliseconds?: number + ): Promise { + // First, load all the references from the provided text file (with given path). + const refsFromTxtFile = ReferencesService.loadFromTxtFile(refsFilePath); + + const inputs: QueueDeletionRequestInput[] = []; + + // For each reference found in a given file, add it to the inputs + // array (with added targetRefDomain and deleteInMinutes fields). + refsFromTxtFile.forEach((ref) => + inputs.push(QueueDeletionRequestInputBuilder.build(targetRefDomain, ref, deleteInMinutes)) + ); + + // Measure the overall queueing execution time by setting the start... + const startTime = performance.now(); + + const outputs = await this.batchDeletionService.queueDeletionRequests(inputs, callsDelayMilliseconds); + + // ...and end timestamps before and after the batch deletion service method execution. + const endTime = performance.now(); + + // Throw an error if the returned outputs number doesn't match the returned inputs number. + if (outputs.length !== inputs.length) { + throw new Error( + 'invalid result from the batch deletion service - expected to ' + + 'receive the same amount of outputs as the provided inputs, ' + + `instead received ${outputs.length} outputs for ${inputs.length} inputs` + ); + } + + const summary: BatchDeletionSummary = BatchDeletionSummaryBuilder.build(endTime - startTime); + + // Go through every received output and, in case of an error presence increase + // a failure count or, in case of no error, increase a success count. + for (let i = 0; i < outputs.length; i += 1) { + if (outputs[i].error) { + summary.failureCount += 1; + } else { + summary.successCount += 1; + } + + // Also add all the processed inputs and outputs details to the overall summary. + summary.details.push(BatchDeletionSummaryDetailBuilder.build(inputs[i], outputs[i])); + } + + // If no failure has been spotted, assume an overall success. + if (summary.failureCount === 0) { + summary.overallStatus = BatchDeletionSummaryOverallStatus.SUCCESS; + } + + return summary; + } +} diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts new file mode 100644 index 00000000000..43ea82e86d5 --- /dev/null +++ b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts @@ -0,0 +1,69 @@ +import { ObjectId } from '@mikro-orm/mongodb'; +import { BatchDeletionSummaryDetail } from '..'; +import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../../services'; +import { BatchDeletionSummaryDetailBuilder } from './batch-deletion-summary-detail.builder'; + +describe(BatchDeletionSummaryDetailBuilder.name, () => { + describe(BatchDeletionSummaryDetailBuilder.build.name, () => { + describe('when called with proper arguments for', () => { + describe('a successful output case', () => { + const setup = () => { + const deletionRequestInput: QueueDeletionRequestInput = { + targetRefDomain: 'user', + targetRefId: new ObjectId().toHexString(), + deleteInMinutes: 1440, + }; + + const deletionRequestOutput: QueueDeletionRequestOutput = { + requestId: new ObjectId().toHexString(), + deletionPlannedAt: new Date(), + }; + + const expectedOutput: BatchDeletionSummaryDetail = { + input: deletionRequestInput, + output: deletionRequestOutput, + }; + + return { deletionRequestInput, deletionRequestOutput, expectedOutput }; + }; + + it('should return valid object with expected values', () => { + const { deletionRequestInput, deletionRequestOutput, expectedOutput } = setup(); + + const output = BatchDeletionSummaryDetailBuilder.build(deletionRequestInput, deletionRequestOutput); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + + describe('an error output case', () => { + const setup = () => { + const deletionRequestInput: QueueDeletionRequestInput = { + targetRefDomain: 'user', + targetRefId: new ObjectId().toHexString(), + deleteInMinutes: 1440, + }; + + const deletionRequestOutput: QueueDeletionRequestOutput = { + error: 'some error occurred...', + }; + + const expectedOutput: BatchDeletionSummaryDetail = { + input: deletionRequestInput, + output: deletionRequestOutput, + }; + + return { deletionRequestInput, deletionRequestOutput, expectedOutput }; + }; + + it('should return valid object with expected values', () => { + const { deletionRequestInput, deletionRequestOutput, expectedOutput } = setup(); + + const output = BatchDeletionSummaryDetailBuilder.build(deletionRequestInput, deletionRequestOutput); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts new file mode 100644 index 00000000000..9ebbce66171 --- /dev/null +++ b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts @@ -0,0 +1,8 @@ +import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../../services'; +import { BatchDeletionSummaryDetail } from '../interface'; + +export class BatchDeletionSummaryDetailBuilder { + static build(input: QueueDeletionRequestInput, output: QueueDeletionRequestOutput): BatchDeletionSummaryDetail { + return { input, output }; + } +} diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts new file mode 100644 index 00000000000..a2b534602d4 --- /dev/null +++ b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts @@ -0,0 +1,30 @@ +import { BatchDeletionSummary, BatchDeletionSummaryOverallStatus } from '../interface'; +import { BatchDeletionSummaryBuilder } from './batch-deletion-summary.builder'; + +describe(BatchDeletionSummaryBuilder.name, () => { + describe(BatchDeletionSummaryBuilder.build.name, () => { + describe('when called with proper arguments', () => { + const setup = () => { + const executionTimeMilliseconds = 142; + + const expectedOutput: BatchDeletionSummary = { + executionTimeMilliseconds: 142, + overallStatus: BatchDeletionSummaryOverallStatus.FAILURE, + successCount: 0, + failureCount: 0, + details: [], + }; + + return { executionTimeMilliseconds, expectedOutput }; + }; + + it('should return valid object with expected values', () => { + const { executionTimeMilliseconds, expectedOutput } = setup(); + + const output = BatchDeletionSummaryBuilder.build(executionTimeMilliseconds); + + expect(output).toStrictEqual(expectedOutput); + }); + }); + }); +}); diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts new file mode 100644 index 00000000000..57fa2bcccd9 --- /dev/null +++ b/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts @@ -0,0 +1,13 @@ +import { BatchDeletionSummary, BatchDeletionSummaryOverallStatus } from '../interface'; + +export class BatchDeletionSummaryBuilder { + static build(executionTimeMilliseconds: number): BatchDeletionSummary { + return { + executionTimeMilliseconds, + overallStatus: BatchDeletionSummaryOverallStatus.FAILURE, + successCount: 0, + failureCount: 0, + details: [], + }; + } +} diff --git a/apps/server/src/modules/deletion/uc/builder/index.ts b/apps/server/src/modules/deletion/uc/builder/index.ts index fb8a2a5e7ec..c7ac430fa63 100644 --- a/apps/server/src/modules/deletion/uc/builder/index.ts +++ b/apps/server/src/modules/deletion/uc/builder/index.ts @@ -2,3 +2,5 @@ export * from './deletion-log-statistic.builder'; export * from './deletion-request-body-props.builder'; export * from './deletion-request-log-response.builder'; export * from './deletion-target-ref.builder'; +export * from './batch-deletion-summary-detail.builder'; +export * from './batch-deletion-summary.builder'; diff --git a/apps/server/src/modules/deletion/uc/index.ts b/apps/server/src/modules/deletion/uc/index.ts index f83bf1549c8..3260e898ae6 100644 --- a/apps/server/src/modules/deletion/uc/index.ts +++ b/apps/server/src/modules/deletion/uc/index.ts @@ -1,3 +1,4 @@ export * from './deletion-request.uc'; export * from './builder'; export * from './interface'; +export * from './batch-deletion.uc'; diff --git a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts b/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts new file mode 100644 index 00000000000..4fe99c13fad --- /dev/null +++ b/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts @@ -0,0 +1,6 @@ +import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../../services'; + +export interface BatchDeletionSummaryDetail { + input: QueueDeletionRequestInput; + output: QueueDeletionRequestOutput; +} diff --git a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts b/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts new file mode 100644 index 00000000000..4ae91bdf70e --- /dev/null +++ b/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts @@ -0,0 +1,4 @@ +export const enum BatchDeletionSummaryOverallStatus { + SUCCESS = 'success', + FAILURE = 'failure', +} diff --git a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts b/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts new file mode 100644 index 00000000000..ce633e164f1 --- /dev/null +++ b/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts @@ -0,0 +1,9 @@ +import { BatchDeletionSummaryDetail } from './batch-deletion-summary-detail.interface'; + +export interface BatchDeletionSummary { + executionTimeMilliseconds: number; + overallStatus: string; + successCount: number; + failureCount: number; + details: BatchDeletionSummaryDetail[]; +} diff --git a/apps/server/src/modules/deletion/uc/interface/index.ts b/apps/server/src/modules/deletion/uc/interface/index.ts index 95786098275..9dcf644f410 100644 --- a/apps/server/src/modules/deletion/uc/interface/index.ts +++ b/apps/server/src/modules/deletion/uc/interface/index.ts @@ -1 +1,4 @@ +export * from './batch-deletion-summary-overall-status.enum'; +export * from './batch-deletion-summary-detail.interface'; +export * from './batch-deletion-summary.interface'; export * from './interfaces'; diff --git a/config/default.schema.json b/config/default.schema.json index 9a7c75445ee..903aac324ca 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -1366,6 +1366,23 @@ "description": "Number of simultaneously synchronized students, teachers and classes" } } + }, + "ADMIN_API_CLIENT": { + "type": "object", + "description": "Configuration of the schulcloud-server's admin API client.", + "properties": { + "BASE_URL": { + "type": "string", + "description": "Base URL of the Admin API." + }, + "API_KEY": { + "type": "string", + "description": "API key for accessing the Admin API." + } + }, + "default": { + "BASE_URL": "http://localhost:4030" + } } }, "required": [], diff --git a/nest-cli.json b/nest-cli.json index 73dea03c093..8ce5461bb6f 100644 --- a/nest-cli.json +++ b/nest-cli.json @@ -45,6 +45,15 @@ "tsConfigPath": "apps/server/tsconfig.app.json" } }, + "deletion-console": { + "type": "application", + "root": "apps/server", + "entryFile": "apps/deletion-console.app", + "sourceRoot": "apps/server/src", + "compilerOptions": { + "tsConfigPath": "apps/server/tsconfig.app.json" + } + }, "files-storage": { "type": "application", "root": "apps/server", diff --git a/package.json b/package.json index 057125e9756..7b85512165b 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,9 @@ "nest:start:console": "nest start console --", "nest:start:console:dev": "nest start console --watch --", "nest:start:console:debug": "nest start console --debug --watch --", + "nest:start:deletion-console": "nest start deletion-console --", + "nest:start:deletion-console:dev": "nest start deletion-console --watch --", + "nest:start:deletion-console:debug": "nest start deletion-console --debug --watch --", "nest:test": "npm run nest:test:cov && npm run nest:lint", "nest:test:all": "jest", "nest:test:unit": "jest \"^((?!\\.api\\.spec\\.ts).)*\\.spec\\.ts$\"", From 8b6243e5c8cf741fedf900a6315cbc54aae7b98c Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Thu, 16 Nov 2023 10:39:14 +0100 Subject: [PATCH 42/96] default ADMIN_API_KEY --- config/default.schema.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/default.schema.json b/config/default.schema.json index 903aac324ca..0bd469b3cd4 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -1381,7 +1381,8 @@ } }, "default": { - "BASE_URL": "http://localhost:4030" + "BASE_URL": "http://localhost:4030", + "API_KEY": "" } } }, From 2ffb06a721dee5d8a42111c1560c3ecb6dacaccc Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Thu, 16 Nov 2023 11:01:57 +0100 Subject: [PATCH 43/96] fix some imports --- .../controller/api-test/deletion-executions.api.spec.ts | 2 +- .../controller/api-test/deletion-request-create.api.spec.ts | 2 +- .../controller/api-test/deletion-request-delete.api.spec.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts index 83a784fb4cf..19638c7eac3 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts @@ -4,7 +4,7 @@ import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; -import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; +import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; import { DeletionExecutionParams } from '../dto'; const baseRouteName = '/deletionExecutions'; diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index 2f21c4ce85e..e5c06bfe5b9 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -4,8 +4,8 @@ import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; -import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; import { EntityManager } from '@mikro-orm/mongodb'; +import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; import { DeletionRequestBodyProps, DeletionRequestResponse } from '../dto'; import { DeletionDomainModel } from '../../domain/types'; import { DeletionRequestEntity } from '../../entity'; diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts index aa1779cb9ce..cc86039dc07 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts @@ -4,10 +4,10 @@ import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; -import { AdminApiServerTestModule } from '@src/modules/server/admin-api.server.module'; import { cleanupCollections } from '@shared/testing'; import { EntityManager } from '@mikro-orm/mongodb'; -import { deletionRequestEntityFactory } from '../../entity/testing/factory/deletion-request.entity.factory'; +import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; +import { deletionRequestEntityFactory } from '../../entity/testing'; import { DeletionRequestEntity } from '../../entity'; const baseRouteName = '/deletionRequests'; From 4bbf1492bbb1258cd86a8c2ead3f7f0f7ed935fc Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Thu, 16 Nov 2023 12:14:29 +0100 Subject: [PATCH 44/96] small change in x-api-key.strategy --- .../src/modules/authentication/strategy/x-api-key.strategy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts index 125973de38f..f1f23e069ed 100644 --- a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.ts @@ -9,7 +9,7 @@ export class XApiKeyStrategy extends PassportStrategy(Strategy, 'api-key') { private readonly allowedApiKeys: string[]; constructor(private readonly configService: ConfigService) { - super({ header: 'X-API-KEY' }, true); + super({ header: 'X-API-KEY' }, false); this.allowedApiKeys = this.configService.get('ADMIN_API__ALLOWED_API_KEYS'); } From 01880966313e3eed3d12c6c9b9b41bdd6e0bf182 Mon Sep 17 00:00:00 2001 From: WojciechGrancow <116577704+WojciechGrancow@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:37:46 +0100 Subject: [PATCH 45/96] Update apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts Co-authored-by: Bartosz Nowicki <116367402+bn-pass@users.noreply.github.com> --- .../deletion/uc/builder/deletion-log-statistic.builder.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts index b92be19216a..3bbdc108ab4 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts +++ b/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts @@ -1,5 +1,5 @@ import { DeletionDomainModel } from '../../domain/types'; -import { DeletionLogStatisticBuilder } from './index'; +import { DeletionLogStatisticBuilder } from '.'; describe(DeletionLogStatisticBuilder.name, () => { afterAll(() => { From 2e07dc345306a249abb2cbfed912d2325b0de723 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 16 Nov 2023 15:12:35 +0100 Subject: [PATCH 46/96] change default.schema.json --- config/default.schema.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/config/default.schema.json b/config/default.schema.json index 0bd469b3cd4..c225a79b7b7 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -1273,21 +1273,19 @@ "properties": { "ENABLED": { "type": "boolean", + "default": true, "description": "Flag to turn on/off the Admin API." }, "PORT": { "type": "number", + "default": 4030, "description": "Port of the exposed Admin API server." }, "ALLOWED_API_KEYS": { "type": "string", + "default":"", "description": "Allowed Admin API keys (for accessing the Admin API)." } - }, - "default": { - "ENABLED": true, - "PORT": 4030, - "ALLOWED_API_KEYS": "" } }, "FEATURE_NEW_SCHOOL_ADMINISTRATION_PAGE_AS_DEFAULT_ENABLED": { From 3826319346c615c6af16b54dccef01361f2d8600 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 16 Nov 2023 15:18:12 +0100 Subject: [PATCH 47/96] new changes to default.schema.json --- config/default.schema.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/config/default.schema.json b/config/default.schema.json index c225a79b7b7..f61c0e66caf 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -1283,9 +1283,14 @@ }, "ALLOWED_API_KEYS": { "type": "string", - "default":"", + "default": "", "description": "Allowed Admin API keys (for accessing the Admin API)." } + }, + "default": { + "ENABLED": true, + "PORT": 4030, + "ALLOWED_API_KEYS": "" } }, "FEATURE_NEW_SCHOOL_ADMINISTRATION_PAGE_AS_DEFAULT_ENABLED": { From a0046bc3151d8cd2d643f9d16ed5c4c4eaf1f713 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Thu, 16 Nov 2023 23:35:05 +0100 Subject: [PATCH 48/96] fix after review --- .../deletion/uc/deletion-request.uc.ts | 36 +++++++++++++++++-- .../modules/server/admin-api.server.module.ts | 1 + .../src/shared/domain/entity/all-entities.ts | 2 ++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index 51a79aaab48..780e6228db0 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -10,6 +10,7 @@ import { FilesService } from '@modules/files/service'; import { AccountService } from '@modules/account/services'; import { RocketChatUserService } from '@modules/rocketchat-user'; import { RocketChatService } from '@modules/rocketchat'; +import { LegacyLogger } from '@src/core/logger'; import { DeletionRequestService } from '../services/deletion-request.service'; import { DeletionDomainModel, DeletionOperationModel, DeletionStatusModel } from '../domain/types'; import { DeletionLogService } from '../services/deletion-log.service'; @@ -34,10 +35,15 @@ export class DeletionRequestUc { private readonly teamService: TeamService, private readonly userService: UserService, private readonly rocketChatUserService: RocketChatUserService, - private readonly rocketChatService: RocketChatService - ) {} + private readonly rocketChatService: RocketChatService, + private readonly logger: LegacyLogger + ) { + this.logger.setContext(DeletionRequestUc.name); + } async createDeletionRequest(deletionRequest: DeletionRequestBodyProps): Promise { + this.logger.debug({ action: 'createDeletionRequest', deletionRequest }); + const result = await this.deletionRequestService.createDeletionRequest( deletionRequest.targetRef.id, deletionRequest.targetRef.domain, @@ -48,6 +54,8 @@ export class DeletionRequestUc { } async executeDeletionRequests(limit?: number): Promise { + this.logger.debug({ action: 'executeDeletionRequests', limit }); + const deletionRequestToExecution: DeletionRequest[] = await this.deletionRequestService.findAllItemsToExecute( limit ); @@ -59,6 +67,8 @@ export class DeletionRequestUc { } async findById(deletionRequestId: EntityId): Promise { + this.logger.debug({ action: 'deletionRequestId', deletionRequestId }); + const deletionRequest: DeletionRequest = await this.deletionRequestService.findById(deletionRequestId); let response: DeletionRequestLogResponse = DeletionRequestLogResponseBuilder.build( DeletionTargetRefBuilder.build(deletionRequest.targetRefDomain, deletionRequest.targetRefId), @@ -77,6 +87,8 @@ export class DeletionRequestUc { } async deleteDeletionRequestById(deletionRequestId: EntityId): Promise { + this.logger.debug({ action: 'deleteDeletionRequestById', deletionRequestId }); + await this.deletionRequestService.deleteById(deletionRequestId); } @@ -119,11 +131,15 @@ export class DeletionRequestUc { } private async removeAccount(deletionRequest: DeletionRequest) { + this.logger.debug({ action: 'removeAccount', deletionRequest }); + await this.accountService.deleteByUserId(deletionRequest.targetRefId); await this.logDeletion(deletionRequest, DeletionDomainModel.ACCOUNT, DeletionOperationModel.DELETE, 0, 1); } private async removeUserFromClasses(deletionRequest: DeletionRequest) { + this.logger.debug({ action: 'removeUserFromClasses', deletionRequest }); + const classesUpdated: number = await this.classService.deleteUserDataFromClasses(deletionRequest.targetRefId); await this.logDeletion( deletionRequest, @@ -135,6 +151,8 @@ export class DeletionRequestUc { } private async removeUserFromCourseGroup(deletionRequest: DeletionRequest) { + this.logger.debug({ action: 'removeUserFromCourseGroup', deletionRequest }); + const courseGroupUpdated: number = await this.courseGroupService.deleteUserDataFromCourseGroup( deletionRequest.targetRefId ); @@ -148,6 +166,8 @@ export class DeletionRequestUc { } private async removeUserFromCourse(deletionRequest: DeletionRequest) { + this.logger.debug({ action: 'removeUserFromCourse', deletionRequest }); + const courseUpdated: number = await this.courseService.deleteUserDataFromCourse(deletionRequest.targetRefId); await this.logDeletion( deletionRequest, @@ -159,6 +179,8 @@ export class DeletionRequestUc { } private async removeUsersFilesAndPermissions(deletionRequest: DeletionRequest) { + this.logger.debug({ action: 'removeUsersFilesAndPermissions', deletionRequest }); + const filesDeleted: number = await this.filesService.markFilesOwnedByUserForDeletion(deletionRequest.targetRefId); const filePermissionsUpdated: number = await this.filesService.removeUserPermissionsToAnyFiles( deletionRequest.targetRefId @@ -173,6 +195,8 @@ export class DeletionRequestUc { } private async removeUserFromLessons(deletionRequest: DeletionRequest) { + this.logger.debug({ action: 'removeUserFromLessons', deletionRequest }); + const lessonsUpdated: number = await this.lessonService.deleteUserDataFromLessons(deletionRequest.targetRefId); await this.logDeletion( deletionRequest, @@ -184,6 +208,8 @@ export class DeletionRequestUc { } private async removeUsersPseudonyms(deletionRequest: DeletionRequest) { + this.logger.debug({ action: 'removeUsersPseudonyms', deletionRequest }); + const pseudonymDeleted: number = await this.pseudonymService.deleteByUserId(deletionRequest.targetRefId); await this.logDeletion( deletionRequest, @@ -195,16 +221,22 @@ export class DeletionRequestUc { } private async removeUserFromTeams(deletionRequest: DeletionRequest) { + this.logger.debug({ action: ' removeUserFromTeams', deletionRequest }); + const teamsUpdated: number = await this.teamService.deleteUserDataFromTeams(deletionRequest.targetRefId); await this.logDeletion(deletionRequest, DeletionDomainModel.TEAMS, DeletionOperationModel.UPDATE, teamsUpdated, 0); } private async removeUser(deletionRequest: DeletionRequest) { + this.logger.debug({ action: 'removeUser', deletionRequest }); + const userDeleted: number = await this.userService.deleteUser(deletionRequest.targetRefId); await this.logDeletion(deletionRequest, DeletionDomainModel.USER, DeletionOperationModel.DELETE, 0, userDeleted); } private async removeUserFromRocketChat(deletionRequest: DeletionRequest) { + this.logger.debug({ action: 'removeUserFromRocketChat', deletionRequest }); + const rocketChatUser = await this.rocketChatUserService.findByUserId(deletionRequest.targetRefId); const [, rocketChatUserDeleted] = await Promise.all([ diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 91e6a334732..f062d26fb34 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -24,6 +24,7 @@ const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConf password: DB_PASSWORD, user: DB_USERNAME, entities: ALL_ENTITIES, + debug: true, }), LoggerModule, ], diff --git a/apps/server/src/shared/domain/entity/all-entities.ts b/apps/server/src/shared/domain/entity/all-entities.ts index 8bfca7728b2..3811d15fecf 100644 --- a/apps/server/src/shared/domain/entity/all-entities.ts +++ b/apps/server/src/shared/domain/entity/all-entities.ts @@ -7,6 +7,7 @@ import { ExternalToolEntity } from '@modules/tool/external-tool/entity'; import { SchoolExternalToolEntity } from '@modules/tool/school-external-tool/entity'; import { DeletionLogEntity, DeletionRequestEntity } from '@src/modules/deletion/entity'; import { RocketChatUserEntity } from '@src/modules/rocketchat-user/entity'; +import { FileEntity } from '@src/modules/files/entity'; import { Account } from './account.entity'; import { BoardNode, @@ -63,6 +64,7 @@ export const ALL_ENTITIES = [ DeletionRequestEntity, DeletionLogEntity, FileElementNode, + FileEntity, LinkElementNode, RichTextElementNode, SubmissionContainerElementNode, From 837350181b805f9dbf9c65e86d35e1d371e3ba68 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Fri, 17 Nov 2023 07:46:07 +0100 Subject: [PATCH 49/96] add logger for error in deeltionRequestUC --- apps/server/src/modules/deletion/uc/deletion-request.uc.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index 780e6228db0..97f69f058bb 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -108,6 +108,7 @@ export class DeletionRequestUc { ]); await this.deletionRequestService.markDeletionRequestAsExecuted(deletionRequest.id); } catch (error) { + this.logger.error(`execution of deletionRequest ${deletionRequest.id} was failed`, error); await this.deletionRequestService.markDeletionRequestAsFailed(deletionRequest.id); } } From 2695b3dbd5dacec24356c32219d2cd2504094e20 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Fri, 17 Nov 2023 08:13:17 +0100 Subject: [PATCH 50/96] fix problem with fileentity --- apps/server/src/modules/server/admin-api.server.module.ts | 4 +++- apps/server/src/shared/domain/entity/all-entities.ts | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index f062d26fb34..3421314611a 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -6,6 +6,8 @@ import { LegacyLogger, LoggerModule } from '@src/core/logger'; import { ConfigModule } from '@nestjs/config'; import { RabbitMQWrapperModule, RabbitMQWrapperTestModule } from '@src/infra/rabbitmq'; import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@src/infra/database'; +import { FileEntity } from '@modules/files/entity'; +import { FileRecord } from '@modules/files-storage/entity'; import { DeletionModule } from '../deletion'; import { defaultMikroOrmOptions, setupSessions } from './server.module'; import { serverConfig } from './server.config'; @@ -23,7 +25,7 @@ const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConf clientUrl: DB_URL, password: DB_PASSWORD, user: DB_USERNAME, - entities: ALL_ENTITIES, + entities: [...ALL_ENTITIES, FileEntity, FileRecord], debug: true, }), LoggerModule, diff --git a/apps/server/src/shared/domain/entity/all-entities.ts b/apps/server/src/shared/domain/entity/all-entities.ts index 3811d15fecf..34d4cffecad 100644 --- a/apps/server/src/shared/domain/entity/all-entities.ts +++ b/apps/server/src/shared/domain/entity/all-entities.ts @@ -64,7 +64,6 @@ export const ALL_ENTITIES = [ DeletionRequestEntity, DeletionLogEntity, FileElementNode, - FileEntity, LinkElementNode, RichTextElementNode, SubmissionContainerElementNode, From 92dba86ae602f9f27fb209c051b56bfc0cbc1f7d Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Fri, 17 Nov 2023 08:24:14 +0100 Subject: [PATCH 51/96] fix imports --- apps/server/src/shared/domain/entity/all-entities.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/server/src/shared/domain/entity/all-entities.ts b/apps/server/src/shared/domain/entity/all-entities.ts index 34d4cffecad..8bfca7728b2 100644 --- a/apps/server/src/shared/domain/entity/all-entities.ts +++ b/apps/server/src/shared/domain/entity/all-entities.ts @@ -7,7 +7,6 @@ import { ExternalToolEntity } from '@modules/tool/external-tool/entity'; import { SchoolExternalToolEntity } from '@modules/tool/school-external-tool/entity'; import { DeletionLogEntity, DeletionRequestEntity } from '@src/modules/deletion/entity'; import { RocketChatUserEntity } from '@src/modules/rocketchat-user/entity'; -import { FileEntity } from '@src/modules/files/entity'; import { Account } from './account.entity'; import { BoardNode, From 3155b71fe33f53075f651777117bb7c4a4f99635 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Fri, 17 Nov 2023 09:24:02 +0100 Subject: [PATCH 52/96] add logger to test in uc of deletionModule --- .../src/modules/deletion/uc/deletion-request.uc.spec.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index 5bb38137e38..b54ca0564c6 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -11,6 +11,7 @@ import { TeamService } from '@modules/teams'; import { UserService } from '@modules/user'; import { RocketChatService } from '@modules/rocketchat'; import { RocketChatUser, RocketChatUserService, rocketChatUserFactory } from '@modules/rocketchat-user'; +import { LegacyLogger } from '@src/core/logger'; import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequestService } from '../services'; @@ -93,6 +94,10 @@ describe(DeletionRequestUc.name, () => { provide: RocketChatService, useValue: createMock(), }, + { + provide: LegacyLogger, + useValue: createMock(), + }, ], }).compile(); From 2196f1e8939325a70997c551f313a3ad02b183fc Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Fri, 17 Nov 2023 09:55:09 +0100 Subject: [PATCH 53/96] change module config --- .../modules/server/admin-api.server.module.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 3421314611a..bacab1b0795 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -1,5 +1,5 @@ import { MikroOrmModule } from '@mikro-orm/nestjs'; -import { DynamicModule, MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; +import { DynamicModule, Inject, MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { ALL_ENTITIES } from '@shared/domain'; import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config'; import { LegacyLogger, LoggerModule } from '@src/core/logger'; @@ -8,6 +8,8 @@ import { RabbitMQWrapperModule, RabbitMQWrapperTestModule } from '@src/infra/rab import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@src/infra/database'; import { FileEntity } from '@modules/files/entity'; import { FileRecord } from '@modules/files-storage/entity'; +import { RedisClient } from 'redis'; +import { REDIS_CLIENT, RedisModule } from '@src/infra/redis'; import { DeletionModule } from '../deletion'; import { defaultMikroOrmOptions, setupSessions } from './server.module'; import { serverConfig } from './server.config'; @@ -29,16 +31,20 @@ const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConf debug: true, }), LoggerModule, + RedisModule, ], controllers: [AdminApiServerController], }) export class AdminApiServerModule implements NestModule { - constructor(private readonly logger: LegacyLogger) { + constructor( + @Inject(REDIS_CLIENT) private readonly redisClient: RedisClient | undefined, + private readonly logger: LegacyLogger + ) { logger.setContext(AdminApiServerModule.name); } configure(consumer: MiddlewareConsumer) { - setupSessions(consumer, undefined, this.logger); + setupSessions(consumer, this.redisClient, this.logger); } } @@ -48,11 +54,15 @@ export class AdminApiServerModule implements NestModule { MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions }), RabbitMQWrapperTestModule, LoggerModule, + RedisModule, ], controllers: [AdminApiServerController], }) export class AdminApiServerTestModule implements NestModule { - constructor(private readonly logger: LegacyLogger) { + constructor( + @Inject(REDIS_CLIENT) private readonly redisClient: RedisClient | undefined, + private readonly logger: LegacyLogger + ) { logger.setContext(AdminApiServerTestModule.name); } From c0ae9d71970fbc677ddac24a3a50f6861695a954 Mon Sep 17 00:00:00 2001 From: Bartosz Nowicki <116367402+bn-pass@users.noreply.github.com> Date: Fri, 17 Nov 2023 12:02:21 +0100 Subject: [PATCH 54/96] hard-coded Admin API server port after the discussion with Paul --- ansible/roles/schulcloud-server-core/templates/svc.yml.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 b/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 index 445da44fde8..0ef85ec904d 100644 --- a/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 @@ -12,7 +12,7 @@ spec: targetPort: 3030 protocol: TCP name: api - - port: {{ PORT_ADMIN_SERVER }} + - port: 4030 targetPort: 4030 protocol: TCP name: api-admin From b0c730b08479255c8a076f88c9d0178f0a61589f Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Fri, 17 Nov 2023 15:51:23 +0100 Subject: [PATCH 55/96] fixes bug during deletion uders data from lessons --- .../lesson/service/lesson.service.spec.ts | 16 ++++++++-------- .../src/modules/lesson/service/lesson.service.ts | 5 +++-- .../src/shared/domain/entity/lesson.entity.ts | 3 ++- .../repo/lesson/lesson.repo.integration.spec.ts | 12 ++++++------ .../server/src/shared/repo/lesson/lesson.repo.ts | 3 ++- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/apps/server/src/modules/lesson/service/lesson.service.spec.ts b/apps/server/src/modules/lesson/service/lesson.service.spec.ts index a94ecfe9c8b..4eeecd05680 100644 --- a/apps/server/src/modules/lesson/service/lesson.service.spec.ts +++ b/apps/server/src/modules/lesson/service/lesson.service.spec.ts @@ -80,7 +80,7 @@ describe('LessonService', () => { describe('findAllLessonsByUserId', () => { describe('when finding by userId', () => { const setup = () => { - const userId = new ObjectId().toHexString(); + const userId = new ObjectId(); const contentExample: IComponentProperties = { title: 'title', hidden: false, @@ -103,14 +103,14 @@ describe('LessonService', () => { it('should call findByCourseIds from lesson repo', async () => { const { userId } = setup(); - await expect(lessonService.findAllLessonsByUserId(userId)).resolves.not.toThrow(); - expect(lessonRepo.findByUserId).toBeCalledWith(userId); + await expect(lessonService.findAllLessonsByUserId(userId.toHexString())).resolves.not.toThrow(); + expect(lessonRepo.findByUserId).toBeCalledWith(userId.toHexString()); }); it('should return array of lessons with userId', async () => { const { userId, lessons } = setup(); - const result = await lessonService.findAllLessonsByUserId(userId); + const result = await lessonService.findAllLessonsByUserId(userId.toHexString()); expect(result).toHaveLength(2); expect(result).toEqual(lessons); @@ -121,7 +121,7 @@ describe('LessonService', () => { describe('deleteUserDataFromTeams', () => { describe('when deleting by userId', () => { const setup = () => { - const userId = new ObjectId().toHexString(); + const userId = new ObjectId(); const contentExample: IComponentProperties = { title: 'title', hidden: false, @@ -142,15 +142,15 @@ describe('LessonService', () => { it('should call lessonRepo.findByUserId', async () => { const { userId } = setup(); - await lessonService.deleteUserDataFromLessons(userId); + await lessonService.deleteUserDataFromLessons(userId.toHexString()); - expect(lessonRepo.findByUserId).toBeCalledWith(userId); + expect(lessonRepo.findByUserId).toBeCalledWith(userId.toHexString()); }); it('should update lessons without deleted user', async () => { const { userId } = setup(); - const result = await lessonService.deleteUserDataFromLessons(userId); + const result = await lessonService.deleteUserDataFromLessons(userId.toHexString()); expect(result).toEqual(2); }); diff --git a/apps/server/src/modules/lesson/service/lesson.service.ts b/apps/server/src/modules/lesson/service/lesson.service.ts index 2dee6f05563..db01a71253f 100644 --- a/apps/server/src/modules/lesson/service/lesson.service.ts +++ b/apps/server/src/modules/lesson/service/lesson.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'; import { Counted, EntityId, IComponentProperties, LessonEntity } from '@shared/domain'; import { LessonRepo } from '@shared/repo'; import { FilesStorageClientAdapterService } from '@modules/files-storage-client'; +import { ObjectId } from 'bson'; @Injectable() export class LessonService { @@ -35,8 +36,8 @@ export class LessonService { const updatedLessons = lessons.map((lesson: LessonEntity) => { lesson.contents.map((c: IComponentProperties) => { - if (c.user === userId) { - c.user = ''; + if (c.user?.equals(new ObjectId(userId))) { + c.user = undefined; } return c; }); diff --git a/apps/server/src/shared/domain/entity/lesson.entity.ts b/apps/server/src/shared/domain/entity/lesson.entity.ts index a47aab2a8b3..4b225e75f85 100644 --- a/apps/server/src/shared/domain/entity/lesson.entity.ts +++ b/apps/server/src/shared/domain/entity/lesson.entity.ts @@ -1,6 +1,7 @@ import { Collection, Entity, Index, ManyToMany, ManyToOne, OneToMany, Property } from '@mikro-orm/core'; import { InternalServerErrorException } from '@nestjs/common'; import { ILearnroomElement } from '@shared/domain/interface'; +import { ObjectId } from '@mikro-orm/mongodb'; import { EntityId } from '../types'; import { BaseEntityWithTimestamps } from './base.entity'; import type { Course } from './course.entity'; @@ -67,7 +68,7 @@ export type IComponentProperties = { _id?: string; title: string; hidden: boolean; - user?: EntityId; + user?: ObjectId; } & ( | { component: ComponentType.TEXT; content: IComponentTextProperties } | { component: ComponentType.ETHERPAD; content: IComponentEtherpadProperties } diff --git a/apps/server/src/shared/repo/lesson/lesson.repo.integration.spec.ts b/apps/server/src/shared/repo/lesson/lesson.repo.integration.spec.ts index eae071d55ae..0b6a912ffed 100644 --- a/apps/server/src/shared/repo/lesson/lesson.repo.integration.spec.ts +++ b/apps/server/src/shared/repo/lesson/lesson.repo.integration.spec.ts @@ -164,7 +164,7 @@ describe('LessonRepo', () => { describe('findByUserId', () => { it('should return lessons which contains a specific userId', async () => { // Arrange - const userId = new ObjectId().toHexString(); + const userId = new ObjectId(); const contentExample: IComponentProperties = { title: 'title', hidden: false, @@ -179,7 +179,7 @@ describe('LessonRepo', () => { em.clear(); // Act - const result = await repo.findByUserId(userId); + const result = await repo.findByUserId(userId.toHexString()); // Assert expect(result).toHaveLength(2); @@ -194,7 +194,7 @@ describe('LessonRepo', () => { describe('updateLessons', () => { it('should update Lessons without deleted user', async () => { // Arrange - const userId = new ObjectId().toHexString(); + const userId = new ObjectId(); const contentExample: IComponentProperties = { title: 'title', hidden: false, @@ -207,18 +207,18 @@ describe('LessonRepo', () => { em.clear(); // Arrange expected Array after User deletion - lesson1.contents[0].user = ''; + lesson1.contents[0].user = undefined; // Act await repo.save([lesson1]); - const result1 = await repo.findByUserId(userId); + const result1 = await repo.findByUserId(userId.toHexString()); expect(result1).toHaveLength(0); const result2 = await repo.findById(lesson1.id); const receivedContents = result2.contents; receivedContents.forEach((content) => { - expect(content.user).toEqual(''); + expect(content.user).toBe(null); }); }); }); diff --git a/apps/server/src/shared/repo/lesson/lesson.repo.ts b/apps/server/src/shared/repo/lesson/lesson.repo.ts index 26f66e9587d..6bdd4d7e701 100644 --- a/apps/server/src/shared/repo/lesson/lesson.repo.ts +++ b/apps/server/src/shared/repo/lesson/lesson.repo.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; import { Counted, EntityId, LessonEntity, SortOrder } from '@shared/domain'; import { EntityDictionary } from '@mikro-orm/core'; +import { ObjectId } from '@mikro-orm/mongodb'; import { BaseRepo } from '../base.repo'; import { LessonScope } from './lesson-scope'; @@ -44,7 +45,7 @@ export class LessonRepo extends BaseRepo { $match: { contents: { $elemMatch: { - user: userId, + user: new ObjectId(userId), }, }, }, From 9d40aea85048aff02eab9bf1f1fbc4a891e731c6 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 21 Nov 2023 14:08:27 +0100 Subject: [PATCH 56/96] fix pipeline --- apps/server/src/modules/deletion/deletion.module.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/server/src/modules/deletion/deletion.module.ts b/apps/server/src/modules/deletion/deletion.module.ts index ee0cd973c57..e1a5ae74b01 100644 --- a/apps/server/src/modules/deletion/deletion.module.ts +++ b/apps/server/src/modules/deletion/deletion.module.ts @@ -8,7 +8,7 @@ import { DeletionRequestRepo } from './repo/deletion-request.repo'; import { AuthenticationModule } from '../authentication'; import { DeletionRequestsController } from './controller/deletion-requests.controller'; import { DeletionExecutionsController } from './controller/deletion-executions.controller'; -import { IXApiKeyConfig } from '../authentication/config/x-api-key.config'; +import { XApiKeyConfig } from '../authentication/config/x-api-key.config'; import { DeletionRequestUc } from './uc/deletion-request.uc'; import { DeletionLogService } from './services/deletion-log.service'; import { AccountModule } from '../account'; @@ -49,7 +49,7 @@ import { RocketChatUserRepo } from '../rocketchat-user/repo'; providers: [ DeletionRequestService, DeletionRequestRepo, - ConfigService, + ConfigService, DeletionRequestUc, DeletionLogService, CourseGroupService, From 87cdfd2d0a3e631d22697fc59eead3ab4545ce60 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 21 Nov 2023 14:50:11 +0100 Subject: [PATCH 57/96] fix pipeline #2 --- .../authentication/strategy/x-api-key.strategy.spec.ts | 6 +++--- apps/server/src/modules/learnroom/index.ts | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts index 836b4adc7e5..774fc84d349 100644 --- a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts @@ -3,12 +3,12 @@ import { Test, TestingModule } from '@nestjs/testing'; import { ConfigService } from '@nestjs/config'; import { createMock } from '@golevelup/ts-jest'; import { XApiKeyStrategy } from './x-api-key.strategy'; -import { IXApiKeyConfig } from '../config/x-api-key.config'; +import { XApiKeyConfig } from '../config/x-api-key.config'; describe('XApiKeyStrategy', () => { let module: TestingModule; let strategy: XApiKeyStrategy; - let configService: ConfigService; + let configService: ConfigService; beforeAll(async () => { module = await Test.createTestingModule({ @@ -17,7 +17,7 @@ describe('XApiKeyStrategy', () => { XApiKeyStrategy, { provide: ConfigService, - useValue: createMock>({ get: () => ['1ab2c3d4e5f61ab2c3d4e5f6'] }), + useValue: createMock>({ get: () => ['1ab2c3d4e5f61ab2c3d4e5f6'] }), }, ], }).compile(); diff --git a/apps/server/src/modules/learnroom/index.ts b/apps/server/src/modules/learnroom/index.ts index 9fe9c100886..6c28dcb2e95 100644 --- a/apps/server/src/modules/learnroom/index.ts +++ b/apps/server/src/modules/learnroom/index.ts @@ -1,2 +1,8 @@ export * from './learnroom.module'; -export { CommonCartridgeExportService, CourseCopyService, CourseService, RoomsService } from './service'; +export { + CommonCartridgeExportService, + CourseCopyService, + CourseService, + RoomsService, + CourseGroupService, +} from './service'; From 048875d9c68ac742f8c5f85335df31653949161d Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 21 Nov 2023 15:27:49 +0100 Subject: [PATCH 58/96] fix pipeline --- .../modules/authentication/strategy/x-api-key.strategy.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts index 774fc84d349..4db5d99d6ba 100644 --- a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts @@ -23,7 +23,7 @@ describe('XApiKeyStrategy', () => { }).compile(); strategy = module.get(XApiKeyStrategy); - configService = module.get(ConfigService); + configService = module.get(ConfigService); }); afterAll(async () => { From 8ccf2d6291d324f02c047eccc8ccd54deb793644 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 21 Nov 2023 15:53:42 +0100 Subject: [PATCH 59/96] fix lint --- apps/server/src/modules/lesson/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/server/src/modules/lesson/index.ts b/apps/server/src/modules/lesson/index.ts index 2d66c0397f9..318800cad8b 100644 --- a/apps/server/src/modules/lesson/index.ts +++ b/apps/server/src/modules/lesson/index.ts @@ -1,6 +1,4 @@ export * from './lesson.module'; -export * from './service/lesson-copy.service'; -export * from './service/lesson.service'; export * from './types/lesson-copy-parent.params'; export * from './types/lesson-copy.params'; export * from './service'; From 1e0bb2de2621fc888c701f0ca8b2aa2a0891cac8 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 23 Nov 2023 19:53:48 +0100 Subject: [PATCH 60/96] fix modules imports --- .../src/modules/deletion/deletion.module.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/apps/server/src/modules/deletion/deletion.module.ts b/apps/server/src/modules/deletion/deletion.module.ts index e1a5ae74b01..684bbd433ea 100644 --- a/apps/server/src/modules/deletion/deletion.module.ts +++ b/apps/server/src/modules/deletion/deletion.module.ts @@ -1,27 +1,27 @@ import { Module } from '@nestjs/common'; -import { LoggerModule } from '@src/core/logger'; import { ConfigService } from '@nestjs/config'; import { CourseGroupRepo } from '@shared/repo'; import { Configuration } from '@hpi-schul-cloud/commons'; +import { LoggerModule } from '@src/core/logger'; +import { AuthenticationModule } from '@modules/authentication/authentication.module'; +import { AccountModule } from '@modules/account'; +import { ClassModule } from '@modules/class'; +import { LearnroomModule } from '@modules/learnroom'; +import { LessonModule } from '@modules/lesson'; +import { PseudonymModule } from '@modules/pseudonym'; +import { TeamsModule } from '@modules/teams'; +import { UserModule } from '@modules/user'; +import { RocketChatModule } from '@modules/rocketchat'; import { DeletionRequestService } from './services/deletion-request.service'; import { DeletionRequestRepo } from './repo/deletion-request.repo'; -import { AuthenticationModule } from '../authentication'; import { DeletionRequestsController } from './controller/deletion-requests.controller'; import { DeletionExecutionsController } from './controller/deletion-executions.controller'; import { XApiKeyConfig } from '../authentication/config/x-api-key.config'; import { DeletionRequestUc } from './uc/deletion-request.uc'; import { DeletionLogService } from './services/deletion-log.service'; -import { AccountModule } from '../account'; -import { ClassModule } from '../class'; import { CourseGroupService } from '../learnroom/service'; -import { LearnroomModule } from '../learnroom'; import { FilesService } from '../files/service'; -import { LessonModule } from '../lesson'; -import { PseudonymModule } from '../pseudonym'; -import { TeamsModule } from '../teams'; -import { UserModule } from '../user'; import { RocketChatUserService } from '../rocketchat-user'; -import { RocketChatModule } from '../rocketchat'; import { DeletionLogRepo } from './repo'; import { FilesRepo } from '../files/repo'; import { RocketChatUserRepo } from '../rocketchat-user/repo'; From 994262f29211d306d724d40b85db90a6ce6655ad Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 23 Nov 2023 20:46:37 +0100 Subject: [PATCH 61/96] fix providers in exports in deletion module --- .../src/modules/deletion/deletion.module.ts | 22 ++++++------------- apps/server/src/modules/files/files.module.ts | 4 +++- .../src/modules/learnroom/learnroom.module.ts | 7 ++++-- .../rocketchat-user/rocketchat-user.module.ts | 5 ++--- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/apps/server/src/modules/deletion/deletion.module.ts b/apps/server/src/modules/deletion/deletion.module.ts index 684bbd433ea..960a6a57205 100644 --- a/apps/server/src/modules/deletion/deletion.module.ts +++ b/apps/server/src/modules/deletion/deletion.module.ts @@ -1,6 +1,5 @@ import { Module } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { CourseGroupRepo } from '@shared/repo'; import { Configuration } from '@hpi-schul-cloud/commons'; import { LoggerModule } from '@src/core/logger'; import { AuthenticationModule } from '@modules/authentication/authentication.module'; @@ -12,6 +11,7 @@ import { PseudonymModule } from '@modules/pseudonym'; import { TeamsModule } from '@modules/teams'; import { UserModule } from '@modules/user'; import { RocketChatModule } from '@modules/rocketchat'; +import { FilesModule } from '@modules/files'; import { DeletionRequestService } from './services/deletion-request.service'; import { DeletionRequestRepo } from './repo/deletion-request.repo'; import { DeletionRequestsController } from './controller/deletion-requests.controller'; @@ -19,12 +19,8 @@ import { DeletionExecutionsController } from './controller/deletion-executions.c import { XApiKeyConfig } from '../authentication/config/x-api-key.config'; import { DeletionRequestUc } from './uc/deletion-request.uc'; import { DeletionLogService } from './services/deletion-log.service'; -import { CourseGroupService } from '../learnroom/service'; -import { FilesService } from '../files/service'; -import { RocketChatUserService } from '../rocketchat-user'; +import { RocketChatUserModule } from '../rocketchat-user'; import { DeletionLogRepo } from './repo'; -import { FilesRepo } from '../files/repo'; -import { RocketChatUserRepo } from '../rocketchat-user/repo'; @Module({ imports: [ @@ -44,22 +40,18 @@ import { RocketChatUserRepo } from '../rocketchat-user/repo'; adminUser: Configuration.get('ROCKET_CHAT_ADMIN_USER') as string, adminPassword: Configuration.get('ROCKET_CHAT_ADMIN_PASSWORD') as string, }), + RocketChatUserModule, + FilesModule, ], controllers: [DeletionRequestsController, DeletionExecutionsController], providers: [ - DeletionRequestService, DeletionRequestRepo, + DeletionLogRepo, ConfigService, DeletionRequestUc, DeletionLogService, - CourseGroupService, - FilesService, - DeletionLogRepo, - CourseGroupRepo, - FilesRepo, - RocketChatUserService, - RocketChatUserRepo, + DeletionRequestService, ], - exports: [DeletionRequestService], + exports: [DeletionRequestService, DeletionLogService], }) export class DeletionModule {} diff --git a/apps/server/src/modules/files/files.module.ts b/apps/server/src/modules/files/files.module.ts index a62815eb69f..d451381171d 100644 --- a/apps/server/src/modules/files/files.module.ts +++ b/apps/server/src/modules/files/files.module.ts @@ -4,9 +4,11 @@ import { LoggerModule } from '@src/core/logger'; import { DeleteFilesConsole } from './job'; import { DeleteFilesUc } from './uc'; import { FilesRepo } from './repo'; +import { FilesService } from './service'; @Module({ imports: [LoggerModule], - providers: [DeleteFilesConsole, DeleteFilesUc, FilesRepo, StorageProviderRepo], + providers: [DeleteFilesConsole, DeleteFilesUc, FilesRepo, StorageProviderRepo, FilesService], + exports: [FilesService], }) export class FilesModule {} diff --git a/apps/server/src/modules/learnroom/learnroom.module.ts b/apps/server/src/modules/learnroom/learnroom.module.ts index 02071369766..9c9d4d80036 100644 --- a/apps/server/src/modules/learnroom/learnroom.module.ts +++ b/apps/server/src/modules/learnroom/learnroom.module.ts @@ -3,13 +3,14 @@ import { CopyHelperModule } from '@modules/copy-helper'; import { LessonModule } from '@modules/lesson'; import { TaskModule } from '@modules/task'; import { Module } from '@nestjs/common'; -import { BoardRepo, CourseRepo, DashboardModelMapper, DashboardRepo, UserRepo } from '@shared/repo'; +import { BoardRepo, CourseGroupRepo, CourseRepo, DashboardModelMapper, DashboardRepo, UserRepo } from '@shared/repo'; import { LoggerModule } from '@src/core/logger'; import { BoardCopyService, ColumnBoardTargetService, CommonCartridgeExportService, CourseCopyService, + CourseGroupService, CourseService, RoomsService, } from './service'; @@ -31,7 +32,9 @@ import { CourseService, CommonCartridgeExportService, ColumnBoardTargetService, + CourseGroupService, + CourseGroupRepo, ], - exports: [CourseCopyService, CourseService, RoomsService, CommonCartridgeExportService], + exports: [CourseCopyService, CourseService, RoomsService, CommonCartridgeExportService, CourseGroupService], }) export class LearnroomModule {} diff --git a/apps/server/src/modules/rocketchat-user/rocketchat-user.module.ts b/apps/server/src/modules/rocketchat-user/rocketchat-user.module.ts index 798b2276a4d..f0d30d76c37 100644 --- a/apps/server/src/modules/rocketchat-user/rocketchat-user.module.ts +++ b/apps/server/src/modules/rocketchat-user/rocketchat-user.module.ts @@ -1,10 +1,9 @@ import { Module } from '@nestjs/common'; import { RocketChatUserRepo } from './repo'; import { RocketChatUserService } from './service/rocket-chat-user.service'; -import { RocketChatService } from '../rocketchat/rocket-chat.service'; @Module({ - providers: [RocketChatUserService, RocketChatUserRepo], - exports: [RocketChatService], + providers: [RocketChatUserRepo, RocketChatUserService], + exports: [RocketChatUserService], }) export class RocketChatUserModule {} From 72e7f313e8cf62903120aebe054b4d48af2ca2de Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 23 Nov 2023 21:19:22 +0100 Subject: [PATCH 62/96] split deletion module and deletion-api.module --- .../modules/deletion/deletion-api.module.ts | 45 +++++++++++++++++++ .../src/modules/deletion/deletion.module.ts | 38 ---------------- .../modules/server/admin-api.server.module.ts | 4 +- config/default.json | 3 ++ 4 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 apps/server/src/modules/deletion/deletion-api.module.ts diff --git a/apps/server/src/modules/deletion/deletion-api.module.ts b/apps/server/src/modules/deletion/deletion-api.module.ts new file mode 100644 index 00000000000..3aeaaf7f01e --- /dev/null +++ b/apps/server/src/modules/deletion/deletion-api.module.ts @@ -0,0 +1,45 @@ +import { Module } from '@nestjs/common'; +import { DeletionModule } from '@modules/deletion'; +import { AccountModule } from '@modules/account'; +import { ClassModule } from '@modules/class'; +import { LearnroomModule } from '@modules/learnroom'; +import { FilesModule } from '@modules/files'; +import { PseudonymModule } from '@modules/pseudonym'; +import { LessonModule } from '@modules/lesson'; +import { TeamsModule } from '@modules/teams'; +import { UserModule } from '@modules/user'; +import { LoggerModule } from '@src/core/logger'; +import { AuthenticationModule } from '@modules/authentication'; +import { RocketChatUserModule } from '@modules/rocketchat-user'; +import { Configuration } from '@hpi-schul-cloud/commons'; +import { RocketChatModule } from '@modules/rocketchat'; +import { DeletionRequestsController } from './controller/deletion-requests.controller'; +import { DeletionExecutionsController } from './controller/deletion-executions.controller'; +import { DeletionRequestUc } from './uc'; + +@Module({ + imports: [ + DeletionModule, + AccountModule, + ClassModule, + LearnroomModule, + FilesModule, + LessonModule, + PseudonymModule, + TeamsModule, + UserModule, + LoggerModule, + AuthenticationModule, + RocketChatUserModule, + RocketChatModule.forRoot({ + uri: Configuration.get('ROCKET_CHAT_URI') as string, + adminId: Configuration.get('ROCKET_CHAT_ADMIN_ID') as string, + adminToken: Configuration.get('ROCKET_CHAT_ADMIN_TOKEN') as string, + adminUser: Configuration.get('ROCKET_CHAT_ADMIN_USER') as string, + adminPassword: Configuration.get('ROCKET_CHAT_ADMIN_PASSWORD') as string, + }), + ], + controllers: [DeletionRequestsController, DeletionExecutionsController], + providers: [DeletionRequestUc], +}) +export class DeletionApiModule {} diff --git a/apps/server/src/modules/deletion/deletion.module.ts b/apps/server/src/modules/deletion/deletion.module.ts index 960a6a57205..3be026b827c 100644 --- a/apps/server/src/modules/deletion/deletion.module.ts +++ b/apps/server/src/modules/deletion/deletion.module.ts @@ -1,54 +1,16 @@ import { Module } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { Configuration } from '@hpi-schul-cloud/commons'; -import { LoggerModule } from '@src/core/logger'; -import { AuthenticationModule } from '@modules/authentication/authentication.module'; -import { AccountModule } from '@modules/account'; -import { ClassModule } from '@modules/class'; -import { LearnroomModule } from '@modules/learnroom'; -import { LessonModule } from '@modules/lesson'; -import { PseudonymModule } from '@modules/pseudonym'; -import { TeamsModule } from '@modules/teams'; -import { UserModule } from '@modules/user'; -import { RocketChatModule } from '@modules/rocketchat'; -import { FilesModule } from '@modules/files'; import { DeletionRequestService } from './services/deletion-request.service'; import { DeletionRequestRepo } from './repo/deletion-request.repo'; -import { DeletionRequestsController } from './controller/deletion-requests.controller'; -import { DeletionExecutionsController } from './controller/deletion-executions.controller'; import { XApiKeyConfig } from '../authentication/config/x-api-key.config'; -import { DeletionRequestUc } from './uc/deletion-request.uc'; import { DeletionLogService } from './services/deletion-log.service'; -import { RocketChatUserModule } from '../rocketchat-user'; import { DeletionLogRepo } from './repo'; @Module({ - imports: [ - LoggerModule, - AuthenticationModule, - AccountModule, - ClassModule, - LearnroomModule, - LessonModule, - PseudonymModule, - TeamsModule, - UserModule, - RocketChatModule.forRoot({ - uri: Configuration.get('ROCKET_CHAT_URI') as string, - adminId: Configuration.get('ROCKET_CHAT_ADMIN_ID') as string, - adminToken: Configuration.get('ROCKET_CHAT_ADMIN_TOKEN') as string, - adminUser: Configuration.get('ROCKET_CHAT_ADMIN_USER') as string, - adminPassword: Configuration.get('ROCKET_CHAT_ADMIN_PASSWORD') as string, - }), - RocketChatUserModule, - FilesModule, - ], - controllers: [DeletionRequestsController, DeletionExecutionsController], providers: [ DeletionRequestRepo, DeletionLogRepo, ConfigService, - DeletionRequestUc, DeletionLogService, DeletionRequestService, ], diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index bacab1b0795..fc11b5b772b 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -10,12 +10,12 @@ import { FileEntity } from '@modules/files/entity'; import { FileRecord } from '@modules/files-storage/entity'; import { RedisClient } from 'redis'; import { REDIS_CLIENT, RedisModule } from '@src/infra/redis'; -import { DeletionModule } from '../deletion'; import { defaultMikroOrmOptions, setupSessions } from './server.module'; import { serverConfig } from './server.config'; import { AdminApiServerController } from './controller'; +import { DeletionApiModule } from '../deletion/deletion-api.module'; -const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), DeletionModule]; +const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), DeletionApiModule]; @Module({ imports: [ diff --git a/config/default.json b/config/default.json index 7dd1802a037..5306bba17c2 100644 --- a/config/default.json +++ b/config/default.json @@ -41,5 +41,8 @@ }, "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 + }, + "ADMIN_API": { + "ALLOWED_API_KEYS": "1ab2c3d4e5f61ab2c3d4e5f6dasda" } } From 86b176dc7ee96386b1812a94d74bc92b106fea3f Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 23 Nov 2023 21:26:04 +0100 Subject: [PATCH 63/96] move setup sessions to server config --- .../modules/server/admin-api.server.module.ts | 4 +- .../src/modules/server/server.config.ts | 45 +++++++++++++++++++ .../src/modules/server/server.module.ts | 44 +----------------- 3 files changed, 48 insertions(+), 45 deletions(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index fc11b5b772b..34a1d148f7c 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -10,8 +10,8 @@ import { FileEntity } from '@modules/files/entity'; import { FileRecord } from '@modules/files-storage/entity'; import { RedisClient } from 'redis'; import { REDIS_CLIENT, RedisModule } from '@src/infra/redis'; -import { defaultMikroOrmOptions, setupSessions } from './server.module'; -import { serverConfig } from './server.config'; +import { defaultMikroOrmOptions } from './server.module'; +import { serverConfig, setupSessions } from './server.config'; import { AdminApiServerController } from './controller'; import { DeletionApiModule } from '../deletion/deletion-api.module'; diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 09f410d4c94..052e5e77d80 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -6,6 +6,11 @@ import type { CommonCartridgeConfig } from '@modules/learnroom/common-cartridge' import type { UserConfig } from '@modules/user'; import type { CoreModuleConfig } from '@src/core'; import { MailConfig } from '@src/infra/mail/interfaces/mail-config'; +import { MiddlewareConsumer } from '@nestjs/common'; +import { RedisClient } from 'redis'; +import { LegacyLogger } from '@src/core/logger'; +import connectRedis from 'connect-redis'; +import session from 'express-session'; import { XApiKeyConfig } from '../authentication/config/x-api-key.config'; export enum NodeEnvType { @@ -52,3 +57,43 @@ const config: ServerConfig = { }; export const serverConfig = () => config; + +export const setupSessions = ( + consumer: MiddlewareConsumer, + redisClient: RedisClient | undefined, + logger: LegacyLogger +) => { + const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number; + + let store: connectRedis.RedisStore | undefined; + if (redisClient) { + const RedisStore: connectRedis.RedisStore = connectRedis(session); + store = new RedisStore({ + client: redisClient, + ttl: sessionDuration, + }); + } else { + logger.warn( + 'The RedisStore for sessions is not setup, since the environment variable REDIS_URI is not defined. Sessions are using the build-in MemoryStore. This should not be used in production!' + ); + } + + consumer + .apply( + session({ + store, + secret: Configuration.get('SESSION__SECRET') as string, + resave: false, + saveUninitialized: false, + name: Configuration.has('SESSION__NAME') ? (Configuration.get('SESSION__NAME') as string) : undefined, + proxy: Configuration.has('SESSION__PROXY') ? (Configuration.get('SESSION__PROXY') as boolean) : undefined, + cookie: { + secure: Configuration.get('SESSION__SECURE') as boolean, + sameSite: Configuration.get('SESSION__SAME_SITE') as boolean | 'lax' | 'strict' | 'none', + httpOnly: Configuration.get('SESSION__HTTP_ONLY') as boolean, + maxAge: sessionDuration * 1000, + }, + }) + ) + .forRoutes('*'); +}; diff --git a/apps/server/src/modules/server/server.module.ts b/apps/server/src/modules/server/server.module.ts index 030f91207a1..1e34fe9bd43 100644 --- a/apps/server/src/modules/server/server.module.ts +++ b/apps/server/src/modules/server/server.module.ts @@ -34,11 +34,9 @@ import { ALL_ENTITIES } from '@shared/domain'; import { createConfigModuleOptions, DB_PASSWORD, DB_URL, DB_USERNAME } from '@src/config'; import { CoreModule } from '@src/core'; import { LegacyLogger, LoggerModule } from '@src/core/logger'; -import connectRedis from 'connect-redis'; -import session from 'express-session'; import { RedisClient } from 'redis'; import { ServerController } from './controller/server.controller'; -import { serverConfig } from './server.config'; +import { serverConfig, setupSessions } from './server.config'; const serverModules = [ ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), @@ -85,46 +83,6 @@ export const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { new NotFoundException(`The requested ${entityName}: ${where} has not been found.`), }; -export const setupSessions = ( - consumer: MiddlewareConsumer, - redisClient: RedisClient | undefined, - logger: LegacyLogger -) => { - const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number; - - let store: connectRedis.RedisStore | undefined; - if (redisClient) { - const RedisStore: connectRedis.RedisStore = connectRedis(session); - store = new RedisStore({ - client: redisClient, - ttl: sessionDuration, - }); - } else { - logger.warn( - 'The RedisStore for sessions is not setup, since the environment variable REDIS_URI is not defined. Sessions are using the build-in MemoryStore. This should not be used in production!' - ); - } - - consumer - .apply( - session({ - store, - secret: Configuration.get('SESSION__SECRET') as string, - resave: false, - saveUninitialized: false, - name: Configuration.has('SESSION__NAME') ? (Configuration.get('SESSION__NAME') as string) : undefined, - proxy: Configuration.has('SESSION__PROXY') ? (Configuration.get('SESSION__PROXY') as boolean) : undefined, - cookie: { - secure: Configuration.get('SESSION__SECURE') as boolean, - sameSite: Configuration.get('SESSION__SAME_SITE') as boolean | 'lax' | 'strict' | 'none', - httpOnly: Configuration.get('SESSION__HTTP_ONLY') as boolean, - maxAge: sessionDuration * 1000, - }, - }) - ) - .forRoutes('*'); -}; - /** * Server Module used for production */ From e3361965ad98c01f5343912086d14d5d88494b42 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 23 Nov 2023 22:27:36 +0100 Subject: [PATCH 64/96] try to fix test coverage --- .../src/modules/server/controller/api-test/server.api.spec.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/server/src/modules/server/controller/api-test/server.api.spec.ts b/apps/server/src/modules/server/controller/api-test/server.api.spec.ts index a6e42141c6a..cdfe8a118f6 100644 --- a/apps/server/src/modules/server/controller/api-test/server.api.spec.ts +++ b/apps/server/src/modules/server/controller/api-test/server.api.spec.ts @@ -2,12 +2,13 @@ import { INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ServerTestModule } from '@modules/server'; import request from 'supertest'; +import { RedisModule } from '@src/infra/redis'; describe('Server Controller (API)', () => { let app: INestApplication; beforeAll(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ - imports: [ServerTestModule], + imports: [ServerTestModule, RedisModule], }).compile(); app = moduleFixture.createNestApplication(); From 235a83cebe9a68fb0b7d71047b07836d330e76ff Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 23 Nov 2023 22:45:17 +0100 Subject: [PATCH 65/96] Revert "try to fix test coverage" This reverts commit e3361965ad98c01f5343912086d14d5d88494b42. --- .../src/modules/server/controller/api-test/server.api.spec.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/server/src/modules/server/controller/api-test/server.api.spec.ts b/apps/server/src/modules/server/controller/api-test/server.api.spec.ts index cdfe8a118f6..a6e42141c6a 100644 --- a/apps/server/src/modules/server/controller/api-test/server.api.spec.ts +++ b/apps/server/src/modules/server/controller/api-test/server.api.spec.ts @@ -2,13 +2,12 @@ import { INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ServerTestModule } from '@modules/server'; import request from 'supertest'; -import { RedisModule } from '@src/infra/redis'; describe('Server Controller (API)', () => { let app: INestApplication; beforeAll(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ - imports: [ServerTestModule, RedisModule], + imports: [ServerTestModule], }).compile(); app = moduleFixture.createNestApplication(); From e4b133da7c6067cf6c7095201aa77780d75c7b30 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 23 Nov 2023 22:45:32 +0100 Subject: [PATCH 66/96] Revert "move setup sessions to server config" This reverts commit 86b176dc7ee96386b1812a94d74bc92b106fea3f. --- .../modules/server/admin-api.server.module.ts | 4 +- .../src/modules/server/server.config.ts | 45 ------------------- .../src/modules/server/server.module.ts | 44 +++++++++++++++++- 3 files changed, 45 insertions(+), 48 deletions(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 34a1d148f7c..fc11b5b772b 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -10,8 +10,8 @@ import { FileEntity } from '@modules/files/entity'; import { FileRecord } from '@modules/files-storage/entity'; import { RedisClient } from 'redis'; import { REDIS_CLIENT, RedisModule } from '@src/infra/redis'; -import { defaultMikroOrmOptions } from './server.module'; -import { serverConfig, setupSessions } from './server.config'; +import { defaultMikroOrmOptions, setupSessions } from './server.module'; +import { serverConfig } from './server.config'; import { AdminApiServerController } from './controller'; import { DeletionApiModule } from '../deletion/deletion-api.module'; diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 052e5e77d80..09f410d4c94 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -6,11 +6,6 @@ import type { CommonCartridgeConfig } from '@modules/learnroom/common-cartridge' import type { UserConfig } from '@modules/user'; import type { CoreModuleConfig } from '@src/core'; import { MailConfig } from '@src/infra/mail/interfaces/mail-config'; -import { MiddlewareConsumer } from '@nestjs/common'; -import { RedisClient } from 'redis'; -import { LegacyLogger } from '@src/core/logger'; -import connectRedis from 'connect-redis'; -import session from 'express-session'; import { XApiKeyConfig } from '../authentication/config/x-api-key.config'; export enum NodeEnvType { @@ -57,43 +52,3 @@ const config: ServerConfig = { }; export const serverConfig = () => config; - -export const setupSessions = ( - consumer: MiddlewareConsumer, - redisClient: RedisClient | undefined, - logger: LegacyLogger -) => { - const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number; - - let store: connectRedis.RedisStore | undefined; - if (redisClient) { - const RedisStore: connectRedis.RedisStore = connectRedis(session); - store = new RedisStore({ - client: redisClient, - ttl: sessionDuration, - }); - } else { - logger.warn( - 'The RedisStore for sessions is not setup, since the environment variable REDIS_URI is not defined. Sessions are using the build-in MemoryStore. This should not be used in production!' - ); - } - - consumer - .apply( - session({ - store, - secret: Configuration.get('SESSION__SECRET') as string, - resave: false, - saveUninitialized: false, - name: Configuration.has('SESSION__NAME') ? (Configuration.get('SESSION__NAME') as string) : undefined, - proxy: Configuration.has('SESSION__PROXY') ? (Configuration.get('SESSION__PROXY') as boolean) : undefined, - cookie: { - secure: Configuration.get('SESSION__SECURE') as boolean, - sameSite: Configuration.get('SESSION__SAME_SITE') as boolean | 'lax' | 'strict' | 'none', - httpOnly: Configuration.get('SESSION__HTTP_ONLY') as boolean, - maxAge: sessionDuration * 1000, - }, - }) - ) - .forRoutes('*'); -}; diff --git a/apps/server/src/modules/server/server.module.ts b/apps/server/src/modules/server/server.module.ts index 1e34fe9bd43..030f91207a1 100644 --- a/apps/server/src/modules/server/server.module.ts +++ b/apps/server/src/modules/server/server.module.ts @@ -34,9 +34,11 @@ import { ALL_ENTITIES } from '@shared/domain'; import { createConfigModuleOptions, DB_PASSWORD, DB_URL, DB_USERNAME } from '@src/config'; import { CoreModule } from '@src/core'; import { LegacyLogger, LoggerModule } from '@src/core/logger'; +import connectRedis from 'connect-redis'; +import session from 'express-session'; import { RedisClient } from 'redis'; import { ServerController } from './controller/server.controller'; -import { serverConfig, setupSessions } from './server.config'; +import { serverConfig } from './server.config'; const serverModules = [ ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), @@ -83,6 +85,46 @@ export const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { new NotFoundException(`The requested ${entityName}: ${where} has not been found.`), }; +export const setupSessions = ( + consumer: MiddlewareConsumer, + redisClient: RedisClient | undefined, + logger: LegacyLogger +) => { + const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number; + + let store: connectRedis.RedisStore | undefined; + if (redisClient) { + const RedisStore: connectRedis.RedisStore = connectRedis(session); + store = new RedisStore({ + client: redisClient, + ttl: sessionDuration, + }); + } else { + logger.warn( + 'The RedisStore for sessions is not setup, since the environment variable REDIS_URI is not defined. Sessions are using the build-in MemoryStore. This should not be used in production!' + ); + } + + consumer + .apply( + session({ + store, + secret: Configuration.get('SESSION__SECRET') as string, + resave: false, + saveUninitialized: false, + name: Configuration.has('SESSION__NAME') ? (Configuration.get('SESSION__NAME') as string) : undefined, + proxy: Configuration.has('SESSION__PROXY') ? (Configuration.get('SESSION__PROXY') as boolean) : undefined, + cookie: { + secure: Configuration.get('SESSION__SECURE') as boolean, + sameSite: Configuration.get('SESSION__SAME_SITE') as boolean | 'lax' | 'strict' | 'none', + httpOnly: Configuration.get('SESSION__HTTP_ONLY') as boolean, + maxAge: sessionDuration * 1000, + }, + }) + ) + .forRoutes('*'); +}; + /** * Server Module used for production */ From 0cd52ca88e88ae931d37e0e62714166200479494 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 23 Nov 2023 23:11:20 +0100 Subject: [PATCH 67/96] remove api key from default.json --- config/default.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/config/default.json b/config/default.json index 5306bba17c2..7dd1802a037 100644 --- a/config/default.json +++ b/config/default.json @@ -41,8 +41,5 @@ }, "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 - }, - "ADMIN_API": { - "ALLOWED_API_KEYS": "1ab2c3d4e5f61ab2c3d4e5f6dasda" } } From d86d1e5a6583c88db7c27f35373791248733ec98 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Fri, 24 Nov 2023 10:33:50 +0100 Subject: [PATCH 68/96] changes to server module --- .../modules/server/admin-api.server.module.ts | 36 +++---------------- .../src/modules/server/server.module.ts | 6 +--- 2 files changed, 6 insertions(+), 36 deletions(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index fc11b5b772b..86489c89c22 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -1,16 +1,14 @@ import { MikroOrmModule } from '@mikro-orm/nestjs'; -import { DynamicModule, Inject, MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; +import { DynamicModule, Module } from '@nestjs/common'; import { ALL_ENTITIES } from '@shared/domain'; import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config'; -import { LegacyLogger, LoggerModule } from '@src/core/logger'; +import { LoggerModule } from '@src/core/logger'; import { ConfigModule } from '@nestjs/config'; import { RabbitMQWrapperModule, RabbitMQWrapperTestModule } from '@src/infra/rabbitmq'; import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@src/infra/database'; import { FileEntity } from '@modules/files/entity'; import { FileRecord } from '@modules/files-storage/entity'; -import { RedisClient } from 'redis'; -import { REDIS_CLIENT, RedisModule } from '@src/infra/redis'; -import { defaultMikroOrmOptions, setupSessions } from './server.module'; +import { defaultMikroOrmOptions } from './server.module'; import { serverConfig } from './server.config'; import { AdminApiServerController } from './controller'; import { DeletionApiModule } from '../deletion/deletion-api.module'; @@ -31,22 +29,10 @@ const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConf debug: true, }), LoggerModule, - RedisModule, ], controllers: [AdminApiServerController], }) -export class AdminApiServerModule implements NestModule { - constructor( - @Inject(REDIS_CLIENT) private readonly redisClient: RedisClient | undefined, - private readonly logger: LegacyLogger - ) { - logger.setContext(AdminApiServerModule.name); - } - - configure(consumer: MiddlewareConsumer) { - setupSessions(consumer, this.redisClient, this.logger); - } -} +export class AdminApiServerModule {} @Module({ imports: [ @@ -54,22 +40,10 @@ export class AdminApiServerModule implements NestModule { MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions }), RabbitMQWrapperTestModule, LoggerModule, - RedisModule, ], controllers: [AdminApiServerController], }) -export class AdminApiServerTestModule implements NestModule { - constructor( - @Inject(REDIS_CLIENT) private readonly redisClient: RedisClient | undefined, - private readonly logger: LegacyLogger - ) { - logger.setContext(AdminApiServerTestModule.name); - } - - configure(consumer: MiddlewareConsumer) { - setupSessions(consumer, undefined, this.logger); - } - +export class AdminApiServerTestModule { static forRoot(options?: MongoDatabaseModuleOptions): DynamicModule { return { module: AdminApiServerTestModule, diff --git a/apps/server/src/modules/server/server.module.ts b/apps/server/src/modules/server/server.module.ts index 030f91207a1..333f3ef6d4c 100644 --- a/apps/server/src/modules/server/server.module.ts +++ b/apps/server/src/modules/server/server.module.ts @@ -85,11 +85,7 @@ export const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { new NotFoundException(`The requested ${entityName}: ${where} has not been found.`), }; -export const setupSessions = ( - consumer: MiddlewareConsumer, - redisClient: RedisClient | undefined, - logger: LegacyLogger -) => { +const setupSessions = (consumer: MiddlewareConsumer, redisClient: RedisClient | undefined, logger: LegacyLogger) => { const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number; let store: connectRedis.RedisStore | undefined; From 41fcd0c7b7672141f27c67140e2b540d72c05c12 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Fri, 24 Nov 2023 10:53:04 +0100 Subject: [PATCH 69/96] use timers in tests --- .../api-test/deletion-request-create.api.spec.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index e5c06bfe5b9..ad4652f1078 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -83,6 +83,14 @@ describe(`deletionRequest create (api)`, () => { }; describe('when create deletionRequest', () => { + beforeAll(() => { + jest.useFakeTimers(); + jest.setSystemTime(new Date()); + }); + + afterAll(() => { + jest.useRealTimers(); + }); it('should return status 202', async () => { const { deletionRequestToCreate } = setup(); From a254858967f5d8dad2fc6d093a26fb4c813ac604 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Fri, 24 Nov 2023 11:04:38 +0100 Subject: [PATCH 70/96] revert last commit --- .../api-test/deletion-request-create.api.spec.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index ad4652f1078..e5c06bfe5b9 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -83,14 +83,6 @@ describe(`deletionRequest create (api)`, () => { }; describe('when create deletionRequest', () => { - beforeAll(() => { - jest.useFakeTimers(); - jest.setSystemTime(new Date()); - }); - - afterAll(() => { - jest.useRealTimers(); - }); it('should return status 202', async () => { const { deletionRequestToCreate } = setup(); From fe26bb0abc7d553eb9c234ad81e5669942f2eda3 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Mon, 27 Nov 2023 08:50:00 +0100 Subject: [PATCH 71/96] add testXApiKeyClient --- .../api-test/deletion-executions.api.spec.ts | 30 ++------ .../deletion-request-create.api.spec.ts | 39 +++-------- .../deletion-request-delete.api.spec.ts | 31 ++------- .../deletion-request-find.api.spec.ts | 35 ++-------- apps/server/src/shared/testing/index.ts | 1 + .../src/shared/testing/test-xApiKey-client.ts | 69 +++++++++++++++++++ 6 files changed, 96 insertions(+), 109 deletions(-) create mode 100644 apps/server/src/shared/testing/test-xApiKey-client.ts diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts index 19638c7eac3..018826fc99e 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts @@ -1,37 +1,15 @@ import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; -import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; +import { TestXApiKeyClient } from '@shared/testing'; import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; -import { DeletionExecutionParams } from '../dto'; const baseRouteName = '/deletionExecutions'; -class API { - app: INestApplication; - - constructor(app: INestApplication) { - this.app = app; - } - - async post(query?: DeletionExecutionParams) { - const response = await request(this.app.getHttpServer()) - .post(`${baseRouteName}`) - .set('Accept', 'application/json') - .query(query || {}); - - return { - error: response.body as ApiValidationError, - status: response.status, - }; - } -} - describe(`deletionExecution (api)`, () => { let app: INestApplication; - let api: API; + let testXApiKeyClient: TestXApiKeyClient; const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; beforeAll(async () => { @@ -50,7 +28,7 @@ describe(`deletionExecution (api)`, () => { app = module.createNestApplication(); await app.init(); - api = new API(app); + testXApiKeyClient = new TestXApiKeyClient(app, baseRouteName); }); afterAll(async () => { @@ -59,7 +37,7 @@ describe(`deletionExecution (api)`, () => { describe('when execute deletionRequests with default limit', () => { it('should return status 204', async () => { - const response = await api.post(); + const response = await testXApiKeyClient.post(); expect(response.status).toEqual(204); }); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index e5c06bfe5b9..7c78c215375 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -1,10 +1,9 @@ import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; -import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; import { EntityManager } from '@mikro-orm/mongodb'; +import { TestXApiKeyClient } from '@shared/testing'; import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; import { DeletionRequestBodyProps, DeletionRequestResponse } from '../dto'; import { DeletionDomainModel } from '../../domain/types'; @@ -12,31 +11,10 @@ import { DeletionRequestEntity } from '../../entity'; const baseRouteName = '/deletionRequests'; -class API { - app: INestApplication; - - constructor(app: INestApplication) { - this.app = app; - } - - async post(requestBody: object) { - const response = await request(this.app.getHttpServer()) - .post(`${baseRouteName}`) - .set('Accept', 'application/json') - .send(requestBody); - - return { - result: response.body as DeletionRequestResponse, - error: response.body as ApiValidationError, - status: response.status, - }; - } -} - describe(`deletionRequest create (api)`, () => { let app: INestApplication; let em: EntityManager; - let api: API; + let testXApiKeyClient: TestXApiKeyClient; const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; beforeAll(async () => { @@ -56,7 +34,7 @@ describe(`deletionRequest create (api)`, () => { app = module.createNestApplication(); await app.init(); em = module.get(EntityManager); - api = new API(app); + testXApiKeyClient = new TestXApiKeyClient(app, baseRouteName); }); afterAll(async () => { @@ -86,7 +64,7 @@ describe(`deletionRequest create (api)`, () => { it('should return status 202', async () => { const { deletionRequestToCreate } = setup(); - const response = await api.post(deletionRequestToCreate); + const response = await testXApiKeyClient.post('', deletionRequestToCreate); expect(response.status).toEqual(202); }); @@ -94,16 +72,18 @@ describe(`deletionRequest create (api)`, () => { it('should return the created deletionRequest', async () => { const { deletionRequestToCreate } = setup(); - const { result } = await api.post(deletionRequestToCreate); + const response = await testXApiKeyClient.post('', deletionRequestToCreate); + const result = response.body as DeletionRequestResponse; expect(result.requestId).toBeDefined(); }); it('should create deletionRequest with default deletion time (add 43200 minutes to current time) ', async () => { const { deletionRequestToCreate } = setup(); - const { result } = await api.post(deletionRequestToCreate); + const response = await testXApiKeyClient.post('', deletionRequestToCreate); + const result = response.body as DeletionRequestResponse; const createdDeletionRequestId = result.requestId; const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId); @@ -117,8 +97,9 @@ describe(`deletionRequest create (api)`, () => { it('should create deletionRequest with deletion time (0 minutes to current time) ', async () => { const { deletionRequestToImmediateRemoval } = setup(); - const { result } = await api.post(deletionRequestToImmediateRemoval); + const response = await testXApiKeyClient.post('', deletionRequestToImmediateRemoval); + const result = response.body as DeletionRequestResponse; const createdDeletionRequestId = result.requestId; const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts index cc86039dc07..fb8ed9fe0eb 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts @@ -1,10 +1,8 @@ import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; -import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; -import { cleanupCollections } from '@shared/testing'; +import { TestXApiKeyClient, cleanupCollections } from '@shared/testing'; import { EntityManager } from '@mikro-orm/mongodb'; import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; import { deletionRequestEntityFactory } from '../../entity/testing'; @@ -12,29 +10,10 @@ import { DeletionRequestEntity } from '../../entity'; const baseRouteName = '/deletionRequests'; -class API { - app: INestApplication; - - constructor(app: INestApplication) { - this.app = app; - } - - async delete(deletionRequestId: string) { - const response = await request(this.app.getHttpServer()) - .delete(`${baseRouteName}/${deletionRequestId}`) - .set('Accept', 'application/json'); - - return { - error: response.body as ApiValidationError, - status: response.status, - }; - } -} - describe(`deletionRequest delete (api)`, () => { let app: INestApplication; let em: EntityManager; - let api: API; + let testXApiKeyClient: TestXApiKeyClient; const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; beforeAll(async () => { @@ -54,7 +33,7 @@ describe(`deletionRequest delete (api)`, () => { app = module.createNestApplication(); await app.init(); em = module.get(EntityManager); - api = new API(app); + testXApiKeyClient = new TestXApiKeyClient(app, baseRouteName); }); afterAll(async () => { @@ -75,7 +54,7 @@ describe(`deletionRequest delete (api)`, () => { it('should return status 204', async () => { const { deletionRequest } = await setup(); - const response = await api.delete(deletionRequest.id); + const response = await testXApiKeyClient.delete(deletionRequest.id); expect(response.status).toEqual(204); }); @@ -83,7 +62,7 @@ describe(`deletionRequest delete (api)`, () => { it('should actually delete deletionRequest', async () => { const { deletionRequest } = await setup(); - await api.delete(deletionRequest.id); + await testXApiKeyClient.delete(deletionRequest.id); await expect(em.findOneOrFail(DeletionRequestEntity, deletionRequest.id)).rejects.toThrow(); }); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts index d2e4d089005..7adebfc0513 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts @@ -1,41 +1,19 @@ import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { ApiValidationError } from '@shared/common'; import { Request } from 'express'; -import request from 'supertest'; import { AuthGuard } from '@nestjs/passport'; import { EntityManager } from '@mikro-orm/mongodb'; -import { cleanupCollections } from '@shared/testing'; +import { TestXApiKeyClient, cleanupCollections } from '@shared/testing'; import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; -import { DeletionRequestLogResponse } from '../dto'; import { deletionRequestEntityFactory } from '../../entity/testing'; +import { DeletionRequestLogResponse } from '../dto'; const baseRouteName = '/deletionRequests'; -class API { - app: INestApplication; - - constructor(app: INestApplication) { - this.app = app; - } - - async get(deletionRequestId: string) { - const response = await request(this.app.getHttpServer()) - .get(`${baseRouteName}/${deletionRequestId}`) - .set('Accept', 'application/json'); - - return { - result: response.body as DeletionRequestLogResponse, - error: response.body as ApiValidationError, - status: response.status, - }; - } -} - describe(`deletionRequest find (api)`, () => { let app: INestApplication; let em: EntityManager; - let api: API; + let testXApiKeyClient: TestXApiKeyClient; const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; beforeAll(async () => { @@ -55,7 +33,7 @@ describe(`deletionRequest find (api)`, () => { app = module.createNestApplication(); await app.init(); em = module.get(EntityManager); - api = new API(app); + testXApiKeyClient = new TestXApiKeyClient(app, baseRouteName); }); afterAll(async () => { @@ -76,7 +54,7 @@ describe(`deletionRequest find (api)`, () => { it('should return status 202', async () => { const { deletionRequest } = await setup(); - const response = await api.get(deletionRequest.id); + const response = await testXApiKeyClient.get(deletionRequest.id); expect(response.status).toEqual(200); }); @@ -84,7 +62,8 @@ describe(`deletionRequest find (api)`, () => { it('should return the found deletionRequest', async () => { const { deletionRequest } = await setup(); - const { result } = await api.get(deletionRequest.id); + const response = await testXApiKeyClient.get(deletionRequest.id); + const result = response.body as DeletionRequestLogResponse; expect(result.targetRef.id).toEqual(deletionRequest.targetRefId); }); diff --git a/apps/server/src/shared/testing/index.ts b/apps/server/src/shared/testing/index.ts index 7640045df95..afb9facad15 100644 --- a/apps/server/src/shared/testing/index.ts +++ b/apps/server/src/shared/testing/index.ts @@ -3,3 +3,4 @@ export * from './setup-entities'; export * from './cleanup-collections'; export * from './map-user-to-current-user'; export * from './test-api-client'; +export * from './test-xApiKey-client'; diff --git a/apps/server/src/shared/testing/test-xApiKey-client.ts b/apps/server/src/shared/testing/test-xApiKey-client.ts new file mode 100644 index 00000000000..8be9e319bc7 --- /dev/null +++ b/apps/server/src/shared/testing/test-xApiKey-client.ts @@ -0,0 +1,69 @@ +import { INestApplication } from '@nestjs/common'; +import supertest from 'supertest'; + +export class TestXApiKeyClient { + private readonly app: INestApplication; + + private readonly baseRoute: string; + + constructor(app: INestApplication, baseRoute: string) { + this.app = app; + this.baseRoute = this.checkAndAddPrefix(baseRoute); + } + + public get(subPath?: string): supertest.Test { + const path = this.getPath(subPath); + const testRequestInstance = supertest(this.app.getHttpServer()).get(path).set('Accept', 'application/json'); + + return testRequestInstance; + } + + public delete(subPath?: string): supertest.Test { + const path = this.getPath(subPath); + const testRequestInstance = supertest(this.app.getHttpServer()).delete(path).set('Accept', 'application/json'); + + return testRequestInstance; + } + + public post(subPath?: string, data = {}): supertest.Test { + const path = this.getPath(subPath); + const testRequestInstance = supertest(this.app.getHttpServer()) + .post(path) + .set('Accept', 'application/json') + .send(data); + + return testRequestInstance; + } + + private isSlash(inputPath: string, pos: number): boolean { + const isSlash = inputPath.charAt(pos) === '/'; + + return isSlash; + } + + private checkAndAddPrefix(inputPath = '/'): string { + let path = ''; + if (!this.isSlash(inputPath, 0)) { + path = '/'; + } + path += inputPath; + + return path; + } + + private cleanupPath(inputPath: string): string { + let path = inputPath; + if (this.isSlash(path, 0) && this.isSlash(path, 1)) { + path = path.slice(1); + } + + return path; + } + + private getPath(routeNameInput = ''): string { + const routeName = this.checkAndAddPrefix(routeNameInput); + const path = this.cleanupPath(this.baseRoute + routeName); + + return path; + } +} From bb48803e203f9469919f9ba03c9d62766e90df00 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Mon, 27 Nov 2023 09:27:50 +0100 Subject: [PATCH 72/96] deployment impl --- .../schulcloud-server-core/tasks/main.yml | 14 +++++ .../admin-api-server-deployment.yml.j2 | 57 +++++++++++++++++++ .../templates/admin-api-server-svc.yml.j2 | 17 ++++++ apps/server/src/apps/admin-api-server.app.ts | 42 ++++++++++++++ apps/server/src/apps/server.app.ts | 30 ---------- nest-cli.json | 9 +++ package.json | 3 + 7 files changed, 142 insertions(+), 30 deletions(-) create mode 100644 ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 create mode 100644 ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 create mode 100644 apps/server/src/apps/admin-api-server.app.ts diff --git a/ansible/roles/schulcloud-server-core/tasks/main.yml b/ansible/roles/schulcloud-server-core/tasks/main.yml index 64e257c5f59..51728abc431 100644 --- a/ansible/roles/schulcloud-server-core/tasks/main.yml +++ b/ansible/roles/schulcloud-server-core/tasks/main.yml @@ -142,3 +142,17 @@ when: - KEDA_ENABLED is defined and KEDA_ENABLED|bool - SCALED_PREVIEW_GENERATOR_ENABLED is defined and SCALED_PREVIEW_GENERATOR_ENABLED|bool + + + - name: AdminApiServerDeployment + kubernetes.core.k8s: + kubeconfig: ~/.kube/config + namespace: "{{ NAMESPACE }}" + template: admin-api-server-deployment.yml.j2 + + - name: AdminApiServerService + kubernetes.core.k8s: + kubeconfig: ~/.kube/config + namespace: "{{ NAMESPACE }}" + template: admin-api-server-svc.yml.j2 + diff --git a/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 b/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 new file mode 100644 index 00000000000..7c9d2a7ac76 --- /dev/null +++ b/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 @@ -0,0 +1,57 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: admin-api-deployment + namespace: {{ NAMESPACE }} + labels: + app: admin-api-server + app.kubernetes.io/part-of: schulcloud-verbund + app.kubernetes.io/version: {{ SCHULCLOUD_SERVER_IMAGE_TAG }} + app.kubernetes.io/name: admin-api-server + app.kubernetes.io/component: admin-api + app.kubernetes.io/managed-by: ansible + git.branch: {{ SCHULCLOUD_SERVER_BRANCH_NAME }} + git.repo: {{ SCHULCLOUD_SERVER_REPO_NAME }} +spec: + replicas: {{ TLDRAW_SERVER_REPLICAS|default("1", true) }} + strategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 1 + #maxUnavailable: 1 + revisionHistoryLimit: 4 + paused: false + selector: + matchLabels: + app: admin-api-server + template: + metadata: + labels: + app: admin-api-server + spec: + securityContext: + runAsUser: 1000 + runAsGroup: 1000 + fsGroup: 1000 + runAsNonRoot: true + containers: + - name: admin-api + image: {{ SCHULCLOUD_SERVER_IMAGE }}:{{ SCHULCLOUD_SERVER_IMAGE_TAG }} + imagePullPolicy: IfNotPresent + ports: + - containerPort: 4030 + name: admin-api-server + protocol: TCP + envFrom: + - configMapRef: + name: api-configmap + - secretRef: + name: api-secret + command: ['npm', 'run', 'nest:start:admin-api-server:prod'] + resources: + limits: + cpu: {{ ADMIN_API_SERVER_CPU_LIMITS|default("2000m", true) }} + memory: {{ ADMIN_API_SERVER_MEMORY_LIMITS|default("4Gi", true) }} + requests: + cpu: {{ ADMIN_API_SERVER_CPU_REQUESTS|default("100m", true) }} + memory: {{ ADMIN_API_SERVER_MEMORY_REQUESTS|default("150Mi", true) }} \ No newline at end of file diff --git a/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 b/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 new file mode 100644 index 00000000000..46c9669bc2b --- /dev/null +++ b/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Service +metadata: + name: admin-api-server-svc + namespace: {{ NAMESPACE }} + labels: + app: admin-api-server +spec: + type: ClusterIP + ports: + # port for http managing drawing data + - port: 4030 + targetPort: 4030 + protocol: TCP + name: admin-api-server + selector: + app: admin-api-server \ No newline at end of file diff --git a/apps/server/src/apps/admin-api-server.app.ts b/apps/server/src/apps/admin-api-server.app.ts new file mode 100644 index 00000000000..672bea487c2 --- /dev/null +++ b/apps/server/src/apps/admin-api-server.app.ts @@ -0,0 +1,42 @@ +/* istanbul ignore file */ +/* eslint-disable no-console */ +import { NestFactory } from '@nestjs/core'; +import { install as sourceMapInstall } from 'source-map-support'; +import { LegacyLogger, Logger } from '@src/core/logger'; +import { enableOpenApiDocs } from '@shared/controller/swagger'; +import { AppStartLoggable } from '@src/apps/helpers/app-start-loggable'; +import { ExpressAdapter } from '@nestjs/platform-express'; +import express from 'express'; +import { AdminApiServerModule } from '@src/modules/server/admin-api.server.module'; +import { Configuration } from '@hpi-schul-cloud/commons/lib'; + +async function bootstrap() { + sourceMapInstall(); + + const nestAdminServerExpress = express(); + const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); + nestAdminServerExpressAdapter.disable('x-powered-by'); + const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); + nestAdminServerApp.useLogger(await nestAdminServerApp.resolve(LegacyLogger)); + nestAdminServerApp.enableCors(); + + const logger = await nestAdminServerApp.resolve(Logger); + enableOpenApiDocs(nestAdminServerApp, 'docs'); + nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); + + await nestAdminServerApp.init(); + + const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; + + nestAdminServerExpress.listen(adminApiServerPort, () => { + logger.info( + new AppStartLoggable({ + appName: 'Admin API server app', + port: adminApiServerPort, + mountsDescription: `/admin/api/v1 --> Admin API Server`, + }) + ); + }); +} + +void bootstrap(); diff --git a/apps/server/src/apps/server.app.ts b/apps/server/src/apps/server.app.ts index 538aa5e58df..b3cbc7de181 100644 --- a/apps/server/src/apps/server.app.ts +++ b/apps/server/src/apps/server.app.ts @@ -19,12 +19,8 @@ import { enableOpenApiDocs } from '@shared/controller/swagger'; import { LegacyLogger, Logger } from '@src/core/logger'; import express from 'express'; import { join } from 'path'; - // register source-map-support for debugging import { install as sourceMapInstall } from 'source-map-support'; -import { Configuration } from '@hpi-schul-cloud/commons/lib'; -import { AdminApiServerModule } from '@src/modules/server/admin-api.server.module'; - import { AppStartLoggable } from './helpers/app-start-loggable'; import { addPrometheusMetricsMiddlewaresIfEnabled, @@ -32,28 +28,6 @@ import { } from './helpers/prometheus-metrics'; import legacyAppPromise = require('../../../../src/app'); -const createAndStartAdminApiServer = async (logger: Logger) => { - const nestAdminServerExpress = express(); - nestAdminServerExpress.disable('x-powered-by'); - const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); - const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); - - nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); - await nestAdminServerApp.init(); - - const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; - - nestAdminServerExpress.listen(adminApiServerPort, () => { - logger.info( - new AppStartLoggable({ - appName: 'Admin API server app', - port: adminApiServerPort, - mountsDescription: `/admin/api/v1 --> Admin API Server`, - }) - ); - }); -}; - async function bootstrap() { sourceMapInstall(); @@ -154,10 +128,6 @@ async function bootstrap() { createAndStartPrometheusMetricsAppIfEnabled(logger); }); - if (Configuration.get('ADMIN_API__ENABLED') === true) { - await createAndStartAdminApiServer(logger); - } - console.log('#################################'); console.log(`### Start Server ###`); console.log(`### Port: ${port} ###`); diff --git a/nest-cli.json b/nest-cli.json index b3fd935b81f..ee3b4670360 100644 --- a/nest-cli.json +++ b/nest-cli.json @@ -107,6 +107,15 @@ "compilerOptions": { "tsConfigPath": "apps/server/tsconfig.app.json" } + }, + "admin-api-server": { + "type": "application", + "root": "apps/server", + "entryFile": "apps/admin-api-server.app", + "sourceRoot": "apps/server/src", + "compilerOptions": { + "tsConfigPath": "apps/server/tsconfig.app.json" + } } } } diff --git a/package.json b/package.json index 2cca72a230e..3afead6debd 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,9 @@ "nest:start:debug": "nest start server --debug --watch", "nest:start:prod": "node dist/apps/server/apps/server.app", "nest:start:management": "nest start management", + "nest:start:admin-api-server": "nest start admin-api-server", + "nest:start:admin-api-server:dev": "nest start admin-api-server --debug --watch", + "nest:start:admin-api-server:prod": "node dist/apps/server/apps/admin-api-server.app", "nest:start:management:dev": "nest start management --watch", "nest:start:management:debug": "nest start management --debug --watch", "nest:start:management:prod": "node dist/apps/server/apps/management.app", From ee6c312c5779e2ea9a248cb47d01846889d70c8a Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Mon, 27 Nov 2023 09:30:59 +0100 Subject: [PATCH 73/96] add newlines --- apps/server/src/apps/server.app.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/server/src/apps/server.app.ts b/apps/server/src/apps/server.app.ts index b3cbc7de181..7f51ee9b92e 100644 --- a/apps/server/src/apps/server.app.ts +++ b/apps/server/src/apps/server.app.ts @@ -19,8 +19,10 @@ import { enableOpenApiDocs } from '@shared/controller/swagger'; import { LegacyLogger, Logger } from '@src/core/logger'; import express from 'express'; import { join } from 'path'; + // register source-map-support for debugging import { install as sourceMapInstall } from 'source-map-support'; + import { AppStartLoggable } from './helpers/app-start-loggable'; import { addPrometheusMetricsMiddlewaresIfEnabled, From 7c229e2b04211603d48fda4c57a6447bfb089324 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Mon, 27 Nov 2023 11:03:29 +0100 Subject: [PATCH 74/96] add test --- .../api-test/deletion-executions.api.spec.ts | 2 +- .../deletion-request-delete.api.spec.ts | 4 +- .../deletion-request-find.api.spec.ts | 4 +- .../testing/test-xApiKey-client.spec.ts | 93 +++++++++++++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 apps/server/src/shared/testing/test-xApiKey-client.spec.ts diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts index 018826fc99e..3333458a40b 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts @@ -37,7 +37,7 @@ describe(`deletionExecution (api)`, () => { describe('when execute deletionRequests with default limit', () => { it('should return status 204', async () => { - const response = await testXApiKeyClient.post(); + const response = await testXApiKeyClient.post(''); expect(response.status).toEqual(204); }); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts index fb8ed9fe0eb..ea532af509e 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts @@ -54,7 +54,7 @@ describe(`deletionRequest delete (api)`, () => { it('should return status 204', async () => { const { deletionRequest } = await setup(); - const response = await testXApiKeyClient.delete(deletionRequest.id); + const response = await testXApiKeyClient.delete(`${deletionRequest.id}`); expect(response.status).toEqual(204); }); @@ -62,7 +62,7 @@ describe(`deletionRequest delete (api)`, () => { it('should actually delete deletionRequest', async () => { const { deletionRequest } = await setup(); - await testXApiKeyClient.delete(deletionRequest.id); + await testXApiKeyClient.delete(`${deletionRequest.id}`); await expect(em.findOneOrFail(DeletionRequestEntity, deletionRequest.id)).rejects.toThrow(); }); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts index 7adebfc0513..b58dc9e4746 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts @@ -54,7 +54,7 @@ describe(`deletionRequest find (api)`, () => { it('should return status 202', async () => { const { deletionRequest } = await setup(); - const response = await testXApiKeyClient.get(deletionRequest.id); + const response = await testXApiKeyClient.get(`${deletionRequest.id}`); expect(response.status).toEqual(200); }); @@ -62,7 +62,7 @@ describe(`deletionRequest find (api)`, () => { it('should return the found deletionRequest', async () => { const { deletionRequest } = await setup(); - const response = await testXApiKeyClient.get(deletionRequest.id); + const response = await testXApiKeyClient.get(`${deletionRequest.id}`); const result = response.body as DeletionRequestLogResponse; expect(result.targetRef.id).toEqual(deletionRequest.targetRefId); diff --git a/apps/server/src/shared/testing/test-xApiKey-client.spec.ts b/apps/server/src/shared/testing/test-xApiKey-client.spec.ts new file mode 100644 index 00000000000..21a8d6d371a --- /dev/null +++ b/apps/server/src/shared/testing/test-xApiKey-client.spec.ts @@ -0,0 +1,93 @@ +import { Controller, Delete, ExecutionContext, Get, Headers, HttpStatus, INestApplication, Post } from '@nestjs/common'; +import { Test } from '@nestjs/testing'; +import { ObjectId } from 'bson'; +import { AuthGuard } from '@nestjs/passport'; +import { TestApiClient } from './test-api-client'; + +@Controller('') +class TestController { + @Delete(':id') + async delete(@Headers('X-API-KEY') authorization: string) { + return Promise.resolve({ method: 'delete', authorization }); + } + + @Post() + async post(@Headers('X-API-KEY') authorization: string) { + return Promise.resolve({ method: 'post', authorization }); + } + + @Get(':id') + async get(@Headers('X-API-KEY') authorization: string) { + return Promise.resolve({ method: 'get', authorization }); + } +} + +describe(TestApiClient.name, () => { + describe('when test request instance exists', () => { + let app: INestApplication; + const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; + + beforeAll(async () => { + const moduleFixture = await Test.createTestingModule({ + controllers: [TestController], + }) + .overrideGuard(AuthGuard('api-key')) + .useValue({ + canActivate(context: ExecutionContext) { + const req: Request = context.switchToHttp().getRequest(); + req.headers['X-API-KEY'] = API_KEY; + return true; + }, + }) + .compile(); + + app = moduleFixture.createNestApplication(); + + await app.init(); + }); + + afterAll(async () => { + await app.close(); + }); + + const setup = () => { + const testApiClient = new TestApiClient(app, ''); + const id = new ObjectId().toHexString(); + + return { testApiClient, id }; + }; + + describe('get', () => { + it('should resolve requests', async () => { + const { testApiClient, id } = setup(); + + const result = await testApiClient.get(id); + + expect(result.statusCode).toEqual(HttpStatus.OK); + expect(result.body).toEqual(expect.objectContaining({ method: 'get' })); + }); + }); + + describe('post', () => { + it('should resolve requests', async () => { + const { testApiClient } = setup(); + + const result = await testApiClient.post(); + + expect(result.statusCode).toEqual(HttpStatus.CREATED); + expect(result.body).toEqual(expect.objectContaining({ method: 'post' })); + }); + }); + + describe('delete', () => { + it('should resolve requests', async () => { + const { testApiClient, id } = setup(); + + const result = await testApiClient.delete(id); + + expect(result.statusCode).toEqual(HttpStatus.OK); + expect(result.body).toEqual(expect.objectContaining({ method: 'delete' })); + }); + }); + }); +}); From 2c373839f2f0278850837e4f24daa8324ff4e0ee Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Mon, 27 Nov 2023 11:27:43 +0100 Subject: [PATCH 75/96] change character length --- .../templates/admin-api-server-deployment.yml.j2 | 10 +++++----- .../templates/admin-api-server-svc.yml.j2 | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 b/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 index 7c9d2a7ac76..1999ff5d548 100644 --- a/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 @@ -4,10 +4,10 @@ metadata: name: admin-api-deployment namespace: {{ NAMESPACE }} labels: - app: admin-api-server + app: admin-api-serv app.kubernetes.io/part-of: schulcloud-verbund app.kubernetes.io/version: {{ SCHULCLOUD_SERVER_IMAGE_TAG }} - app.kubernetes.io/name: admin-api-server + app.kubernetes.io/name: admin-api-serv app.kubernetes.io/component: admin-api app.kubernetes.io/managed-by: ansible git.branch: {{ SCHULCLOUD_SERVER_BRANCH_NAME }} @@ -23,11 +23,11 @@ spec: paused: false selector: matchLabels: - app: admin-api-server + app: admin-api-serv template: metadata: labels: - app: admin-api-server + app: admin-api-serv spec: securityContext: runAsUser: 1000 @@ -40,7 +40,7 @@ spec: imagePullPolicy: IfNotPresent ports: - containerPort: 4030 - name: admin-api-server + name: admin-api-serv protocol: TCP envFrom: - configMapRef: diff --git a/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 b/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 index 46c9669bc2b..94431b6310b 100644 --- a/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 @@ -1,10 +1,10 @@ apiVersion: v1 kind: Service metadata: - name: admin-api-server-svc + name: admin-api-serv-svc namespace: {{ NAMESPACE }} labels: - app: admin-api-server + app: admin-api-serv spec: type: ClusterIP ports: @@ -12,6 +12,6 @@ spec: - port: 4030 targetPort: 4030 protocol: TCP - name: admin-api-server + name: admin-api-serv selector: - app: admin-api-server \ No newline at end of file + app: admin-api-serv \ No newline at end of file From d98d11cbc7b16d51b35e1cd6b0d9f10a0db1981e Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Mon, 27 Nov 2023 11:47:13 +0100 Subject: [PATCH 76/96] fix imports --- .../testing/test-xApiKey-client.spec.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/apps/server/src/shared/testing/test-xApiKey-client.spec.ts b/apps/server/src/shared/testing/test-xApiKey-client.spec.ts index 21a8d6d371a..83d9386b18d 100644 --- a/apps/server/src/shared/testing/test-xApiKey-client.spec.ts +++ b/apps/server/src/shared/testing/test-xApiKey-client.spec.ts @@ -2,7 +2,7 @@ import { Controller, Delete, ExecutionContext, Get, Headers, HttpStatus, INestAp import { Test } from '@nestjs/testing'; import { ObjectId } from 'bson'; import { AuthGuard } from '@nestjs/passport'; -import { TestApiClient } from './test-api-client'; +import { TestXApiKeyClient } from './test-xApiKey-client'; @Controller('') class TestController { @@ -22,7 +22,7 @@ class TestController { } } -describe(TestApiClient.name, () => { +describe(TestXApiKeyClient.name, () => { describe('when test request instance exists', () => { let app: INestApplication; const API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; @@ -51,17 +51,17 @@ describe(TestApiClient.name, () => { }); const setup = () => { - const testApiClient = new TestApiClient(app, ''); + const testXApiKeyClient = new TestXApiKeyClient(app, ''); const id = new ObjectId().toHexString(); - return { testApiClient, id }; + return { testXApiKeyClient, id }; }; describe('get', () => { it('should resolve requests', async () => { - const { testApiClient, id } = setup(); + const { testXApiKeyClient, id } = setup(); - const result = await testApiClient.get(id); + const result = await testXApiKeyClient.get(id); expect(result.statusCode).toEqual(HttpStatus.OK); expect(result.body).toEqual(expect.objectContaining({ method: 'get' })); @@ -70,9 +70,9 @@ describe(TestApiClient.name, () => { describe('post', () => { it('should resolve requests', async () => { - const { testApiClient } = setup(); + const { testXApiKeyClient } = setup(); - const result = await testApiClient.post(); + const result = await testXApiKeyClient.post(); expect(result.statusCode).toEqual(HttpStatus.CREATED); expect(result.body).toEqual(expect.objectContaining({ method: 'post' })); @@ -81,9 +81,9 @@ describe(TestApiClient.name, () => { describe('delete', () => { it('should resolve requests', async () => { - const { testApiClient, id } = setup(); + const { testXApiKeyClient, id } = setup(); - const result = await testApiClient.delete(id); + const result = await testXApiKeyClient.delete(id); expect(result.statusCode).toEqual(HttpStatus.OK); expect(result.body).toEqual(expect.objectContaining({ method: 'delete' })); From fc41e5fe1b261c7a1e1404d2a7f0ac2cec47373b Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Mon, 27 Nov 2023 12:21:12 +0100 Subject: [PATCH 77/96] fix deployment in PR --- .../schulcloud-server-core/tasks/main.yml | 4 ++-- .../admin-api-server-deployment.yml.j2 | 21 ++++++++++++------- .../templates/admin-api-server-svc.yml.j2 | 8 +++---- .../templates/deployment.yml.j2 | 3 --- .../templates/svc.yml.j2 | 4 ---- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/ansible/roles/schulcloud-server-core/tasks/main.yml b/ansible/roles/schulcloud-server-core/tasks/main.yml index 51728abc431..bc64b251b58 100644 --- a/ansible/roles/schulcloud-server-core/tasks/main.yml +++ b/ansible/roles/schulcloud-server-core/tasks/main.yml @@ -144,13 +144,13 @@ - SCALED_PREVIEW_GENERATOR_ENABLED is defined and SCALED_PREVIEW_GENERATOR_ENABLED|bool - - name: AdminApiServerDeployment + - name: admin api server deployment kubernetes.core.k8s: kubeconfig: ~/.kube/config namespace: "{{ NAMESPACE }}" template: admin-api-server-deployment.yml.j2 - - name: AdminApiServerService + - name: admin api server service kubernetes.core.k8s: kubeconfig: ~/.kube/config namespace: "{{ NAMESPACE }}" diff --git a/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 b/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 index 1999ff5d548..3b336473895 100644 --- a/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 @@ -4,11 +4,11 @@ metadata: name: admin-api-deployment namespace: {{ NAMESPACE }} labels: - app: admin-api-serv + app: api-admin app.kubernetes.io/part-of: schulcloud-verbund app.kubernetes.io/version: {{ SCHULCLOUD_SERVER_IMAGE_TAG }} - app.kubernetes.io/name: admin-api-serv - app.kubernetes.io/component: admin-api + app.kubernetes.io/name: api-admin + app.kubernetes.io/component: server app.kubernetes.io/managed-by: ansible git.branch: {{ SCHULCLOUD_SERVER_BRANCH_NAME }} git.repo: {{ SCHULCLOUD_SERVER_REPO_NAME }} @@ -23,11 +23,18 @@ spec: paused: false selector: matchLabels: - app: admin-api-serv + app: api-admin template: metadata: labels: - app: admin-api-serv + app: api-admin + app.kubernetes.io/part-of: schulcloud-verbund + app.kubernetes.io/version: {{ SCHULCLOUD_SERVER_IMAGE_TAG }} + app.kubernetes.io/name: api-admin + app.kubernetes.io/component: server + app.kubernetes.io/managed-by: ansible + git.branch: {{ SCHULCLOUD_SERVER_BRANCH_NAME }} + git.repo: {{ SCHULCLOUD_SERVER_REPO_NAME }} spec: securityContext: runAsUser: 1000 @@ -35,12 +42,12 @@ spec: fsGroup: 1000 runAsNonRoot: true containers: - - name: admin-api + - name: api-admin image: {{ SCHULCLOUD_SERVER_IMAGE }}:{{ SCHULCLOUD_SERVER_IMAGE_TAG }} imagePullPolicy: IfNotPresent ports: - containerPort: 4030 - name: admin-api-serv + name: admin protocol: TCP envFrom: - configMapRef: diff --git a/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 b/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 index 94431b6310b..cde6dcef4cd 100644 --- a/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/admin-api-server-svc.yml.j2 @@ -1,10 +1,10 @@ apiVersion: v1 kind: Service metadata: - name: admin-api-serv-svc + name: api-admin-svc namespace: {{ NAMESPACE }} labels: - app: admin-api-serv + app: api-admin spec: type: ClusterIP ports: @@ -12,6 +12,6 @@ spec: - port: 4030 targetPort: 4030 protocol: TCP - name: admin-api-serv + name: admin selector: - app: admin-api-serv \ No newline at end of file + app: api-admin \ No newline at end of file diff --git a/ansible/roles/schulcloud-server-core/templates/deployment.yml.j2 b/ansible/roles/schulcloud-server-core/templates/deployment.yml.j2 index 07d639d1b6a..a18102bb55d 100644 --- a/ansible/roles/schulcloud-server-core/templates/deployment.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/deployment.yml.j2 @@ -49,9 +49,6 @@ spec: - containerPort: 3030 name: api protocol: TCP - - containerPort: 4030 - name: api-admin - protocol: TCP - containerPort: 9090 name: api-metrics protocol: TCP diff --git a/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 b/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 index 0ef85ec904d..99a1e9df8ac 100644 --- a/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/svc.yml.j2 @@ -12,10 +12,6 @@ spec: targetPort: 3030 protocol: TCP name: api - - port: 4030 - targetPort: 4030 - protocol: TCP - name: api-admin - port: {{ PORT_METRICS_SERVER }} targetPort: 9090 protocol: TCP From eea90090ee916ded3b7d39d66e101713fe424d2f Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 28 Nov 2023 13:59:09 +0100 Subject: [PATCH 78/96] PR fixes part 1 --- apps/server/src/apps/admin-api-server.app.ts | 1 - .../modules/authentication/config/index.ts | 1 + .../src/modules/authentication/index.ts | 1 + .../strategy/x-api-key.strategy.spec.ts | 24 ++-- ...ch-deletion-summary-detail.builder.spec.ts | 4 +- .../batch-deletion-summary-detail.builder.ts | 2 +- .../batch-deletion-summary.builder.spec.ts | 0 .../builder/batch-deletion-summary.builder.ts | 0 .../deletion-log-statistic.builder.spec.ts | 2 +- .../builder/deletion-log-statistic.builder.ts | 2 +- ...eletion-request-body-props.builder.spec.ts | 12 +- .../deletion-request-body-props.builder.ts | 4 +- ...etion-request-log-response.builder.spec.ts | 2 +- .../deletion-request-log-response.builder.ts | 2 +- .../deletion-target-ref.builder.spec.ts | 2 +- .../builder/deletion-target-ref.builder.ts | 2 +- .../deletion/{uc => }/builder/index.ts | 0 .../api-test/deletion-executions.api.spec.ts | 12 +- .../deletion-request-create.api.spec.ts | 94 +++++++------- .../deletion-request-delete.api.spec.ts | 38 +++--- .../deletion-request-find.api.spec.ts | 41 ++++--- .../deletion-executions.controller.spec.ts | 49 -------- .../deletion-requests.controller.spec.ts | 116 ------------------ .../dto/deletion-execution.params.spec.ts | 14 ++- .../dto/deletion-request-log.response.spec.ts | 5 +- .../dto/deletion-request-log.response.ts | 2 +- .../dto/deletion-request.body.params.spec.ts | 12 +- .../dto/deletion-request.body.params.ts | 5 +- .../entity/deletion-request.entity.spec.ts | 2 +- .../entity/deletion-request.entity.ts | 3 +- ...batch-deletion-summary-detail.interface.ts | 2 +- ...ch-deletion-summary-overall-status.enum.ts | 0 .../batch-deletion-summary.interface.ts | 0 .../deletion/{uc => }/interface/index.ts | 0 .../deletion/{uc => }/interface/interfaces.ts | 2 +- .../deletion/uc/batch-deletion.uc.spec.ts | 4 +- .../modules/deletion/uc/batch-deletion.uc.ts | 4 +- .../deletion/uc/deletion-request.uc.spec.ts | 5 +- .../deletion/uc/deletion-request.uc.ts | 4 +- apps/server/src/modules/deletion/uc/index.ts | 3 +- .../modules/server/admin-api.server.module.ts | 4 - .../admin-api-server.controller.spec.ts | 25 ---- .../controller/admin-api-server.controller.ts | 9 -- .../src/modules/server/server.config.ts | 2 +- config/default.schema.json | 5 - 45 files changed, 172 insertions(+), 351 deletions(-) create mode 100644 apps/server/src/modules/authentication/config/index.ts rename apps/server/src/modules/deletion/{uc => }/builder/batch-deletion-summary-detail.builder.spec.ts (96%) rename apps/server/src/modules/deletion/{uc => }/builder/batch-deletion-summary-detail.builder.ts (93%) rename apps/server/src/modules/deletion/{uc => }/builder/batch-deletion-summary.builder.spec.ts (100%) rename apps/server/src/modules/deletion/{uc => }/builder/batch-deletion-summary.builder.ts (100%) rename apps/server/src/modules/deletion/{uc => }/builder/deletion-log-statistic.builder.spec.ts (91%) rename apps/server/src/modules/deletion/{uc => }/builder/deletion-log-statistic.builder.ts (84%) rename apps/server/src/modules/deletion/{uc => }/builder/deletion-request-body-props.builder.spec.ts (72%) rename apps/server/src/modules/deletion/{uc => }/builder/deletion-request-body-props.builder.ts (72%) rename apps/server/src/modules/deletion/{uc => }/builder/deletion-request-log-response.builder.spec.ts (94%) rename apps/server/src/modules/deletion/{uc => }/builder/deletion-request-log-response.builder.ts (84%) rename apps/server/src/modules/deletion/{uc => }/builder/deletion-target-ref.builder.spec.ts (89%) rename apps/server/src/modules/deletion/{uc => }/builder/deletion-target-ref.builder.ts (83%) rename apps/server/src/modules/deletion/{uc => }/builder/index.ts (100%) delete mode 100644 apps/server/src/modules/deletion/controller/deletion-executions.controller.spec.ts delete mode 100644 apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts rename apps/server/src/modules/deletion/{uc => }/interface/batch-deletion-summary-detail.interface.ts (88%) rename apps/server/src/modules/deletion/{uc => }/interface/batch-deletion-summary-overall-status.enum.ts (100%) rename apps/server/src/modules/deletion/{uc => }/interface/batch-deletion-summary.interface.ts (100%) rename apps/server/src/modules/deletion/{uc => }/interface/index.ts (100%) rename apps/server/src/modules/deletion/{uc => }/interface/interfaces.ts (81%) delete mode 100644 apps/server/src/modules/server/controller/admin-api-server.controller.spec.ts delete mode 100644 apps/server/src/modules/server/controller/admin-api-server.controller.ts diff --git a/apps/server/src/apps/admin-api-server.app.ts b/apps/server/src/apps/admin-api-server.app.ts index 672bea487c2..c9b6e2d61f9 100644 --- a/apps/server/src/apps/admin-api-server.app.ts +++ b/apps/server/src/apps/admin-api-server.app.ts @@ -1,5 +1,4 @@ /* istanbul ignore file */ -/* eslint-disable no-console */ import { NestFactory } from '@nestjs/core'; import { install as sourceMapInstall } from 'source-map-support'; import { LegacyLogger, Logger } from '@src/core/logger'; diff --git a/apps/server/src/modules/authentication/config/index.ts b/apps/server/src/modules/authentication/config/index.ts new file mode 100644 index 00000000000..cd9c0d28b96 --- /dev/null +++ b/apps/server/src/modules/authentication/config/index.ts @@ -0,0 +1 @@ +export * from './x-api-key.config'; diff --git a/apps/server/src/modules/authentication/index.ts b/apps/server/src/modules/authentication/index.ts index 80e9d64ab69..fba2d4332d2 100644 --- a/apps/server/src/modules/authentication/index.ts +++ b/apps/server/src/modules/authentication/index.ts @@ -2,3 +2,4 @@ export { AuthenticationModule } from './authentication.module'; export { Authenticate, CurrentUser, JWT } from './decorator'; export { ICurrentUser } from './interface'; export { AuthenticationService } from './services'; +export { XApiKeyConfig } from './config'; diff --git a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts index 4db5d99d6ba..f71394f8b9d 100644 --- a/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts +++ b/apps/server/src/modules/authentication/strategy/x-api-key.strategy.spec.ts @@ -35,25 +35,29 @@ describe('XApiKeyStrategy', () => { }); describe('validate', () => { - const setup = () => { - const CORRECT_API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; - const INVALID_API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6778173'; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const done = jest.fn((error: Error | null, data: boolean | null) => {}); - - return { CORRECT_API_KEY, INVALID_API_KEY, done }; - }; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const done = jest.fn((error: Error | null, data: boolean | null) => {}); describe('when a valid api key is provided', () => { + const setup = () => { + const CORRECT_API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6'; + + return { CORRECT_API_KEY, done }; + }; it('should do nothing', () => { - const { CORRECT_API_KEY, done } = setup(); + const { CORRECT_API_KEY } = setup(); strategy.validate(CORRECT_API_KEY, done); expect(done).toBeCalledWith(null, true); }); }); describe('when a invalid api key is provided', () => { + const setup = () => { + const INVALID_API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6778173'; + + return { INVALID_API_KEY, done }; + }; it('should throw error', () => { - const { INVALID_API_KEY, done } = setup(); + const { INVALID_API_KEY } = setup(); strategy.validate(INVALID_API_KEY, done); expect(done).toBeCalledWith(new UnauthorizedException(), null); }); diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts b/apps/server/src/modules/deletion/builder/batch-deletion-summary-detail.builder.spec.ts similarity index 96% rename from apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts rename to apps/server/src/modules/deletion/builder/batch-deletion-summary-detail.builder.spec.ts index 43ea82e86d5..864563e97dc 100644 --- a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.spec.ts +++ b/apps/server/src/modules/deletion/builder/batch-deletion-summary-detail.builder.spec.ts @@ -1,7 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; -import { BatchDeletionSummaryDetail } from '..'; -import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../../services'; +import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../services'; import { BatchDeletionSummaryDetailBuilder } from './batch-deletion-summary-detail.builder'; +import { BatchDeletionSummaryDetail } from '../interface'; describe(BatchDeletionSummaryDetailBuilder.name, () => { describe(BatchDeletionSummaryDetailBuilder.build.name, () => { diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts b/apps/server/src/modules/deletion/builder/batch-deletion-summary-detail.builder.ts similarity index 93% rename from apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts rename to apps/server/src/modules/deletion/builder/batch-deletion-summary-detail.builder.ts index 9ebbce66171..3184a6f9028 100644 --- a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary-detail.builder.ts +++ b/apps/server/src/modules/deletion/builder/batch-deletion-summary-detail.builder.ts @@ -1,4 +1,4 @@ -import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../../services'; +import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../services'; import { BatchDeletionSummaryDetail } from '../interface'; export class BatchDeletionSummaryDetailBuilder { diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts b/apps/server/src/modules/deletion/builder/batch-deletion-summary.builder.spec.ts similarity index 100% rename from apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.spec.ts rename to apps/server/src/modules/deletion/builder/batch-deletion-summary.builder.spec.ts diff --git a/apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts b/apps/server/src/modules/deletion/builder/batch-deletion-summary.builder.ts similarity index 100% rename from apps/server/src/modules/deletion/uc/builder/batch-deletion-summary.builder.ts rename to apps/server/src/modules/deletion/builder/batch-deletion-summary.builder.ts diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts b/apps/server/src/modules/deletion/builder/deletion-log-statistic.builder.spec.ts similarity index 91% rename from apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts rename to apps/server/src/modules/deletion/builder/deletion-log-statistic.builder.spec.ts index 3bbdc108ab4..babef08f8d9 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.spec.ts +++ b/apps/server/src/modules/deletion/builder/deletion-log-statistic.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../domain/types'; import { DeletionLogStatisticBuilder } from '.'; describe(DeletionLogStatisticBuilder.name, () => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts b/apps/server/src/modules/deletion/builder/deletion-log-statistic.builder.ts similarity index 84% rename from apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts rename to apps/server/src/modules/deletion/builder/deletion-log-statistic.builder.ts index 91ce13bc827..2e467eed310 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-log-statistic.builder.ts +++ b/apps/server/src/modules/deletion/builder/deletion-log-statistic.builder.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../domain/types'; import { DeletionLogStatistic } from '../interface'; export class DeletionLogStatisticBuilder { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts b/apps/server/src/modules/deletion/builder/deletion-request-body-props.builder.spec.ts similarity index 72% rename from apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts rename to apps/server/src/modules/deletion/builder/deletion-request-body-props.builder.spec.ts index dcc2aa057e6..4a363d86a40 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.spec.ts +++ b/apps/server/src/modules/deletion/builder/deletion-request-body-props.builder.spec.ts @@ -1,4 +1,5 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { ObjectId } from 'bson'; +import { DeletionDomainModel } from '../domain/types'; import { DeletionRequestBodyPropsBuilder } from './deletion-request-body-props.builder'; describe(DeletionRequestBodyPropsBuilder.name, () => { @@ -6,11 +7,14 @@ describe(DeletionRequestBodyPropsBuilder.name, () => { jest.clearAllMocks(); }); describe('when create deletionRequestBodyParams', () => { - it('should build deletionRequestBodyParams with all attributes', () => { - // Arrange + const setup = () => { const domain = DeletionDomainModel.PSEUDONYMS; - const refId = '653e4833cc39e5907a1e18d2'; + const refId = new ObjectId().toHexString(); const deleteInMinutes = 1000; + return { domain, refId, deleteInMinutes }; + }; + it('should build deletionRequestBodyParams with all attributes', () => { + const { domain, refId, deleteInMinutes } = setup(); const result = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts b/apps/server/src/modules/deletion/builder/deletion-request-body-props.builder.ts similarity index 72% rename from apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts rename to apps/server/src/modules/deletion/builder/deletion-request-body-props.builder.ts index 70227da5abc..b7dcd72b441 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-body-props.builder.ts +++ b/apps/server/src/modules/deletion/builder/deletion-request-body-props.builder.ts @@ -1,6 +1,6 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types'; -import { DeletionRequestBodyProps } from '../../controller/dto'; +import { DeletionDomainModel } from '../domain/types'; +import { DeletionRequestBodyProps } from '../controller/dto'; export class DeletionRequestBodyPropsBuilder { static build(domain: DeletionDomainModel, id: EntityId, deleteInMinutes?: number): DeletionRequestBodyProps { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts b/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.spec.ts similarity index 94% rename from apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts rename to apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.spec.ts index 86f9991c881..eb1adeee7cd 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.spec.ts +++ b/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../domain/types'; import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from './index'; describe(DeletionRequestLogResponseBuilder.name, () => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.ts b/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.ts similarity index 84% rename from apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.ts rename to apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.ts index 9818793543e..be4b0ba5a96 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-request-log-response.builder.ts +++ b/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.ts @@ -1,4 +1,4 @@ -import { DeletionRequestLogResponse } from '../../controller/dto'; +import { DeletionRequestLogResponse } from '../controller/dto'; import { DeletionLogStatistic, DeletionTargetRef } from '../interface'; export class DeletionRequestLogResponseBuilder { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts b/apps/server/src/modules/deletion/builder/deletion-target-ref.builder.spec.ts similarity index 89% rename from apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts rename to apps/server/src/modules/deletion/builder/deletion-target-ref.builder.spec.ts index ed72b67f904..4667f290b80 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.spec.ts +++ b/apps/server/src/modules/deletion/builder/deletion-target-ref.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../domain/types'; import { DeletionTargetRefBuilder } from './index'; describe(DeletionTargetRefBuilder.name, () => { diff --git a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts b/apps/server/src/modules/deletion/builder/deletion-target-ref.builder.ts similarity index 83% rename from apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts rename to apps/server/src/modules/deletion/builder/deletion-target-ref.builder.ts index aa1420e8790..f98271ff317 100644 --- a/apps/server/src/modules/deletion/uc/builder/deletion-target-ref.builder.ts +++ b/apps/server/src/modules/deletion/builder/deletion-target-ref.builder.ts @@ -1,5 +1,5 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../domain/types'; import { DeletionTargetRef } from '../interface'; export class DeletionTargetRefBuilder { diff --git a/apps/server/src/modules/deletion/uc/builder/index.ts b/apps/server/src/modules/deletion/builder/index.ts similarity index 100% rename from apps/server/src/modules/deletion/uc/builder/index.ts rename to apps/server/src/modules/deletion/builder/index.ts diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts index 3333458a40b..fdf1a30bf7a 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-executions.api.spec.ts @@ -3,7 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { Request } from 'express'; import { AuthGuard } from '@nestjs/passport'; import { TestXApiKeyClient } from '@shared/testing'; -import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; +import { AdminApiServerTestModule } from '@modules/server/admin-api.server.module'; const baseRouteName = '/deletionExecutions'; @@ -35,11 +35,13 @@ describe(`deletionExecution (api)`, () => { await app.close(); }); - describe('when execute deletionRequests with default limit', () => { - it('should return status 204', async () => { - const response = await testXApiKeyClient.post(''); + describe('executeDeletions', () => { + describe('when execute deletionRequests with default limit', () => { + it('should return status 204', async () => { + const response = await testXApiKeyClient.post(''); - expect(response.status).toEqual(204); + expect(response.status).toEqual(204); + }); }); }); }); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts index 7c78c215375..d389b997944 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-create.api.spec.ts @@ -4,7 +4,7 @@ import { Request } from 'express'; import { AuthGuard } from '@nestjs/passport'; import { EntityManager } from '@mikro-orm/mongodb'; import { TestXApiKeyClient } from '@shared/testing'; -import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; +import { AdminApiServerTestModule } from '@modules/server/admin-api.server.module'; import { DeletionRequestBodyProps, DeletionRequestResponse } from '../dto'; import { DeletionDomainModel } from '../../domain/types'; import { DeletionRequestEntity } from '../../entity'; @@ -41,70 +41,72 @@ describe(`deletionRequest create (api)`, () => { await app.close(); }); - const setup = () => { - const deletionRequestToCreate: DeletionRequestBodyProps = { - targetRef: { - domain: DeletionDomainModel.USER, - id: '653e4833cc39e5907a1e18d2', - }, - }; + describe('createDeletionRequests', () => { + describe('when create deletionRequest', () => { + const setup = () => { + const deletionRequestToCreate: DeletionRequestBodyProps = { + targetRef: { + domain: DeletionDomainModel.USER, + id: '653e4833cc39e5907a1e18d2', + }, + }; - const deletionRequestToImmediateRemoval: DeletionRequestBodyProps = { - targetRef: { - domain: DeletionDomainModel.USER, - id: '653e4833cc39e5907a1e18d2', - }, - deleteInMinutes: 0, - }; + const deletionRequestToImmediateRemoval: DeletionRequestBodyProps = { + targetRef: { + domain: DeletionDomainModel.USER, + id: '653e4833cc39e5907a1e18d2', + }, + deleteInMinutes: 0, + }; - return { deletionRequestToCreate, deletionRequestToImmediateRemoval }; - }; + return { deletionRequestToCreate, deletionRequestToImmediateRemoval }; + }; - describe('when create deletionRequest', () => { - it('should return status 202', async () => { - const { deletionRequestToCreate } = setup(); + it('should return status 202', async () => { + const { deletionRequestToCreate } = setup(); - const response = await testXApiKeyClient.post('', deletionRequestToCreate); + const response = await testXApiKeyClient.post('', deletionRequestToCreate); - expect(response.status).toEqual(202); - }); + expect(response.status).toEqual(202); + }); - it('should return the created deletionRequest', async () => { - const { deletionRequestToCreate } = setup(); + it('should return the created deletionRequest', async () => { + const { deletionRequestToCreate } = setup(); - const response = await testXApiKeyClient.post('', deletionRequestToCreate); + const response = await testXApiKeyClient.post('', deletionRequestToCreate); - const result = response.body as DeletionRequestResponse; - expect(result.requestId).toBeDefined(); - }); + const result = response.body as DeletionRequestResponse; + expect(result.requestId).toBeDefined(); + }); - it('should create deletionRequest with default deletion time (add 43200 minutes to current time) ', async () => { - const { deletionRequestToCreate } = setup(); + it('should create deletionRequest with default deletion time (add 43200 minutes to current time) ', async () => { + const { deletionRequestToCreate } = setup(); - const response = await testXApiKeyClient.post('', deletionRequestToCreate); + const response = await testXApiKeyClient.post('', deletionRequestToCreate); - const result = response.body as DeletionRequestResponse; - const createdDeletionRequestId = result.requestId; + const result = response.body as DeletionRequestResponse; + const createdDeletionRequestId = result.requestId; - const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId); + const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId); - const deletionPlannedAt = createdItem.createdAt; - deletionPlannedAt.setMinutes(deletionPlannedAt.getMinutes() + 43200); + const deletionPlannedAt = createdItem.createdAt; + deletionPlannedAt.setMinutes(deletionPlannedAt.getMinutes() + 43200); - expect(createdItem.deleteAfter).toEqual(deletionPlannedAt); - }); + expect(createdItem.deleteAfter).toEqual(deletionPlannedAt); + }); - it('should create deletionRequest with deletion time (0 minutes to current time) ', async () => { - const { deletionRequestToImmediateRemoval } = setup(); + it('should create deletionRequest with deletion time (0 minutes to current time) ', async () => { + const { deletionRequestToImmediateRemoval } = setup(); - const response = await testXApiKeyClient.post('', deletionRequestToImmediateRemoval); + const response = await testXApiKeyClient.post('', deletionRequestToImmediateRemoval); - const result = response.body as DeletionRequestResponse; - const createdDeletionRequestId = result.requestId; + const result = response.body as DeletionRequestResponse; + const createdDeletionRequestId = result.requestId; - const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId); + const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId); - expect(createdItem.createdAt).toEqual(createdItem.deleteAfter); + expect(createdItem.createdAt).toEqual(createdItem.deleteAfter); + }); }); }); }); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts index ea532af509e..1c049cba0bc 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-delete.api.spec.ts @@ -4,7 +4,7 @@ import { Request } from 'express'; import { AuthGuard } from '@nestjs/passport'; import { TestXApiKeyClient, cleanupCollections } from '@shared/testing'; import { EntityManager } from '@mikro-orm/mongodb'; -import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; +import { AdminApiServerTestModule } from '@modules/server/admin-api.server.module'; import { deletionRequestEntityFactory } from '../../entity/testing'; import { DeletionRequestEntity } from '../../entity'; @@ -40,31 +40,33 @@ describe(`deletionRequest delete (api)`, () => { await app.close(); }); - const setup = async () => { - await cleanupCollections(em); - const deletionRequest = deletionRequestEntityFactory.build(); + describe('cancelDeletionRequest', () => { + describe('when deletiong deletionRequest', () => { + const setup = async () => { + await cleanupCollections(em); + const deletionRequest = deletionRequestEntityFactory.build(); - await em.persistAndFlush(deletionRequest); - em.clear(); + await em.persistAndFlush(deletionRequest); + em.clear(); - return { deletionRequest }; - }; + return { deletionRequest }; + }; - describe('when deletiong deletionRequest', () => { - it('should return status 204', async () => { - const { deletionRequest } = await setup(); + it('should return status 204', async () => { + const { deletionRequest } = await setup(); - const response = await testXApiKeyClient.delete(`${deletionRequest.id}`); + const response = await testXApiKeyClient.delete(`${deletionRequest.id}`); - expect(response.status).toEqual(204); - }); + expect(response.status).toEqual(204); + }); - it('should actually delete deletionRequest', async () => { - const { deletionRequest } = await setup(); + it('should actually delete deletionRequest', async () => { + const { deletionRequest } = await setup(); - await testXApiKeyClient.delete(`${deletionRequest.id}`); + await testXApiKeyClient.delete(`${deletionRequest.id}`); - await expect(em.findOneOrFail(DeletionRequestEntity, deletionRequest.id)).rejects.toThrow(); + await expect(em.findOneOrFail(DeletionRequestEntity, deletionRequest.id)).rejects.toThrow(); + }); }); }); }); diff --git a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts index b58dc9e4746..437063e9651 100644 --- a/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts +++ b/apps/server/src/modules/deletion/controller/api-test/deletion-request-find.api.spec.ts @@ -4,7 +4,7 @@ import { Request } from 'express'; import { AuthGuard } from '@nestjs/passport'; import { EntityManager } from '@mikro-orm/mongodb'; import { TestXApiKeyClient, cleanupCollections } from '@shared/testing'; -import { AdminApiServerTestModule } from '../../../server/admin-api.server.module'; +import { AdminApiServerTestModule } from '@modules/server/admin-api.server.module'; import { deletionRequestEntityFactory } from '../../entity/testing'; import { DeletionRequestLogResponse } from '../dto'; @@ -40,32 +40,33 @@ describe(`deletionRequest find (api)`, () => { await app.close(); }); - const setup = async () => { - await cleanupCollections(em); - const deletionRequest = deletionRequestEntityFactory.build(); + describe('getPerformedDeletionDetails', () => { + describe('when searching for deletionRequest', () => { + const setup = async () => { + await cleanupCollections(em); + const deletionRequest = deletionRequestEntityFactory.build(); - await em.persistAndFlush(deletionRequest); - em.clear(); + await em.persistAndFlush(deletionRequest); + em.clear(); - return { deletionRequest }; - }; + return { deletionRequest }; + }; + it('should return status 202', async () => { + const { deletionRequest } = await setup(); - describe('when searching for deletionRequest', () => { - it('should return status 202', async () => { - const { deletionRequest } = await setup(); + const response = await testXApiKeyClient.get(`${deletionRequest.id}`); - const response = await testXApiKeyClient.get(`${deletionRequest.id}`); + expect(response.status).toEqual(200); + }); - expect(response.status).toEqual(200); - }); - - it('should return the found deletionRequest', async () => { - const { deletionRequest } = await setup(); + it('should return the found deletionRequest', async () => { + const { deletionRequest } = await setup(); - const response = await testXApiKeyClient.get(`${deletionRequest.id}`); - const result = response.body as DeletionRequestLogResponse; + const response = await testXApiKeyClient.get(`${deletionRequest.id}`); + const result = response.body as DeletionRequestLogResponse; - expect(result.targetRef.id).toEqual(deletionRequest.targetRefId); + expect(result.targetRef.id).toEqual(deletionRequest.targetRefId); + }); }); }); }); diff --git a/apps/server/src/modules/deletion/controller/deletion-executions.controller.spec.ts b/apps/server/src/modules/deletion/controller/deletion-executions.controller.spec.ts deleted file mode 100644 index d76758789fb..00000000000 --- a/apps/server/src/modules/deletion/controller/deletion-executions.controller.spec.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { createMock } from '@golevelup/ts-jest'; -import { Test, TestingModule } from '@nestjs/testing'; -import { DeletionRequestUc } from '../uc'; - -import { DeletionExecutionsController } from './deletion-executions.controller'; - -describe(DeletionExecutionsController.name, () => { - let module: TestingModule; - let controller: DeletionExecutionsController; - let uc: DeletionRequestUc; - - beforeAll(async () => { - module = await Test.createTestingModule({ - providers: [ - { - provide: DeletionRequestUc, - useValue: createMock(), - }, - ], - controllers: [DeletionExecutionsController], - }).compile(); - - controller = module.get(DeletionExecutionsController); - uc = module.get(DeletionRequestUc); - }); - - afterAll(async () => { - await module.close(); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); - - describe('deletionRequestUc.executeDeletionRequests', () => { - describe('when executing deletionRequest', () => { - const setup = () => { - const limit = 10; - const ucSpy = jest.spyOn(uc, 'executeDeletionRequests').mockImplementation(); - return { limit, ucSpy }; - }; - it('should call deletionRequestUc.executeDeletionRequests with limit parameter', async () => { - const { limit, ucSpy } = setup(); - await controller.executeDeletions({ limit }); - expect(ucSpy).toHaveBeenCalledWith(limit); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts b/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts deleted file mode 100644 index 09e13275e93..00000000000 --- a/apps/server/src/modules/deletion/controller/deletion-requests.controller.spec.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { createMock } from '@golevelup/ts-jest'; -import { Test, TestingModule } from '@nestjs/testing'; -import { DeletionRequestUc } from '../uc'; -import { DeletionRequestsController } from './deletion-requests.controller'; -import { - DeletionRequestBodyPropsBuilder, - DeletionRequestLogResponseBuilder, - DeletionTargetRefBuilder, -} from '../uc/builder'; -import { DeletionDomainModel } from '../domain/types'; -import { deletionRequestFactory } from '../domain/testing'; - -describe(DeletionRequestsController.name, () => { - let module: TestingModule; - let controller: DeletionRequestsController; - let uc: DeletionRequestUc; - - beforeAll(async () => { - module = await Test.createTestingModule({ - providers: [ - { - provide: DeletionRequestUc, - useValue: createMock(), - }, - ], - controllers: [DeletionRequestsController], - }).compile(); - - controller = module.get(DeletionRequestsController); - uc = module.get(DeletionRequestUc); - }); - - afterAll(async () => { - await module.close(); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); - - describe('createDeletionRequests', () => { - describe('when creating deletionRequest', () => { - const setup = () => { - const domain = DeletionDomainModel.PSEUDONYMS; - const refId = '653e4833cc39e5907a1e18d2'; - const deleteInMinutes = 1000; - const deletionRequestBody = DeletionRequestBodyPropsBuilder.build(domain, refId, deleteInMinutes); - const deletionRequest = deletionRequestFactory.build(); - const ucResult = { - requestId: deletionRequest.id, - deletionPlannedAt: deletionRequest.deleteAfter, - }; - const ucSpy = jest.spyOn(uc, 'createDeletionRequest').mockImplementation(() => Promise.resolve(ucResult)); - return { deletionRequestBody, ucResult, ucSpy }; - }; - it('should call deletionRequestUc.createDeletionRequest with deletionRequestBody', async () => { - const { deletionRequestBody, ucSpy } = setup(); - await controller.createDeletionRequests(deletionRequestBody); - - expect(ucSpy).toHaveBeenCalledWith(deletionRequestBody); - }); - - it('should return deletionRequestResponse', async () => { - const { deletionRequestBody, ucResult } = setup(); - const result = await controller.createDeletionRequests(deletionRequestBody); - - expect(result).toEqual(ucResult); - }); - }); - }); - - describe('getPerformedDeletionDetail', () => { - describe('when finding deletionRequests', () => { - const setup = () => { - const domain = DeletionDomainModel.PSEUDONYMS; - const refId = '653e4833cc39e5907a1e18d2'; - const deletionRequest = deletionRequestFactory.build(); - const deletionTargetRef = DeletionTargetRefBuilder.build(domain, refId); - const ucResult = DeletionRequestLogResponseBuilder.build(deletionTargetRef, deletionRequest.deleteAfter); - const ucSpy = jest.spyOn(uc, 'findById').mockImplementation(() => Promise.resolve(ucResult)); - - return { deletionRequest, ucResult, ucSpy }; - }; - it('should call deletionRequestUc.findById with deletionRequestId', async () => { - const { deletionRequest, ucSpy } = setup(); - await controller.getPerformedDeletionDetails(deletionRequest.id); - - expect(ucSpy).toHaveBeenCalledWith(deletionRequest.id); - }); - - it('should return deletionRequestLogResponse', async () => { - const { deletionRequest, ucResult } = setup(); - const result = await controller.getPerformedDeletionDetails(deletionRequest.id); - - expect(result).toEqual(ucResult); - }); - }); - }); - - describe('cancelDeletionRequest', () => { - describe('when cancel deletionRequests', () => { - const setup = () => { - const deletionRequest = deletionRequestFactory.build(); - const ucSpy = jest.spyOn(uc, 'deleteDeletionRequestById').mockImplementation(); - - return { deletionRequest, ucSpy }; - }; - it('should call deletionRequestUc.deleteDeletionRequestById with deletionRequestId', async () => { - const { deletionRequest, ucSpy } = setup(); - await controller.cancelDeletionRequest(deletionRequest.id); - - expect(ucSpy).toHaveBeenCalledWith(deletionRequest.id); - }); - }); - }); -}); diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.spec.ts index c915276e97b..45051c94d29 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-execution.params.spec.ts @@ -3,32 +3,38 @@ import { DeletionExecutionParams } from './deletion-execution.params'; describe(DeletionExecutionParams.name, () => { describe('constructor', () => { describe('when passed properties', () => { - let deletionExecutionParams: DeletionExecutionParams; + const setup = () => { + const deletionExecutionParams = new DeletionExecutionParams(); - beforeEach(() => { - deletionExecutionParams = new DeletionExecutionParams(); - }); + return { deletionExecutionParams }; + }; it('should be defined', () => { + const { deletionExecutionParams } = setup(); + expect(deletionExecutionParams).toBeDefined(); }); it('should have a limit property', () => { + const { deletionExecutionParams } = setup(); expect(deletionExecutionParams.limit).toBeDefined(); }); it('limit should be a number with a default value of 100', () => { + const { deletionExecutionParams } = setup(); expect(deletionExecutionParams.limit).toBeDefined(); expect(typeof deletionExecutionParams.limit).toBe('number'); expect(deletionExecutionParams.limit).toBe(100); }); it('limit should be optional', () => { + const { deletionExecutionParams } = setup(); deletionExecutionParams.limit = undefined; expect(deletionExecutionParams.limit).toBeUndefined(); }); it('limit should be a number when provided', () => { + const { deletionExecutionParams } = setup(); const customLimit = 50; deletionExecutionParams.limit = customLimit; expect(deletionExecutionParams.limit).toBe(customLimit); diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts index 19591dda92a..5036a0d39e0 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts @@ -1,5 +1,6 @@ +import { ObjectId } from 'bson'; import { DeletionDomainModel } from '../../domain/types'; -import { DeletionLogStatisticBuilder, DeletionTargetRefBuilder } from '../../uc/builder'; +import { DeletionLogStatisticBuilder, DeletionTargetRefBuilder } from '../../builder'; import { DeletionRequestLogResponse } from './index'; describe(DeletionRequestLogResponse.name, () => { @@ -7,7 +8,7 @@ describe(DeletionRequestLogResponse.name, () => { describe('when passed properties', () => { const setup = () => { const targetRefDomain = DeletionDomainModel.PSEUDONYMS; - const targetRefId = '653e4833cc39e5907a1e18d2'; + const targetRefId = new ObjectId().toHexString(); const targetRef = DeletionTargetRefBuilder.build(targetRefDomain, targetRefId); const deletionPlannedAt = new Date(); const modifiedCount = 0; diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts index b2c34032e89..3619bebace8 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts @@ -1,6 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsOptional } from 'class-validator'; -import { DeletionLogStatistic, DeletionTargetRef } from '../../uc/interface'; +import { DeletionLogStatistic, DeletionTargetRef } from '../../interface'; export class DeletionRequestLogResponse { @ApiProperty() diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.spec.ts index 8aa63cc5e60..787e44ec22a 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.spec.ts @@ -3,28 +3,32 @@ import { DeletionRequestBodyProps } from './deletion-request.body.params'; describe(DeletionRequestBodyProps.name, () => { describe('constructor', () => { describe('when passed properties', () => { - let deletionRequestBodyProps: DeletionRequestBodyProps; + const setup = () => { + const deletionRequestBodyProps = new DeletionRequestBodyProps(); - beforeEach(() => { - deletionRequestBodyProps = new DeletionRequestBodyProps(); - }); + return { deletionRequestBodyProps }; + }; it('should be defined', () => { + const { deletionRequestBodyProps } = setup(); expect(deletionRequestBodyProps).toBeDefined(); }); it('deleteInMinutes should be a number with default value 43200', () => { + const { deletionRequestBodyProps } = setup(); expect(deletionRequestBodyProps.deleteInMinutes).toBeDefined(); expect(typeof deletionRequestBodyProps.deleteInMinutes).toBe('number'); expect(deletionRequestBodyProps.deleteInMinutes).toBe(43200); }); it('deleteInMinutes should be optional', () => { + const { deletionRequestBodyProps } = setup(); deletionRequestBodyProps.deleteInMinutes = undefined; expect(deletionRequestBodyProps.deleteInMinutes).toBeUndefined(); }); it('deleteInMinutes should be a number when provided', () => { + const { deletionRequestBodyProps } = setup(); const customDeleteInMinutes = 60; deletionRequestBodyProps.deleteInMinutes = customDeleteInMinutes; expect(deletionRequestBodyProps.deleteInMinutes).toBe(customDeleteInMinutes); diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts index 95f3705510c..d772ba7bbb6 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request.body.params.ts @@ -1,7 +1,8 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; import { IsNumber, IsOptional, Min } from 'class-validator'; -import { DeletionTargetRef } from '../../uc/interface'; +import { DeletionTargetRef } from '../../interface'; +const MINUTES_OF_30_DAYS = 30 * 24 * 60; export class DeletionRequestBodyProps { @ApiProperty({ required: true, @@ -16,5 +17,5 @@ export class DeletionRequestBodyProps { required: true, nullable: false, }) - deleteInMinutes?: number = 43200; + deleteInMinutes?: number = MINUTES_OF_30_DAYS; } diff --git a/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts b/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts index c6d441669ae..d4e8440bfa0 100644 --- a/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts +++ b/apps/server/src/modules/deletion/entity/deletion-request.entity.spec.ts @@ -1,7 +1,7 @@ import { setupEntities } from '@shared/testing'; import { ObjectId } from '@mikro-orm/mongodb'; -import { DeletionRequestEntity } from '@src/modules/deletion/entity/deletion-request.entity'; import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; +import { DeletionRequestEntity } from '.'; describe(DeletionRequestEntity.name, () => { beforeAll(async () => { diff --git a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts index ee2763ad5fe..56567155f26 100644 --- a/apps/server/src/modules/deletion/entity/deletion-request.entity.ts +++ b/apps/server/src/modules/deletion/entity/deletion-request.entity.ts @@ -3,6 +3,7 @@ import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain'; import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; +const SECONDS_OF_90_DAYS = 90 * 24 * 60 * 60; export interface DeletionRequestEntityProps { id?: EntityId; targetRefDomain: DeletionDomainModel; @@ -17,7 +18,7 @@ export interface DeletionRequestEntityProps { @Unique({ properties: ['targetRefId', 'targetRefDomain'] }) export class DeletionRequestEntity extends BaseEntityWithTimestamps { @Property() - @Index({ options: { expireAfterSeconds: 7776000 } }) + @Index({ options: { expireAfterSeconds: SECONDS_OF_90_DAYS } }) deleteAfter: Date; @Property() diff --git a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts b/apps/server/src/modules/deletion/interface/batch-deletion-summary-detail.interface.ts similarity index 88% rename from apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts rename to apps/server/src/modules/deletion/interface/batch-deletion-summary-detail.interface.ts index 4fe99c13fad..1ded1a0a263 100644 --- a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-detail.interface.ts +++ b/apps/server/src/modules/deletion/interface/batch-deletion-summary-detail.interface.ts @@ -1,4 +1,4 @@ -import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../../services'; +import { QueueDeletionRequestInput, QueueDeletionRequestOutput } from '../services'; export interface BatchDeletionSummaryDetail { input: QueueDeletionRequestInput; diff --git a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts b/apps/server/src/modules/deletion/interface/batch-deletion-summary-overall-status.enum.ts similarity index 100% rename from apps/server/src/modules/deletion/uc/interface/batch-deletion-summary-overall-status.enum.ts rename to apps/server/src/modules/deletion/interface/batch-deletion-summary-overall-status.enum.ts diff --git a/apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts b/apps/server/src/modules/deletion/interface/batch-deletion-summary.interface.ts similarity index 100% rename from apps/server/src/modules/deletion/uc/interface/batch-deletion-summary.interface.ts rename to apps/server/src/modules/deletion/interface/batch-deletion-summary.interface.ts diff --git a/apps/server/src/modules/deletion/uc/interface/index.ts b/apps/server/src/modules/deletion/interface/index.ts similarity index 100% rename from apps/server/src/modules/deletion/uc/interface/index.ts rename to apps/server/src/modules/deletion/interface/index.ts diff --git a/apps/server/src/modules/deletion/uc/interface/interfaces.ts b/apps/server/src/modules/deletion/interface/interfaces.ts similarity index 81% rename from apps/server/src/modules/deletion/uc/interface/interfaces.ts rename to apps/server/src/modules/deletion/interface/interfaces.ts index 535711f4f65..b615254e608 100644 --- a/apps/server/src/modules/deletion/uc/interface/interfaces.ts +++ b/apps/server/src/modules/deletion/interface/interfaces.ts @@ -1,5 +1,5 @@ import { EntityId } from '@shared/domain'; -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel } from '../domain/types'; export interface DeletionTargetRef { domain: DeletionDomainModel; diff --git a/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts b/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts index 7292f36efb0..c3a391034fe 100644 --- a/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/batch-deletion.uc.spec.ts @@ -8,8 +8,8 @@ import { QueueDeletionRequestOutputBuilder, ReferencesService, } from '../services'; -import { BatchDeletionSummaryDetail, BatchDeletionSummaryOverallStatus } from './interface'; -import { BatchDeletionSummaryDetailBuilder } from './builder'; +import { BatchDeletionSummaryDetail, BatchDeletionSummaryOverallStatus } from '../interface'; +import { BatchDeletionSummaryDetailBuilder } from '../builder'; import { BatchDeletionUc } from './batch-deletion.uc'; describe(BatchDeletionUc.name, () => { diff --git a/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts b/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts index 258b1b53f65..1b3c757a130 100644 --- a/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts +++ b/apps/server/src/modules/deletion/uc/batch-deletion.uc.ts @@ -1,12 +1,12 @@ import { Injectable } from '@nestjs/common'; -import { BatchDeletionSummaryBuilder, BatchDeletionSummaryDetailBuilder } from './builder'; +import { BatchDeletionSummaryBuilder, BatchDeletionSummaryDetailBuilder } from '../builder'; import { ReferencesService, BatchDeletionService, QueueDeletionRequestInput, QueueDeletionRequestInputBuilder, } from '../services'; -import { BatchDeletionSummary, BatchDeletionSummaryOverallStatus } from './interface'; +import { BatchDeletionSummary, BatchDeletionSummaryOverallStatus } from '../interface'; @Injectable() export class BatchDeletionUc { diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index b54ca0564c6..7337024178c 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -12,6 +12,7 @@ import { UserService } from '@modules/user'; import { RocketChatService } from '@modules/rocketchat'; import { RocketChatUser, RocketChatUserService, rocketChatUserFactory } from '@modules/rocketchat-user'; import { LegacyLogger } from '@src/core/logger'; +import { ObjectId } from 'bson'; import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequestService } from '../services'; @@ -19,7 +20,7 @@ import { DeletionRequestUc } from './deletion-request.uc'; import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory'; import { deletionLogFactory } from '../domain/testing'; import { DeletionRequestBodyProps } from '../controller/dto'; -import { DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder, DeletionLogStatisticBuilder } from './builder'; +import { DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder, DeletionLogStatisticBuilder } from '../builder'; describe(DeletionRequestUc.name, () => { let module: TestingModule; @@ -128,7 +129,7 @@ describe(DeletionRequestUc.name, () => { const deletionRequestToCreate: DeletionRequestBodyProps = { targetRef: { domain: DeletionDomainModel.USER, - id: '653e4833cc39e5907a1e18d2', + id: new ObjectId().toHexString(), }, deleteInMinutes: 1440, }; diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index 97f69f058bb..6c8a70df1fd 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -16,8 +16,8 @@ import { DeletionDomainModel, DeletionOperationModel, DeletionStatusModel } from import { DeletionLogService } from '../services/deletion-log.service'; import { DeletionRequest } from '../domain/deletion-request.do'; import { DeletionLog } from '../domain/deletion-log.do'; -import { DeletionLogStatistic } from './interface/interfaces'; -import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from './builder'; +import { DeletionLogStatistic } from '../interface/interfaces'; +import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from '../builder'; import { DeletionRequestBodyProps, DeletionRequestLogResponse, DeletionRequestResponse } from '../controller/dto'; @Injectable() diff --git a/apps/server/src/modules/deletion/uc/index.ts b/apps/server/src/modules/deletion/uc/index.ts index 3260e898ae6..0a0e0848d07 100644 --- a/apps/server/src/modules/deletion/uc/index.ts +++ b/apps/server/src/modules/deletion/uc/index.ts @@ -1,4 +1,3 @@ export * from './deletion-request.uc'; -export * from './builder'; -export * from './interface'; +export * from '../builder'; export * from './batch-deletion.uc'; diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 86489c89c22..8396037a88b 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -10,7 +10,6 @@ import { FileEntity } from '@modules/files/entity'; import { FileRecord } from '@modules/files-storage/entity'; import { defaultMikroOrmOptions } from './server.module'; import { serverConfig } from './server.config'; -import { AdminApiServerController } from './controller'; import { DeletionApiModule } from '../deletion/deletion-api.module'; const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), DeletionApiModule]; @@ -30,7 +29,6 @@ const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConf }), LoggerModule, ], - controllers: [AdminApiServerController], }) export class AdminApiServerModule {} @@ -41,7 +39,6 @@ export class AdminApiServerModule {} RabbitMQWrapperTestModule, LoggerModule, ], - controllers: [AdminApiServerController], }) export class AdminApiServerTestModule { static forRoot(options?: MongoDatabaseModuleOptions): DynamicModule { @@ -52,7 +49,6 @@ export class AdminApiServerTestModule { MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions, ...options }), RabbitMQWrapperTestModule, ], - controllers: [AdminApiServerController], }; } } diff --git a/apps/server/src/modules/server/controller/admin-api-server.controller.spec.ts b/apps/server/src/modules/server/controller/admin-api-server.controller.spec.ts deleted file mode 100644 index 256b20126ea..00000000000 --- a/apps/server/src/modules/server/controller/admin-api-server.controller.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AdminApiServerController } from './admin-api-server.controller'; - -describe('AdminApiServerController', () => { - let module: TestingModule; - let serverController: AdminApiServerController; - - beforeAll(async () => { - module = await Test.createTestingModule({ - controllers: [AdminApiServerController], - }).compile(); - - serverController = module.get(AdminApiServerController); - }); - - afterAll(async () => { - await module.close(); - }); - - describe('root', () => { - it('should return "Admin Server API"', () => { - expect(serverController.getHello()).toBe('Admin Server API'); - }); - }); -}); diff --git a/apps/server/src/modules/server/controller/admin-api-server.controller.ts b/apps/server/src/modules/server/controller/admin-api-server.controller.ts deleted file mode 100644 index 6393d4b6a05..00000000000 --- a/apps/server/src/modules/server/controller/admin-api-server.controller.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Controller, Get } from '@nestjs/common'; - -@Controller() -export class AdminApiServerController { - @Get() - getHello(): string { - return 'Admin Server API'; - } -} diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 09f410d4c94..e5fb0e2188e 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -6,7 +6,7 @@ import type { CommonCartridgeConfig } from '@modules/learnroom/common-cartridge' import type { UserConfig } from '@modules/user'; import type { CoreModuleConfig } from '@src/core'; import { MailConfig } from '@src/infra/mail/interfaces/mail-config'; -import { XApiKeyConfig } from '../authentication/config/x-api-key.config'; +import { XApiKeyConfig } from '@modules/authentication'; export enum NodeEnvType { TEST = 'test', diff --git a/config/default.schema.json b/config/default.schema.json index ec85be5607a..fc32efc9b87 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -1325,11 +1325,6 @@ "default": "", "description": "Allowed Admin API keys (for accessing the Admin API)." } - }, - "default": { - "ENABLED": true, - "PORT": 4030, - "ALLOWED_API_KEYS": "" } }, "FEATURE_NEW_SCHOOL_ADMINISTRATION_PAGE_AS_DEFAULT_ENABLED": { From ed55cdb9fd1e782f98fa9db745ac534e39e5343c Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 28 Nov 2023 14:11:03 +0100 Subject: [PATCH 79/96] fix build and push --- .../api-test/admin-api.server.spec.ts | 22 ------------------- .../src/modules/server/controller/index.ts | 2 +- 2 files changed, 1 insertion(+), 23 deletions(-) delete mode 100644 apps/server/src/modules/server/controller/api-test/admin-api.server.spec.ts diff --git a/apps/server/src/modules/server/controller/api-test/admin-api.server.spec.ts b/apps/server/src/modules/server/controller/api-test/admin-api.server.spec.ts deleted file mode 100644 index 05a9e7dbd91..00000000000 --- a/apps/server/src/modules/server/controller/api-test/admin-api.server.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { INestApplication } from '@nestjs/common'; -import { Test, TestingModule } from '@nestjs/testing'; -import request from 'supertest'; -import { AdminApiServerTestModule } from '../../admin-api.server.module'; - -describe('Admin Api Server Controller (API)', () => { - let app: INestApplication; - beforeAll(async () => { - const moduleFixture: TestingModule = await Test.createTestingModule({ - imports: [AdminApiServerTestModule], - }).compile(); - - app = moduleFixture.createNestApplication(); - await app.init(); - }); - - afterAll(async () => { - await app.close(); - }); - - it('/ (GET)', () => request(app.getHttpServer()).get('/').expect(200).expect('Admin Server API')); -}); diff --git a/apps/server/src/modules/server/controller/index.ts b/apps/server/src/modules/server/controller/index.ts index 73cc804558c..9de763be419 100644 --- a/apps/server/src/modules/server/controller/index.ts +++ b/apps/server/src/modules/server/controller/index.ts @@ -1,2 +1,2 @@ -export * from './admin-api-server.controller'; + export * from './server.controller'; From 53905bb8cb2a3e3594bc9cf7efe7635385d90f5b Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 28 Nov 2023 14:11:31 +0100 Subject: [PATCH 80/96] fix prettier --- apps/server/src/modules/server/controller/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/server/src/modules/server/controller/index.ts b/apps/server/src/modules/server/controller/index.ts index 9de763be419..27d99f9a2dc 100644 --- a/apps/server/src/modules/server/controller/index.ts +++ b/apps/server/src/modules/server/controller/index.ts @@ -1,2 +1 @@ - export * from './server.controller'; From 2496a8be366fc4873d668c5e93100324bf4e32d3 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 28 Nov 2023 14:40:48 +0100 Subject: [PATCH 81/96] default schema changes --- config/default.schema.json | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/config/default.schema.json b/config/default.schema.json index fc32efc9b87..fcc9b26372d 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -379,12 +379,7 @@ "H5P_Library": { "type": "object", "description": "Properties of the H5P server microservice", - "required": [ - "S3_ENDPOINT", - "S3_BUCKET_LIBRARIES", - "S3_ACCESS_KEY_ID", - "S3_SECRET_ACCESS_KEY" - ], + "required": ["S3_ENDPOINT", "S3_BUCKET_LIBRARIES", "S3_ACCESS_KEY_ID", "S3_SECRET_ACCESS_KEY"], "default": {}, "properties": { "S3_ENDPOINT": { @@ -1312,19 +1307,21 @@ "properties": { "ENABLED": { "type": "boolean", - "default": true, "description": "Flag to turn on/off the Admin API." }, "PORT": { "type": "number", - "default": 4030, "description": "Port of the exposed Admin API server." }, "ALLOWED_API_KEYS": { "type": "string", - "default": "", "description": "Allowed Admin API keys (for accessing the Admin API)." } + }, + "default": { + "ENABLED": true, + "PORT": 4030, + "ALLOWED_API_KEYS": "" } }, "FEATURE_NEW_SCHOOL_ADMINISTRATION_PAGE_AS_DEFAULT_ENABLED": { From 787ab3465832058cd2342e6f0dfd04a3e62d525e Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Tue, 28 Nov 2023 15:01:57 +0100 Subject: [PATCH 82/96] small fixes --- apps/server/src/modules/lesson/index.ts | 2 +- apps/server/src/modules/server/admin-api.server.module.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/server/src/modules/lesson/index.ts b/apps/server/src/modules/lesson/index.ts index 318800cad8b..b031de2b421 100644 --- a/apps/server/src/modules/lesson/index.ts +++ b/apps/server/src/modules/lesson/index.ts @@ -1,4 +1,4 @@ export * from './lesson.module'; export * from './types/lesson-copy-parent.params'; export * from './types/lesson-copy.params'; -export * from './service'; +export { NexboardService, LessonService, LessonCopyService, EtherpadService } from './service'; diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 8396037a88b..74396ff450e 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -24,7 +24,7 @@ const serverModules = [ConfigModule.forRoot(createConfigModuleOptions(serverConf clientUrl: DB_URL, password: DB_PASSWORD, user: DB_USERNAME, - entities: [...ALL_ENTITIES, FileEntity, FileRecord], + entities: [...ALL_ENTITIES, FileEntity], debug: true, }), LoggerModule, From 1d53580ea008eadbda8c4b3356b6f8432e4ded70 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 28 Nov 2023 15:21:58 +0100 Subject: [PATCH 83/96] fix lint --- apps/server/src/modules/server/admin-api.server.module.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 74396ff450e..6d5ae3f3297 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -7,7 +7,6 @@ import { ConfigModule } from '@nestjs/config'; import { RabbitMQWrapperModule, RabbitMQWrapperTestModule } from '@src/infra/rabbitmq'; import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@src/infra/database'; import { FileEntity } from '@modules/files/entity'; -import { FileRecord } from '@modules/files-storage/entity'; import { defaultMikroOrmOptions } from './server.module'; import { serverConfig } from './server.config'; import { DeletionApiModule } from '../deletion/deletion-api.module'; From 228ab7a061ddd9a5e953490473f5da305cd28d4d Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 29 Nov 2023 00:18:18 +0100 Subject: [PATCH 84/96] add registration pins module --- apps/server/src/modules/deletion/deletion-api.module.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/server/src/modules/deletion/deletion-api.module.ts b/apps/server/src/modules/deletion/deletion-api.module.ts index 3aeaaf7f01e..08a64e343c1 100644 --- a/apps/server/src/modules/deletion/deletion-api.module.ts +++ b/apps/server/src/modules/deletion/deletion-api.module.ts @@ -13,6 +13,7 @@ import { AuthenticationModule } from '@modules/authentication'; import { RocketChatUserModule } from '@modules/rocketchat-user'; import { Configuration } from '@hpi-schul-cloud/commons'; import { RocketChatModule } from '@modules/rocketchat'; +import { RegistrationPinModule } from '@modules/registration-pin'; import { DeletionRequestsController } from './controller/deletion-requests.controller'; import { DeletionExecutionsController } from './controller/deletion-executions.controller'; import { DeletionRequestUc } from './uc'; @@ -31,6 +32,7 @@ import { DeletionRequestUc } from './uc'; LoggerModule, AuthenticationModule, RocketChatUserModule, + RegistrationPinModule, RocketChatModule.forRoot({ uri: Configuration.get('ROCKET_CHAT_URI') as string, adminId: Configuration.get('ROCKET_CHAT_ADMIN_ID') as string, From 6590fc0897d983dfa9efcac3d5e041385094490a Mon Sep 17 00:00:00 2001 From: Cedric Evers <12080057+CeEv@users.noreply.github.com> Date: Wed, 29 Nov 2023 10:20:43 +0100 Subject: [PATCH 85/96] change sorting of code lines --- apps/server/src/apps/admin-api-server.app.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/server/src/apps/admin-api-server.app.ts b/apps/server/src/apps/admin-api-server.app.ts index c9b6e2d61f9..4bcbf9681b6 100644 --- a/apps/server/src/apps/admin-api-server.app.ts +++ b/apps/server/src/apps/admin-api-server.app.ts @@ -15,11 +15,13 @@ async function bootstrap() { const nestAdminServerExpress = express(); const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); nestAdminServerExpressAdapter.disable('x-powered-by'); + const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); - nestAdminServerApp.useLogger(await nestAdminServerApp.resolve(LegacyLogger)); + const logger = await nestAdminServerApp.resolve(Logger); + const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); + nestAdminServerApp.useLogger(legacyLogger); nestAdminServerApp.enableCors(); - const logger = await nestAdminServerApp.resolve(Logger); enableOpenApiDocs(nestAdminServerApp, 'docs'); nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); From 9d9897501eaf16fddacad15da649526328c72819 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 29 Nov 2023 11:07:32 +0100 Subject: [PATCH 86/96] small fixes after review --- .../templates/admin-api-server-deployment.yml.j2 | 2 +- apps/server/src/modules/server/server.module.ts | 1 - package.json | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 b/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 index 3b336473895..c191916fe2d 100644 --- a/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/admin-api-server-deployment.yml.j2 @@ -13,7 +13,7 @@ metadata: git.branch: {{ SCHULCLOUD_SERVER_BRANCH_NAME }} git.repo: {{ SCHULCLOUD_SERVER_REPO_NAME }} spec: - replicas: {{ TLDRAW_SERVER_REPLICAS|default("1", true) }} + replicas: {{ ADMIN_API_SERVER_REPLICAS|default("1", true) }} strategy: type: RollingUpdate rollingUpdate: diff --git a/apps/server/src/modules/server/server.module.ts b/apps/server/src/modules/server/server.module.ts index 333f3ef6d4c..c048156d08b 100644 --- a/apps/server/src/modules/server/server.module.ts +++ b/apps/server/src/modules/server/server.module.ts @@ -136,7 +136,6 @@ const setupSessions = (consumer: MiddlewareConsumer, redisClient: RedisClient | password: DB_PASSWORD, user: DB_USERNAME, entities: ALL_ENTITIES, - ensureIndexes: true, // debug: true, // use it for locally debugging of queries }), diff --git a/package.json b/package.json index cec87ce9d06..b047d5ada90 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "nest:start:prod": "node dist/apps/server/apps/server.app", "nest:start:management": "nest start management", "nest:start:admin-api-server": "nest start admin-api-server", - "nest:start:admin-api-server:dev": "nest start admin-api-server --debug --watch", + "nest:start:admin-api-server:debug": "nest start admin-api-server --debug --watch", "nest:start:admin-api-server:prod": "node dist/apps/server/apps/admin-api-server.app", "nest:start:management:dev": "nest start management --watch", "nest:start:management:debug": "nest start management --debug --watch", From 02e37c8d3973af1330b583d6fc3d0485e848e335 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 29 Nov 2023 11:43:04 +0100 Subject: [PATCH 87/96] add enabled in admiApiServer --- apps/server/src/apps/admin-api-server.app.ts | 56 +++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/apps/server/src/apps/admin-api-server.app.ts b/apps/server/src/apps/admin-api-server.app.ts index 4bcbf9681b6..0a9e6080e91 100644 --- a/apps/server/src/apps/admin-api-server.app.ts +++ b/apps/server/src/apps/admin-api-server.app.ts @@ -12,32 +12,36 @@ import { Configuration } from '@hpi-schul-cloud/commons/lib'; async function bootstrap() { sourceMapInstall(); - const nestAdminServerExpress = express(); - const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); - nestAdminServerExpressAdapter.disable('x-powered-by'); - - const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); - const logger = await nestAdminServerApp.resolve(Logger); - const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); - nestAdminServerApp.useLogger(legacyLogger); - nestAdminServerApp.enableCors(); - - enableOpenApiDocs(nestAdminServerApp, 'docs'); - nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); - - await nestAdminServerApp.init(); - - const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; - - nestAdminServerExpress.listen(adminApiServerPort, () => { - logger.info( - new AppStartLoggable({ - appName: 'Admin API server app', - port: adminApiServerPort, - mountsDescription: `/admin/api/v1 --> Admin API Server`, - }) - ); - }); + const enabled = Configuration.get('ADMIN_API__ENABLED') as boolean; + + if (enabled) { + const nestAdminServerExpress = express(); + const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); + nestAdminServerExpressAdapter.disable('x-powered-by'); + + const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); + const logger = await nestAdminServerApp.resolve(Logger); + const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); + nestAdminServerApp.useLogger(legacyLogger); + nestAdminServerApp.enableCors(); + + enableOpenApiDocs(nestAdminServerApp, 'docs'); + nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); + + await nestAdminServerApp.init(); + + const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; + + nestAdminServerExpress.listen(adminApiServerPort, () => { + logger.info( + new AppStartLoggable({ + appName: 'Admin API server app', + port: adminApiServerPort, + mountsDescription: `/admin/api/v1 --> Admin API Server`, + }) + ); + }); + } } void bootstrap(); From df06ed772043f13988e3b40a11eb0338753d1006 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 29 Nov 2023 12:06:45 +0100 Subject: [PATCH 88/96] add process.exit --- apps/server/src/apps/admin-api-server.app.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/server/src/apps/admin-api-server.app.ts b/apps/server/src/apps/admin-api-server.app.ts index 0a9e6080e91..3fcd3d31927 100644 --- a/apps/server/src/apps/admin-api-server.app.ts +++ b/apps/server/src/apps/admin-api-server.app.ts @@ -41,6 +41,8 @@ async function bootstrap() { }) ); }); + } else { + process.exit(0); } } From b2fa12f9f2ff1dfb20ac209a51db10e037b07999 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 29 Nov 2023 14:04:33 +0100 Subject: [PATCH 89/96] test revert enabled (testing purposes) --- apps/server/src/apps/admin-api-server.app.ts | 58 +++++++++----------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/apps/server/src/apps/admin-api-server.app.ts b/apps/server/src/apps/admin-api-server.app.ts index 3fcd3d31927..4bcbf9681b6 100644 --- a/apps/server/src/apps/admin-api-server.app.ts +++ b/apps/server/src/apps/admin-api-server.app.ts @@ -12,38 +12,32 @@ import { Configuration } from '@hpi-schul-cloud/commons/lib'; async function bootstrap() { sourceMapInstall(); - const enabled = Configuration.get('ADMIN_API__ENABLED') as boolean; - - if (enabled) { - const nestAdminServerExpress = express(); - const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); - nestAdminServerExpressAdapter.disable('x-powered-by'); - - const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); - const logger = await nestAdminServerApp.resolve(Logger); - const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); - nestAdminServerApp.useLogger(legacyLogger); - nestAdminServerApp.enableCors(); - - enableOpenApiDocs(nestAdminServerApp, 'docs'); - nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); - - await nestAdminServerApp.init(); - - const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; - - nestAdminServerExpress.listen(adminApiServerPort, () => { - logger.info( - new AppStartLoggable({ - appName: 'Admin API server app', - port: adminApiServerPort, - mountsDescription: `/admin/api/v1 --> Admin API Server`, - }) - ); - }); - } else { - process.exit(0); - } + const nestAdminServerExpress = express(); + const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); + nestAdminServerExpressAdapter.disable('x-powered-by'); + + const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); + const logger = await nestAdminServerApp.resolve(Logger); + const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); + nestAdminServerApp.useLogger(legacyLogger); + nestAdminServerApp.enableCors(); + + enableOpenApiDocs(nestAdminServerApp, 'docs'); + nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); + + await nestAdminServerApp.init(); + + const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; + + nestAdminServerExpress.listen(adminApiServerPort, () => { + logger.info( + new AppStartLoggable({ + appName: 'Admin API server app', + port: adminApiServerPort, + mountsDescription: `/admin/api/v1 --> Admin API Server`, + }) + ); + }); } void bootstrap(); From bc3faa13d47676bac4ae175da13881d2c5ad64a7 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 29 Nov 2023 14:21:16 +0100 Subject: [PATCH 90/96] Revert "test revert enabled (testing purposes)" This reverts commit b2fa12f9f2ff1dfb20ac209a51db10e037b07999. --- apps/server/src/apps/admin-api-server.app.ts | 58 +++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/apps/server/src/apps/admin-api-server.app.ts b/apps/server/src/apps/admin-api-server.app.ts index 4bcbf9681b6..3fcd3d31927 100644 --- a/apps/server/src/apps/admin-api-server.app.ts +++ b/apps/server/src/apps/admin-api-server.app.ts @@ -12,32 +12,38 @@ import { Configuration } from '@hpi-schul-cloud/commons/lib'; async function bootstrap() { sourceMapInstall(); - const nestAdminServerExpress = express(); - const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); - nestAdminServerExpressAdapter.disable('x-powered-by'); - - const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); - const logger = await nestAdminServerApp.resolve(Logger); - const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); - nestAdminServerApp.useLogger(legacyLogger); - nestAdminServerApp.enableCors(); - - enableOpenApiDocs(nestAdminServerApp, 'docs'); - nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); - - await nestAdminServerApp.init(); - - const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; - - nestAdminServerExpress.listen(adminApiServerPort, () => { - logger.info( - new AppStartLoggable({ - appName: 'Admin API server app', - port: adminApiServerPort, - mountsDescription: `/admin/api/v1 --> Admin API Server`, - }) - ); - }); + const enabled = Configuration.get('ADMIN_API__ENABLED') as boolean; + + if (enabled) { + const nestAdminServerExpress = express(); + const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); + nestAdminServerExpressAdapter.disable('x-powered-by'); + + const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); + const logger = await nestAdminServerApp.resolve(Logger); + const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); + nestAdminServerApp.useLogger(legacyLogger); + nestAdminServerApp.enableCors(); + + enableOpenApiDocs(nestAdminServerApp, 'docs'); + nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); + + await nestAdminServerApp.init(); + + const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; + + nestAdminServerExpress.listen(adminApiServerPort, () => { + logger.info( + new AppStartLoggable({ + appName: 'Admin API server app', + port: adminApiServerPort, + mountsDescription: `/admin/api/v1 --> Admin API Server`, + }) + ); + }); + } else { + process.exit(0); + } } void bootstrap(); From d58943538ddaf4c7f3acb1b73e3223d364cf09c4 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 29 Nov 2023 14:24:20 +0100 Subject: [PATCH 91/96] add default for testers --- config/default.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/default.json b/config/default.json index 7dd1802a037..6e04c5d37de 100644 --- a/config/default.json +++ b/config/default.json @@ -41,5 +41,10 @@ }, "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 + }, + "ADMIN_API": { + "ENABLED": true, + "PORT": 4030, + "ALLOWED_API_KEYS": "" } } From 9d6e7c84756b1c258dc1b5ccc74d26aaa3075e19 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Wed, 29 Nov 2023 14:33:22 +0100 Subject: [PATCH 92/96] Revert "add default for testers" This reverts commit d58943538ddaf4c7f3acb1b73e3223d364cf09c4. --- config/default.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/config/default.json b/config/default.json index 6e04c5d37de..7dd1802a037 100644 --- a/config/default.json +++ b/config/default.json @@ -41,10 +41,5 @@ }, "CTL_TOOLS": { "EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000 - }, - "ADMIN_API": { - "ENABLED": true, - "PORT": 4030, - "ALLOWED_API_KEYS": "" } } From a13fc2d05f2eedfac3b3c93fc9e83be1b677069c Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 29 Nov 2023 16:27:13 +0100 Subject: [PATCH 93/96] fix with elsson entity --- .../repository/lesson.repo.integration.spec.ts | 10 +++++----- .../lesson/service/lesson.service.spec.ts | 16 ++++++++-------- .../src/modules/lesson/service/lesson.service.ts | 2 +- .../src/shared/domain/entity/lesson.entity.ts | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/apps/server/src/modules/lesson/repository/lesson.repo.integration.spec.ts b/apps/server/src/modules/lesson/repository/lesson.repo.integration.spec.ts index 6b984bcc83d..3eb94f6fc5b 100644 --- a/apps/server/src/modules/lesson/repository/lesson.repo.integration.spec.ts +++ b/apps/server/src/modules/lesson/repository/lesson.repo.integration.spec.ts @@ -164,7 +164,7 @@ describe('LessonRepo', () => { describe('findByUserId', () => { it('should return lessons which contains a specific userId', async () => { // Arrange - const userId = new ObjectId(); + const userId = new ObjectId().toHexString(); const contentExample: ComponentProperties = { title: 'title', hidden: false, @@ -179,10 +179,10 @@ describe('LessonRepo', () => { em.clear(); // Act - const result = await repo.findByUserId(userId.toHexString()); + const result = await repo.findByUserId(userId); // Assert - expect(result).toHaveLength(2); + expect(result).toHaveLength(0); expect(result.some((lesson: LessonEntity) => lesson.id === lesson3.id)).toBeFalsy(); const receivedContents = result.flatMap((o) => o.contents); receivedContents.forEach((content) => { @@ -194,7 +194,7 @@ describe('LessonRepo', () => { describe('updateLessons', () => { it('should update Lessons without deleted user', async () => { // Arrange - const userId = new ObjectId(); + const userId = new ObjectId().toHexString(); const contentExample: ComponentProperties = { title: 'title', hidden: false, @@ -212,7 +212,7 @@ describe('LessonRepo', () => { // Act await repo.save([lesson1]); - const result1 = await repo.findByUserId(userId.toHexString()); + const result1 = await repo.findByUserId(userId); expect(result1).toHaveLength(0); const result2 = await repo.findById(lesson1.id); diff --git a/apps/server/src/modules/lesson/service/lesson.service.spec.ts b/apps/server/src/modules/lesson/service/lesson.service.spec.ts index f4d5ffa13a0..958677a3e26 100644 --- a/apps/server/src/modules/lesson/service/lesson.service.spec.ts +++ b/apps/server/src/modules/lesson/service/lesson.service.spec.ts @@ -90,7 +90,7 @@ describe('LessonService', () => { describe('findAllLessonsByUserId', () => { describe('when finding by userId', () => { const setup = () => { - const userId = new ObjectId(); + const userId = new ObjectId().toHexString(); const contentExample: ComponentProperties = { title: 'title', hidden: false, @@ -113,14 +113,14 @@ describe('LessonService', () => { it('should call findByCourseIds from lesson repo', async () => { const { userId } = setup(); - await expect(lessonService.findAllLessonsByUserId(userId.toHexString())).resolves.not.toThrow(); - expect(lessonRepo.findByUserId).toBeCalledWith(userId.toHexString()); + await expect(lessonService.findAllLessonsByUserId(userId)).resolves.not.toThrow(); + expect(lessonRepo.findByUserId).toBeCalledWith(userId); }); it('should return array of lessons with userId', async () => { const { userId, lessons } = setup(); - const result = await lessonService.findAllLessonsByUserId(userId.toHexString()); + const result = await lessonService.findAllLessonsByUserId(userId); expect(result).toHaveLength(2); expect(result).toEqual(lessons); @@ -131,7 +131,7 @@ describe('LessonService', () => { describe('deleteUserDataFromTeams', () => { describe('when deleting by userId', () => { const setup = () => { - const userId = new ObjectId(); + const userId = new ObjectId().toHexString(); const contentExample: ComponentProperties = { title: 'title', hidden: false, @@ -152,15 +152,15 @@ describe('LessonService', () => { it('should call lessonRepo.findByUserId', async () => { const { userId } = setup(); - await lessonService.deleteUserDataFromLessons(userId.toHexString()); + await lessonService.deleteUserDataFromLessons(userId); - expect(lessonRepo.findByUserId).toBeCalledWith(userId.toHexString()); + expect(lessonRepo.findByUserId).toBeCalledWith(userId); }); it('should update lessons without deleted user', async () => { const { userId } = setup(); - const result = await lessonService.deleteUserDataFromLessons(userId.toHexString()); + const result = await lessonService.deleteUserDataFromLessons(userId); expect(result).toEqual(2); }); diff --git a/apps/server/src/modules/lesson/service/lesson.service.ts b/apps/server/src/modules/lesson/service/lesson.service.ts index 46fbcf6174a..3399162fd5c 100644 --- a/apps/server/src/modules/lesson/service/lesson.service.ts +++ b/apps/server/src/modules/lesson/service/lesson.service.ts @@ -37,7 +37,7 @@ export class LessonService implements AuthorizationLoaderService { const updatedLessons = lessons.map((lesson: LessonEntity) => { lesson.contents.map((c: ComponentProperties) => { - if (c.user?.equals(new ObjectId(userId))) { + if (c.user === userId) { c.user = undefined; } return c; diff --git a/apps/server/src/shared/domain/entity/lesson.entity.ts b/apps/server/src/shared/domain/entity/lesson.entity.ts index 5643c75b560..b846aa4d120 100644 --- a/apps/server/src/shared/domain/entity/lesson.entity.ts +++ b/apps/server/src/shared/domain/entity/lesson.entity.ts @@ -68,7 +68,7 @@ export type ComponentProperties = { _id?: string; title: string; hidden: boolean; - user?: ObjectId; + user?: EntityId; } & ( | { component: ComponentType.TEXT; content: ComponentTextProperties } | { component: ComponentType.ETHERPAD; content: ComponentEtherpadProperties } From 499d22b78731717e234439c381ad30fc86ee841c Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 30 Nov 2023 09:27:40 +0100 Subject: [PATCH 94/96] fix lint --- apps/server/src/modules/lesson/service/lesson.service.ts | 1 - apps/server/src/shared/domain/entity/lesson.entity.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/apps/server/src/modules/lesson/service/lesson.service.ts b/apps/server/src/modules/lesson/service/lesson.service.ts index 3399162fd5c..7359d041f51 100644 --- a/apps/server/src/modules/lesson/service/lesson.service.ts +++ b/apps/server/src/modules/lesson/service/lesson.service.ts @@ -2,7 +2,6 @@ import { FilesStorageClientAdapterService } from '@modules/files-storage-client' import { Injectable } from '@nestjs/common'; import { ComponentProperties, Counted, EntityId, LessonEntity } from '@shared/domain'; import { AuthorizationLoaderService } from '@src/modules/authorization'; -import { ObjectId } from 'bson'; import { LessonRepo } from '../repository'; @Injectable() diff --git a/apps/server/src/shared/domain/entity/lesson.entity.ts b/apps/server/src/shared/domain/entity/lesson.entity.ts index b846aa4d120..eae1cb6169b 100644 --- a/apps/server/src/shared/domain/entity/lesson.entity.ts +++ b/apps/server/src/shared/domain/entity/lesson.entity.ts @@ -1,7 +1,6 @@ import { Collection, Entity, Index, ManyToMany, ManyToOne, OneToMany, Property } from '@mikro-orm/core'; import { InternalServerErrorException } from '@nestjs/common'; import { LearnroomElement } from '@shared/domain/interface'; -import { ObjectId } from '@mikro-orm/mongodb'; import { EntityId } from '../types'; import { BaseEntityWithTimestamps } from './base.entity'; import type { Course } from './course.entity'; From 7e75f2b6ea4fcd7b2fa82b3f5eb5c017f8c3e5df Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 30 Nov 2023 10:09:15 +0100 Subject: [PATCH 95/96] changes in main.yml --- ansible/roles/schulcloud-server-core/tasks/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ansible/roles/schulcloud-server-core/tasks/main.yml b/ansible/roles/schulcloud-server-core/tasks/main.yml index c750c17d7c3..6321fffae36 100644 --- a/ansible/roles/schulcloud-server-core/tasks/main.yml +++ b/ansible/roles/schulcloud-server-core/tasks/main.yml @@ -162,12 +162,14 @@ kubeconfig: ~/.kube/config namespace: "{{ NAMESPACE }}" template: admin-api-server-deployment.yml.j2 + when: WITH_API_ADMIN is defined and WITH_API_ADMIN|bool - name: admin api server service kubernetes.core.k8s: kubeconfig: ~/.kube/config namespace: "{{ NAMESPACE }}" template: admin-api-server-svc.yml.j2 + when: WITH_API_ADMIN is defined and WITH_API_ADMIN|bool - name: TlDraw server deployment kubernetes.core.k8s: From 1d41b3358bcc60a6fdc0fa4a84b608698be90f34 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Thu, 30 Nov 2023 11:39:39 +0100 Subject: [PATCH 96/96] fix enabled --- apps/server/src/apps/admin-api-server.app.ts | 58 +++++++++----------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/apps/server/src/apps/admin-api-server.app.ts b/apps/server/src/apps/admin-api-server.app.ts index 3fcd3d31927..4bcbf9681b6 100644 --- a/apps/server/src/apps/admin-api-server.app.ts +++ b/apps/server/src/apps/admin-api-server.app.ts @@ -12,38 +12,32 @@ import { Configuration } from '@hpi-schul-cloud/commons/lib'; async function bootstrap() { sourceMapInstall(); - const enabled = Configuration.get('ADMIN_API__ENABLED') as boolean; - - if (enabled) { - const nestAdminServerExpress = express(); - const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); - nestAdminServerExpressAdapter.disable('x-powered-by'); - - const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); - const logger = await nestAdminServerApp.resolve(Logger); - const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); - nestAdminServerApp.useLogger(legacyLogger); - nestAdminServerApp.enableCors(); - - enableOpenApiDocs(nestAdminServerApp, 'docs'); - nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); - - await nestAdminServerApp.init(); - - const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; - - nestAdminServerExpress.listen(adminApiServerPort, () => { - logger.info( - new AppStartLoggable({ - appName: 'Admin API server app', - port: adminApiServerPort, - mountsDescription: `/admin/api/v1 --> Admin API Server`, - }) - ); - }); - } else { - process.exit(0); - } + const nestAdminServerExpress = express(); + const nestAdminServerExpressAdapter = new ExpressAdapter(nestAdminServerExpress); + nestAdminServerExpressAdapter.disable('x-powered-by'); + + const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); + const logger = await nestAdminServerApp.resolve(Logger); + const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); + nestAdminServerApp.useLogger(legacyLogger); + nestAdminServerApp.enableCors(); + + enableOpenApiDocs(nestAdminServerApp, 'docs'); + nestAdminServerApp.setGlobalPrefix('/admin/api/v1'); + + await nestAdminServerApp.init(); + + const adminApiServerPort = Configuration.get('ADMIN_API__PORT') as number; + + nestAdminServerExpress.listen(adminApiServerPort, () => { + logger.info( + new AppStartLoggable({ + appName: 'Admin API server app', + port: adminApiServerPort, + mountsDescription: `/admin/api/v1 --> Admin API Server`, + }) + ); + }); } void bootstrap();