Skip to content

Commit ad782cc

Browse files
Revert "pass slack tokens from api service"
This reverts commit 51b73bc.
1 parent 51b73bc commit ad782cc

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

src/utils/slack-utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export async function say(env, log, slackContext, message) {
2626
channelId: slackContext.channelId,
2727
threadTs: slackContext.threadTs,
2828
env: {
29-
SLACK_BOT_TOKEN: slackContext.slackBotToken,
30-
SLACK_SIGNING_SECRET: slackContext.slackSigningSecret,
31-
SLACK_TOKEN_WORKSPACE_INTERNAL: slackContext.slackTokenWorkspaceInternal,
32-
SLACK_OPS_CHANNEL_WORKSPACE_INTERNAL: slackContext.slackOpsChannelWorkspaceInternal,
29+
SLACK_BOT_TOKEN: env.SLACK_BOT_TOKEN,
30+
SLACK_SIGNING_SECRET: env.SLACK_SIGNING_SECRET,
31+
SLACK_TOKEN_WORKSPACE_INTERNAL: env.SLACK_TOKEN_WORKSPACE_INTERNAL,
32+
SLACK_OPS_CHANNEL_WORKSPACE_INTERNAL: env.SLACK_OPS_CHANNEL_WORKSPACE_INTERNAL,
3333
},
3434
};
3535
const slackTarget = SLACK_TARGETS.WORKSPACE_INTERNAL;

test/utils/slack-utils.test.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,43 @@ describe('slack-utils', () => {
6666
beforeEach(() => {
6767
log = { error: sinon.spy() };
6868
env = {
69-
slackBotToken: 'test-bot-token',
70-
slackSigningSecret: 'test-signing-secret',
71-
slackTokenWorkspaceInternal: 'test-workspace-token',
72-
slackOpsChannelWorkspaceInternal: 'test-ops-channel',
69+
SLACK_BOT_TOKEN: 'test-bot-token',
70+
SLACK_SIGNING_SECRET: 'test-signing-secret',
71+
SLACK_TOKEN_WORKSPACE_INTERNAL: 'test-workspace-token',
72+
SLACK_OPS_CHANNEL_WORKSPACE_INTERNAL: 'test-ops-channel',
7373
};
7474
slackContext = {
7575
channelId: 'C12345678',
7676
threadTs: '12345.67890',
7777
};
7878
});
7979

80+
it('should send a message to Slack when all parameters are valid', async () => {
81+
mockHasText.returns(true);
82+
83+
await say(env, log, slackContext, 'Test message');
84+
85+
expect(mockBaseSlackClient.createFrom.calledOnce).to.be.true;
86+
expect(mockBaseSlackClient.createFrom.calledWith({
87+
channelId: 'C12345678',
88+
threadTs: '12345.67890',
89+
env: {
90+
SLACK_BOT_TOKEN: 'test-bot-token',
91+
SLACK_SIGNING_SECRET: 'test-signing-secret',
92+
SLACK_TOKEN_WORKSPACE_INTERNAL: 'test-workspace-token',
93+
SLACK_OPS_CHANNEL_WORKSPACE_INTERNAL: 'test-ops-channel',
94+
},
95+
}, 'workspace_internal')).to.be.true;
96+
97+
expect(mockSlackClient.postMessage.calledOnce).to.be.true;
98+
expect(mockSlackClient.postMessage.calledWith({
99+
channel: 'C12345678',
100+
thread_ts: '12345.67890',
101+
text: 'Test message',
102+
unfurl_links: false,
103+
})).to.be.true;
104+
});
105+
80106
it('should not send message if threadTs is missing or empty', async () => {
81107
mockHasText.withArgs('C12345678').returns(true);
82108
mockHasText.withArgs('').returns(false);
@@ -156,6 +182,32 @@ describe('slack-utils', () => {
156182
})).to.be.true;
157183
});
158184

185+
it('should handle error when env is missing required properties', async () => {
186+
const incompleteEnv = {
187+
SLACK_BOT_TOKEN: 'test-bot-token',
188+
// Missing other required properties
189+
};
190+
191+
// Make BaseSlackClient.createFrom throw when it receives incomplete env
192+
mockBaseSlackClient.createFrom.withArgs(sinon.match({
193+
env: sinon.match({
194+
SLACK_BOT_TOKEN: 'test-bot-token',
195+
SLACK_SIGNING_SECRET: undefined,
196+
SLACK_TOKEN_WORKSPACE_INTERNAL: undefined,
197+
SLACK_OPS_CHANNEL_WORKSPACE_INTERNAL: undefined,
198+
}),
199+
})).throws(new Error('Missing required environment variables'));
200+
201+
await say(incompleteEnv, log, slackContext, 'Test message');
202+
203+
expect(log.error.calledOnce).to.be.true;
204+
expect(log.error.calledWith('Error sending Slack message:', {
205+
error: 'Missing required environment variables',
206+
stack: sinon.match.string,
207+
errorType: 'Error',
208+
})).to.be.true;
209+
});
210+
159211
it('should handle empty message string', async () => {
160212
mockHasText.returns(true);
161213

0 commit comments

Comments
 (0)