Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedwiper committed Nov 1, 2023
1 parent 7f11099 commit 07d82fa
Show file tree
Hide file tree
Showing 103 changed files with 477 additions and 40 deletions.
2 changes: 2 additions & 0 deletions apps/server/src/apps/helpers/app-start-loggable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AppStartLoggable } from './app-start-loggable';

describe('AppStartLoggable', () => {
describe('getLogMessage', () => {
const expectedMessage = 'Successfully started listening...';
Expand Down
11 changes: 6 additions & 5 deletions apps/server/src/apps/helpers/prometheus-metrics.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createMock } from '@golevelup/ts-jest';
import { Configuration } from '@hpi-schul-cloud/commons';
import { IConfig } from '@hpi-schul-cloud/commons/lib/interfaces/IConfig';
import { createPrometheusMetricsApp } from '@shared/infra/metrics/prometheus/app';
import { PrometheusMetricsConfig } from '@shared/infra/metrics/prometheus/config';
import { createAPIResponseTimeMetricMiddleware } from '@shared/infra/metrics/prometheus/middleware';

import {
createAPIResponseTimeMetricMiddleware,
createPrometheusMetricsApp,
PrometheusMetricsConfig,
} from '@shared/infra/metrics';
import { Logger } from '@src/core/logger/logger';
import express, { Express, NextFunction, Request, RequestHandler, Response } from 'express';
import { Logger } from 'winston';
import {
addPrometheusMetricsMiddlewaresIfEnabled,
createAndStartPrometheusMetricsAppIfEnabled,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ValidationErrorDetailResponse } from './validation-error-detail.response';

describe('ValidationErrorDetailResponse', () => {
describe('when creating a ValidationErrorDetailResponse instance', () => {
it('should have field and errors defined', () => {
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/core/logger/error-logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger as WinstonLogger } from 'winston';
import { ErrorLoggable } from '../error/loggable/error.loggable';
import { ErrorLogger } from './error-logger';

describe('ErrorLogger', () => {
let module: TestingModule;
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/core/logger/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger, Logger as WinstonLogger } from 'winston';
import { Logger as WinstonLogger } from 'winston';
import { Loggable } from './interfaces/loggable';
import { Logger } from './logger';
import { ErrorLogMessage, LogMessage, ValidationErrorLogMessage } from './types/logging.types';

class SampleLoggable implements Loggable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { passwordPattern } from './password-pattern';

describe('password patter', () => {
it('should accept a valid password', () => {
expect(passwordPattern.test('Asdf 1ds df!')).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Account } from '@shared/domain/entity/account.entity';
import { accountFactory } from '@shared/testing/factory/account.factory';
import { AccountEntityToDtoMapper } from './account-entity-to-dto.mapper';

describe('AccountEntityToDtoMapper', () => {
beforeEach(() => {
jest.useFakeTimers();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { Account } from '@shared/domain/entity/account.entity';
import { accountDtoFactory } from '@shared/testing/factory/account-dto.factory';
import { accountFactory } from '@shared/testing/factory/account.factory';
import { AccountDto } from '../services/dto/account.dto';
import { AccountResponseMapper } from './account-response.mapper';

describe('AccountResponseMapper', () => {
describe('mapToResponseFromEntity', () => {
describe('When mapping AccountEntity to AccountResponse', () => {
Expand Down
3 changes: 1 addition & 2 deletions apps/server/src/modules/account/uc/account.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { AuthorizationError } from '@shared/common/error/authorization.error';
import { EntityNotFoundError } from '@shared/common/error/entity-not-found.error';
import { ForbiddenOperationError } from '@shared/common/error/forbidden-operation.error';
import { ValidationError } from '@shared/common/error/validation.error';
import { Account } from '@shared/domain/entity/account.entity';
import { Role } from '@shared/domain/entity/role.entity';
import { SchoolRolePermission, SchoolRoles } from '@shared/domain/entity/school.entity';
Expand All @@ -20,9 +21,7 @@ import { userFactory } from '@shared/testing/factory/user.factory';
import { setupEntities } from '@shared/testing/setup-entities';
import { BruteForcePrevention } from '@src/imports-from-feathers';
import { ICurrentUser } from '@src/modules/authentication/interface/user';

import { ObjectId } from 'bson';
import { ValidationError } from 'class-validator';
import { AccountByIdBodyParams } from '../controller/dto/account-by-id.body.params';
import { AccountByIdParams } from '../controller/dto/account-by-id.params';
import { AccountSearchListResponse } from '../controller/dto/account-search-list.response';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ValidationError } from '@shared/common/error/validation.error';
import { UserDO } from '@shared/domain/domainobject/user.do';
import { Permission } from '@shared/domain/interface/permission.enum';
import { RoleName } from '@shared/domain/interface/rolename.enum';
Expand All @@ -6,7 +7,6 @@ import { schoolFactory } from '@shared/testing/factory/school.factory';
import { userDoFactory } from '@shared/testing/factory/user.do.factory';
import { userFactory } from '@shared/testing/factory/user.factory';
import { setupEntities } from '@shared/testing/setup-entities';
import { ValidationError } from 'class-validator';
import { JwtPayload } from 'jsonwebtoken';
import { CreateJwtPayload } from '../interface/jwt-payload';
import { ICurrentUser, OauthCurrentUser } from '../interface/user';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Test, TestingModule } from '@nestjs/testing';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { UnauthorizedException } from '@nestjs/common';
import { setupEntities } from '@shared/testing/setup-entities';
import { JwtPayload } from 'jsonwebtoken';
import { jwtConstants } from '../constants';
import { JwtPayload } from '../interface/jwt-payload';
import { JwtValidationAdapter } from './jwt-validation.adapter';
import { JwtStrategy } from './jwt.strategy';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { Logger, UnauthorizedException } from '@nestjs/common';
import { UnauthorizedException } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { Test, TestingModule } from '@nestjs/testing';
import { LegacySchoolDo } from '@shared/domain/domainobject/legacy-school.do';
Expand All @@ -16,6 +16,7 @@ import { schoolFactory } from '@shared/testing/factory/school.factory';
import { systemFactory } from '@shared/testing/factory/system.factory';
import { userFactory } from '@shared/testing/factory/user.factory';
import { setupEntities } from '@shared/testing/setup-entities';
import { Logger } from '@src/core/logger/logger';
import { AccountDto } from '@src/modules/account/services/dto/account.dto';
import { LdapAuthorizationBodyParams } from '../controllers/dto/ldap-authorization.body.params';
import { ICurrentUser } from '../interface/user';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Permission } from '@shared/domain/interface/permission.enum';
import { AuthorizationContextBuilder } from './authorization-context.builder';
import { Action } from './types/action.enum';

describe('AuthorizationContextBuilder', () => {
it('Should allow to set required permissions.', () => {
const permissionA = 'a' as Permission;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { Permission } from '@shared/domain/interface/permission.enum';
import { courseFactory } from '@shared/testing/factory/course.factory';
import { roleFactory } from '@shared/testing/factory/role.factory';
import { taskFactory } from '@shared/testing/factory/task.factory';
import { userFactory } from '@shared/testing/factory/user.factory';
import { setupEntities } from '@shared/testing/setup-entities';
import { AuthorizationHelper } from './authorization.helper';

describe('AuthorizationHelper', () => {
// If we have a class with no dependencies, do we still wanna build a testing module? Or is it fine like this?
const service = new AuthorizationHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { linkElementFactory } from '@shared/testing/factory/domainobject/board/l
import { submissionContainerElementFactory } from '@shared/testing/factory/domainobject/board/submission-container-element.do.factory';
import { submissionItemFactory } from '@shared/testing/factory/domainobject/board/submission-item.do.factory';
import { setupEntities } from '@shared/testing/setup-entities';
import { FileDto } from '@src/modules/files-storage-client/dto/file.dto';
import { FilesStorageClientAdapterService } from '@src/modules/files-storage-client/service/files-storage-client.service';
import { FileDto } from '@src/modules/files-storage/dto/file.dto';
import { FileRecordParentType } from '@src/modules/files-storage/entity/filerecord.entity';
import { RecursiveDeleteVisitor } from './recursive-delete.vistor';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { NotFoundException, UnprocessableEntityException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { ValidationError } from '@shared/common/error/validation.error';
import { SubmissionItem } from '@shared/domain/domainobject/board/submission-item.do';
import { cardFactory } from '@shared/testing/factory/domainobject/board/card.do.factory';
import { richTextElementFactory } from '@shared/testing/factory/domainobject/board/rich-text-element.do.factory';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ClassSourceOptions } from '../class-source-options.do';

describe(ClassSourceOptions.name, () => {
describe('constructor', () => {
describe('When a contructor is called', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ClassSourceOptionsEntity } from '../class-source-options.entity';

describe(ClassSourceOptionsEntity.name, () => {
describe('constructor', () => {
describe('When a contructor is called', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CopyFileDto } from './copy-file.dto';

describe('CopyFileDto', () => {
it('Should create well formed copy file dto.', () => {
const id = 'id123';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { FileRecordParentType } from '@shared/infra/rabbitmq/exchange/files-storage';
import { FileDto } from './file.dto';

describe('FileDto', () => {
it('Should create well formed file dto.', () => {
const id = 'id123';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { FileRecordParentType } from '@shared/infra/rabbitmq/exchange/files-storage';
import { lessonFactory } from '@shared/testing/factory/lesson.factory';
import { taskFactory } from '@shared/testing/factory/task.factory';
import { setupEntities } from '@shared/testing/setup-entities';
import { CopyFilesOfParentParamBuilder } from './copy-files-of-parent-param.builder';
import { FileParamBuilder } from './files-storage-param.builder';

describe('CopyFilesOfParentParamBuilder', () => {
beforeAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { FileRecordParentType } from '@shared/infra/rabbitmq/exchange/files-storage';
import { ICopyFileDomainObjectProps } from '../interfaces/copy-file-domain-object-props';
import { IFileDomainObjectProps } from '../interfaces/file-domain-object-props';
import { FilesStorageClientMapper } from './files-storage-client.mapper';

describe('FilesStorageClientMapper', () => {
describe('fileDto mapper', () => {
const record = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { FileRecordParentType } from '@shared/infra/rabbitmq/exchange/files-storage';
import { lessonFactory } from '@shared/testing/factory/lesson.factory';
import { taskFactory } from '@shared/testing/factory/task.factory';
import { setupEntities } from '@shared/testing/setup-entities';
import { FileParamBuilder } from './files-storage-param.builder';

describe('FileParamBuilder', () => {
beforeAll(async () => {
await setupEntities();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { schoolFactory } from '@shared/testing/factory/school.factory';
import { taskFactory } from '@shared/testing/factory/task.factory';
import { setupEntities } from '@shared/testing/setup-entities';
import { LegacyLogger } from '@src/core/logger/legacy-logger.service';
import { CopyFilesOfParentParamBuilder } from '../mapper/copy-files-of-parent-param.builder';
import { FilesStorageClientMapper } from '../mapper/files-storage-client.mapper';
import { FileParamBuilder } from '../mapper/files-storage-param.builder';
import { FilesStorageClientAdapterService } from './files-storage-client.service';
import { FilesStorageProducer } from './files-storage.producer';

describe('FilesStorageClientAdapterService', () => {
let module: TestingModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import {
FileRecordParentType,
FilesStorageExchange,
FilesStorageEvents,
} from '@shared/infra/rabbitmq/exchange/files-storage';
import { setupEntities } from '@shared/testing/setup-entities';
import { LegacyLogger } from '@src/core/logger/legacy-logger.service';
import { ErrorMapper } from '../mapper/error.mapper';
import { FilesStorageProducer } from './files-storage.producer';

describe('FilesStorageProducer', () => {
let module: TestingModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NotAcceptableException, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { AntivirusService } from '@shared/infra/antivirus/antivirus.service';
import { GetFile } from '@shared/infra/s3-client/interfaces';
import { S3ClientAdapter } from '@shared/infra/s3-client/s3-client.adapter';
import { fileRecordFactory } from '@shared/testing/factory/filerecord.factory';
import { setupEntities } from '@shared/testing/setup-entities';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { EntityManager } from '@mikro-orm/core';
import { HttpStatus, INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { UserAndAccountTestFactory } from '@shared/testing/factory/user-and-account.test.factory';
import { TestApiClient } from '@shared/testing/test-api-client';
import { H5PEditorTestModule } from '../../h5p-editor-test.module';

describe('H5PEditor Controller (api)', () => {
let app: INestApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import AdmZip from 'adm-zip';
import { parseStringPromise } from 'xml2js';
import { CommonCartridgeVersion, CommonCartridgeResourceType } from './common-cartridge-enums';
import { ICommonCartridgeFileBuilderOptions, CommonCartridgeFileBuilder } from './common-cartridge-file-builder';
import { ICommonCartridgeOrganizationProps } from './common-cartridge-organization-item-element';
import { ICommonCartridgeResourceProps } from './common-cartridge-resource-item-element';

describe('CommonCartridgeFileBuilder', () => {
let archive: AdmZip;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Builder } from 'xml2js';
import { CommonCartridgeResourceType, CommonCartridgeVersion } from './common-cartridge-enums';
import { ICommonCartridgeLtiResourceProps, CommonCartridgeLtiResource } from './common-cartridge-lti-resource';

describe('CommonCartridgeLtiResource', () => {
const propsVersion1: ICommonCartridgeLtiResourceProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { CommonCartridgeVersion } from './common-cartridge-enums';
import { CommonCartridgeManifestElement } from './common-cartridge-manifest-element';
import { ICommonCartridgeMetadataProps } from './common-cartridge-metadata-element';

describe('CommonCartridgeManifestElement', () => {
const metadataPropsV3: ICommonCartridgeMetadataProps = {
version: CommonCartridgeVersion.V_1_3_0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { CommonCartridgeVersion } from './common-cartridge-enums';
import { ICommonCartridgeMetadataProps, CommonCartridgeMetadataElement } from './common-cartridge-metadata-element';

describe('CommonCartridgeMetadataElement', () => {
describe('transform', () => {
it('should return correct metadata regardless of common cartridge version', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ICommonCartridgeElement } from './common-cartridge-element.interface';
import { CommonCartridgeOrganizationWrapperElement } from './common-cartridge-organization-wrapper-element';

describe('CommonCartridgeOrganizationWrapperElement', () => {
it('should transform the organization elements into the expected structure', () => {
const organizationElementsMock: ICommonCartridgeElement[] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ICommonCartridgeElement } from './common-cartridge-element.interface';
import { CommonCartridgeResourceWrapperElement } from './common-cartridge-resource-wrapper-element';

describe('CommonCartridgeResourceWrapperElement', () => {
it('should transform the resource elements into an array of transformed objects', () => {
const resourceElementsMock: ICommonCartridgeElement[] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { INestApplication, StreamableFile } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { Permission } from '@shared/domain/interface/permission.enum';
import { cleanupCollections } from '@shared/testing/cleanup-collections';
import { courseFactory } from '@shared/testing/factory/course.factory';
import { UserAndAccountTestFactory } from '@shared/testing/factory/user-and-account.test.factory';
import { TestApiClient } from '@shared/testing/test-api-client';
import { ServerTestModule } from '@src/modules/server/server.module';
import { CourseMetadataListResponse } from '../dto/course-metadata.response';

const createStudent = () => {
const { studentUser, studentAccount } = UserAndAccountTestFactory.buildStudent({}, [Permission.COURSE_VIEW]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { EntityManager, ObjectId } from '@mikro-orm/mongodb';
import { ExecutionContext, INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';

import { DashboardEntity, GridElement } from '@shared/domain/entity/dashboard.entity';
import { User } from '@shared/domain/entity/user.entity';
import { Permission } from '@shared/domain/interface/permission.enum';
import { RoleName } from '@shared/domain/interface/rolename.enum';
import { IDashboardRepo } from '@shared/repo/dashboard/dashboard.repo';
import { courseFactory } from '@shared/testing/factory/course.factory';
import { roleFactory } from '@shared/testing/factory/role.factory';
import { userFactory } from '@shared/testing/factory/user.factory';
import { mapUserToCurrentUser } from '@shared/testing/map-user-to-current-user';
import { JwtAuthGuard } from '@src/modules/authentication/guard/jwt-auth.guard';
import { ICurrentUser } from '@src/modules/authentication/interface/user';
import { ServerTestModule } from '@src/modules/server/server.module';
import { Request } from 'express';
import request from 'supertest';
import { DashboardResponse } from '../dto/dashboard.response';

describe('Dashboard Controller (API)', () => {
let app: INestApplication;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import { userFactory } from '@shared/testing/factory/user.factory';
import { mapUserToCurrentUser } from '@shared/testing/map-user-to-current-user';
import { JwtAuthGuard } from '@src/modules/authentication/guard/jwt-auth.guard';
import { ICurrentUser } from '@src/modules/authentication/interface/user';
import { ServerTestModule } from '@src/modules/server/server.module';

import { Request } from 'express';
import request from 'supertest';

Configuration.set('FEATURE_COPY_SERVICE_ENABLED', true);
Configuration.set('INCOMING_REQUEST_TIMEOUT_COPY_API', 1);
// eslint-disable-next-line import/first
import { ServerTestModule } from '@src/modules/server/server.module';

// This needs to be in a separate test file because of the above configuration.
// When we find a way to mock the config, it should be moved alongside the other API tests.
Expand Down
Loading

0 comments on commit 07d82fa

Please sign in to comment.