Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-5249 rename entities #4428

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EntityManager } from '@mikro-orm/core';
import { HttpStatus, INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { Account, RoleName, SchoolEntity, System, User } from '@shared/domain';
import { Account, RoleName, SchoolEntity, SystemEntity, User } from '@shared/domain';
import { accountFactory, roleFactory, schoolFactory, systemFactory, userFactory } from '@shared/testing';
import { SSOErrorCode } from '@src/modules/oauth/error/sso-error-code.enum';
import { OauthTokenResponse } from '@src/modules/oauth/service/dto';
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('Login Controller (api)', () => {
let account: Account;
let user: User;
let school: SchoolEntity;
let system: System;
let system: SystemEntity;

beforeAll(async () => {
const schoolExternalId = 'mockSchoolExternalId';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createMock } from '@golevelup/ts-jest';
import { UnauthorizedException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { System } from '@shared/domain';
import { SystemEntity } from '@shared/domain';
import { systemFactory } from '@shared/testing';
import { LegacyLogger } from '@src/core/logger';
import { LdapService } from './ldap.service';
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('LdapService', () => {
describe('checkLdapCredentials', () => {
describe('when credentials are correct', () => {
it('should login successfully', async () => {
const system: System = systemFactory.withLdapConfig().buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig().buildWithId();
await expect(
ldapService.checkLdapCredentials(system, 'connectSucceeds', 'mockPassword')
).resolves.not.toThrow();
Expand All @@ -68,7 +68,7 @@ describe('LdapService', () => {

describe('when no ldap config is provided', () => {
it('should throw error', async () => {
const system: System = systemFactory.buildWithId();
const system: SystemEntity = systemFactory.buildWithId();
await expect(ldapService.checkLdapCredentials(system, 'mockUsername', 'mockPassword')).rejects.toThrow(
new Error(`no LDAP config found in system ${system.id}`)
);
Expand All @@ -77,7 +77,7 @@ describe('LdapService', () => {

describe('when user is not authorized', () => {
it('should throw unauthorized error', async () => {
const system: System = systemFactory.withLdapConfig().buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig().buildWithId();
await expect(ldapService.checkLdapCredentials(system, 'mockUsername', 'mockPassword')).rejects.toThrow(
new UnauthorizedException('User could not authenticate')
);
Expand All @@ -86,7 +86,7 @@ describe('LdapService', () => {

describe('when connected flag is not set', () => {
it('should throw unauthorized error', async () => {
const system: System = systemFactory.withLdapConfig().buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig().buildWithId();
await expect(ldapService.checkLdapCredentials(system, 'connectWithoutFlag', 'mockPassword')).rejects.toThrow(
new UnauthorizedException('User could not authenticate')
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { System } from '@shared/domain';
import { SystemEntity } from '@shared/domain';
import { ErrorUtils } from '@src/core/error/utils';
import { LegacyLogger } from '@src/core/logger';
import { Client, createClient } from 'ldapjs';
Expand All @@ -11,7 +11,7 @@ export class LdapService {
this.logger.setContext(LdapService.name);
}

async checkLdapCredentials(system: System, username: string, password: string): Promise<void> {
async checkLdapCredentials(system: SystemEntity, username: string, password: string): Promise<void> {
const connection = await this.connect(system, username, password);
if (connection.connected) {
connection.unbind();
Expand All @@ -20,7 +20,7 @@ export class LdapService {
throw new UnauthorizedException('User could not authenticate');
}

private connect(system: System, username: string, password: string): Promise<Client> {
private connect(system: SystemEntity, username: string, password: string): Promise<Client> {
const { ldapConfig } = system;
if (!ldapConfig) {
throw Error(`no LDAP config found in system ${system.id}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { UnauthorizedException } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { Test, TestingModule } from '@nestjs/testing';
import { LegacySchoolDo, RoleName, System, User } from '@shared/domain';
import { LegacySchoolDo, RoleName, SystemEntity, User } from '@shared/domain';
import { LegacySchoolRepo, SystemRepo, UserRepo } from '@shared/repo';
import {
accountDtoFactory,
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('LdapStrategy', () => {
const setup = () => {
const username = 'mockUserName';

const system: System = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();

const user: User = userFactory.withRoleByName(RoleName.STUDENT).buildWithId({ ldapDn: undefined });

Expand Down Expand Up @@ -134,7 +134,7 @@ describe('LdapStrategy', () => {
const setup = () => {
const username = 'mockUserName';

const system: System = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();

const user: User = userFactory.withRoleByName(RoleName.STUDENT).buildWithId({ ldapDn: 'mockLdapDn' });

Expand Down Expand Up @@ -181,7 +181,7 @@ describe('LdapStrategy', () => {
const setup = () => {
const username = 'mockUserName';

const system: System = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();

const user: User = userFactory.withRoleByName(RoleName.STUDENT).buildWithId({ ldapDn: 'mockLdapDn' });

Expand Down Expand Up @@ -228,7 +228,7 @@ describe('LdapStrategy', () => {
const setup = () => {
const username = 'mockUserName';

const system: System = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();

const user: User = userFactory.withRoleByName(RoleName.STUDENT).buildWithId({ ldapDn: 'mockLdapDn' });

Expand Down Expand Up @@ -275,7 +275,7 @@ describe('LdapStrategy', () => {
const setup = () => {
const username = 'mockUserName';

const system: System = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();

const user: User = userFactory.withRoleByName(RoleName.STUDENT).buildWithId({ ldapDn: 'mockLdapDn' });

Expand Down Expand Up @@ -327,7 +327,7 @@ describe('LdapStrategy', () => {
const error = new Error('error');
const username = 'mockUserName';

const system: System = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig({ rootPath: 'rootPath' }).buildWithId();

const user: User = userFactory.withRoleByName(RoleName.STUDENT).buildWithId({ ldapDn: 'mockLdapDn' });

Expand Down Expand Up @@ -382,7 +382,7 @@ describe('LdapStrategy', () => {
const setup = () => {
const username = 'mockUserName';

const system: System = systemFactory.withLdapConfig().buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig().buildWithId();

const user: User = userFactory
.withRoleByName(RoleName.STUDENT)
Expand Down Expand Up @@ -444,7 +444,7 @@ describe('LdapStrategy', () => {
const setup = () => {
const username = 'mockUserName';

const system: System = systemFactory.withLdapConfig().buildWithId();
const system: SystemEntity = systemFactory.withLdapConfig().buildWithId();

const user: User = userFactory
.withRoleByName(RoleName.STUDENT)
Expand Down
11 changes: 8 additions & 3 deletions apps/server/src/modules/authentication/strategy/ldap.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { LegacySchoolDo, System, User } from '@shared/domain';
import { LegacySchoolDo, SystemEntity, User } from '@shared/domain';
import { LegacySchoolRepo, SystemRepo, UserRepo } from '@shared/repo';
import { ErrorLoggable } from '@src/core/error/loggable/error.loggable';
import { Logger } from '@src/core/logger';
Expand Down Expand Up @@ -28,7 +28,7 @@ export class LdapStrategy extends PassportStrategy(Strategy, 'ldap') {
async validate(request: { body: LdapAuthorizationBodyParams }): Promise<ICurrentUser> {
const { username, password, systemId, schoolId } = this.extractParamsFromRequest(request);

const system: System = await this.systemRepo.findById(systemId);
const system: SystemEntity = await this.systemRepo.findById(systemId);

const school: LegacySchoolDo = await this.schoolRepo.findById(schoolId);

Expand Down Expand Up @@ -72,7 +72,12 @@ export class LdapStrategy extends PassportStrategy(Strategy, 'ldap') {
return value;
}

private async checkCredentials(account: AccountDto, system: System, ldapDn: string, password: string): Promise<void> {
private async checkCredentials(
account: AccountDto,
system: SystemEntity,
ldapDn: string,
password: string
): Promise<void> {
try {
await this.ldapService.checkLdapCredentials(system, ldapDn, password);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpModule } from '@nestjs/axios';
import { DynamicModule, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { Account, Role, SchoolEntity, SchoolYear, System, User } from '@shared/domain';
import { Account, Role, SchoolEntity, SchoolYearEntity, SystemEntity, User } from '@shared/domain';
import { MongoMemoryDatabaseModule } from '@shared/infra/database';
import { MongoDatabaseModuleOptions } from '@shared/infra/database/mongo-memory-database/types';
import { RabbitMQWrapperTestModule } from '@shared/infra/rabbitmq';
Expand All @@ -16,7 +16,7 @@ import { config, s3Config } from './fwu-learning-contents.config';
import { FwuLearningContentsUc } from './uc/fwu-learning-contents.uc';

const imports = [
MongoMemoryDatabaseModule.forRoot({ entities: [User, Account, Role, SchoolEntity, System, SchoolYear] }),
MongoMemoryDatabaseModule.forRoot({ entities: [User, Account, Role, SchoolEntity, SystemEntity, SchoolYearEntity] }),
AuthorizationModule,
AuthenticationModule,
ConfigModule.forRoot(createConfigModuleOptions(config)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs';
import { HttpModule } from '@nestjs/axios';
import { Module, NotFoundException } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { Account, Role, SchoolEntity, SchoolYear, System, User } from '@shared/domain';
import { Account, Role, SchoolEntity, SchoolYearEntity, SystemEntity, User } from '@shared/domain';
import { RabbitMQWrapperModule } from '@shared/infra/rabbitmq';
import { S3ClientModule } from '@shared/infra/s3-client';
import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config';
Expand Down Expand Up @@ -36,7 +36,7 @@ const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
clientUrl: DB_URL,
password: DB_PASSWORD,
user: DB_USERNAME,
entities: [User, Account, Role, SchoolEntity, System, SchoolYear],
entities: [User, Account, Role, SchoolEntity, SystemEntity, SchoolYearEntity],

// debug: true, // use it for locally debugging of querys
}),
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/modules/group/repo/group-domain.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { ExternalSource, ExternalSourceEntity, Role, SchoolEntity, System, User } from '@shared/domain';
import { ExternalSource, ExternalSourceEntity, Role, SchoolEntity, SystemEntity, User } from '@shared/domain';
import { Group, GroupProps, GroupTypes, GroupUser } from '../domain';
import { GroupEntity, GroupEntityProps, GroupEntityTypes, GroupUserEntity, GroupValidPeriodEntity } from '../entity';

Expand Down Expand Up @@ -63,7 +63,7 @@ export class GroupDomainMapper {
): ExternalSourceEntity {
const mapped = new ExternalSourceEntity({
externalId: externalSource.externalId,
system: em.getReference(System, externalSource.systemId),
system: em.getReference(SystemEntity, externalSource.systemId),
});

return mapped;
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/modules/h5p-editor/h5p-editor-test.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DynamicModule, Module } from '@nestjs/common';
import { Account, Role, SchoolEntity, SchoolYear, User } from '@shared/domain';
import { Account, Role, SchoolEntity, SchoolYearEntity, User } from '@shared/domain';
import { MongoMemoryDatabaseModule } from '@shared/infra/database';
import { MongoDatabaseModuleOptions } from '@shared/infra/database/mongo-memory-database/types';
import { RabbitMQWrapperTestModule } from '@shared/infra/rabbitmq';
Expand All @@ -12,7 +12,7 @@ import { H5PEditorModule } from './h5p-editor.module';

const imports = [
H5PEditorModule,
MongoMemoryDatabaseModule.forRoot({ entities: [Account, Role, SchoolEntity, SchoolYear, User] }),
MongoMemoryDatabaseModule.forRoot({ entities: [Account, Role, SchoolEntity, SchoolYearEntity, User] }),
AuthenticationApiModule,
AuthorizationModule,
AuthenticationModule,
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/modules/h5p-editor/h5p-editor.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Dictionary, IPrimaryKey } from '@mikro-orm/core';
import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs';
import { Module, NotFoundException } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { Account, Role, SchoolEntity, SchoolYear, System, User } from '@shared/domain';
import { Account, Role, SchoolEntity, SchoolYearEntity, SystemEntity, User } from '@shared/domain';
import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config';
import { CoreModule } from '@src/core';
import { Logger } from '@src/core/logger';
Expand All @@ -28,7 +28,7 @@ const imports = [
clientUrl: DB_URL,
password: DB_PASSWORD,
user: DB_USERNAME,
entities: [User, Account, Role, SchoolEntity, System, SchoolYear],
entities: [User, Account, Role, SchoolEntity, SystemEntity, SchoolYearEntity],

// debug: true, // use it for locally debugging of querys
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { SchoolYear } from '@shared/domain';
import { SchoolYearEntity } from '@shared/domain';
import { MongoMemoryDatabaseModule } from '@shared/infra/database';
import { cleanupCollections } from '@shared/testing';
import { schoolYearFactory } from '@shared/testing/factory/schoolyear.factory';
Expand Down Expand Up @@ -29,14 +29,14 @@ describe('schoolyear repo', () => {
});

it('should implement entityName getter', () => {
expect(repo.entityName).toBe(SchoolYear);
expect(repo.entityName).toBe(SchoolYearEntity);
});

it('should create a schoolyear', async () => {
const schoolYear = schoolYearFactory.build();
await repo.save(schoolYear);
em.clear();
const storedSchoolYears = await em.find(SchoolYear, {});
const storedSchoolYears = await em.find(SchoolYearEntity, {});
expect(storedSchoolYears).toHaveLength(1);
const storedSchoolYear = storedSchoolYears[0];
expect(storedSchoolYear).toEqual(schoolYear);
Expand All @@ -48,7 +48,7 @@ describe('schoolyear repo', () => {
describe('findCurrentYear', () => {
describe('when date is between schoolyears start and end date', () => {
const setup = async () => {
const schoolYear: SchoolYear = schoolYearFactory.build({
const schoolYear: SchoolYearEntity = schoolYearFactory.build({
startDate: new Date('2020-08-01'),
endDate: new Date('9999-07-31'),
});
Expand All @@ -70,7 +70,7 @@ describe('schoolyear repo', () => {

describe('when date is not between schoolyears start and end date', () => {
const setup = async () => {
const schoolYear: SchoolYear = schoolYearFactory.build({
const schoolYear: SchoolYearEntity = schoolYearFactory.build({
startDate: new Date('2020-08-01'),
endDate: new Date('2021-07-31'),
});
Expand Down
10 changes: 5 additions & 5 deletions apps/server/src/modules/legacy-school/repo/schoolyear.repo.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Injectable } from '@nestjs/common';
import { SchoolYear } from '@shared/domain';
import { SchoolYearEntity } from '@shared/domain';
import { BaseRepo } from '@shared/repo/base.repo';

@Injectable()
export class SchoolYearRepo extends BaseRepo<SchoolYear> {
export class SchoolYearRepo extends BaseRepo<SchoolYearEntity> {
get entityName() {
return SchoolYear;
return SchoolYearEntity;
}

async findCurrentYear(): Promise<SchoolYear> {
async findCurrentYear(): Promise<SchoolYearEntity> {
const currentDate = new Date();
const year: SchoolYear | null = await this._em.findOneOrFail(SchoolYear, {
const year: SchoolYearEntity | null = await this._em.findOneOrFail(SchoolYearEntity, {
$and: [{ startDate: { $lte: currentDate } }, { endDate: { $gte: currentDate } }],
});
return year;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { FederalStateRepo } from '@shared/repo';
import { federalStateFactory, setupEntities } from '@shared/testing';
import { FederalState } from '@shared/domain';
import { FederalStateEntity } from '@shared/domain';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { FederalStateService } from './federal-state.service';
import { FederalStateNames } from '../types/federal-state-names.enum';
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('FederalStateService', () => {

describe('findFederalStateByName', () => {
const setup = () => {
const federalState: FederalState = federalStateFactory.build({ name: FederalStateNames.NIEDERSACHEN });
const federalState: FederalStateEntity = federalStateFactory.build({ name: FederalStateNames.NIEDERSACHEN });
federalStateRepo.findByName.mockResolvedValue(federalState);

return {
Expand All @@ -45,7 +45,7 @@ describe('FederalStateService', () => {
it('should return a federal state', async () => {
const { federalState } = setup();

const result: FederalState = await service.findFederalStateByName(federalState.name);
const result: FederalStateEntity = await service.findFederalStateByName(federalState.name);

expect(result).toBeDefined();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable } from '@nestjs/common';
import { FederalStateRepo } from '@shared/repo';
import { FederalState } from '@shared/domain';
import { FederalStateEntity } from '@shared/domain';

@Injectable()
export class FederalStateService {
constructor(private readonly federalStateRepo: FederalStateRepo) {}

// TODO: N21-990 Refactoring: Create domain objects for schoolYear and federalState
async findFederalStateByName(name: string): Promise<FederalState> {
const federalState: FederalState = await this.federalStateRepo.findByName(name);
async findFederalStateByName(name: string): Promise<FederalStateEntity> {
const federalState: FederalStateEntity = await this.federalStateRepo.findByName(name);

return federalState;
}
Expand Down
Loading
Loading