-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- update cdd.processor.ts to use new service - add new env's to worker config file and .env.sample.worker - update and add tests
- Loading branch information
Showing
9 changed files
with
185 additions
and
112 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { workerEnvConfig } from '../config/worker'; | ||
import { SlackMessageService } from './slackMessage.service'; | ||
import { App as SlackApp } from '@slack/bolt'; | ||
|
||
@Module({ | ||
imports: [ConfigModule.forFeature(() => workerEnvConfig())], | ||
providers: [ | ||
{ | ||
provide: SlackApp, | ||
inject: [ConfigService], | ||
useFactory: (config: ConfigService) => { | ||
return new SlackApp({ | ||
signingSecret: config.getOrThrow('slackApp.signingSecret'), | ||
token: config.getOrThrow('slackApp.botToken'), | ||
}); | ||
}, | ||
}, | ||
SlackMessageService, | ||
], | ||
exports: [SlackMessageService], | ||
}) | ||
export class SlackMessageModule {} |
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,65 @@ | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import { Logger } from 'winston'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { SlackMessageService } from './slackMessage.service'; | ||
import { App as SlackApp } from '@slack/bolt'; | ||
|
||
describe('SlackMessageService', () => { | ||
let service: SlackMessageService; | ||
let slackApp: DeepMocked<SlackApp>; | ||
let configService: DeepMocked<ConfigService>; | ||
let logger: DeepMocked<Logger>; | ||
|
||
const mockChannel = 'mock-channel'; | ||
const mockHeader = 'Test Header'; | ||
const mockBody = 'Test Body'; | ||
|
||
beforeEach(() => { | ||
slackApp = createMock<SlackApp>({ | ||
client: { | ||
chat: { | ||
postMessage: jest.fn(), | ||
}, | ||
}, | ||
}); | ||
configService = createMock<ConfigService>(); | ||
logger = createMock<Logger>(); | ||
|
||
configService.getOrThrow.mockReturnValue(mockChannel); | ||
|
||
service = new SlackMessageService(slackApp, configService, logger); | ||
}); | ||
|
||
describe('sendMessage', () => { | ||
it('should send a message to Slack', async () => { | ||
await service.sendMessage({ header: mockHeader, body: mockBody }); | ||
|
||
expect(slackApp.client.chat.postMessage).toHaveBeenCalledWith({ | ||
channel: mockChannel, | ||
text: mockHeader, | ||
blocks: [ | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: mockBody, | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('should log an error if Slack message fails', async () => { | ||
(slackApp.client.chat.postMessage as jest.Mock).mockRejectedValue( | ||
new Error('Slack API Error') | ||
); | ||
|
||
await service.sendMessage({ header: mockHeader, body: mockBody }); | ||
|
||
expect(logger.error).toHaveBeenCalledWith( | ||
'Failed to send message to Slack', | ||
expect.any(Error) | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.