Skip to content

Commit

Permalink
fix for service injection
Browse files Browse the repository at this point in the history
  • Loading branch information
sszafGCA committed Oct 24, 2023
1 parent 490459f commit bfd3671
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
7 changes: 6 additions & 1 deletion apps/server/src/modules/server/server.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { IAccountConfig } from '@modules/account';
import type { IFilesStorageClientConfig } from '@modules/files-storage-client';
import type { IUserConfig } from '@modules/user';
import type { ICommonCartridgeConfig } from '@modules/learnroom/common-cartridge';
import { IMailConfig } from '@shared/infra/mail/interfaces/mail-config';

export enum NodeEnvType {
TEST = 'test',
Expand All @@ -19,7 +20,8 @@ export interface IServerConfig
IFilesStorageClientConfig,
IAccountConfig,
IIdentityManagementConfig,
ICommonCartridgeConfig {
ICommonCartridgeConfig,
IMailConfig {
NODE_ENV: string;
SC_DOMAIN: string;
}
Expand All @@ -39,6 +41,9 @@ const config: IServerConfig = {
FEATURE_IDENTITY_MANAGEMENT_ENABLED: Configuration.get('FEATURE_IDENTITY_MANAGEMENT_ENABLED') as boolean,
FEATURE_IDENTITY_MANAGEMENT_STORE_ENABLED: Configuration.get('FEATURE_IDENTITY_MANAGEMENT_STORE_ENABLED') as boolean,
FEATURE_IDENTITY_MANAGEMENT_LOGIN_ENABLED: Configuration.get('FEATURE_IDENTITY_MANAGEMENT_LOGIN_ENABLED') as boolean,
BLACKLISTED_DOMAINS: Configuration.has('ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS')
? (Configuration.get('ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS') as string).split(',').map((domain) => domain.trim())
: ([] as string[]),
};

export const serverConfig = () => config;
3 changes: 3 additions & 0 deletions apps/server/src/shared/infra/mail/interfaces/mail-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IMailConfig {
BLACKLISTED_DOMAINS: string[];
}
9 changes: 7 additions & 2 deletions apps/server/src/shared/infra/mail/mail.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { Test, TestingModule } from '@nestjs/testing';
import { Configuration } from '@hpi-schul-cloud/commons/lib';
import { createMock } from '@golevelup/ts-jest';
import { ConfigService } from '@nestjs/config';
import { Mail } from './mail.interface';
import { MailService } from './mail.service';
import { IMailConfig } from './interfaces/mail-config';

describe('MailService', () => {
let module: TestingModule;
Expand All @@ -15,12 +17,15 @@ describe('MailService', () => {
};

beforeAll(async () => {
Configuration.set('ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS', 'schul-cloud.org, example.com');
module = await Test.createTestingModule({
providers: [
MailService,
{ provide: AmqpConnection, useValue: { publish: () => {} } },
{ provide: 'MAIL_SERVICE_OPTIONS', useValue: mailServiceOptions },
{
provide: ConfigService,
useValue: createMock<ConfigService<IMailConfig, true>>({ get: () => ['schul-cloud.org', 'example.com'] }),
},
],
}).compile();

Expand Down
18 changes: 5 additions & 13 deletions apps/server/src/shared/infra/mail/mail.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { Inject, Injectable } from '@nestjs/common';
import { Configuration } from '@hpi-schul-cloud/commons/lib';

import { ConfigService } from '@nestjs/config';
import { Mail } from './mail.interface';
import { IMailConfig } from './interfaces/mail-config';

interface MailServiceOptions {
exchange: string;
Expand All @@ -15,18 +15,10 @@ export class MailService {

constructor(
private readonly amqpConnection: AmqpConnection,
@Inject('MAIL_SERVICE_OPTIONS') private readonly options: MailServiceOptions
@Inject('MAIL_SERVICE_OPTIONS') private readonly options: MailServiceOptions,
private readonly configService: ConfigService<IMailConfig, true>
) {
this.domainBlacklist = this.getDomainBlacklist();
}

private getDomainBlacklist(): string[] {
if (Configuration.has('ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS')) {
const domainBlackList = Configuration.get('ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS') as string;
const domainBlackListArray = domainBlackList.split(',').map((domain) => domain.trim());
return domainBlackListArray;
}
return [] as string[];
this.domainBlacklist = this.configService.get<string[]>('BLACKLISTED_DOMAINS');
}

public async send(data: Mail): Promise<void> {
Expand Down

0 comments on commit bfd3671

Please sign in to comment.