Skip to content
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
5 changes: 0 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ updates:
slack:
patterns:
- "@slack/*"
dev-sinon:
dependency-type: "development"
patterns:
- "@types/sinon"
- "sinon"
dev-chai:
dependency-type: "development"
patterns:
Expand Down
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions packages/oauth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
"jsonwebtoken": "^9"
},
"devDependencies": {
"@types/sinon": "^22.0.2",
"rewiremock": "^3",
"sinon": "^22.1.0"
"rewiremock": "^3"
}
}
6 changes: 3 additions & 3 deletions packages/oauth/src/callback-options.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'node:assert/strict';
import { IncomingMessage, ServerResponse } from 'node:http';
import { Socket } from 'node:net';
import { describe, it } from 'node:test';
import sinon from 'sinon';

import { type CallbackOptions, escapeHtml } from './callback-options';
import { MissingStateError } from './errors';
Expand Down Expand Up @@ -43,8 +43,8 @@ describe('CallbackOptions', async () => {
const options = {
scopes: ['commands', 'chat:write'],
};
const req = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const resp = sinon.createStubInstance(ServerResponse) as ServerResponse;
const req = new IncomingMessage(new Socket());
const resp = new ServerResponse(req);
callbackOptions.success?.(installation, options, req, resp);

const error = new MissingStateError();
Expand Down
6 changes: 3 additions & 3 deletions packages/oauth/src/install-path-options.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'node:assert/strict';
import { IncomingMessage, ServerResponse } from 'node:http';
import { Socket } from 'node:net';
import { describe, it } from 'node:test';
import sinon from 'sinon';

import type { InstallPathOptions } from './install-path-options';

Expand All @@ -16,8 +16,8 @@ describe('InstallPathOptions', async () => {
},
};
assert.ok(installPathOptions);
const req = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const resp = sinon.createStubInstance(ServerResponse) as ServerResponse;
const req = new IncomingMessage(new Socket());
const resp = new ServerResponse(req);
const options = { scopes: ['commands', 'chat:write'] };
const result = await installPathOptions.beforeRedirection?.(req, resp, options);
assert.strictEqual(result, false);
Expand Down
118 changes: 41 additions & 77 deletions packages/oauth/src/install-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import assert from 'node:assert/strict';
import { beforeEach, describe, it } from 'node:test';
import { beforeEach, describe, it, mock } from 'node:test';
import url from 'node:url';
import type { OAuthV2AccessArguments, OauthV2AccessResponse, WebClientOptions } from '@slack/web-api';
import rewiremock from 'rewiremock';
import sinon from 'sinon';
import {
type AuthorizationError,
ErrorCode,
Expand All @@ -20,10 +19,10 @@ rewiremock(() => require('@slack/web-api')).with({
// @ts-expect-error: missing some irrelevant class properties in this mock
WebClient: class {
auth = {
test: sinon.fake.resolves({ bot_id: '' }),
test: async () => ({ bot_id: '' }),
};
oauth = {
access: sinon.fake.resolves({
access: async () => ({
team_id: 'fake-v1-team-id',
team_name: 'fake-team-name',
access_token: 'token',
Expand Down Expand Up @@ -51,6 +50,13 @@ rewiremock.disable();

const webClientOptions: WebClientOptions = { timeout: 1000 };

function createFakeStateStore() {
return {
generateStateParam: mock.fn<StateStore['generateStateParam']>(async () => 'fakeState'),
verifyStateParam: mock.fn<StateStore['verifyStateParam']>(async () => ({ scopes: [] })),
};
}

async function mockedV2AccessResp(options: OAuthV2AccessArguments): Promise<OauthV2AccessResponse> {
const mockedResp: OauthV2AccessResponse = {
ok: true,
Expand Down Expand Up @@ -225,10 +231,7 @@ describe('InstallProvider', async () => {

it('should build a default installer given a clientID, client secret and state store', async () => {
// stateStore for testing
const fakeStateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({}),
};
const fakeStateStore = createFakeStateStore();

const installer = new InstallProvider({ clientId, clientSecret, stateStore: fakeStateStore, logger: noopLogger });
assert.ok(installer instanceof InstallProvider);
Expand Down Expand Up @@ -313,10 +316,7 @@ describe('InstallProvider', async () => {
clientId,
clientSecret,
installUrlOptions: undefined,
stateStore: {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({ scopes: [] }),
},
stateStore: createFakeStateStore(),
logger: noopLogger,
});
const req = {};
Expand Down Expand Up @@ -355,10 +355,7 @@ describe('InstallProvider', async () => {
clientSecret,
installUrlOptions,
directInstall: true,
stateStore: {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({ scopes: [] }),
},
stateStore: createFakeStateStore(),
logger: noopLogger,
});
const req = {};
Expand Down Expand Up @@ -391,10 +388,7 @@ describe('InstallProvider', async () => {
clientSecret,
installUrlOptions,
directInstall: true,
stateStore: {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({ scopes: [] }),
},
stateStore: createFakeStateStore(),
logger: noopLogger,
});
const req = {};
Expand Down Expand Up @@ -432,10 +426,7 @@ describe('InstallProvider', async () => {

describe('installer.generateInstallUrl', async () => {
it('should return a generated v2 url', async () => {
const fakeStateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({}),
};
const fakeStateStore = createFakeStateStore();
const installer = new InstallProvider({ clientId, clientSecret, stateStore: fakeStateStore, logger: noopLogger });
const scopes = ['channels:read'];
const teamId = '1234Team';
Expand All @@ -452,9 +443,9 @@ describe('InstallProvider', async () => {
try {
const generatedUrl = await installer.generateInstallUrl(installUrlOptions, stateVerification);
assert.ok(generatedUrl);
assert.strictEqual(fakeStateStore.generateStateParam.callCount, 1);
assert.strictEqual(fakeStateStore.verifyStateParam.callCount, 0);
assert.strictEqual(fakeStateStore.generateStateParam.calledWith(installUrlOptions), true);
assert.strictEqual(fakeStateStore.generateStateParam.mock.callCount(), 1);
assert.strictEqual(fakeStateStore.verifyStateParam.mock.callCount(), 0);
assert.deepEqual(fakeStateStore.generateStateParam.mock.calls[0].arguments[0], installUrlOptions);

const parsedUrl = url.parse(generatedUrl, true);
assert.strictEqual(parsedUrl.query.state, 'fakeState');
Expand All @@ -468,10 +459,7 @@ describe('InstallProvider', async () => {
}
});
it('should not call generate state param when state validation is false', async () => {
const fakeStateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({}),
};
const fakeStateStore = createFakeStateStore();
const authorizationUrl = 'https://dev.slack.com/oauth/v2/authorize';
const installer = new InstallProvider({
clientId,
Expand All @@ -495,17 +483,14 @@ describe('InstallProvider', async () => {
try {
const generatedUrl = await installer.generateInstallUrl(installUrlOptions, stateVerification);
assert.ok(generatedUrl);
assert.strictEqual(fakeStateStore.generateStateParam.callCount, 0);
assert.strictEqual(fakeStateStore.verifyStateParam.callCount, 0);
assert.strictEqual(fakeStateStore.generateStateParam.mock.callCount(), 0);
assert.strictEqual(fakeStateStore.verifyStateParam.mock.callCount(), 0);
} catch (error) {
assert.fail((error as Error).message);
}
});
it('should return a generated url when passed a custom authorizationUrl', async () => {
const fakeStateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({}),
};
const fakeStateStore = createFakeStateStore();
const authorizationUrl = 'https://dev.slack.com/oauth/v2/authorize';
const installer = new InstallProvider({
clientId,
Expand All @@ -528,9 +513,9 @@ describe('InstallProvider', async () => {
try {
const generatedUrl = await installer.generateInstallUrl(installUrlOptions);
assert.ok(generatedUrl);
assert.strictEqual(fakeStateStore.generateStateParam.callCount, 1);
assert.strictEqual(fakeStateStore.verifyStateParam.callCount, 0);
assert.strictEqual(fakeStateStore.generateStateParam.calledWith(installUrlOptions), true);
assert.strictEqual(fakeStateStore.generateStateParam.mock.callCount(), 1);
assert.strictEqual(fakeStateStore.verifyStateParam.mock.callCount(), 0);
assert.deepEqual(fakeStateStore.generateStateParam.mock.calls[0].arguments[0], installUrlOptions);

const parsedUrl = url.parse(generatedUrl, true);
assert.strictEqual(parsedUrl.query.state, 'fakeState');
Expand All @@ -546,10 +531,7 @@ describe('InstallProvider', async () => {
});

it('should return a generated v1 url', async () => {
const fakeStateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({}),
};
const fakeStateStore = createFakeStateStore();
const installer = new InstallProvider({
clientId,
clientSecret,
Expand All @@ -571,9 +553,9 @@ describe('InstallProvider', async () => {
const generatedUrl = await installer.generateInstallUrl(installUrlOptions, stateVerification);
assert.ok(generatedUrl);
const parsedUrl = url.parse(generatedUrl, true);
assert.strictEqual(fakeStateStore.generateStateParam.callCount, 1);
assert.strictEqual(fakeStateStore.verifyStateParam.callCount, 0);
assert.strictEqual(fakeStateStore.generateStateParam.calledWith(installUrlOptions), true);
assert.strictEqual(fakeStateStore.generateStateParam.mock.callCount(), 1);
assert.strictEqual(fakeStateStore.verifyStateParam.mock.callCount(), 0);
assert.deepEqual(fakeStateStore.generateStateParam.mock.calls[0].arguments[0], installUrlOptions);
assert.strictEqual(parsedUrl.pathname, '/oauth/authorize');
assert.strictEqual(parsedUrl.query.state, 'fakeState');
assert.strictEqual(scopes.join(','), parsedUrl.query.scope);
Expand Down Expand Up @@ -655,7 +637,7 @@ describe('InstallProvider', async () => {
});

describe('installer.handleCallback', async () => {
let fakeStateStore: StateStore;
let fakeStateStore: ReturnType<typeof createFakeStateStore>;
let sent = false;
const res = {
end: () => {
Expand All @@ -664,10 +646,7 @@ describe('InstallProvider', async () => {
setHeader: () => {},
};
beforeEach(() => {
fakeStateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({ scopes: [] }),
};
fakeStateStore = createFakeStateStore();
sent = false;
});

Expand Down Expand Up @@ -791,7 +770,7 @@ describe('InstallProvider', async () => {
// @ts-expect-error req is not an instance of IncomingMessage, TODO: type or stub IncomingMessage for req properly
await installer.handleCallback(req, res, successExpectedCallbackOptions);
assert.strictEqual(sent, true);
assert.strictEqual((fakeStateStore.verifyStateParam as sinon.SinonSpy).callCount, 1);
assert.strictEqual(fakeStateStore.verifyStateParam.mock.callCount(), 1);
});

it('should call the success callback for a v2 url', async () => {
Expand All @@ -815,7 +794,7 @@ describe('InstallProvider', async () => {
// @ts-expect-error req is not an instance of IncomingMessage, TODO: type or stub IncomingMessage for req properly
await installer.handleCallback(req, res, successExpectedCallbackOptions);
assert.strictEqual(sent, true);
assert.strictEqual((fakeStateStore.verifyStateParam as sinon.SinonSpy).callCount, 1);
assert.strictEqual(fakeStateStore.verifyStateParam.mock.callCount(), 1);
});

it('should call the success callback for a v1 url', async () => {
Expand All @@ -840,7 +819,7 @@ describe('InstallProvider', async () => {
// @ts-expect-error req is not an instance of IncomingMessage, TODO: type or stub IncomingMessage for req properly
await installer.handleCallback(req, res, successExpectedCallbackOptions);
assert.strictEqual(sent, true);
assert.strictEqual((fakeStateStore.verifyStateParam as sinon.SinonSpy).callCount, 1);
assert.strictEqual(fakeStateStore.verifyStateParam.mock.callCount(), 1);
});

it('should call the success callback for a v1 url', async () => {
Expand All @@ -866,14 +845,11 @@ describe('InstallProvider', async () => {
// @ts-expect-error req is not an instance of IncomingMessage, TODO: type or stub IncomingMessage for req properly
await installer.handleCallback(req, res, successExpectedCallbackOptions);
assert.strictEqual(sent, true);
assert.strictEqual((fakeStateStore.verifyStateParam as sinon.SinonSpy).callCount, 1);
assert.strictEqual(fakeStateStore.verifyStateParam.mock.callCount(), 1);
});

it('should not verify state when stateVerification is false', async () => {
const fakeStateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({}),
};
const fakeStateStore = createFakeStateStore();
const installer = new InstallProvider({
clientId,
clientSecret,
Expand All @@ -895,7 +871,7 @@ describe('InstallProvider', async () => {
// @ts-expect-error req is not an instance of IncomingMessage, TODO: type or stub IncomingMessage for req properly
await installer.handleCallback(req, res, successExpectedCallbackOptions);
assert.strictEqual(sent, true);
assert.strictEqual(fakeStateStore.verifyStateParam.callCount, 0);
assert.strictEqual(fakeStateStore.verifyStateParam.mock.callCount(), 0);
});

it('should terminate the processing when callbackOptions.beforeInstallation() returns false', async () => {
Expand Down Expand Up @@ -1079,10 +1055,7 @@ describe('InstallProvider', async () => {
});

it('should fail if the state value is not in cookies', async () => {
const fakeStateStore: StateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({ scopes: [] }),
};
const fakeStateStore = createFakeStateStore();
const installer = new InstallProvider({
clientId,
clientSecret,
Expand All @@ -1107,10 +1080,7 @@ describe('InstallProvider', async () => {
assert.strictEqual(sent, true);
});
it('should fail if there is a different state value in cookies', async () => {
const fakeStateStore: StateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({ scopes: [] }),
};
const fakeStateStore = createFakeStateStore();
const installer = new InstallProvider({
clientId,
clientSecret,
Expand All @@ -1135,10 +1105,7 @@ describe('InstallProvider', async () => {
assert.strictEqual(sent, true);
});
it('should not fail if no state cookie returned and legacyStateVerification is enabled', async () => {
const fakeStateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({}),
};
const fakeStateStore = createFakeStateStore();
const installer = new InstallProvider({
clientId,
clientSecret,
Expand All @@ -1163,10 +1130,7 @@ describe('InstallProvider', async () => {
assert.strictEqual(sent, true);
});
it('should not fail if a different state cookie returned and legacyStateVerification is enabled', async () => {
const fakeStateStore: StateStore = {
generateStateParam: sinon.fake.resolves('fakeState'),
verifyStateParam: sinon.fake.resolves({ scopes: [] }),
};
const fakeStateStore = createFakeStateStore();
const installer = new InstallProvider({
clientId,
clientSecret,
Expand Down
Loading
Loading