Skip to content

Commit 02d1841

Browse files
committed
refactor(sns-validation): merge sns validation schema with main schema
1 parent 06ce8ee commit 02d1841

File tree

7 files changed

+134
-185
lines changed

7 files changed

+134
-185
lines changed

lib/apiGateway/schema.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ const key = Joi.alternatives().try([
102102
)
103103
])
104104

105+
const stringOrGetAtt = (attributeName) => {
106+
return Joi.alternatives().try([
107+
Joi.string(),
108+
Joi.object({
109+
'Fn::GetAtt': Joi.array()
110+
.length(2)
111+
.ordered(
112+
Joi.string().required(),
113+
Joi.string()
114+
.valid(attributeName)
115+
.required()
116+
)
117+
.required()
118+
}).error(
119+
customErrorBuilder(
120+
'object.child',
121+
`"topicName" must be in the format "{ 'Fn::GetAtt': ['<ResourceId>', '${attributeName}'] }"`
122+
)
123+
)
124+
])
125+
}
126+
105127
const allowedProxies = ['kinesis', 'sqs', 's3', 'sns']
106128

107129
const proxiesSchemas = {
@@ -117,7 +139,7 @@ const proxiesSchemas = {
117139
key: key.required()
118140
})
119141
}),
120-
sns: Joi.object({ sns: proxy }),
142+
sns: Joi.object({ sns: proxy.append({ topicName: stringOrGetAtt('TopicName').required() }) }),
121143
sqs: Joi.object({ sqs: proxy.append({ requestParameters }) })
122144
}
123145

lib/apiGateway/validate.test.js

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ describe('#validateServiceProxies()', () => {
412412
expect(json.events[0].http.cors.methods).to.deep.equal(['POST', 'OPTIONS'])
413413
})
414414

415-
it('should throw if "authorizationType" is not a string', async () => {
415+
it('should throw if "authorizationType" is not a string', () => {
416416
serverlessApigatewayServiceProxy.serverless.service.custom = {
417417
apiGatewayServiceProxies: [
418418
{
@@ -581,7 +581,7 @@ describe('#validateServiceProxies()', () => {
581581
const proxiesToTest = [
582582
{ proxy: 'kinesis', props: { streamName: 'streamName' } },
583583
{ proxy: 's3', props: { bucket: 'bucket', action: 'PutObject', key: 'myKey' } },
584-
{ proxy: 'sns', props: {} }
584+
{ proxy: 'sns', props: { topicName: 'topicName' } }
585585
]
586586
proxiesToTest.forEach(({ proxy, props }) => {
587587
it(`should throw if requestParameters is set for ${proxy}`, () => {
@@ -851,6 +851,115 @@ describe('#validateServiceProxies()', () => {
851851
})
852852
})
853853

854+
describe('sns', () => {
855+
it('should throw error if the "topicName" property doesn\'t exist', () => {
856+
serverlessApigatewayServiceProxy.serverless.service.custom = {
857+
apiGatewayServiceProxies: [
858+
{
859+
sns: {
860+
path: 'sns',
861+
method: 'post'
862+
}
863+
}
864+
]
865+
}
866+
867+
expect(() => serverlessApigatewayServiceProxy.validateServiceProxies()).to.throw(
868+
serverless.classes.Error,
869+
'child "sns" fails because [child "topicName" fails because ["topicName" is required]]'
870+
)
871+
})
872+
873+
it('should throw error if the "topicName" property is not a string or an AWS intrinsic function', () => {
874+
serverlessApigatewayServiceProxy.serverless.service.custom = {
875+
apiGatewayServiceProxies: [
876+
{
877+
sns: {
878+
path: 'sns',
879+
method: 'post',
880+
topicName: ['xx', 'yy']
881+
}
882+
}
883+
]
884+
}
885+
886+
expect(() => serverlessApigatewayServiceProxy.validateServiceProxies()).to.throw(
887+
serverless.classes.Error,
888+
'child "sns" fails because [child "topicName" fails because ["topicName" must be a string, "topicName" must be an object]]'
889+
)
890+
})
891+
892+
it('should throw error if the "topicName" property is missing the AWS intrinsic function "Fn::GetAtt"', () => {
893+
serverlessApigatewayServiceProxy.serverless.service.custom = {
894+
apiGatewayServiceProxies: [
895+
{
896+
sns: {
897+
path: 'sns',
898+
method: 'post',
899+
topicName: { xxx: 'SNSResourceId' }
900+
}
901+
}
902+
]
903+
}
904+
905+
expect(() => serverlessApigatewayServiceProxy.validateServiceProxies()).to.throw(
906+
serverless.classes.Error,
907+
`child "sns" fails because [child "topicName" fails because ["topicName" must be a string, "topicName" must be in the format "{ 'Fn::GetAtt': ['<ResourceId>', 'TopicName'] }"]]`
908+
)
909+
})
910+
911+
it('should throw error if the "topicName" property is an intrinsic function "Fn::GetAtt" but specifies a property other than TopicName', () => {
912+
serverlessApigatewayServiceProxy.serverless.service.custom = {
913+
apiGatewayServiceProxies: [
914+
{
915+
sns: {
916+
path: 'sns',
917+
method: 'post',
918+
topicName: { 'Fn::GetAtt': ['SNSResourceId', 'Arn'] }
919+
}
920+
}
921+
]
922+
}
923+
924+
expect(() => serverlessApigatewayServiceProxy.validateServiceProxies()).to.throw(
925+
serverless.classes.Error,
926+
`child "sns" fails because [child "topicName" fails because ["topicName" must be a string, "topicName" must be in the format "{ 'Fn::GetAtt': ['<ResourceId>', 'TopicName'] }"]]`
927+
)
928+
})
929+
930+
it('should not show error if topicName is valid string', () => {
931+
serverlessApigatewayServiceProxy.serverless.service.custom = {
932+
apiGatewayServiceProxies: [
933+
{
934+
sns: {
935+
path: 'sns',
936+
method: 'post',
937+
topicName: 'someTopicName'
938+
}
939+
}
940+
]
941+
}
942+
943+
expect(() => serverlessApigatewayServiceProxy.validateServiceProxies()).to.not.throw()
944+
})
945+
946+
it('should not show error if topicName is valid intrinsic function', () => {
947+
serverlessApigatewayServiceProxy.serverless.service.custom = {
948+
apiGatewayServiceProxies: [
949+
{
950+
sns: {
951+
path: 'sns',
952+
method: 'post',
953+
topicName: { 'Fn::GetAtt': ['SNSResourceId', 'TopicName'] }
954+
}
955+
}
956+
]
957+
}
958+
959+
expect(() => serverlessApigatewayServiceProxy.validateServiceProxies()).to.not.throw()
960+
})
961+
})
962+
854963
describe('sqs', () => {
855964
it('should throw if requestParameters is not a string to string', () => {
856965
serverlessApigatewayServiceProxy.serverless.service.custom = {

lib/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const compileS3ServiceProxy = require('./package/s3/compileS3ServiceProxy')
2727
// SNS
2828
const compileMethodsToSns = require('./package/sns/compileMethodsToSns')
2929
const compileIamRoleToSns = require('./package/sns/compileIamRoleToSns')
30-
const validateSnsServiceProxy = require('./package/sns/validateSnsServiceProxy')
3130
const compileSnsServiceProxy = require('./package/sns/compileSnsServiceProxy')
3231

3332
class ServerlessApigatewayServiceProxy {
@@ -57,7 +56,6 @@ class ServerlessApigatewayServiceProxy {
5756
compileS3ServiceProxy,
5857
compileMethodsToSns,
5958
compileIamRoleToSns,
60-
validateSnsServiceProxy,
6159
compileSnsServiceProxy,
6260
getStackInfo,
6361
validate,

lib/package/sns/compileSnsServiceProxy.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22
module.exports = {
33
async compileSnsServiceProxy() {
4-
this.validateSnsServiceProxy()
54
this.compileIamRoleToSns()
65
this.compileMethodsToSns()
76
}

lib/package/sns/schema.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/package/sns/validateSnsServiceProxy.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

lib/package/sns/validateSnsServiceProxy.test.js

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)