Skip to content

Commit

Permalink
specs updated!
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Maj committed Sep 26, 2024
1 parent 9f8d442 commit 820ef1f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 313 deletions.
5 changes: 1 addition & 4 deletions src/receivers/AwsLambdaReceiver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import crypto from 'crypto';
/* eslint-disable @typescript-eslint/no-explicit-any */
import querystring from 'querystring';
import { ConsoleLogger, LogLevel, type Logger } from '@slack/logger';
import tsscmp from 'tsscmp';
Expand Down Expand Up @@ -208,7 +207,7 @@ export default class AwsLambdaReceiver implements Receiver {
if (!isAcknowledged) {
this.logger.error(
'An incoming event was not acknowledged within 3 seconds. ' +
'Ensure that the ack() argument is called in a listener.',
'Ensure that the ack() argument is called in a listener.',
);
}
}, 3001);
Expand Down Expand Up @@ -257,7 +256,6 @@ export default class AwsLambdaReceiver implements Receiver {
};
}

// eslint-disable-next-line class-methods-use-this
private getRawBody(awsEvent: AwsEvent): string {
if (typeof awsEvent.body === 'undefined' || awsEvent.body == null) {
return '';
Expand All @@ -268,7 +266,6 @@ export default class AwsLambdaReceiver implements Receiver {
return awsEvent.body;
}

// eslint-disable-next-line class-methods-use-this
private parseRequestBody(stringBody: string, contentType: string | undefined, logger: Logger): any {
if (contentType === 'application/x-www-form-urlencoded') {
const parsedBody = querystring.parse(stringBody);
Expand Down
9 changes: 7 additions & 2 deletions test/unit/.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"require": ["ts-node/register", "source-map-support/register"],
"spec": ["test/unit/**/*.spec.ts"],
"require": [
"ts-node/register",
"source-map-support/register"
],
"spec": [
"test/unit/**/*.spec.ts"
],
"timeout": 3000
}
33 changes: 33 additions & 0 deletions test/unit/helpers/receivers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventEmitter } from 'node:events';
import crypto from 'node:crypto';
import sinon, { type SinonSpy } from 'sinon';
import type App from '../../../src/App';
import type { Receiver, ReceiverEvent } from '../../../src/types';
Expand Down Expand Up @@ -66,3 +67,35 @@ export function withHttpsCreateServer(spy: SinonSpy): Override {
},
};
}

export function createDummyAWSPayload(
// biome-ignore lint/suspicious/noExplicitAny: HTTP request bodies can be anything
body: any,
timestamp: number = Math.floor(Date.now() / 1000),
headers?: Record<string, string>,
isBase64Encoded = false,
) {
const signature = crypto.createHmac('sha256', 'my-secret').update(`v0:${timestamp}:${body}`).digest('hex');
const realBody = isBase64Encoded ? Buffer.from(body).toString('base64') : body;
return {
resource: '/slack/events',
path: '/slack/events',
httpMethod: 'POST',
headers: 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: realBody,
isBase64Encoded,
};
}
Loading

0 comments on commit 820ef1f

Please sign in to comment.