Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(apigateway): migrate tests to jest #11060

Merged
merged 2 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-apigateway/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ nyc.config.js
.LAST_PACKAGE
*.snk
!.eslintrc.js
!jest.config.js

junit.xml
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-apigateway/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dist

tsconfig.json
.eslintrc.js
jest.config.js

# exclude cdk artifacts
**/cdk.out
Expand Down
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-apigateway/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const baseConfig = require('cdk-build-tools/config/jest.config');
module.exports = {
...baseConfig,
coverageThreshold: {
global: {
branches: 80,
statements: 60,
},
},
};
5 changes: 2 additions & 3 deletions packages/@aws-cdk/aws-apigateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"cloudformation": "AWS::ApiGateway",
"env": {
"AWSLINT_BASE_CONSTRUCT": true
}
},
"jest": true
},
"keywords": [
"aws",
Expand All @@ -72,11 +73,9 @@
"license": "Apache-2.0",
"devDependencies": {
"@aws-cdk/assert": "0.0.0",
"@types/nodeunit": "^0.0.31",
"cdk-build-tools": "0.0.0",
"cdk-integ-tools": "0.0.0",
"cfn2ts": "0.0.0",
"nodeunit": "^0.11.3",
"pkglint": "0.0.0"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Test } from 'nodeunit';
import '@aws-cdk/assert/jest';
import * as apigateway from '../lib';

export = {
'if jsonWithStandardFields method called with no parameter'(test: Test) {
describe('access log', () => {
test('if jsonWithStandardFields method called with no parameter', () => {
const testFormat = apigateway.AccessLogFormat.jsonWithStandardFields();
test.deepEqual(testFormat.toString(), '{"requestId":"$context.requestId","ip":"$context.identity.sourceIp","user":"$context.identity.user","caller":"$context.identity.caller","requestTime":"$context.requestTime","httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath","status":"$context.status","protocol":"$context.protocol","responseLength":"$context.responseLength"}');
expect(testFormat.toString()).toEqual('{"requestId":"$context.requestId","ip":"$context.identity.sourceIp","user":"$context.identity.user","caller":"$context.identity.caller","requestTime":"$context.requestTime","httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath","status":"$context.status","protocol":"$context.protocol","responseLength":"$context.responseLength"}');
});

test.done();
},

'if jsonWithStandardFields method called with all parameters false'(test: Test) {
test('if jsonWithStandardFields method called with all parameters false', () => {
const testFormat = apigateway.AccessLogFormat.jsonWithStandardFields({
caller: false,
httpMethod: false,
Expand All @@ -21,19 +19,15 @@ export = {
status: false,
user: false,
});
test.deepEqual(testFormat.toString(), '{"requestId":"$context.requestId"}');

test.done();
},
expect(testFormat.toString()).toEqual('{"requestId":"$context.requestId"}');
});

'if clf method called'(test: Test) {
test('if clf method called', () => {
const testFormat = apigateway.AccessLogFormat.clf();
test.deepEqual(testFormat.toString(), '$context.identity.sourceIp $context.identity.caller $context.identity.user [$context.requestTime] "$context.httpMethod $context.resourcePath $context.protocol" $context.status $context.responseLength $context.requestId');
expect(testFormat.toString()).toEqual('$context.identity.sourceIp $context.identity.caller $context.identity.user [$context.requestTime] "$context.httpMethod $context.resourcePath $context.protocol" $context.status $context.responseLength $context.requestId');
});

test.done();
},

'if custom method called'(test: Test) {
test('if custom method called', () => {
const testFormat = apigateway.AccessLogFormat.custom(JSON.stringify({
requestId: apigateway.AccessLogField.contextRequestId(),
sourceIp: apigateway.AccessLogField.contextIdentitySourceIp(),
Expand All @@ -44,8 +38,6 @@ export = {
email: apigateway.AccessLogField.contextAuthorizerClaims('email'),
},
}));
test.deepEqual(testFormat.toString(), '{"requestId":"$context.requestId","sourceIp":"$context.identity.sourceIp","method":"$context.httpMethod","accountId":"$context.identity.accountId","userContext":{"sub":"$context.authorizer.claims.sub","email":"$context.authorizer.claims.email"}}');

test.done();
},
};
expect(testFormat.toString()).toEqual('{"requestId":"$context.requestId","sourceIp":"$context.identity.sourceIp","method":"$context.httpMethod","accountId":"$context.identity.accountId","userContext":{"sub":"$context.authorizer.claims.sub","email":"$context.authorizer.claims.email"}}');
});
});
Original file line number Diff line number Diff line change
@@ -1,59 +1,56 @@
import '@aws-cdk/assert/jest';
import * as path from 'path';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import * as apigw from '../lib';

export = {
'apigateway.ApiDefinition.fromJson': {
'happy case'(test: Test) {
describe('api definition', () => {
describe('apigateway.ApiDefinition.fromJson', () => {
test('happy case', () => {
const stack = new cdk.Stack();
const definition = {
key1: 'val1',
};
const config = apigw.ApiDefinition.fromInline(definition).bind(stack);
test.deepEqual(config.inlineDefinition, definition);
test.ok(config.s3Location === undefined);
test.done();
},

'fails if Json definition is empty'(test: Test) {
test.throws(
() => defineRestApi(apigw.ApiDefinition.fromInline({})),
/cannot be empty/);
test.done();
},

'fails if definition is not an object'(test: Test) {
test.throws(
() => defineRestApi(apigw.ApiDefinition.fromInline('not-json')),
/should be of type object/);
test.done();
},
},

'apigateway.ApiDefinition.fromAsset': {
'happy case'(test: Test) {
expect(config.inlineDefinition).toEqual(definition);
expect(config.s3Location).toBeUndefined();
});

test('fails if Json definition is empty', () => {
expect(
() => defineRestApi(apigw.ApiDefinition.fromInline({})))
.toThrow(/cannot be empty/);
});

test('fails if definition is not an object', () => {
expect(
() => defineRestApi(apigw.ApiDefinition.fromInline('not-json')))
.toThrow(/should be of type object/);
});
});

describe('apigateway.ApiDefinition.fromAsset', () => {
test('happy case', () => {
const stack = new cdk.Stack();
const config = apigw.ApiDefinition.fromAsset(path.join(__dirname, 'sample-definition.yaml')).bind(stack);
test.ok(config.inlineDefinition === undefined);
test.ok(config.s3Location !== undefined);
test.deepEqual(stack.resolve(config.s3Location!.bucket), {
expect(config.inlineDefinition).toBeUndefined();
expect(config.s3Location).toBeDefined();
expect(stack.resolve(config.s3Location!.bucket)).toEqual({
Ref: 'AssetParameters68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fbS3Bucket42039E29',
});
test.done();
},

'fails if a directory is given for an asset'(test: Test) {
});

test('fails if a directory is given for an asset', () => {
// GIVEN
const fileAsset = apigw.ApiDefinition.fromAsset(path.join(__dirname, 'authorizers'));

// THEN
test.throws(() => defineRestApi(fileAsset), /Asset cannot be a \.zip file or a directory/);
test.done();
},
expect(() => defineRestApi(fileAsset)).toThrow(/Asset cannot be a \.zip file or a directory/);

'only one Asset object gets created even if multiple functions use the same AssetApiDefinition'(test: Test) {
});

test('only one Asset object gets created even if multiple functions use the same AssetApiDefinition', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
Expand All @@ -73,26 +70,26 @@ export = {
const synthesized = assembly.stacks[0];

// API1 has an asset, API2 does not
test.deepEqual(synthesized.assets.length, 1);
test.done();
},
},
expect(synthesized.assets.length).toEqual(1);

});
});

'apigateway.ApiDefinition.fromBucket': {
'happy case'(test: Test) {
describe('apigateway.ApiDefinition.fromBucket', () => {
test('happy case', () => {
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'my-bucket');
const config = apigw.ApiDefinition.fromBucket(bucket, 'my-key', 'my-version').bind(stack);
test.ok(config.inlineDefinition === undefined);
test.ok(config.s3Location !== undefined);
test.deepEqual(stack.resolve(config.s3Location!.bucket), {
expect(config.inlineDefinition).toBeUndefined();
expect(config.s3Location).toBeDefined();
expect(stack.resolve(config.s3Location!.bucket)).toEqual({
Ref: 'mybucket15D133BF',
});
test.equals(config.s3Location!.key, 'my-key');
test.done();
},
},
};
expect(config.s3Location!.key).toEqual('my-key');

});
});
});

function defineRestApi(definition: apigw.ApiDefinition) {
const stack = new cdk.Stack();
Expand Down
Loading