Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Mar 29, 2022
1 parent 953e77d commit 41b4902
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/App-basic-features.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,22 @@ describe('App basic features', () => {
}
});

describe('with developerMode', () => {
it('should accept developerMode: true', async () => {
// Arrange
const overrides = mergeOverrides(
withNoopAppMetadata(),
withSuccessfulBotUserFetchingWebClient('B_FAKE_BOT_ID', 'U_FAKE_BOT_USER_ID'),
);
const MockApp = await importApp(overrides);
// Act
const app = new MockApp({ token: '', appToken: '', developerMode: true });
// Assert
assert.equal((app as any).logLevel, LogLevel.DEBUG);
assert.equal((app as any).socketMode, true);
});
});

// TODO: tests for ignoreSelf option
// TODO: tests for logger and logLevel option
// TODO: tests for providing botId and botUserId options
Expand Down
166 changes: 164 additions & 2 deletions src/receivers/AwsLambdaReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,27 @@ describe('AwsLambdaReceiver', function () {
it('should instantiate with default logger', async (): Promise<void> => {
const awsReceiver = new AwsLambdaReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
});
assert.isNotNull(awsReceiver);
});

it('should have start method', async (): Promise<void> => {
const awsReceiver = new AwsLambdaReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
});
const handler: AwsHandler = await awsReceiver.start();
assert.isNotNull(handler);
const startedHandler: AwsHandler = await awsReceiver.start();
assert.isNotNull(startedHandler);
});

it('should have stop method', async (): Promise<void> => {
const awsReceiver = new AwsLambdaReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
});
await awsReceiver.start();
await awsReceiver.stop();
});

it('should accept events', async (): Promise<void> => {
Expand Down Expand Up @@ -380,6 +391,157 @@ describe('AwsLambdaReceiver', function () {
);
assert.equal(response1.statusCode, 404);
});

it('should accept ssl_check requests', async (): Promise<void> => {
const awsReceiver = new AwsLambdaReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
});
const handler = awsReceiver.toHandler();
const body = 'ssl_check=1&token=legacy-fixed-token';
const awsEvent = {
resource: '/slack/events',
path: '/slack/events',
httpMethod: 'POST',
headers: {
Accept: 'application/json,*/*',
'Content-Type': 'application/x-www-form-urlencoded',
Host: 'xxx.execute-api.ap-northeast-1.amazonaws.com',
'User-Agent': 'Slackbot 1.0 (+https://api.slack.com/robots)',
},
multiValueHeaders: {},
queryStringParameters: null,
multiValueQueryStringParameters: null,
pathParameters: null,
stageVariables: null,
requestContext: {},
body,
isBase64Encoded: false,
};
const response = await handler(
awsEvent,
{},
(_error, _result) => {},
);
assert.equal(response.statusCode, 200);
});

const urlVerificationBody = JSON.stringify({
token: 'Jhj5dZrVaK7ZwHHjRyZWjbDl',
challenge: '3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P',
type: 'url_verification',
});

it('should accept url_verification requests', async (): Promise<void> => {
const timestamp = Math.floor(Date.now() / 1000);
const awsReceiver = new AwsLambdaReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
});
const handler = awsReceiver.toHandler();
const signature = crypto.createHmac('sha256', 'my-secret').update(`v0:${timestamp}:${urlVerificationBody}`).digest('hex');
const awsEvent = {
resource: '/slack/events',
path: '/slack/events',
httpMethod: 'POST',
headers: {
Accept: 'application/json,*/*',
'Content-Type': 'application/json',
Host: 'xxx.execute-api.ap-northeast-1.amazonaws.com',
'User-Agent': 'Slackbot 1.0 (+https://api.slack.com/robots)',
'X-Slack-Request-Timestamp': `${timestamp}`,
'X-Slack-Signature': `v0=${signature}`,
},
multiValueHeaders: {},
queryStringParameters: null,
multiValueQueryStringParameters: null,
pathParameters: null,
stageVariables: null,
requestContext: {},
body: urlVerificationBody,
isBase64Encoded: false,
};
const response = await handler(
awsEvent,
{},
(_error, _result) => {},
);
assert.equal(response.statusCode, 200);
});

it('should detect invalid signature', async (): Promise<void> => {
const awsReceiver = new AwsLambdaReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
});
const handler = awsReceiver.toHandler();
const timestamp = Math.floor(Date.now() / 1000);
const signature = crypto.createHmac('sha256', 'my-secret').update(`v0:${timestamp}:${urlVerificationBody}`).digest('hex');
const awsEvent = {
resource: '/slack/events',
path: '/slack/events',
httpMethod: 'POST',
headers: {
Accept: 'application/json,*/*',
'Content-Type': 'application/json',
Host: 'xxx.execute-api.ap-northeast-1.amazonaws.com',
'User-Agent': 'Slackbot 1.0 (+https://api.slack.com/robots)',
'X-Slack-Request-Timestamp': `${timestamp}`,
'X-Slack-Signature': `v0=${signature}XXXXXXXX`, // invalid signature
},
multiValueHeaders: {},
queryStringParameters: null,
multiValueQueryStringParameters: null,
pathParameters: null,
stageVariables: null,
requestContext: {},
body: urlVerificationBody,
isBase64Encoded: false,
};
const response = await handler(
awsEvent,
{},
(_error, _result) => {},
);
assert.equal(response.statusCode, 401);
});

it('should detect too old request timestamp', async (): Promise<void> => {
const awsReceiver = new AwsLambdaReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
});
const handler = awsReceiver.toHandler();
const timestamp = Math.floor(Date.now() / 1000) - 600; // 10 minutes ago
const signature = crypto.createHmac('sha256', 'my-secret').update(`v0:${timestamp}:${urlVerificationBody}`).digest('hex');
const awsEvent = {
resource: '/slack/events',
path: '/slack/events',
httpMethod: 'POST',
headers: {
Accept: 'application/json,*/*',
'Content-Type': 'application/json',
Host: 'xxx.execute-api.ap-northeast-1.amazonaws.com',
'User-Agent': 'Slackbot 1.0 (+https://api.slack.com/robots)',
'X-Slack-Request-Timestamp': `${timestamp}`,
'X-Slack-Signature': `v0=${signature}`,
},
multiValueHeaders: {},
queryStringParameters: null,
multiValueQueryStringParameters: null,
pathParameters: null,
stageVariables: null,
requestContext: {},
body: urlVerificationBody,
isBase64Encoded: false,
};
const response = await handler(
awsEvent,
{},
(_error, _result) => {},
);
assert.equal(response.statusCode, 401);
});
});

// Composable overrides
Expand Down
22 changes: 22 additions & 0 deletions src/receivers/HTTPResponseAck.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'mocha';
import sinon from 'sinon';
import { assert } from 'chai';
import { IncomingMessage, ServerResponse } from 'http';
import { HTTPResponseAck } from './HTTPResponseAck';
import { createFakeLogger } from '../test-helpers';

describe('HTTPResponseAck', async () => {
it('should work', async () => {
const httpRequest = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const httpResponse: ServerResponse = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;
const ack = new HTTPResponseAck({
logger: createFakeLogger(),
processBeforeResponse: false,
httpRequest,
httpResponse,
});
assert.isDefined(ack);
assert.isDefined(ack.bind());
ack.ack(); // no exception
});
});

0 comments on commit 41b4902

Please sign in to comment.