@incdevco/framework
Version:
node.js lambda framework
754 lines (430 loc) • 17.4 kB
JavaScript
/* globals AWS CryptoJS expect inject sinon */
var AWS4 = 'AWS4';
var AWS4_REQUEST = 'aws4_request';
var sandbox = sinon.sandbox.create();
describe('angular aws', function () {
beforeEach(module('aws'));
afterEach(function() {
sandbox.verifyAndRestore();
});
describe('ApiGatewayRequestInterceptor', function() {
var Interceptor;
beforeEach(inject(function (_ApiGatewayRequestInterceptor_) {
Interceptor = _ApiGatewayRequestInterceptor_;
}));
describe('constructor', function() {
it('should throw an exception when no apiKey is provided', function () {
try {
new Interceptor({});
throw new Error('No Exception Thrown');
} catch (e) {
expect(e.message).to.equal('No apiKey Provided', 'e.message');
}
});
});
});
describe('AwsSignature', function() {
var AWS_SHA_256,
AwsSignature;
beforeEach(inject(function (_AWS_SHA_256_, _AwsSignature_) {
AWS_SHA_256 = _AWS_SHA_256_;
AwsSignature = _AwsSignature_;
}));
afterEach(function () {
});
describe('buildAuthorizationHeader', function() {
var accessKey,
credentialScope,
headers,
signature,
signedHeaders;
beforeEach(function () {
accessKey = 'accessKey';
credentialScope = 'credentialScope';
headers = {};
signature = 'signature';
signedHeaders = 'signedHeaders';
});
it('should', function () {
var stub = sandbox.stub(AwsSignature, 'buildCanonicalSignedHeaders');
stub.withArgs(headers).returns(signedHeaders);
var result = AwsSignature.buildAuthorizationHeader(accessKey, credentialScope, headers, signature);
expect(stub.called).to.be.ok;
expect(result).to.equal(AWS_SHA_256
+ ' Credential=' + accessKey + '/' + credentialScope
+ ', SignedHeaders=' + signedHeaders
+ ', Signature=' + signature, 'result');
});
it('should throw an exception when accessKey is undefined', function (done) {
try {
AwsSignature.buildAuthorizationHeader(undefined, credentialScope, headers, signature);
done(new Error('No Exception Thrown'));
} catch (e) {
expect(e.message).to.equal('No accessKey Provided', 'e.message');
done();
}
});
});
describe('buildCanonicalHeaders', function() {
var headers;
beforeEach(function () {
headers = {
'key':'value'
};
});
it('should', function () {
var result = AwsSignature.buildCanonicalHeaders(headers);
expect(result).to.equal('key:value\n', 'result');
});
});
describe('buildCanonicalQueryString', function() {
var query;
var expected;
beforeEach(function() {
query = {
key: 'value'
};
expected = 'key=value';
});
it('should', function () {
var result = AwsSignature.buildCanonicalQueryString(query);
expect(result).to.equal(expected, 'result');
});
});
describe('buildCanonicalRequest', function() {
var canonicalHeaders,
canonicalQueryString,
canonicalSignedHeaders,
canonicalUrl,
expected,
request;
beforeEach(function() {
canonicalQueryString = 'canonicalQueryString';
canonicalUrl = 'canonicalUrl';
expected = 'METHOD\ncanonicalUrl\ncanonicalQueryString\nundefined\nundefined\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
request = {
method: 'METHOD',
url: 'url',
headers: 'headers',
data: ''
};
});
it('should', function () {
sandbox.stub(AwsSignature, 'buildCanonicalUri').returns(canonicalUrl);
sandbox.stub(AwsSignature, 'buildCanonicalQueryString').returns(canonicalQueryString);
sandbox.stub(AwsSignature, 'buildCanonicalHeaders').returns(canonicalHeaders);
sandbox.stub(AwsSignature, 'buildCanonicalSignedHeaders').returns(canonicalSignedHeaders);
var result = AwsSignature.buildCanonicalRequest(request);
expect(result).to.equal(expected, 'result');
});
});
describe('buildCanonicalSignedHeaders', function() {
var expected, headers;
beforeEach(function() {
expected = 'akey;ckey;dkey;fkey';
headers = {
dkey: 'dvalue',
akey: 'avalue',
fkey: 'fvalue',
ckey: 'cvalue'
};
});
it('should', function () {
var result = AwsSignature.buildCanonicalSignedHeaders(headers);
expect(result).to.equal(expected, 'result');
});
});
describe('buildCanonicalUri', function() {
var expected, uri;
beforeEach(function() {
expected = 'not%20uri%20encoded%20string';
uri = 'not uri encoded string';
});
it('should', function () {
var result = AwsSignature.buildCanonicalUri(uri);
expect(result).to.equal(expected, 'result');
});
});
describe('buildCredentialScope', function() {
var datetime, expected, region, serviceName;
beforeEach(function() {
datetime = AwsSignature.createDateTime();
region = 'us-west-2';
serviceName = 'execute-api';
expected = datetime.substr(0, 8)
+ '/' + region
+ '/' + serviceName
+ '/' + AWS4_REQUEST;
});
it('should', function () {
var result = AwsSignature.buildCredentialScope(datetime, region, serviceName);
expect(result).to.equal(expected, 'result');
});
});
describe('buildStringToSign', function() {
var datetime, expected, credentialScope, hashedCanonicalRequest;
beforeEach(function() {
datetime = AwsSignature.createDateTime();
credentialScope = 'credentialScope';
hashedCanonicalRequest = 'hashedCanonicalRequest';
expected = AWS_SHA_256 + '\n'
+ datetime + '\n'
+ credentialScope + '\n'
+ hashedCanonicalRequest;
});
it('should', function () {
var result = AwsSignature.buildStringToSign(datetime, credentialScope, hashedCanonicalRequest);
expect(result).to.equal(expected, 'result');
});
});
describe('calculateSignature', function() {
var expected, key, stringToSign;
beforeEach(function() {
key = 'credentialScope';
stringToSign = 'hashedCanonicalRequest';
expected = AwsSignature.hmac(key, stringToSign);
expected = AwsSignature.hexEncode(expected);
});
it('should', function () {
var result = AwsSignature.calculateSignature(key, stringToSign);
expect(result).to.equal(expected, 'result');
});
});
describe('calculateSigningKey', function() {
var expected, datetime, secretKey, region, serviceName;
beforeEach(function() {
datetime = AwsSignature.createDateTime();
secretKey = 'secretKey';
region = 'us-west-2';
serviceName = 'execute-api';
expected = AwsSignature.hmac(AWS4 + secretKey, datetime.substr(0, 8 ));
expected = AwsSignature.hmac(expected, region);
expected = AwsSignature.hmac(expected, serviceName);
expected = AwsSignature.hmac(expected, AWS4_REQUEST);
});
it('should', function () {
var result = AwsSignature.calculateSigningKey(datetime, secretKey, region, serviceName);
expect(result).to.deep.equal(expected, 'result');
});
});
describe('createDateTime', function() {
var expected;
beforeEach(function() {
expected = new Date().toISOString()
.replace(/\.\d{3}Z$/, 'Z')
.replace(/[:\-]|\.\d{3}/g, '');
});
it('should', function () {
var result = AwsSignature.createDateTime();
expect(result).to.deep.equal(expected, 'result');
});
});
describe('hash', function() {
var expected, value;
beforeEach(function() {
expected = CryptoJS.SHA256(value);
});
it('should', function () {
var result = AwsSignature.hash(value);
expect(result).to.deep.equal(expected, 'result');
});
});
describe('hashCanonicalRequest', function() {
var expected, request;
beforeEach(function() {
request = '';
expected = AwsSignature.hash(request);
expected = AwsSignature.hexEncode(expected);
});
it('should', function () {
var result = AwsSignature.hashCanonicalRequest(request);
expect(result).to.equal(expected, 'result');
});
});
describe('hexEncode', function() {
var expected, value;
beforeEach(function() {
value = CryptoJS.SHA256('test');
expected = value.toString(CryptoJS.enc.Hex);
});
it('should', function () {
var result = AwsSignature.hexEncode(value);
expect(result).to.deep.equal(expected, 'result');
});
});
describe('hmac', function() {
var expected, secret, value;
beforeEach(function() {
secret = 'secret';
value = 'test';
expected = CryptoJS.HmacSHA256(value, secret, {
asBytes: true
});
});
it('should', function () {
var result = AwsSignature.hmac(secret, value);
expect(result).to.deep.equal(expected, 'result');
});
});
describe('sign', function() {
var credentials,
config,
datetime,
expected,
request;
beforeEach(function() {
datetime = '20170519T044016Z';
credentials = {
accessKey: 'accessKey',
secretKey: 'secretKey',
sessionToken: 'sessionToken'
};
config = {
apiKey: 'QFFatAoOsI6gQcTlNckge55fAyzvg50C6NjpbUmm',
endpoint: 'https://api.skostler.com',
region: 'us-west-2',
serviceName: 'execute-api'
};
request = {
data: '',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'GET',
url: 'https://api.skostler.com/development/cameras'
};
expected = {
"method": "GET",
"url": "https://api.skostler.com/development/cameras",
"headers": {
"x-api-key": "QFFatAoOsI6gQcTlNckge55fAyzvg50C6NjpbUmm",
"Accept": "application/json",
"x-amz-date": "20170519T044016Z",
"Authorization": "AWS4-HMAC-SHA256 Credential=accessKey/20170519/us-west-2/execute-api/aws4_request, SignedHeaders=accept;host;x-amz-date;x-api-key, Signature=da5af39282a23d02855f40676e80d7f3358007a9d49112e5bd6ed29b946269d2",
"x-amz-security-token": "sessionToken",
"Content-Type": "application/json"
},
"data": ""
};
});
it('should', function () {
sandbox.stub(AwsSignature, 'createDateTime').returns(datetime);
var result = AwsSignature.sign(request, credentials, config);
//console.log('result', JSON.stringify(result, null, 2));
//console.log('expected', JSON.stringify(expected, null, 2));
expect(result.headers).to.deep.equal(expected.headers, 'headers');
expect(result.url).to.deep.equal(expected.url, 'url');
});
});
});
describe('CognitoIdentity', function() {
var AWSHelper, CognitoIdentity,
expected, region, $rootScope, config;
beforeEach(inject(function (_AWSHelper_,
_CognitoIdentity_,
_$rootScope_) {
config = {};
region = 'region';
AWSHelper = _AWSHelper_;
CognitoIdentity = _CognitoIdentity_;
expected = {
credentials: {
accessKey: undefined,
expireTime: null,
secretKey: undefined,
sessionToken: undefined
},
expireTime: null,
identity: {
identityId: undefined,
identityPoolId: undefined,
expireTime: null,
logins: undefined
}
};
$rootScope = _$rootScope_;
}));
it('should', function (done) {
var credentials = new AWS.Credentials({});
sandbox.stub(credentials, 'get')
.callsArg(0);
sandbox.stub(AWSHelper, 'getCognitoIdentityCredentials')
.returns(credentials);
CognitoIdentity(config, region)
.then(function (result) {
expect(result).to.deep.equal(expected, 'result');
return done();
})
.catch(done);
$rootScope.$apply();
});
it('should reject when exception is received', function (done) {
var credentials = new AWS.Credentials({});
expected = new Error('exception');
sandbox.stub(credentials, 'get')
.callsArgWith(0, expected);
sandbox.stub(AWSHelper, 'getCognitoIdentityCredentials')
.returns(credentials);
CognitoIdentity(config, region)
.then(function () {
throw new Error('resolved');
}, function (result) {
expect(result).to.deep.equal(expected, 'exception');
return done();
})
.catch(done);
$rootScope.$apply();
});
});
describe('CognitoSync', function() {
var AWSHelper, CognitoSync, datasetName, expected, manager, $rootScope;
beforeEach(inject(function (_AWSHelper_, _CognitoSync_, _$rootScope_) {
AWSHelper = _AWSHelper_;
CognitoSync = _CognitoSync_;
datasetName = 'datasetName';
$rootScope = _$rootScope_;
}));
it('should', function (done) {
var manager = {
openOrCreateDataset: function (datasetName, cb) {
return cb(null, datasetName);
}
};
sandbox.stub(AWSHelper, 'getCognitoSyncManager')
.returns(manager);
sandbox.stub(manager, 'openOrCreateDataset')
.callsArgWith(1, null, expected);
$rootScope.$emit('aws.credentials');
CognitoSync(datasetName)
.then(function (result) {
expect(result._dataset).to.equal(expected, 'result');
return done();
})
.catch(done);
$rootScope.$apply();
});
it('should reject with exception', function (done) {
var manager = {
openOrCreateDataset: function (datasetName, cb) {
return cb(null, datasetName);
}
};
sandbox.stub(AWSHelper, 'getCognitoSyncManager')
.returns(manager);
expected = new Error('exception');
sandbox.stub(manager, 'openOrCreateDataset')
.callsArgWith(1, expected);
$rootScope.$emit('aws.credentials');
CognitoSync(datasetName)
.then(function () {
throw new Error('resolved');
}, function (result) {
expect(result).to.equal(expected, 'exception');
return done();
})
.catch(done);
$rootScope.$apply();
});
});
});