Skip to content

Commit

Permalink
Split HTTPReceiver request tests into own describe block, add a test …
Browse files Browse the repository at this point in the history
…for invoking the installRedirectHandler
  • Loading branch information
Filip Maj authored and filmaj committed Aug 27, 2021
1 parent 90081bd commit fef4dc2
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/receivers/HTTPReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ describe('HTTPReceiver', function () {
});
assert.isNotNull(receiver);
});
});
describe('request handling', function () {
it('should invoke installer generateInstallUrl if a request comes into the install path', async function () {
// Arrange
const installProviderStub = sinon.createStubInstance(InstallProvider);
Expand Down Expand Up @@ -113,7 +115,7 @@ describe('HTTPReceiver', function () {
assert(installProviderStub.generateInstallUrl.calledWith(sinon.match({ metadata, scopes, userScopes })));
assert.isTrue(writeHead.calledWith(200));
});
it('should rediect installers if directInstallEnabled is true', async function () {
it('should redirect installers if directInstallEnabled is true', async function () {
// Arrange
const installProviderStub = sinon.createStubInstance(InstallProvider);
const overrides = mergeOverrides(
Expand Down Expand Up @@ -156,7 +158,52 @@ describe('HTTPReceiver', function () {
assert(installProviderStub.generateInstallUrl.calledWith(sinon.match({ metadata, scopes, userScopes })));
assert.isTrue(writeHead.calledWith(302, sinon.match.object));
});
it('should return a 404 if a request comes into neither the install path nor the redirect URI path', async function () {
it('should invoke installer handler if a request comes into the redirect URI path', async function () {
// Arrange
const installProviderStub = sinon.createStubInstance(InstallProvider, {
handleCallback: sinon.stub().resolves() as unknown as Promise<void>,
});
const overrides = mergeOverrides(
withHttpCreateServer(this.fakeCreateServer),
withHttpsCreateServer(sinon.fake.throws('Should not be used.')),
);
const HTTPReceiver = await importHTTPReceiver(overrides);

const metadata = 'this is bat country';
const scopes = ['channels:read'];
const userScopes = ['chat:write'];
const callbackOptions = {};
const receiver = new HTTPReceiver({
logger: noopLogger,
clientId: 'my-clientId',
clientSecret: 'my-client-secret',
signingSecret: 'secret',
stateSecret: 'state-secret',
scopes,
installerOptions: {
authVersion: 'v2',
redirectUriPath: '/heyo',
callbackOptions,
metadata,
userScopes,
},
});
assert.isNotNull(receiver);
receiver.installer = installProviderStub as unknown as InstallProvider;
const fakeReq: IncomingMessage = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
fakeReq.url = '/heyo';
fakeReq.headers = { host: 'localhost' };
fakeReq.method = 'GET';
const fakeRes: ServerResponse & {} = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;
const writeHead = sinon.fake();
const end = sinon.fake();
fakeRes.writeHead = writeHead;
fakeRes.end = end;
/* eslint-disable-next-line @typescript-eslint/await-thenable */
await receiver.requestListener(fakeReq, fakeRes);
assert(installProviderStub.handleCallback.calledWith(fakeReq, fakeRes, callbackOptions));
});
it('should throw if a request comes into neither the install path nor the redirect URI path', async function () {
// Arrange
const installProviderStub = sinon.createStubInstance(InstallProvider);
const overrides = mergeOverrides(
Expand Down

0 comments on commit fef4dc2

Please sign in to comment.