Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

WIP - Obscure value mocking #161

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"chai": "latest",
"mocha": "latest",
"typescript": "~2.6.2",
"chai-date-string": "^0.1.0",
"chai-match": "^1.1.1",
"dependency-check": "^2.9.1",
"proxyquire-2": "^1.0.7"
},
Expand Down
69 changes: 45 additions & 24 deletions src/awsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ export interface PropertyBase {
export interface PrimitiveProperty extends PropertyBase {
PrimitiveType: AWSPrimitiveType,

Type: undefined
ItemType: undefined
Type: undefined,
ItemType: undefined,
PrimitiveItemType: undefined
}

export interface ComplexProperty extends PropertyBase {
Type: string | string[],

PrimitiveType: undefined
ItemType: undefined
PrimitiveType: undefined,
ItemType: undefined,
PrimitiveItemType: undefined
}

Expand All @@ -49,7 +49,7 @@ export interface ItemType {
PrimitiveItemType: undefined
}

export type ListProperty = ListPropertyBase & (PrimitiveItemType | ItemType)
export type ListProperty = ListPropertyBase & (PrimitiveItemType | ItemType);

export interface MapPropertyBase extends PropertyBase {
Type: 'Map',
Expand All @@ -58,9 +58,11 @@ export interface MapPropertyBase extends PropertyBase {
PrimitiveType: undefined
}

export type MapProperty = MapPropertyBase & (PrimitiveItemType | ItemType)
export type MapProperty = MapPropertyBase & (PrimitiveItemType | ItemType);

export type AggregateProperty = ListProperty | MapProperty;

export type Property = PrimitiveProperty | ComplexProperty | ListProperty | MapProperty
export type Property = PrimitiveProperty | ComplexProperty | AggregateProperty;

export const awsPropertyTemplate: PropertyBase = {
Documentation: '',
Expand All @@ -70,8 +72,8 @@ export const awsPropertyTemplate: PropertyBase = {

export interface ResourcePropertyType {
Documentation: string,
Properties: {[propertyName: string]: Property | undefined}
AdditionalProperties?: undefined;
Properties: {[propertyName: string]: Property | undefined},
AdditionalProperties?: undefined
}

export interface ResourceType {
Expand All @@ -84,29 +86,30 @@ export interface ResourceType {
export type Type = ResourceType | ResourcePropertyType;

export const awsResourceTypeTemplate: ResourceType = {
Documentation: '',
AdditionalProperties: false,
Properties: {}
Documentation: '',
AdditionalProperties: false,
Properties: {}
};

export const awsResourcePropertyTypeTemplate: ResourcePropertyType = {
Documentation: '',
Properties: {}
Documentation: '',
Properties: {}
};

export interface PrimitiveAttribute {
PrimitiveType: AWSPrimitiveType
}
export type PrimitiveAttribute = PrimitiveProperty;

export interface ListAttribute {
Type: 'List',
PrimitiveItemType: AWSPrimitiveType
}
export type ComplexAttribute = ComplexProperty;

export type ListAttribute = ListProperty;

export type MapAttribute = MapProperty;

export type Attribute = PrimitiveAttribute | ListAttribute;
export type AggregateAttribute = ListAttribute | MapAttribute;

export type Attribute = PrimitiveAttribute | ComplexAttribute | AggregateAttribute;

export type AWSResourcesSpecification = {
PropertyTypes: {[propertyName: string]: ResourcePropertyType | undefined}
PropertyTypes: {[propertyName: string]: ResourcePropertyType | undefined},
ResourceTypes: {[resourceName: string]: ResourceType | undefined}
}

Expand All @@ -116,7 +119,25 @@ type ResourceRefTypes = {[resourceName: string]: string | undefined};

export const awsResourceRefTypes = require('../data/aws_resource_ref_types.json') as ResourceRefTypes;

type ParameterTypes = {[parameterName: string]: 'string' | 'number' | 'array' | undefined};
export type ParameterValue = string | string[] | number | number[] | undefined;

export type Parameter = {
AllowedPattern?: string,
AllowedValues?: ParameterValue[],
ConstraintDescription?: string,
Default?: ParameterValue,
Description?: string,
MaxLength?: number,
MaxValue?: number,
MinLength?: number,
MinValue?: number,
NoEcho?: boolean,
Type: string
}

type ParameterType = 'string' | 'number' | 'array';

type ParameterTypes = { [parameterName: string]: ParameterType };

export const awsParameterTypes = require('../data/aws_parameter_types.json') as ParameterTypes;

Expand Down
24 changes: 18 additions & 6 deletions src/test/apiTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect} from 'chai';
import path = require('path');
import api = require('../api');

const util = require('util');
describe('api', () => {
describe('validateFile', () => {
it('should validate a file', () => {
Expand Down Expand Up @@ -37,12 +37,24 @@ describe('api', () => {
it('should reset validator state', () => {
const file = path.join(__dirname, '../../testData/valid/yaml/2.yaml');
const validator = require('../validator');
validator.addParameterValue('CertificateArn', 'arn:aws:something');
const result = api.validateFile(file);
let result;

// we first pass the validation because of an invalid parameter value
validator.addParameterValue('CertificateArn', 'invalid_arn_string');
result = validator.validateFile(file);
// console.log(util.inspect(result, false, null));
expect(result).to.have.property('templateValid', false);
expect(result.errors.crit).to.have.length(1);
expect(result.errors.crit[0].message).to.contain('\'string_input_CertificateArn\'');
})

// we try to reset the system
validator.resetValidator();

// we try again in hopes the system has forgotten the previously defined parameter value
result = validator.validateFile(file);
// console.log(util.inspect(result, false, null));
expect(result).to.have.property('templateValid', true);
expect(result.errors.crit).to.have.length(0);
});
});

describe('validateJsonObject', () => {
Expand All @@ -53,4 +65,4 @@ describe('api', () => {
expect(result.errors.crit).to.have.length(0);
});
});
});
});
2 changes: 1 addition & 1 deletion src/test/indexTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('index', () => {

it('only-guess-parameters should allow opting in to parameter mocking', (done) => {
exec('node lib/index.js validate testData/valid/yaml/no-guess-parameters.yaml --only-guess-parameters Param1', function(error, stdout, stderr) {
expect(stdout).to.contain('1 crit');
expect(stdout).to.contain('2 crit');
expect(stdout).to.contain('Value for parameter was not provided');
done();
});
Expand Down
Loading