-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into N21-1336-change-tool-versioning
- Loading branch information
Showing
57 changed files
with
1,613 additions
and
2,533 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
apps/server/src/core/error/loggable/axios-error.loggable.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { axiosErrorFactory } from '@shared/testing/factory'; | ||
import { AxiosError } from 'axios'; | ||
import { AxiosErrorLoggable } from './axios-error.loggable'; | ||
|
||
describe(AxiosErrorLoggable.name, () => { | ||
describe('getLogMessage', () => { | ||
const setup = () => { | ||
const error = { | ||
error: 'invalid_request', | ||
}; | ||
const type = 'mockType'; | ||
const axiosError: AxiosError = axiosErrorFactory.withError(error).build(); | ||
|
||
const axiosErrorLoggable = new AxiosErrorLoggable(axiosError, type); | ||
|
||
return { axiosErrorLoggable, error, axiosError }; | ||
}; | ||
|
||
it('should return error log message', () => { | ||
const { axiosErrorLoggable, error, axiosError } = setup(); | ||
|
||
const result = axiosErrorLoggable.getLogMessage(); | ||
|
||
expect(result).toEqual({ | ||
type: 'mockType', | ||
message: axiosError.message, | ||
data: JSON.stringify(error), | ||
stack: 'mockStack', | ||
}); | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
apps/server/src/core/error/loggable/axios-error.loggable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { HttpException, HttpStatus } from '@nestjs/common'; | ||
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger'; | ||
import { AxiosError } from 'axios'; | ||
|
||
export class AxiosErrorLoggable extends HttpException implements Loggable { | ||
constructor(private readonly axiosError: AxiosError, protected readonly type: string) { | ||
super(JSON.stringify(axiosError.response?.data), axiosError.status ?? HttpStatus.INTERNAL_SERVER_ERROR, { | ||
cause: axiosError.cause, | ||
}); | ||
} | ||
|
||
getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage { | ||
return { | ||
message: this.axiosError.message, | ||
type: this.type, | ||
data: JSON.stringify(this.axiosError.response?.data), | ||
stack: this.axiosError.stack, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './error.loggable'; | ||
export * from './axios-error.loggable'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IMailConfig { | ||
ADDITIONAL_BLACKLISTED_EMAIL_DOMAINS: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
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; | ||
|
@@ -19,6 +22,10 @@ describe('MailService', () => { | |
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(); | ||
|
||
|
@@ -34,13 +41,43 @@ describe('MailService', () => { | |
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('should send given data to queue', async () => { | ||
const data: Mail = { mail: { plainTextContent: 'content', subject: 'Test' }, recipients: ['[email protected]'] }; | ||
const amqpConnectionSpy = jest.spyOn(amqpConnection, 'publish'); | ||
describe('send', () => { | ||
describe('when recipients array is empty', () => { | ||
it('should not send email', async () => { | ||
const data: Mail = { | ||
mail: { plainTextContent: 'content', subject: 'Test' }, | ||
recipients: ['[email protected]'], | ||
}; | ||
|
||
await service.send(data); | ||
const amqpConnectionSpy = jest.spyOn(amqpConnection, 'publish'); | ||
|
||
const expectedParams = [mailServiceOptions.exchange, mailServiceOptions.routingKey, data, { persistent: true }]; | ||
expect(amqpConnectionSpy).toHaveBeenCalledWith(...expectedParams); | ||
await service.send(data); | ||
|
||
expect(amqpConnectionSpy).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
describe('when sending email', () => { | ||
it('should remove email address that have blacklisted domain and send given data to queue', async () => { | ||
const data: Mail = { | ||
mail: { plainTextContent: 'content', subject: 'Test' }, | ||
recipients: ['[email protected]', '[email protected]', '[email protected]', '[email protected]'], | ||
cc: ['[email protected]', '[email protected]', '[email protected]'], | ||
bcc: ['[email protected]', '[email protected]', '[email protected]'], | ||
replyTo: ['[email protected]', '[email protected]', '[email protected]'], | ||
}; | ||
|
||
const amqpConnectionSpy = jest.spyOn(amqpConnection, 'publish'); | ||
|
||
await service.send(data); | ||
|
||
expect(data.recipients).toEqual(['[email protected]']); | ||
expect(data.cc).toEqual([]); | ||
expect(data.bcc).toEqual(['[email protected]']); | ||
expect(data.replyTo).toEqual(['[email protected]']); | ||
|
||
const expectedParams = [mailServiceOptions.exchange, mailServiceOptions.routingKey, data, { persistent: true }]; | ||
expect(amqpConnectionSpy).toHaveBeenCalledWith(...expectedParams); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.