Skip to content

Commit

Permalink
add client factory and init test
Browse files Browse the repository at this point in the history
  • Loading branch information
sdinkov committed Dec 20, 2024
1 parent 06a973a commit 2fe346b
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apps/server/src/infra/vidis-client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './generated/api';
export * from './generated/models';
export * from './vidis-client-config';
export * from './vidis-client-factory';
export * from './vidis-client.module';
3 changes: 3 additions & 0 deletions apps/server/src/infra/vidis-client/vidis-client-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface VidisClientConfig {
VIDIS_API_CLIENT_BASE_URL: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createMock } from '@golevelup/ts-jest';
import { ServerTestModule } from '@modules/server';
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { VidisClientFactory } from './vidis-client-factory';
import { VidisClientModule } from './vidis-client.module';

describe.skip('VidisClientFactory Integration', () => {
let module: TestingModule;
let sut: VidisClientFactory;

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [ServerTestModule, VidisClientModule],
})
.overrideProvider(ConfigService)
.useValue(
createMock<ConfigService>({
getOrThrow: (key: string) => {
switch (key) {
case 'VIDIS_API_CLIENT_BASE_URL':
return 'https://test2.schulportal-thueringen.de/tip-ms/api';

default:
throw new Error(`Unknown key: ${key}`);
}
},
})
)
.compile();

sut = module.get(VidisClientFactory);
});

afterAll(async () => {
await module.close();
});

it('should be defined', () => {
expect(sut).toBeDefined();
});
});
64 changes: 64 additions & 0 deletions apps/server/src/infra/vidis-client/vidis-client-factory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { faker } from '@faker-js/faker';
import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { ServerConfig } from '@modules/server';
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { VidisClientFactory } from './vidis-client-factory';

describe('TspClientFactory', () => {
let module: TestingModule;
let sut: VidisClientFactory;
let configServiceMock: DeepMocked<ConfigService<ServerConfig, true>>;

beforeAll(async () => {
module = await Test.createTestingModule({
providers: [
VidisClientFactory,
{
provide: ConfigService,
useValue: createMock<ConfigService<ServerConfig, true>>({
getOrThrow: (key: string) => {
switch (key) {
case 'VIDIS_API_CLIENT_BASE_URL':
return faker.internet.url();
default:
throw new Error(`Unknown key: ${key}`);
}
},
}),
},
],
}).compile();

sut = module.get(VidisClientFactory);
configServiceMock = module.get(ConfigService);
});

afterAll(async () => {
await module.close();
});

beforeEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
jest.clearAllMocks();
});

it('should be defined', () => {
expect(sut).toBeDefined();
});

describe('createExportClient', () => {
describe('when createExportClient is called', () => {
it('should return ExportApiInterface', () => {
const result = sut.createExportClient({
password: faker.string.alpha(),
username: faker.string.alpha(),
});

expect(result).toBeDefined();
expect(configServiceMock.getOrThrow).toHaveBeenCalledTimes(0);
});
});
});
});
30 changes: 30 additions & 0 deletions apps/server/src/infra/vidis-client/vidis-client-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Configuration, IDMBetreiberApiFactory, IDMBetreiberApiInterface } from './generated';
import { VidisClientConfig } from './vidis-client-config';

type FactoryParams = {
username: string;
password: string;
};

@Injectable()
export class VidisClientFactory {
private readonly baseUrl: string;

constructor(private readonly configService: ConfigService<VidisClientConfig, true>) {
this.baseUrl = configService.getOrThrow<string>('VIDIS_API_CLIENT_BASE_URL');
}

public createExportClient(params: FactoryParams): IDMBetreiberApiInterface {
const factory = IDMBetreiberApiFactory(
new Configuration({
username: params.username,
password: params.password,
basePath: this.baseUrl,
})
);

return factory;
}
}
9 changes: 9 additions & 0 deletions apps/server/src/infra/vidis-client/vidis-client.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { VidisClientFactory } from './vidis-client-factory';

@Module({
imports: [],
providers: [VidisClientFactory],
exports: [VidisClientFactory],
})
export class VidisClientModule {}

0 comments on commit 2fe346b

Please sign in to comment.