Skip to content

Commit

Permalink
Fix #935 enterprise_id in InstallationQuery can be invalid for Slack …
Browse files Browse the repository at this point in the history
…Connect channel events (#949)
  • Loading branch information
seratch authored May 31, 2021
1 parent 0bb7ae9 commit f009d9c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
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

0 comments on commit f009d9c

Please sign in to comment.