Skip to content

Commit

Permalink
BC-5597 - Fix unstable tests (#4497)
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax authored Oct 25, 2023
1 parent e5bd1c9 commit 3e112cc
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 };
Expand Down Expand Up @@ -62,6 +64,8 @@ describe(`${baseRouteName} (api)`, () => {
return true;
},
})
.overrideProvider(NodeClam)
.useValue(createMock<NodeClam>())
.compile();

app = module.createNestApplication();
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -107,6 +104,8 @@ describe(`${baseRouteName} (api)`, () => {
return true;
},
})
.overrideProvider(NodeClam)
.useValue(createMock<NodeClam>())
.compile();

app = module.createNestApplication();
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -101,6 +103,8 @@ describe(`${baseRouteName} (api)`, () => {
return true;
},
})
.overrideProvider(NodeClam)
.useValue(createMock<NodeClam>())
.compile();

app = module.createNestApplication();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
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';
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', () => {
Expand Down Expand Up @@ -114,6 +116,8 @@ describe('files-storage controller (API)', () => {
return true;
},
})
.overrideProvider(NodeClam)
.useValue(createMock<NodeClam>())
.compile();

app = module.createNestApplication();
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -62,6 +64,8 @@ describe(`${baseRouteName} (api)`, () => {
return true;
},
})
.overrideProvider(NodeClam)
.useValue(createMock<NodeClam>())
.compile();

app = module.createNestApplication();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
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';
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 {
Expand Down Expand Up @@ -111,6 +113,8 @@ describe('File Controller (API) - preview', () => {
return true;
},
})
.overrideProvider(NodeClam)
.useValue(createMock<NodeClam>())
.compile();

app = module.createNestApplication();
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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/';

Expand Down Expand Up @@ -62,6 +64,8 @@ describe(`${baseRouteName} (api)`, () => {
return true;
},
})
.overrideProvider(NodeClam)
.useValue(createMock<NodeClam>())
.compile();

app = module.createNestApplication();
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -126,6 +128,8 @@ describe(`${baseRouteName} (api)`, () => {
return true;
},
})
.overrideProvider(NodeClam)
.useValue(createMock<NodeClam>())
.compile();

app = module.createNestApplication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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'),
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';

Expand All @@ -42,6 +46,8 @@ describe('Task copy (API)', () => {
return true;
},
})
.overrideProvider(FilesStorageClientAdapterService)
.useValue(createMock<FilesStorageClientAdapterService>())
.compile();

app = moduleFixture.createNestApplication();
Expand Down

0 comments on commit 3e112cc

Please sign in to comment.