Skip to content

Commit

Permalink
test(dynamodb): added tests for dynamo db adapter and improved other …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
H4ad committed Feb 17, 2022
1 parent 49c8633 commit 1bffb81
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 76 deletions.
15 changes: 12 additions & 3 deletions src/v2/adapters/aws/dynamodb.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class DynamoDBAdapter
public canHandle(event: unknown): event is DynamoDBStreamEvent {
const dynamoDBevent = event as Partial<DynamoDBStreamEvent>;

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

const eventSource = dynamoDBevent.Records[0]?.eventSource;

Expand All @@ -89,8 +89,17 @@ export class DynamoDBAdapter
this.options?.dynamoDBForwardMethod,
'POST'
);
const headers = { host: 'dynamodb.amazonaws.com' };
const [body] = getEventBodyAsBuffer(JSON.stringify(event), false);

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

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

return {
method,
Expand Down
32 changes: 6 additions & 26 deletions test/adapters/aws/alb.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
getPathWithQueryStringParams,
ILogger,
} from '../../../src/v2/core';
import { createCanHandleTestsForAdapter } from '../utils/can-handle';
import {
createAlbEvent,
createAlbEventWithMultiValueHeaders,
} from './utils/alb-event';
import { allAWSEvents } from './utils/events';

describe(AlbAdapter.name, () => {
let adapter!: AlbAdapter;
Expand All @@ -27,27 +27,7 @@ describe(AlbAdapter.name, () => {
});
});

describe('canHandle', () => {
it('should return true when is valid alb event', () => {
const events = allAWSEvents.filter(
([adapterName]) => adapterName === adapter.getAdapterName()
)!;

for (const [, albEvent] of events) {
expect(adapter.canHandle(albEvent)).toBe(true);
}
});

it('should return false when is not a valid alb event', () => {
const events = allAWSEvents.filter(
([adapterName]) => adapterName !== adapter.getAdapterName()
);

for (const [, event] of events) {
expect(adapter.canHandle(event)).toBe(false);
}
});
});
createCanHandleTestsForAdapter(() => new AlbAdapter(), undefined);

describe('getRequest', () => {
it('should return the correct mapping for the request', () => {
Expand Down Expand Up @@ -85,7 +65,7 @@ describe(AlbAdapter.name, () => {

const resultPath = getPathWithQueryStringParams(
path,
event.queryStringParameters!
event.queryStringParameters
);
expect(result).toHaveProperty('path', resultPath);
});
Expand Down Expand Up @@ -125,7 +105,7 @@ describe(AlbAdapter.name, () => {

const resultPath = getPathWithQueryStringParams(
path,
event.multiValueQueryStringParameters!
event.multiValueQueryStringParameters
);
expect(result).toHaveProperty('path', resultPath);
});
Expand Down Expand Up @@ -159,7 +139,7 @@ describe(AlbAdapter.name, () => {

const resultPath = getPathWithQueryStringParams(
path,
event.queryStringParameters!
event.queryStringParameters
);
expect(result).toHaveProperty('path', resultPath);
});
Expand Down Expand Up @@ -203,7 +183,7 @@ describe(AlbAdapter.name, () => {

const resultPath = getPathWithQueryStringParams(
path.replace(stripBasePath, ''),
event.queryStringParameters!
event.queryStringParameters
);
expect(result).toHaveProperty('path', resultPath);
});
Expand Down
30 changes: 5 additions & 25 deletions test/adapters/aws/api-gateway-v1.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
ILogger,
} from '../../../src/v2/core';
import { ServerlessResponse } from '../../../src/v2/network';
import { createCanHandleTestsForAdapter } from '../utils/can-handle';
import { createApiGatewayV1 } from './utils/api-gateway-v1';
import { allAWSEvents } from './utils/events';

describe(ApiGatewayV1Adapter.name, () => {
let adapter!: ApiGatewayV1Adapter;
Expand All @@ -26,27 +26,7 @@ describe(ApiGatewayV1Adapter.name, () => {
});
});

describe('canHandle', () => {
it('should return true when is valid event', () => {
const events = allAWSEvents.filter(
([adapterName]) => adapterName === adapter.getAdapterName()
)!;

for (const [, event] of events) {
expect(adapter.canHandle(event)).toBe(true);
}
});

it('should return false when is not a valid event', () => {
const events = allAWSEvents.filter(
([adapterName]) => adapterName !== adapter.getAdapterName()
);

for (const [, event] of events) {
expect(adapter.canHandle(event)).toBe(false);
}
});
});
createCanHandleTestsForAdapter(() => new ApiGatewayV1Adapter(), undefined);

describe('getRequest', () => {
it('should return the correct mapping for the request', () => {
Expand Down Expand Up @@ -77,7 +57,7 @@ describe(ApiGatewayV1Adapter.name, () => {

const resultPath = getPathWithQueryStringParams(
path,
event.queryStringParameters!
event.queryStringParameters
);
expect(result).toHaveProperty('path', resultPath);
});
Expand All @@ -103,7 +83,7 @@ describe(ApiGatewayV1Adapter.name, () => {

const resultPath = getPathWithQueryStringParams(
path,
event.queryStringParameters!
event.queryStringParameters
);
expect(result).toHaveProperty('path', resultPath);
});
Expand Down Expand Up @@ -133,7 +113,7 @@ describe(ApiGatewayV1Adapter.name, () => {

const resultPath = getPathWithQueryStringParams(
path.replace('/prod', ''),
event.queryStringParameters!
event.queryStringParameters
);
expect(result).toHaveProperty('path', resultPath);
});
Expand Down
24 changes: 2 additions & 22 deletions test/adapters/aws/api-gateway-v2.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
ILogger,
} from '../../../src/v2/core';
import { ServerlessResponse } from '../../../src/v2/network';
import { createCanHandleTestsForAdapter } from '../utils/can-handle';
import { createApiGatewayV2 } from './utils/api-gateway-v2';
import { allAWSEvents } from './utils/events';

describe(ApiGatewayV2Adapter.name, () => {
let adapter!: ApiGatewayV2Adapter;
Expand All @@ -26,27 +26,7 @@ describe(ApiGatewayV2Adapter.name, () => {
});
});

describe('canHandle', () => {
it('should return true when is valid event', () => {
const events = allAWSEvents.filter(
([adapterName]) => adapterName === adapter.getAdapterName()
)!;

for (const [, event] of events) {
expect(adapter.canHandle(event)).toBe(true);
}
});

it('should return false when is not a valid event', () => {
const events = allAWSEvents.filter(
([adapterName]) => adapterName !== adapter.getAdapterName()
);

for (const [, event] of events) {
expect(adapter.canHandle(event)).toBe(false);
}
});
});
createCanHandleTestsForAdapter(() => new ApiGatewayV2Adapter(), undefined);

describe('getRequest', () => {
it('should return the correct mapping for the request', () => {
Expand Down
129 changes: 129 additions & 0 deletions test/adapters/aws/dynamodb.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { DynamoDBStreamEvent } from 'aws-lambda';
import { DynamoDBAdapter } 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 { createDynamoDBEvent } from './utils/dynamodb';

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

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

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

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

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

const result = adapter.getRequest(event);

expect(result.method).toBe('POST');
expect(result.path).toBe('/dynamo');
expect(result.headers).toHaveProperty('host', 'dynamodb.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 = createDynamoDBEvent();

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

const customAdapter = new DynamoDBAdapter({
dynamoDBForwardMethod: method,
dynamoDBForwardPath: path,
});

const result = customAdapter.getRequest(event);

expect(result.method).toBe(method);
expect(result.path).toBe(path);
expect(result.headers).toHaveProperty('host', 'dynamodb.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 call succeed', () => {
const event = createDynamoDBEvent();

const error = new Error('fail because I need to test.');
const resolver: Resolver<DynamoDBStreamEvent> = {
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);
});
});
});
Loading

0 comments on commit 1bffb81

Please sign in to comment.