Skip to content

Commit

Permalink
test(sns): added tests to sns
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Feb 22, 2022
1 parent 003b075 commit 32e2a33
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,6 @@ dist

# Compiled code
lib/

# IDEs
.idea
2 changes: 2 additions & 0 deletions src/v2/adapters/aws/dynamodb.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface DynamoDBAdapterOptions {
/**
* The adapter to handle requests from AWS DynamoDB.
*
* The option of `responseWithErrors` is ignored by this adapter and we always call `resolver.fail` with the error.
*
* {@link https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html Event Reference}
*
* @example```typescript
Expand Down
2 changes: 2 additions & 0 deletions src/v2/adapters/aws/event-bridge.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export type EventBridgeEventAll = EventBridgeEvent<any, any>;
/**
* The adapter to handle requests from AWS EventBridge (Cloudwatch Events).
*
* The option of `responseWithErrors` is ignored by this adapter and we always call `resolver.fail` with the error.
*
* {@link https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html Event Reference}
*
* @example```typescript
Expand Down
19 changes: 15 additions & 4 deletions src/v2/adapters/aws/sns.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface SNSAdapterOptions {
/**
* The adapter to handle requests from AWS SNS.
*
* The option of `responseWithErrors` is ignored by this adapter and we always call `resolver.fail` with the error.
*
* {@link https://docs.aws.amazon.com/pt_br/lambda/latest/dg/with-sns.html Event Reference}
*
* @example```typescript
Expand Down Expand Up @@ -70,7 +72,7 @@ export class SNSAdapter
public canHandle(event: unknown): event is SNSEvent {
const snsEvent = event as Partial<SNSEvent>;

if (!Array.isArray(snsEvent.Records)) return false;
if (!Array.isArray(snsEvent?.Records)) return false;

const eventSource = snsEvent.Records[0]?.EventSource;

Expand All @@ -81,13 +83,22 @@ export class SNSAdapter
* @inheritDoc
*/
public getRequest(event: SNSEvent): AdapterRequest {
const path = getDefaultIfUndefined(this.options?.snsForwardPath, '/sqs');
const path = getDefaultIfUndefined(this.options?.snsForwardPath, '/sns');
const method = getDefaultIfUndefined(
this.options?.snsForwardMethod,
'POST'
);
const headers = { host: 'sns.amazonaws.com' };
const [body] = getEventBodyAsBuffer(JSON.stringify(event), false);

const [body, contentLength] = getEventBodyAsBuffer(
JSON.stringify(event),
false
);

const headers = {
host: 'sns.amazonaws.com',
'content-type': 'application/json',
'content-length': String(contentLength),
};

return {
method,
Expand Down
2 changes: 2 additions & 0 deletions src/v2/adapters/aws/sqs.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface SQSAdapterOptions {
/**
* The adapter to handle requests from AWS SQS.
*
* The option of `responseWithErrors` is ignored by this adapter and we always call `resolver.fail` with the error.
*
* {@link https://docs.aws.amazon.com/pt_br/lambda/latest/dg/with-sqs.html Event Reference}
*
* @example```typescript
Expand Down
129 changes: 129 additions & 0 deletions test/adapters/aws/sns.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { SNSEvent } from 'aws-lambda';
import { SNSAdapter } from '../../../src/v2/adapters/aws';
import { Resolver } from '../../../src/v2/contracts';
import {
EmptyResponse,
getEventBodyAsBuffer,
IEmptyResponse,
ILogger,
} from '../../../src/v2/core';
import { createCanHandleTestsForAdapter } from '../utils/can-handle';
import { createSNSEvent } from './utils/sns';

describe(SNSAdapter.name, () => {
let adapter!: SNSAdapter;

beforeEach(() => {
adapter = new SNSAdapter();
});

describe('getAdapterName', () => {
it('should be the same name of the class', () => {
expect(adapter.getAdapterName()).toBe(SNSAdapter.name);
});
});

createCanHandleTestsForAdapter(() => new SNSAdapter(), undefined);

describe('getRequest', () => {
it('should return the correct mapping for the request', () => {
const event = createSNSEvent();

const result = adapter.getRequest(event);

expect(result.method).toBe('POST');
expect(result.path).toBe('/sns');
expect(result.headers).toHaveProperty('host', 'sns.amazonaws.com');
expect(result.headers).toHaveProperty('content-type', 'application/json');

const [bodyBuffer, contentLength] = getEventBodyAsBuffer(
JSON.stringify(event),
false
);

expect(result.body).toBeInstanceOf(Buffer);
expect(result.body).toStrictEqual(bodyBuffer);

expect(result.headers).toHaveProperty(
'content-length',
String(contentLength)
);
});

it('should return the correct mapping for the request with custom path and method', () => {
const event = createSNSEvent();

const method = 'PUT';
const path = '/custom/sns';

const customAdapter = new SNSAdapter({
snsForwardMethod: method,
snsForwardPath: path,
});

const result = customAdapter.getRequest(event);

expect(result.method).toBe(method);
expect(result.path).toBe(path);
expect(result.headers).toHaveProperty('host', 'sns.amazonaws.com');
expect(result.headers).toHaveProperty('content-type', 'application/json');

const [bodyBuffer, contentLength] = getEventBodyAsBuffer(
JSON.stringify(event),
false
);

expect(result.body).toBeInstanceOf(Buffer);
expect(result.body).toStrictEqual(bodyBuffer);

expect(result.headers).toHaveProperty(
'content-length',
String(contentLength)
);
});
});

describe('getResponse', () => {
it('should return the correct mapping for the response', () => {
const result = adapter.getResponse();

expect(result).toBe(EmptyResponse);
});
});

describe('onErrorWhileForwarding', () => {
it('should resolver just call fail without get response', () => {
const event = createSNSEvent();

const error = new Error('fail because I need to test.');
const resolver: Resolver<SNSEvent> = {
fail: jest.fn(),
succeed: jest.fn(),
};

const oldGetResponse = adapter.getResponse.bind(adapter);

let getResponseResult: IEmptyResponse;

adapter.getResponse = jest.fn(() => {
getResponseResult = oldGetResponse();

return getResponseResult;
});

adapter.onErrorWhileForwarding({
event,
error,
resolver,
log: {} as ILogger,
respondWithErrors: false,
});

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(adapter.getResponse).toHaveBeenCalledTimes(0);

expect(resolver.fail).toHaveBeenCalledTimes(1);
expect(resolver.succeed).toHaveBeenCalledTimes(0);
});
});
});
3 changes: 3 additions & 0 deletions test/adapters/aws/utils/alb-event.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ALBEvent } from 'aws-lambda';

/**
* Sample event from {@link https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html}
*/
export function createAlbEvent(
httpMethod: string,
path: string,
Expand Down
3 changes: 3 additions & 0 deletions test/adapters/aws/utils/dynamodb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { DynamoDBStreamEvent } from 'aws-lambda';

/**
* Sample event from {@link https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html}
*/
export function createDynamoDBEvent(): DynamoDBStreamEvent {
return {
Records: [
Expand Down
6 changes: 6 additions & 0 deletions test/adapters/aws/utils/event-bridge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { EventBridgeEvent } from 'aws-lambda';

/**
* Sample event from {@link https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html}
*/
export function createEventBridgeEvent(): EventBridgeEvent<any, any> {
return {
version: '0',
Expand All @@ -21,6 +24,9 @@ export function createEventBridgeEvent(): EventBridgeEvent<any, any> {
};
}

/**
* Sample event from {@link https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html}
*/
export function createEventBridgeEventSimple(): EventBridgeEvent<any, any> {
return {
version: '0',
Expand Down
3 changes: 3 additions & 0 deletions test/adapters/aws/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ApiGatewayV2Adapter,
DynamoDBAdapter,
EventBridgeAdapter,
SNSAdapter,
SQSAdapter,
} from '../../../../src/v2/adapters/aws';
import {
Expand All @@ -18,6 +19,7 @@ import {
createEventBridgeEventSimple,
} from './event-bridge';
import { createSQSEvent } from './sqs';
import { createSNSEvent } from './sns';

export const allAWSEvents: Array<[string, any]> = [
['fake-to-test-undefined-event', undefined],
Expand Down Expand Up @@ -62,4 +64,5 @@ export const allAWSEvents: Array<[string, any]> = [
[EventBridgeAdapter.name, createEventBridgeEvent()],
[EventBridgeAdapter.name, createEventBridgeEventSimple()],
[SQSAdapter.name, createSQSEvent()],
[SNSAdapter.name, createSNSEvent()],
];
42 changes: 42 additions & 0 deletions test/adapters/aws/utils/sns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { SNSEvent } from 'aws-lambda';

/**
* Sample event from {@link https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html}
*/
export function createSNSEvent(): SNSEvent {
return {
Records: [
{
EventVersion: '1.0',
EventSubscriptionArn:
'arn:aws:sns:us-east-2:123456789012:sns-lambda:21be56ed-a058-49f5-8c98-aedd2564c486',
EventSource: 'aws:sns',
Sns: {
SignatureVersion: '1',
Timestamp: '2019-01-02T12:45:07.000Z',
Signature:
'tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==',
SigningCertUrl:
'https://sns.us-east-2.amazonaws.com/SimpleNotificationService-ac565b8b1a6c5d002d285f9598aa1d9b.pem',
MessageId: '95df01b4-ee98-5cb9-9903-4c221d41eb5e',
Message: 'Hello from SNS!',
MessageAttributes: {
Test: {
Type: 'String',
Value: 'TestString',
},
TestBinary: {
Type: 'Binary',
Value: 'TestBinary',
},
},
Type: 'Notification',
UnsubscribeUrl:
'https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe&amp;SubscriptionArn=arn:aws:sns:us-east-2:123456789012:test-lambda:21be56ed-a058-49f5-8c98-aedd2564c486',
TopicArn: 'arn:aws:sns:us-east-2:123456789012:sns-lambda',
Subject: 'TestInvoke',
},
},
],
};
}
3 changes: 3 additions & 0 deletions test/adapters/aws/utils/sqs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { SQSEvent } from 'aws-lambda';

/**
* Sample event from {@link https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html}
*/
export function createSQSEvent(): SQSEvent {
return {
Records: [
Expand Down

0 comments on commit 32e2a33

Please sign in to comment.