From e5bd1c9efc885eb84fe4590c186ee48e8bda4f95 Mon Sep 17 00:00:00 2001 From: virgilchiriac <17074330+virgilchiriac@users.noreply.github.com> Date: Wed, 25 Oct 2023 10:39:56 +0200 Subject: [PATCH 1/2] BC-5649 - fix prometheus test (#4498) --- apps/server/src/shared/infra/metrics/prometheus/app.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/shared/infra/metrics/prometheus/app.spec.ts b/apps/server/src/shared/infra/metrics/prometheus/app.spec.ts index 9be4558d3f8..8e751298e90 100644 --- a/apps/server/src/shared/infra/metrics/prometheus/app.spec.ts +++ b/apps/server/src/shared/infra/metrics/prometheus/app.spec.ts @@ -6,7 +6,7 @@ describe('createPrometheusMetricsApp', () => { describe('should create an app that should', () => { it('collect all the available metrics and expose them on given route', async () => { const testMetricsRoute = '/prometheus-metrics'; - const exampleDefaultMetricName = 'process_heap_bytes'; + const exampleDefaultMetricName = 'process_cpu_user_seconds_total'; const exampleMetricsRouteMetric = `sc_api_response_time_in_seconds_count{` + `method="GET",` + From 3e112cc96f7f5b0e673639a6d36495154a14854d Mon Sep 17 00:00:00 2001 From: Max Bischof <106820326+bischofmax@users.noreply.github.com> Date: Wed, 25 Oct 2023 13:29:56 +0200 Subject: [PATCH 2/2] BC-5597 - Fix unstable tests (#4497) --- .github/workflows/test.yml | 2 +- .../account-idm-to-dto.mapper.db.spec.ts | 20 +++++++++++++++---- .../api-test/files-security.api.spec.ts | 12 +++++++---- .../files-storage-copy-files.api.spec.ts | 17 ++++++++-------- .../files-storage-delete-files.api.spec.ts | 12 +++++++---- .../files-storage-download-upload.api.spec.ts | 12 +++++++---- .../files-storage-list-files.api.spec.ts | 12 +++++++---- .../files-storage-preview.api.spec.ts | 12 +++++++---- .../files-storage-rename-file.api.spec.ts | 12 +++++++---- .../files-storage-restore-files.api.spec.ts | 12 +++++++---- .../service/school-year.service.spec.ts | 2 -- .../api-test/task-copy-timeout.api.spec.ts | 10 ++++++++-- 12 files changed, 89 insertions(+), 46 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 964ab20c8f8..cbf52acce55 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -94,7 +94,7 @@ jobs: SONAR_TOKEN: ${{ secrets.SONARCLOUD_TOKEN }} nest_lint: runs-on: ubuntu-latest - timeout-minutes: 5 + timeout-minutes: 6 steps: - name: checkout uses: actions/checkout@v3 diff --git a/apps/server/src/modules/account/mapper/account-idm-to-dto.mapper.db.spec.ts b/apps/server/src/modules/account/mapper/account-idm-to-dto.mapper.db.spec.ts index 2430afe6081..0774457472a 100644 --- a/apps/server/src/modules/account/mapper/account-idm-to-dto.mapper.db.spec.ts +++ b/apps/server/src/modules/account/mapper/account-idm-to-dto.mapper.db.spec.ts @@ -54,15 +54,27 @@ describe('AccountIdmToDtoMapperDb', () => { }); describe('when date is undefined', () => { - it('should use actual date', () => { + const setup = () => { const testIdmEntity: IdmAccount = { id: 'id', }; + + const dateMock = new Date(); + jest.useFakeTimers(); + jest.setSystemTime(dateMock); + + return { testIdmEntity, dateMock }; + }; + + it('should use actual date', () => { + const { testIdmEntity, dateMock } = setup(); + const ret = mapper.mapToDto(testIdmEntity); - const now = new Date(); - expect(ret.createdAt).toEqual(now); - expect(ret.updatedAt).toEqual(now); + expect(ret.createdAt).toEqual(dateMock); + expect(ret.updatedAt).toEqual(dateMock); + + jest.useRealTimers(); }); }); diff --git a/apps/server/src/modules/files-storage/controller/api-test/files-security.api.spec.ts b/apps/server/src/modules/files-storage/controller/api-test/files-security.api.spec.ts index d6b6f4b7479..4694b1817ac 100644 --- a/apps/server/src/modules/files-storage/controller/api-test/files-security.api.spec.ts +++ b/apps/server/src/modules/files-storage/controller/api-test/files-security.api.spec.ts @@ -1,9 +1,11 @@ +import { createMock } from '@golevelup/ts-jest'; import { EntityManager } from '@mikro-orm/mongodb'; +import { ICurrentUser } from '@modules/authentication'; +import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ApiValidationError } from '@shared/common'; import { Permission } from '@shared/domain'; -import { ICurrentUser } from '@modules/authentication'; import { cleanupCollections, fileRecordFactory, @@ -12,12 +14,12 @@ import { schoolFactory, userFactory, } from '@shared/testing'; -import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; -import { FilesStorageTestModule } from '@modules/files-storage'; -import { FileRecordListResponse, ScanResultParams } from '@modules/files-storage/controller/dto'; +import NodeClam from 'clamscan'; import { Request } from 'express'; import request from 'supertest'; import { FileRecord, FileRecordParentType } from '../../entity'; +import { FilesStorageTestModule } from '../../files-storage-test.module'; +import { FileRecordListResponse, ScanResultParams } from '../dto'; const baseRouteName = '/file-security'; const scanResult: ScanResultParams = { virus_detected: false }; @@ -62,6 +64,8 @@ describe(`${baseRouteName} (api)`, () => { return true; }, }) + .overrideProvider(NodeClam) + .useValue(createMock()) .compile(); app = module.createNestApplication(); diff --git a/apps/server/src/modules/files-storage/controller/api-test/files-storage-copy-files.api.spec.ts b/apps/server/src/modules/files-storage/controller/api-test/files-storage-copy-files.api.spec.ts index e1b792dea36..22a7a11fb5b 100644 --- a/apps/server/src/modules/files-storage/controller/api-test/files-storage-copy-files.api.spec.ts +++ b/apps/server/src/modules/files-storage/controller/api-test/files-storage-copy-files.api.spec.ts @@ -1,5 +1,7 @@ import { createMock } from '@golevelup/ts-jest'; import { EntityManager, ObjectId } from '@mikro-orm/mongodb'; +import { ICurrentUser } from '@modules/authentication'; +import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ApiValidationError } from '@shared/common'; @@ -15,19 +17,14 @@ import { schoolFactory, userFactory, } from '@shared/testing'; -import { ICurrentUser } from '@modules/authentication'; -import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; -import { FILES_STORAGE_S3_CONNECTION, FilesStorageTestModule } from '@modules/files-storage'; -import { - CopyFileParams, - CopyFilesOfParentParams, - FileRecordListResponse, - FileRecordResponse, -} from '@modules/files-storage/controller/dto'; +import NodeClam from 'clamscan'; import { Request } from 'express'; import FileType from 'file-type-cjs/file-type-cjs-index'; import request from 'supertest'; import { FileRecordParentType } from '../../entity'; +import { FilesStorageTestModule } from '../../files-storage-test.module'; +import { FILES_STORAGE_S3_CONNECTION } from '../../files-storage.config'; +import { CopyFileParams, CopyFilesOfParentParams, FileRecordListResponse, FileRecordResponse } from '../dto'; import { availableParentTypes } from './mocks'; const baseRouteName = '/file/copy'; @@ -107,6 +104,8 @@ describe(`${baseRouteName} (api)`, () => { return true; }, }) + .overrideProvider(NodeClam) + .useValue(createMock()) .compile(); app = module.createNestApplication(); diff --git a/apps/server/src/modules/files-storage/controller/api-test/files-storage-delete-files.api.spec.ts b/apps/server/src/modules/files-storage/controller/api-test/files-storage-delete-files.api.spec.ts index 7a25915b3d5..0557843b8eb 100644 --- a/apps/server/src/modules/files-storage/controller/api-test/files-storage-delete-files.api.spec.ts +++ b/apps/server/src/modules/files-storage/controller/api-test/files-storage-delete-files.api.spec.ts @@ -1,5 +1,7 @@ import { createMock } from '@golevelup/ts-jest'; import { EntityManager, ObjectId } from '@mikro-orm/mongodb'; +import { ICurrentUser } from '@modules/authentication'; +import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ApiValidationError } from '@shared/common'; @@ -14,14 +16,14 @@ import { schoolFactory, userFactory, } from '@shared/testing'; -import { ICurrentUser } from '@modules/authentication'; -import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; -import { FILES_STORAGE_S3_CONNECTION, FilesStorageTestModule } from '@modules/files-storage'; -import { FileRecordListResponse, FileRecordResponse } from '@modules/files-storage/controller/dto'; +import NodeClam from 'clamscan'; import { Request } from 'express'; import FileType from 'file-type-cjs/file-type-cjs-index'; import request from 'supertest'; import { FileRecordParentType, PreviewStatus } from '../../entity'; +import { FilesStorageTestModule } from '../../files-storage-test.module'; +import { FILES_STORAGE_S3_CONNECTION } from '../../files-storage.config'; +import { FileRecordListResponse, FileRecordResponse } from '../dto'; import { availableParentTypes } from './mocks'; const baseRouteName = '/file/delete'; @@ -101,6 +103,8 @@ describe(`${baseRouteName} (api)`, () => { return true; }, }) + .overrideProvider(NodeClam) + .useValue(createMock()) .compile(); app = module.createNestApplication(); diff --git a/apps/server/src/modules/files-storage/controller/api-test/files-storage-download-upload.api.spec.ts b/apps/server/src/modules/files-storage/controller/api-test/files-storage-download-upload.api.spec.ts index 331282060e2..e86b778e0ce 100644 --- a/apps/server/src/modules/files-storage/controller/api-test/files-storage-download-upload.api.spec.ts +++ b/apps/server/src/modules/files-storage/controller/api-test/files-storage-download-upload.api.spec.ts @@ -1,5 +1,7 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { EntityManager } from '@mikro-orm/mongodb'; +import { ICurrentUser } from '@modules/authentication'; +import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ApiValidationError } from '@shared/common'; @@ -7,16 +9,16 @@ import { EntityId, Permission } from '@shared/domain'; import { AntivirusService } from '@shared/infra/antivirus'; import { S3ClientAdapter } from '@shared/infra/s3-client'; import { cleanupCollections, mapUserToCurrentUser, roleFactory, schoolFactory, userFactory } from '@shared/testing'; -import { ICurrentUser } from '@modules/authentication'; -import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; -import { FILES_STORAGE_S3_CONNECTION, FilesStorageTestModule } from '@modules/files-storage'; -import { FileRecordResponse } from '@modules/files-storage/controller/dto'; +import NodeClam from 'clamscan'; import { Request } from 'express'; import FileType from 'file-type-cjs/file-type-cjs-index'; import request from 'supertest'; import { FileRecord } from '../../entity'; import { ErrorType } from '../../error'; +import { FilesStorageTestModule } from '../../files-storage-test.module'; +import { FILES_STORAGE_S3_CONNECTION } from '../../files-storage.config'; import { TestHelper } from '../../helper/test-helper'; +import { FileRecordResponse } from '../dto'; import { availableParentTypes } from './mocks'; jest.mock('file-type-cjs/file-type-cjs-index', () => { @@ -114,6 +116,8 @@ describe('files-storage controller (API)', () => { return true; }, }) + .overrideProvider(NodeClam) + .useValue(createMock()) .compile(); app = module.createNestApplication(); diff --git a/apps/server/src/modules/files-storage/controller/api-test/files-storage-list-files.api.spec.ts b/apps/server/src/modules/files-storage/controller/api-test/files-storage-list-files.api.spec.ts index b4d974fb24e..35410d712c8 100644 --- a/apps/server/src/modules/files-storage/controller/api-test/files-storage-list-files.api.spec.ts +++ b/apps/server/src/modules/files-storage/controller/api-test/files-storage-list-files.api.spec.ts @@ -1,4 +1,7 @@ +import { createMock } from '@golevelup/ts-jest'; import { EntityManager } from '@mikro-orm/mongodb'; +import { ICurrentUser } from '@modules/authentication'; +import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ApiValidationError } from '@shared/common'; @@ -11,13 +14,12 @@ import { schoolFactory, userFactory, } from '@shared/testing'; -import { ICurrentUser } from '@modules/authentication'; -import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; -import { FilesStorageTestModule } from '@modules/files-storage'; -import { FileRecordListResponse, FileRecordResponse } from '@modules/files-storage/controller/dto'; +import NodeClam from 'clamscan'; import { Request } from 'express'; import request from 'supertest'; import { FileRecordParentType, PreviewStatus } from '../../entity'; +import { FilesStorageTestModule } from '../../files-storage-test.module'; +import { FileRecordListResponse, FileRecordResponse } from '../dto'; import { availableParentTypes } from './mocks'; const baseRouteName = '/file/list'; @@ -62,6 +64,8 @@ describe(`${baseRouteName} (api)`, () => { return true; }, }) + .overrideProvider(NodeClam) + .useValue(createMock()) .compile(); app = module.createNestApplication(); diff --git a/apps/server/src/modules/files-storage/controller/api-test/files-storage-preview.api.spec.ts b/apps/server/src/modules/files-storage/controller/api-test/files-storage-preview.api.spec.ts index 82cc8545a97..22749024b31 100644 --- a/apps/server/src/modules/files-storage/controller/api-test/files-storage-preview.api.spec.ts +++ b/apps/server/src/modules/files-storage/controller/api-test/files-storage-preview.api.spec.ts @@ -1,5 +1,7 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { EntityManager, ObjectId } from '@mikro-orm/mongodb'; +import { ICurrentUser } from '@modules/authentication'; +import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; import { ExecutionContext, INestApplication, NotFoundException, StreamableFile } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ApiValidationError } from '@shared/common'; @@ -7,18 +9,18 @@ import { EntityId, Permission } from '@shared/domain'; import { AntivirusService } from '@shared/infra/antivirus'; import { S3ClientAdapter } from '@shared/infra/s3-client'; import { cleanupCollections, mapUserToCurrentUser, roleFactory, schoolFactory, userFactory } from '@shared/testing'; -import { ICurrentUser } from '@modules/authentication'; -import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; -import { FILES_STORAGE_S3_CONNECTION, FilesStorageTestModule } from '@modules/files-storage'; -import { FileRecordResponse } from '@modules/files-storage/controller/dto'; +import NodeClam from 'clamscan'; import { Request } from 'express'; import FileType from 'file-type-cjs/file-type-cjs-index'; import request from 'supertest'; import { FileRecord, ScanStatus } from '../../entity'; import { ErrorType } from '../../error'; +import { FilesStorageTestModule } from '../../files-storage-test.module'; +import { FILES_STORAGE_S3_CONNECTION } from '../../files-storage.config'; import { TestHelper } from '../../helper/test-helper'; import { PreviewWidth } from '../../interface'; import { PreviewOutputMimeTypes } from '../../interface/preview-output-mime-types.enum'; +import { FileRecordResponse } from '../dto'; jest.mock('file-type-cjs/file-type-cjs-index', () => { return { @@ -111,6 +113,8 @@ describe('File Controller (API) - preview', () => { return true; }, }) + .overrideProvider(NodeClam) + .useValue(createMock()) .compile(); app = module.createNestApplication(); diff --git a/apps/server/src/modules/files-storage/controller/api-test/files-storage-rename-file.api.spec.ts b/apps/server/src/modules/files-storage/controller/api-test/files-storage-rename-file.api.spec.ts index e28f4dc327f..fb1dfa866eb 100644 --- a/apps/server/src/modules/files-storage/controller/api-test/files-storage-rename-file.api.spec.ts +++ b/apps/server/src/modules/files-storage/controller/api-test/files-storage-rename-file.api.spec.ts @@ -1,9 +1,11 @@ +import { createMock } from '@golevelup/ts-jest'; import { EntityManager } from '@mikro-orm/mongodb'; +import { ICurrentUser } from '@modules/authentication'; +import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ApiValidationError } from '@shared/common'; import { Permission } from '@shared/domain'; -import { ICurrentUser } from '@modules/authentication'; import { cleanupCollections, fileRecordFactory, @@ -12,12 +14,12 @@ import { schoolFactory, userFactory, } from '@shared/testing'; -import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; -import { FilesStorageTestModule } from '@modules/files-storage'; -import { FileRecordResponse, RenameFileParams } from '@modules/files-storage/controller/dto'; +import NodeClam from 'clamscan'; import { Request } from 'express'; import request from 'supertest'; import { FileRecord, FileRecordParentType } from '../../entity'; +import { FilesStorageTestModule } from '../../files-storage-test.module'; +import { FileRecordResponse, RenameFileParams } from '../dto'; const baseRouteName = '/file/rename/'; @@ -62,6 +64,8 @@ describe(`${baseRouteName} (api)`, () => { return true; }, }) + .overrideProvider(NodeClam) + .useValue(createMock()) .compile(); app = module.createNestApplication(); diff --git a/apps/server/src/modules/files-storage/controller/api-test/files-storage-restore-files.api.spec.ts b/apps/server/src/modules/files-storage/controller/api-test/files-storage-restore-files.api.spec.ts index f6e9a694ad3..496f399d41b 100644 --- a/apps/server/src/modules/files-storage/controller/api-test/files-storage-restore-files.api.spec.ts +++ b/apps/server/src/modules/files-storage/controller/api-test/files-storage-restore-files.api.spec.ts @@ -1,5 +1,7 @@ import { createMock } from '@golevelup/ts-jest'; import { EntityManager, ObjectId } from '@mikro-orm/mongodb'; +import { ICurrentUser } from '@modules/authentication'; +import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { ApiValidationError } from '@shared/common'; @@ -14,14 +16,14 @@ import { schoolFactory, userFactory, } from '@shared/testing'; -import { ICurrentUser } from '@modules/authentication'; -import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; -import { FILES_STORAGE_S3_CONNECTION, FilesStorageTestModule } from '@modules/files-storage'; -import { FileRecordListResponse, FileRecordResponse } from '@modules/files-storage/controller/dto'; +import NodeClam from 'clamscan'; import { Request } from 'express'; import FileType from 'file-type-cjs/file-type-cjs-index'; import request from 'supertest'; import { FileRecordParentType, PreviewStatus } from '../../entity'; +import { FilesStorageTestModule } from '../../files-storage-test.module'; +import { FILES_STORAGE_S3_CONNECTION } from '../../files-storage.config'; +import { FileRecordListResponse, FileRecordResponse } from '../dto'; import { availableParentTypes } from './mocks'; const baseRouteName = '/file/restore'; @@ -126,6 +128,8 @@ describe(`${baseRouteName} (api)`, () => { return true; }, }) + .overrideProvider(NodeClam) + .useValue(createMock()) .compile(); app = module.createNestApplication(); diff --git a/apps/server/src/modules/legacy-school/service/school-year.service.spec.ts b/apps/server/src/modules/legacy-school/service/school-year.service.spec.ts index 041b80d41d1..4aded660643 100644 --- a/apps/server/src/modules/legacy-school/service/school-year.service.spec.ts +++ b/apps/server/src/modules/legacy-school/service/school-year.service.spec.ts @@ -35,7 +35,6 @@ describe('SchoolYearService', () => { describe('getCurrentSchoolYear', () => { const setup = () => { - jest.setSystemTime(new Date('2022-06-01').getTime()); const schoolYear: SchoolYearEntity = schoolYearFactory.build({ startDate: new Date('2021-09-01'), endDate: new Date('2022-12-31'), @@ -60,7 +59,6 @@ describe('SchoolYearService', () => { describe('findById', () => { const setup = () => { - jest.setSystemTime(new Date('2022-06-01').getTime()); const schoolYear: SchoolYearEntity = schoolYearFactory.build({ startDate: new Date('2021-09-01'), endDate: new Date('2022-12-31'), diff --git a/apps/server/src/modules/task/controller/api-test/task-copy-timeout.api.spec.ts b/apps/server/src/modules/task/controller/api-test/task-copy-timeout.api.spec.ts index 446a0fab032..d5546c435da 100644 --- a/apps/server/src/modules/task/controller/api-test/task-copy-timeout.api.spec.ts +++ b/apps/server/src/modules/task/controller/api-test/task-copy-timeout.api.spec.ts @@ -1,9 +1,12 @@ +import { createMock } from '@golevelup/ts-jest'; import { Configuration } from '@hpi-schul-cloud/commons/lib'; import { IConfig } from '@hpi-schul-cloud/commons/lib/interfaces/IConfig'; import { EntityManager } from '@mikro-orm/mongodb'; +import { ICurrentUser } from '@modules/authentication'; +import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; +import { FilesStorageClientAdapterService } from '@modules/files-storage-client'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { ICurrentUser } from '@modules/authentication'; import { cleanupCollections, courseFactory, @@ -12,12 +15,13 @@ import { taskFactory, userFactory, } from '@shared/testing'; -import { JwtAuthGuard } from '@modules/authentication/guard/jwt-auth.guard'; import { Request } from 'express'; import request from 'supertest'; +// config must be set outside before the server module is imported, otherwise the configuration is already set Configuration.set('FEATURE_COPY_SERVICE_ENABLED', true); Configuration.set('INCOMING_REQUEST_TIMEOUT_COPY_API', 1); + // eslint-disable-next-line import/first import { ServerTestModule } from '@modules/server/server.module'; @@ -42,6 +46,8 @@ describe('Task copy (API)', () => { return true; }, }) + .overrideProvider(FilesStorageClientAdapterService) + .useValue(createMock()) .compile(); app = moduleFixture.createNestApplication();