Skip to content

Commit

Permalink
Fix #874: Enable using BoltJS without passing a botId (#1087)
Browse files Browse the repository at this point in the history
* Remove thrown exception if botId isn't present
* fix linting errors
  • Loading branch information
misscoded committed Aug 28, 2021
1 parent da3baca commit 8326547
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
58 changes: 25 additions & 33 deletions src/middleware/builtin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import sinon from 'sinon';
import rewiremock from 'rewiremock';
import { Logger } from '@slack/logger';
import { WebClient } from '@slack/web-api';
import { ErrorCode, ContextMissingPropertyError } from '../errors';
import { ErrorCode } from '../errors';
import { Override, createFakeLogger } from '../test-helpers';
import {
SlackEventMiddlewareArgs,
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('directMention()', () => {

try {
await middleware(fakeArgs);
} catch (err: any) {
} catch (err) {
error = err;
}

Expand Down Expand Up @@ -345,37 +345,6 @@ describe('directMention()', () => {
});

describe('ignoreSelf()', () => {
it('should handle context missing error', async () => {
// Arrange
const fakeNext = sinon.fake.resolves(null);
const fakeBotUserId = undefined;
const fakeArgs = {
next: fakeNext,
context: { botUserId: fakeBotUserId, botId: fakeBotUserId },
} as unknown as MemberJoinedOrLeftChannelMiddlewareArgs;

const { ignoreSelf: getIgnoreSelfMiddleware } = await importBuiltin();

// Act
const middleware = getIgnoreSelfMiddleware();

let error;
try {
await middleware(fakeArgs);
} catch (err: any) {
error = err;
}

// Assert
const expectedError = new ContextMissingPropertyError(
'botId',
'Cannot ignore events from the app without a bot ID. Ensure authorize callback returns a botId.',
);

assert.equal(error.code, expectedError.code);
assert.equal(error.missingProperty, expectedError.missingProperty);
});

it("should immediately call next(), because incoming middleware args don't contain event", async () => {
// Arrange
const fakeNext = sinon.fake();
Expand Down Expand Up @@ -428,6 +397,29 @@ describe('ignoreSelf()', () => {
assert(fakeNext.notCalled);
});

it('should filter an event out when only a botUserId is passed', async () => {
// Arrange
const fakeNext = sinon.fake();
const fakeBotUserId = 'BUSER1';
const fakeArgs = {
next: fakeNext,
context: { botUserId: fakeBotUserId },
event: {
type: 'tokens_revoked',
user: fakeBotUserId,
},
} as unknown as TokensRevokedMiddlewareArgs;

const { ignoreSelf: getIgnoreSelfMiddleware } = await importBuiltin();

// Act
const middleware = getIgnoreSelfMiddleware();
await middleware(fakeArgs);

// Assert
assert(fakeNext.notCalled);
});

it("should filter an event out, because it matches our own app and shouldn't be retained", async () => {
// Arrange
const fakeNext = sinon.fake();
Expand Down
8 changes: 0 additions & 8 deletions src/middleware/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,6 @@ export function matchEventType(pattern: EventTypePattern): Middleware<SlackEvent

export function ignoreSelf(): Middleware<AnyMiddlewareArgs> {
return async (args) => {
// When context does not have a botId in it, then this middleware cannot perform its job. Bail immediately.
if (args.context.botId === undefined) {
throw new ContextMissingPropertyError(
'botId',
'Cannot ignore events from the app without a bot ID. Ensure authorize callback returns a botId.',
);
}

const botId = args.context.botId as string;
const botUserId = args.context.botUserId !== undefined ? (args.context.botUserId as string) : undefined;

Expand Down

0 comments on commit 8326547

Please sign in to comment.