Skip to content
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

Fix #935 enterprise_id in InstallationQuery can be invalid for Slack Connect channel events #949

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,62 @@ describe('App', () => {
return overrides;
}

describe('authorize', () => {
it('should extract valid enterprise_id in a shared channel #935', async () => {
// Arrange
const fakeAxiosPost = sinon.fake.resolves({});
const overrides = buildOverrides([withNoopWebClient(), withAxiosPost(fakeAxiosPost)]);
const App = await importApp(overrides); // eslint-disable-line @typescript-eslint/naming-convention, no-underscore-dangle, id-blacklist, id-match

// Act
let workedAsExpected = false;
const app = new App({
receiver: fakeReceiver,
authorize: async ({ enterpriseId }) => {
if (enterpriseId !== undefined) {
throw new Error('the enterprise_id must be undefined in this scenario');
}
return dummyAuthorizationResult;
},
});
app.event('message', async () => {
workedAsExpected = true;
});
await fakeReceiver.sendEvent({
ack: noop,
body: {
team_id: 'T_connected_grid_workspace',
enterprise_id: 'E_org_id',
api_app_id: 'A111',
event: {
type: 'message',
text: ':wave: Hi, this is my first message in a Slack Connect channel!',
user: 'U111',
ts: '1622099033.001500',
team: 'T_this_non_grid_workspace',
channel: 'C111',
channel_type: 'channel',
},
type: 'event_callback',
authorizations: [
{
enterprise_id: null,
team_id: 'T_this_non_grid_workspace',
user_id: 'U_authed_user',
is_bot: true,
is_enterprise_install: false,
},
],
is_ext_shared_channel: true,
event_context: '2-message-T_connected_grid_workspace-A111-C111',
},
});

// Assert
assert.isTrue(workedAsExpected);
});
});

describe('routing', () => {
function createReceiverEvents(): ReceiverEvent[] {
return [
Expand Down
10 changes: 4 additions & 6 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -991,12 +991,10 @@ function buildSource<IsEnterpriseInstall extends boolean>(
const enterpriseId: string | undefined = (() => {
if (type === IncomingEventType.Event) {
const bodyAsEvent = body as SlackEventMiddlewareArgs['body'];
if (
Array.isArray(bodyAsEvent.authorizations) &&
bodyAsEvent.authorizations[0] !== undefined &&
bodyAsEvent.authorizations[0].enterprise_id !== null
) {
return bodyAsEvent.authorizations[0].enterprise_id;
if (Array.isArray(bodyAsEvent.authorizations) && bodyAsEvent.authorizations[0] !== undefined) {
// The enteprise_id here can be null when the workspace is not in an Enterprise Grid
const theId = bodyAsEvent.authorizations[0].enterprise_id;
return theId !== null ? theId : undefined;
}
return bodyAsEvent.enterprise_id;
}
Expand Down