-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed the issue with member joined\left channel event not firing for own bot #236
Merged
shaydewael
merged 8 commits into
slackapi:master
from
TK95:fix-member_joined/left_channel-event-not-firing-for-own-bot
Oct 4, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
213ae4b
Fixed the issue with member joined\left channel event not firing for …
TK95 6e8313d
Merge branch 'master' into fix-member_joined/left_channel-event-not-f…
95f61f2
Merge branch 'master' into fix-member_joined/left_channel-event-not-f…
c91d671
Refactored 'ignoreSelf' middleware to make it more readable
TK95 b166deb
Merge branch 'master' into fix-member_joined/left_channel-event-not-f…
f88bad0
Merge branch 'master' into fix-member_joined/left_channel-event-not-f…
4366796
Merge branch 'master' into fix-member_joined/left_channel-event-not-f…
faa8426
Merge branch 'master' into fix-member_joined/left_channel-event-not-f…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import sinon from 'sinon'; | |
import { ErrorCode } from '../errors'; | ||
import { Override, delay, wrapToResolveOnFirstCall } from '../test-helpers'; | ||
import rewiremock from 'rewiremock'; | ||
import { SlackEventMiddlewareArgs, NextMiddleware, Context, MessageEvent } from '../types'; | ||
import { SlackEventMiddlewareArgs, NextMiddleware, Context, MessageEvent, ContextMissingPropertyError, SlackCommandMiddlewareArgs } from '../types'; | ||
|
||
describe('matchMessage()', () => { | ||
function initializeTestCase(pattern: string | RegExp): Mocha.AsyncFunc { | ||
|
@@ -261,13 +261,180 @@ describe('directMention()', () => { | |
}); | ||
}); | ||
|
||
describe('ignoreSelf()', () => { | ||
it('should handle context missing error', async () => { | ||
// Arrange | ||
const fakeNext = sinon.fake(); | ||
const fakeBotUserId = undefined; | ||
const fakeArgs = { | ||
next: fakeNext, | ||
context: { botUserId: fakeBotUserId }, | ||
} as unknown as MemberJoinedOrLeftChannelMiddlewareArgs; | ||
|
||
const { ignoreSelf: getIgnoreSelfMiddleware, contextMissingPropertyError } = await importBuiltin(); | ||
|
||
// Act | ||
const middleware = getIgnoreSelfMiddleware(); | ||
middleware(fakeArgs); | ||
await delay(); | ||
|
||
// Assert | ||
const expectedError = contextMissingPropertyError( | ||
'botId', | ||
'Cannot ignore events from the app without a bot ID. Ensure authorize callback returns a botId.', | ||
); | ||
|
||
const contextMissingError: ContextMissingPropertyError = fakeNext.getCall(0).lastArg; | ||
|
||
assert.equal(contextMissingError.code, expectedError.code); | ||
assert.equal(contextMissingError.missingProperty, expectedError.missingProperty); | ||
}); | ||
|
||
it('should immediately call next(), because incoming middleware args don\'t contain event', async () => { | ||
// Arrange | ||
const fakeNext = sinon.fake(); | ||
const fakeBotUserId = 'BUSER1'; | ||
const fakeArgs = { | ||
next: fakeNext, | ||
context: { botUserId: fakeBotUserId }, | ||
command: { | ||
command: '/fakeCommand', | ||
}, | ||
} as unknown as CommandMiddlewareArgs; | ||
|
||
const { ignoreSelf: getIgnoreSelfMiddleware } = await importBuiltin(); | ||
|
||
// Act | ||
const middleware = getIgnoreSelfMiddleware(); | ||
middleware(fakeArgs); | ||
await delay(); | ||
|
||
// Assert | ||
assert(fakeNext.calledOnce); | ||
}); | ||
|
||
it('should look for an event identified as a bot message from the same bot ID as this app and skip it', async () => { | ||
// Arrange | ||
const fakeNext = sinon.fake(); | ||
const fakeBotUserId = 'BUSER1'; | ||
// TODO: Fix typings here | ||
const fakeArgs = { | ||
next: fakeNext, | ||
context: { botUserId: fakeBotUserId, botId: fakeBotUserId }, | ||
event: { | ||
type: 'message', | ||
subtype: 'bot_message', | ||
bot_id: fakeBotUserId, | ||
}, | ||
message: { | ||
type: 'message', | ||
subtype: 'bot_message', | ||
bot_id: fakeBotUserId, | ||
}, | ||
} as any; | ||
|
||
const { ignoreSelf: getIgnoreSelfMiddleware } = await importBuiltin(); | ||
|
||
// Act | ||
const middleware = getIgnoreSelfMiddleware(); | ||
middleware(fakeArgs); | ||
await delay(); | ||
|
||
// 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(); | ||
const fakeBotUserId = 'BUSER1'; | ||
const fakeArgs = { | ||
next: fakeNext, | ||
context: { botUserId: fakeBotUserId, botId: fakeBotUserId }, | ||
event: { | ||
type: 'tokens_revoked', | ||
user: fakeBotUserId, | ||
}, | ||
} as unknown as TokensRevokedMiddlewareArgs; | ||
|
||
const { ignoreSelf: getIgnoreSelfMiddleware } = await importBuiltin(); | ||
|
||
// Act | ||
const middleware = getIgnoreSelfMiddleware(); | ||
middleware(fakeArgs); | ||
await delay(); | ||
|
||
// Assert | ||
assert(fakeNext.notCalled); | ||
}); | ||
|
||
it('should filter an event out, because it matches our own app and shouldn\'t be retained', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this the same test as above? |
||
// Arrange | ||
const fakeNext = sinon.fake(); | ||
const fakeBotUserId = 'BUSER1'; | ||
const fakeArgs = { | ||
next: fakeNext, | ||
context: { botUserId: fakeBotUserId, botId: fakeBotUserId }, | ||
event: { | ||
type: 'tokens_revoked', | ||
user: fakeBotUserId, | ||
}, | ||
} as unknown as TokensRevokedMiddlewareArgs; | ||
|
||
const { ignoreSelf: getIgnoreSelfMiddleware } = await importBuiltin(); | ||
|
||
// Act | ||
const middleware = getIgnoreSelfMiddleware(); | ||
middleware(fakeArgs); | ||
await delay(); | ||
|
||
// Assert | ||
assert(fakeNext.notCalled); | ||
}); | ||
|
||
it('shouldn\'t filter an event out, because it should be retained', async () => { | ||
// Arrange | ||
const fakeNext = sinon.fake(); | ||
const fakeBotUserId = 'BUSER1'; | ||
const eventsWhichShouldNotBeFilteredOut = ['member_joined_channel', 'member_left_channel']; | ||
|
||
const listOfFakeArgs = eventsWhichShouldNotBeFilteredOut.map((eventType) => { | ||
return { | ||
next: fakeNext, | ||
context: { botUserId: fakeBotUserId, botId: fakeBotUserId }, | ||
event: { | ||
type: eventType, | ||
user: fakeBotUserId, | ||
}, | ||
} as unknown as MemberJoinedOrLeftChannelMiddlewareArgs; | ||
}); | ||
|
||
const { ignoreSelf: getIgnoreSelfMiddleware } = await importBuiltin(); | ||
|
||
// Act | ||
const middleware = getIgnoreSelfMiddleware(); | ||
listOfFakeArgs.forEach(middleware); | ||
|
||
await delay(); | ||
|
||
// Assert | ||
assert.equal(fakeNext.callCount, listOfFakeArgs.length); | ||
}); | ||
}); | ||
|
||
/* Testing Harness */ | ||
|
||
interface DummyContext { | ||
matches?: RegExpExecArray; | ||
} | ||
|
||
type MessageMiddlewareArgs = SlackEventMiddlewareArgs<'message'> & { next: NextMiddleware, context: Context }; | ||
type TokensRevokedMiddlewareArgs = SlackEventMiddlewareArgs<'tokens_revoked'> & { next: NextMiddleware, context: Context }; | ||
|
||
type MemberJoinedOrLeftChannelMiddlewareArgs = SlackEventMiddlewareArgs<'member_joined_channel' | 'member_left_channel'> | ||
& { next: NextMiddleware, context: Context }; | ||
|
||
type CommandMiddlewareArgs = SlackCommandMiddlewareArgs & { next: NextMiddleware; context: Context }; | ||
|
||
async function importBuiltin( | ||
overrides: Override = {}, | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, I wasn't able to provide the correct type here :(
If someone could help, it would be great!