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 e5d6afb commit 148046a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 312 deletions.
3 changes: 0 additions & 3 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 @@ -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 148046a

Please sign in to comment.