Skip to content

Commit

Permalink
Merge pull request #16 from nitely/feature/use_built_in_normalization
Browse files Browse the repository at this point in the history
Use built-in Serverless normalization
  • Loading branch information
jthomerson committed May 3, 2017
2 parents 8c7fc6d + b7a3d6f commit 7a6bae6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 55 deletions.
18 changes: 2 additions & 16 deletions src/index.js
Expand Up @@ -43,10 +43,8 @@ module.exports = Class.extend({
},

addEventPermission: function(fnName, fnDef, topicName) {
var normalizedFnName = this._normalize(fnName),
normalizedTopicName = this._normalizeTopicName(topicName),
fnRef = normalizedFnName + 'LambdaFunction',
permRef = normalizedFnName + 'LambdaPermission' + normalizedTopicName,
var fnRef = this.provider.naming.getLambdaLogicalId(fnName),
permRef = this.provider.naming.getLambdaSnsPermissionLogicalId(fnName, topicName),
permission;

permission = {
Expand Down Expand Up @@ -157,16 +155,4 @@ module.exports = Class.extend({
});
},

_normalize: function(s) {
if (_.isEmpty(s)) {
return;
}

return s[0].toUpperCase() + s.substr(1);
},

_normalizeTopicName: function(arn) {
return this._normalize(arn.replace(/[^0-9A-Za-z]/g, ''));
},

});
50 changes: 11 additions & 39 deletions src/tests/index.test.js
Expand Up @@ -7,11 +7,12 @@ var expect = require('expect.js'),

describe('serverless-plugin-external-sns-events', function() {

function createMockServerless(requestFunc) {
function createMockServerless(requestFunc, namingObj) {
var serverless, provider;

provider = {
request: requestFunc,
naming: namingObj || {},
};

serverless = {
Expand Down Expand Up @@ -60,28 +61,31 @@ describe('serverless-plugin-external-sns-events', function() {
describe('addEventPermission', function() {

it('can compile lambda permission with correct FunctionName and SourceArn', function() {
var topicName = 'cool-Topic',
var expFunctionName = 'MyFuncLambdaFunction',
expResourceName = 'MyFuncLambdaPermissionCoolTopic',
getLambdaLogicalIdSpy = sinon.stub().returns(expFunctionName),
getLambdaSnsPermissionLogicalIdSpy = sinon.stub().returns(expResourceName),
topicName = 'cool-Topic',
functionName = 'myFunc',
mockServerless = createMockServerless(createMockRequest(sinon.stub())),
naming = { getLambdaLogicalId: getLambdaLogicalIdSpy, getLambdaSnsPermissionLogicalId: getLambdaSnsPermissionLogicalIdSpy },
mockServerless = createMockServerless(createMockRequest(sinon.stub()), naming),
spyRequestFunc = sinon.spy(mockServerless.getProvider('aws'), 'request'),
plugin = new Plugin(mockServerless, {}),
expPerm, expResourceName, actualPerm;
expPerm, actualPerm;

plugin.addEventPermission(functionName, { name: functionName }, topicName);

expect(spyRequestFunc.callCount).to.be(0);
expect(Object.keys(mockServerless.service.provider.compiledCloudFormationTemplate.Resources).length).to.be(1);

expResourceName = 'MyFuncLambdaPermissionCoolTopic';

expect(expResourceName in mockServerless.service.provider.compiledCloudFormationTemplate.Resources).to.be(true);

actualPerm = mockServerless.service.provider.compiledCloudFormationTemplate.Resources[expResourceName];

expPerm = {
Type: 'AWS::Lambda::Permission',
Properties: {
FunctionName: { 'Fn::GetAtt': [ 'MyFuncLambdaFunction', 'Arn' ] },
FunctionName: { 'Fn::GetAtt': [ expFunctionName, 'Arn' ] },
Action: 'lambda:InvokeFunction',
Principal: 'sns.amazonaws.com',
SourceArn: { 'Fn::Join': [ ':', [ 'arn:aws:sns', { 'Ref': 'AWS::Region' }, { 'Ref': 'AWS::AccountId' }, 'cool-Topic' ] ] },
Expand Down Expand Up @@ -365,36 +369,4 @@ describe('serverless-plugin-external-sns-events', function() {

});

describe('_normalize', function() {
var plugin = new Plugin();

it('returns undefined for empty strings', function() {
expect(plugin._normalize('')).to.be(undefined);
expect(plugin._normalize(false)).to.be(undefined);
expect(plugin._normalize()).to.be(undefined);
expect(plugin._normalize('', true)).to.be(undefined);
expect(plugin._normalize(false, true)).to.be(undefined);
expect(plugin._normalize(undefined, true)).to.be(undefined);
});

it('only modifies the first letter', function() {
expect(plugin._normalize('someTHING')).to.eql('SomeTHING');
expect(plugin._normalize('SomeTHING')).to.eql('SomeTHING');
expect(plugin._normalize('s')).to.eql('S');
expect(plugin._normalize('S')).to.eql('S');
});

});


describe('_normalizeTopicName', function() {
var plugin = new Plugin();

it('produces expected output for a string', function() {
expect(plugin._normalizeTopicName('foo-topic')).to
.eql('Footopic');
});

});

});

0 comments on commit 7a6bae6

Please sign in to comment.