Skip to content

Commit

Permalink
Adjusted unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
julienblin committed Jun 16, 2018
1 parent 32f9bda commit 48c62a4
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 112 deletions.
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./builder-aws";
export * from "./builder";
export * from "./container";
export * from "./errors";
Expand Down
8 changes: 5 additions & 3 deletions src/middlewares/logging.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { FunctionArg, FunctionExecution, Middleware } from "../core/builder";
import { defaultConfidentialityReplacer, safeJSONStringify } from "../core/utils";

export const contextErrorLog = (arg: FunctionArg<any, any>, message?: any) => {
arg.context.log(message);
export const contextErrorLog = (arg: FunctionArg<any, any>, message?: string) => {
if (message) {
arg.context.log(message);
}
};

/**
* This middleware logs errors & event/context before re-throwing them as-is.
*/
export const errorLogging = (errorFunc: (arg: FunctionArg<any, any>, message?: any) => void = contextErrorLog)
export const errorLogging = (errorFunc: (arg: FunctionArg<any, any>, message?: string) => void = contextErrorLog)
: Middleware<any, any> => {
return async (arg: FunctionArg<any, any>, next: FunctionExecution<any, any>): Promise<any> => {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { expect } from "chai";
import { describe, it } from "mocha";
import { lambda } from "../../../src/core/builder";
import { uno } from "../../../src/core/builder";
import { awsLambdaAdapter } from "../../../src/core/builder-aws";
import { UnoEvent } from "../../../src/core/schemas";
import { createLambdaContext } from "../lambda-helper-test";

describe("builder", () => {
describe("builder AWS", () => {

it("should run a simple handler.", async () => {

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.handler(async () => {});

const result = await handler(
Expand All @@ -20,7 +22,7 @@ describe("builder", () => {

it("should return the handler value.", async () => {

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.handler(async () => ({ foo: "bar" }));

const result = await handler(
Expand All @@ -37,25 +39,28 @@ describe("builder", () => {

const order: number[] = [];

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use([
async (arg, next) => {
order.push(1);
expect(arg.event).to.deep.equal(originalInput);
arg.event = alteredInput;
expect(arg.event.original.foo).to.equal("bar");
arg.event = {
...arg.event,
original: alteredInput,
} as UnoEvent;
const response = await next(arg);
order.push(5);
return response;
},
async (arg, next) => {
order.push(2);
expect(arg.event).to.deep.equal(alteredInput);
expect(arg.event.original.bar).to.equal("foo");
const response = await next(arg);
order.push(4);
expect(response).to.equal("hello");
return "world";
}])
.handler(async (arg) => {
.handler(async () => {
order.push(3);

return "hello";
Expand Down
14 changes: 7 additions & 7 deletions test/unit/handlers/authorizer-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as awsLambda from "aws-lambda";
import { expect } from "chai";
import { lambda } from "../../../src/core/builder";
import { uno } from "../../../src/core/builder";
import { awsLambdaAdapter } from "../../../src/core/builder-aws";
import { randomStr } from "../../../src/core/utils";
import { authorizerBearer } from "../../../src/handlers/authorizer";
import { createLambdaContext } from "../lambda-helper-test";
Expand All @@ -9,7 +9,7 @@ describe("authorizerBearer handler", () => {

it("should parse bearerToken", async () => {

const authorizerResult = (bearerToken: string): awsLambda.CustomAuthorizerResult => ({
const authorizerResult = (bearerToken: string) => ({
policyDocument: {
Statement: [],
Version: "2012-10-17",
Expand All @@ -19,7 +19,7 @@ describe("authorizerBearer handler", () => {

const inputBearerToken = randomStr();

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.handler(authorizerBearer(async ({ bearerToken }) => authorizerResult(bearerToken)));

const lambdaResult = await handler(
Expand All @@ -29,22 +29,22 @@ describe("authorizerBearer handler", () => {
type: randomStr(),
},
createLambdaContext(),
(e, r) => { }) as awsLambda.CustomAuthorizerResult;
(e, r) => { });

expect(lambdaResult.principalId).equal(inputBearerToken);
});

it("should throw if no bearer token", async () => {

const authorizerResult = (bearerToken: string): awsLambda.CustomAuthorizerResult => ({
const authorizerResult = (bearerToken: string) => ({
policyDocument: {
Statement: [],
Version: "2012-10-17",
},
principalId: bearerToken,
});

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.handler(authorizerBearer(async ({ bearerToken }) => authorizerResult(bearerToken)));

try {
Expand Down
18 changes: 9 additions & 9 deletions test/unit/handlers/health-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as awsLambda from "aws-lambda";
import { expect } from "chai";
import * as HttpStatusCodes from "http-status-codes";
import { lambda } from "../../../src/core/builder";
import { uno } from "../../../src/core/builder";
import { awsLambdaAdapter } from "../../../src/core/builder-aws";
import { randomStr } from "../../../src/core/utils";
import { health } from "../../../src/handlers/health";
import { HealthCheckResult, HealthCheckStatus } from "../../../src/services/health-check";
Expand All @@ -12,7 +12,7 @@ describe("health handler", () => {
it("should run health checks", async () => {

const name = randomStr();
const handler = lambda()
const handler = uno(awsLambdaAdapter())
.handler(health(name, async ({ }) => []));

const lambdaResult = await handler(
Expand All @@ -27,45 +27,45 @@ describe("health handler", () => {
it("should run health checks with API Events - OK", async () => {

const name = randomStr();
const handler = lambda()
const handler = uno(awsLambdaAdapter())
.handler(health(name, async ({ }) => []));

const lambdaResult = await handler(
createAPIGatewayProxyEvent(),
createLambdaContext(),
(e, r) => { }) as awsLambda.APIGatewayProxyResult;
(e, r) => { });

expect(lambdaResult.statusCode).to.equal(HttpStatusCodes.OK);
});

it("should run health checks with API Events - Warning", async () => {

const name = randomStr();
const handler = lambda()
const handler = uno(awsLambdaAdapter())
.handler(health(name, async ({ }) => [
async () => ({ name: "Warning", status: HealthCheckStatus.Warning }),
]));

const lambdaResult = await handler(
createAPIGatewayProxyEvent(),
createLambdaContext(),
(e, r) => { }) as awsLambda.APIGatewayProxyResult;
(e, r) => { });

expect(lambdaResult.statusCode).to.equal(HttpStatusCodes.BAD_REQUEST);
});

it("should run health checks with API Events - Error", async () => {

const name = randomStr();
const handler = lambda()
const handler = uno(awsLambdaAdapter())
.handler(health(name, async ({ }) => [
async () => ({ name: "Error", status: HealthCheckStatus.Error }),
]));

const lambdaResult = await handler(
createAPIGatewayProxyEvent(),
createLambdaContext(),
(e, r) => { }) as awsLambda.APIGatewayProxyResult;
(e, r) => { });

expect(lambdaResult.statusCode).to.equal(HttpStatusCodes.INTERNAL_SERVER_ERROR);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import * as awsLambda from "aws-lambda";
import { expect } from "chai";
import * as HttpStatusCode from "http-status-codes";
import { lambda } from "../../../src/core/builder";
import { proxy, proxyByMethod, proxyRouter } from "../../../src/handlers/proxy";
import { parseBodyAsJSON, parseParameters } from "../../../src/middlewares/proxy";
import { uno } from "../../../src/core/builder";
import { awsLambdaAdapter } from "../../../src/core/builder-aws";
import { http, httpMethodRouter, httpRouter } from "../../../src/handlers/http";
import { parseBodyAsJSON, parseParameters } from "../../../src/middlewares/http";
import { createAPIGatewayProxyEvent, createLambdaContext } from "../lambda-helper-test";

describe("proxy handler", () => {
describe("http handler", () => {

it("should run proxy handler with anonymous object", async () => {

const testBody = { foo: "bar" };
const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use(parseBodyAsJSON())
.handler(proxy<{}>(async ({ services: { body } }) => body()));
.handler(http(async ({ services: { body } }) => body()));

const lambdaResult = await handler(
createAPIGatewayProxyEvent({
Expand All @@ -22,14 +23,14 @@ describe("proxy handler", () => {
createLambdaContext(),
(e, r) => { }) as awsLambda.APIGatewayProxyResult;

expect(lambdaResult.body).to.deep.equal(testBody);
expect(lambdaResult.body).to.equal(JSON.stringify(testBody));
expect(lambdaResult.statusCode).to.equal(HttpStatusCode.OK);
});

it("should run proxy handler with undefined", async () => {

const handler = lambda()
.handler(proxy<{}>(async () => undefined));
const handler = uno(awsLambdaAdapter())
.handler(http(async () => undefined));

try {
await handler(
Expand All @@ -45,8 +46,8 @@ describe("proxy handler", () => {

it("should run proxy handler with APIGatewayProxyResult", async () => {

const handler = lambda()
.handler(proxy<{}>(async () => ({
const handler = uno(awsLambdaAdapter())
.handler(http(async () => ({
body: "hello",
statusCode: HttpStatusCode.IM_A_TEAPOT,
})));
Expand All @@ -66,8 +67,8 @@ describe("proxyByMethod handler", () => {

it("should run the correct handler", async () => {

const handler = lambda()
.handler(proxyByMethod<{}>({
const handler = uno(awsLambdaAdapter())
.handler(httpMethodRouter({
get: async () => "get-method",
post: async () => "post-method",
}));
Expand All @@ -84,8 +85,8 @@ describe("proxyByMethod handler", () => {

it("should return methodNotAllowed", async () => {

const handler = lambda()
.handler(proxyByMethod<{}>({
const handler = uno(awsLambdaAdapter())
.handler(httpMethodRouter({
get: async () => "get-method",
post: async () => "post-method",
}));
Expand All @@ -110,9 +111,9 @@ describe("proxyRouter handler", () => {

it("should route simple calls and parameters", async () => {

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use(parseParameters())
.handler(proxyRouter<{}>({
.handler(httpRouter({
":firstParam/another-route": {
get: async ({ services }) => "first-param-" + services.parameters().firstParam,
},
Expand Down Expand Up @@ -152,9 +153,9 @@ describe("proxyRouter handler", () => {

it("should throw if path parameter not found.", async () => {

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use(parseParameters())
.handler(proxyRouter<{}>({
.handler(httpRouter({
users: {
get: async () => "list-method",
},
Expand All @@ -176,9 +177,9 @@ describe("proxyRouter handler", () => {

it("should throw if method not found.", async () => {

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use(parseParameters())
.handler(proxyRouter<{}>({
.handler(httpRouter({
users: {
get: async () => "list-method",
},
Expand All @@ -203,9 +204,9 @@ describe("proxyRouter handler", () => {

it("should throw if route not found.", async () => {

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use(parseParameters())
.handler(proxyRouter<{}>({
.handler(httpRouter({
users: {
get: async () => "list-method",
},
Expand Down
9 changes: 5 additions & 4 deletions test/unit/middlewares/bypass-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from "chai";
import { describe, it } from "mocha";
import { lambda } from "../../../src/core/builder";
import { uno } from "../../../src/core/builder";
import { awsLambdaAdapter } from "../../../src/core/builder-aws";
import { bypass } from "../../../src/middlewares/bypass";
import { createLambdaContext } from "../lambda-helper-test";

Expand All @@ -9,7 +10,7 @@ describe("bypass middleware", () => {
it("should let execution through", async () => {

let executed = false;
const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use(bypass(() => false))
.handler(async ({}) => {
executed = true;
Expand All @@ -26,7 +27,7 @@ describe("bypass middleware", () => {
it("should bypass execution", async () => {

let executed = false;
const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use(bypass(() => true))
.handler(async ({}) => {
executed = true;
Expand All @@ -43,7 +44,7 @@ describe("bypass middleware", () => {
it("should bypass execution and return alternate", async () => {

let executed = false;
const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use(bypass(() => true, async () => 1))
.handler(async ({}) => {
executed = true;
Expand Down
7 changes: 4 additions & 3 deletions test/unit/middlewares/container-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from "chai";
import { describe, it } from "mocha";
import { lambda } from "../../../src/core/builder";
import { uno } from "../../../src/core/builder";
import { awsLambdaAdapter } from "../../../src/core/builder-aws";
import { createContainerFactory } from "../../../src/core/container";
import { randomStr } from "../../../src/core/utils";
import { container } from "../../../src/middlewares/container";
Expand All @@ -22,9 +23,9 @@ describe("container middleware", () => {
c: ({ builder }) => builder.scoped(randomStr()),
});

const handler = lambda()
const handler = uno(awsLambdaAdapter())
.use(container(() => createContainer()))
.handler<any, any, ContainerContract>(async ({ services: { a, b, c } }) => ({
.handler<any, ContainerContract>(async ({ services: { a, b, c } }) => ({
scoped1: c(),
scoped2: c(),
singleton: a(),
Expand Down
Loading

0 comments on commit 48c62a4

Please sign in to comment.