Skip to content

Commit

Permalink
Bugfix -> SNS consumer queue name is always undefined (#128)
Browse files Browse the repository at this point in the history
* Fixing queueName on SNS consumer

* Adding tests to cover the bugfix

* SNS patch version

* Fixing typo

Co-authored-by: Igor Savin <[email protected]>

---------

Co-authored-by: Igor Savin <[email protected]>
  • Loading branch information
CarlosGamero and kibertoad authored Apr 24, 2024
1 parent dc8470e commit 9038e87
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/sns/lib/sns/AbstractSnsSqsConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export abstract class AbstractSnsSqsConsumer<
this.subscriptionConfig,
{ logger: this.logger },
)
this.queueName = initSnsSqsResult.queueName
this.queueUrl = initSnsSqsResult.queueUrl
this.topicArn = initSnsSqsResult.topicArn
this.subscriptionArn = initSnsSqsResult.subscriptionArn
Expand Down
9 changes: 8 additions & 1 deletion packages/sns/lib/utils/snsInitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export async function initSnsSqs(
'If locatorConfig.subscriptionArn is not specified, creationConfig.queue parameter is mandatory, as there will be an attempt to create the missing queue',
)
}
if (!creationConfig.queue.QueueName) {
throw new Error('If locatorConfig.subscriptionArn is not specified, creationConfig.queue.QueueName parameter is mandatory, as there will be an attempt to create the missing queue')
}
if (!subscriptionConfig) {
throw new Error(
'If locatorConfig.subscriptionArn is not specified, subscriptionConfig parameter is mandatory, as there will be an attempt to create the missing subscription',
Expand Down Expand Up @@ -58,12 +61,12 @@ export async function initSnsSqs(
return {
subscriptionArn,
topicArn,
queueName: creationConfig.queue.QueueName,
queueUrl,
}
}

// Check for existing resources, using the locators

const queuePromise = getQueueAttributes(sqsClient, locatorConfig)
const topicPromise = getTopicAttributes(snsClient, locatorConfig.topicArn)

Expand All @@ -76,10 +79,14 @@ export async function initSnsSqs(
throw new Error(`Topic with topicArn ${locatorConfig.topicArn} does not exist.`)
}

const splitUrl = locatorConfig.queueUrl.split('/')
const queueName = splitUrl[splitUrl.length - 1]

return {
subscriptionArn: locatorConfig.subscriptionArn,
topicArn: locatorConfig.topicArn,
queueUrl: locatorConfig.queueUrl,
queueName,
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sns/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@message-queue-toolkit/sns",
"version": "13.0.0",
"version": "13.0.1",
"private": false,
"license": "MIT",
"description": "SNS adapter for message-queue-toolkit",
Expand Down
4 changes: 4 additions & 0 deletions packages/sns/test/consumers/SnsSqsPermissionConsumer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('SnsSqsPermissionConsumer', () => {
expect(newConsumer.subscriptionProps.queueUrl).toBe(
'http://s3.localhost.localstack.cloud:4566/000000000000/existingQueue',
)
expect(newConsumer.subscriptionProps.queueName).toBe('existingQueue')
expect(newConsumer.subscriptionProps.topicArn).toEqual(arn)
expect(newConsumer.subscriptionProps.subscriptionArn).toBe(
'arn:aws:sns:eu-west-1:000000000000:user_permissions:bdf640a2-bedf-475a-98b8-758b88c87395',
Expand Down Expand Up @@ -106,6 +107,7 @@ describe('SnsSqsPermissionConsumer', () => {
expect(newConsumer.subscriptionProps.queueUrl).toBe(
'http://sqs.eu-west-1.localstack:4566/000000000000/existingQueue',
)
expect(newConsumer.subscriptionProps.queueName).toBe('existingQueue')

const attributes = await getQueueAttributes(sqsClient, {
queueUrl: newConsumer.subscriptionProps.queueUrl,
Expand Down Expand Up @@ -152,6 +154,7 @@ describe('SnsSqsPermissionConsumer', () => {
const attributes = await getQueueAttributes(sqsClient, {
queueUrl: newConsumer.subscriptionProps.queueUrl,
})
expect(newConsumer.subscriptionProps.queueName).toBe('existingQueue')

expect(attributes.result?.attributes!.Policy).toBe(
'{"Version":"2012-10-17","Id":"__default_policy_ID","Statement":[{"Sid":"AllowSNSPublish","Effect":"Allow","Principal":{"AWS":"*"},"Action":"sqs:SendMessage","Resource":"arn:aws:sqs:eu-west-1:000000000000:existingQueue","Condition":{"ArnLike":{"aws:SourceArn":"someservice-"}}}]}',
Expand Down Expand Up @@ -181,6 +184,7 @@ describe('SnsSqsPermissionConsumer', () => {
expect(newConsumer.subscriptionProps.queueUrl).toBe(
'http://sqs.eu-west-1.localstack:4566/000000000000/existingQueue',
)
expect(newConsumer.subscriptionProps.queueName).toBe('existingQueue')

const attributes = await getQueueAttributes(sqsClient, {
queueUrl: newConsumer.subscriptionProps.queueUrl,
Expand Down
1 change: 1 addition & 0 deletions packages/sns/test/consumers/SnsSqsPermissionConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export class SnsSqsPermissionConsumer extends AbstractSnsSqsConsumer<
return {
topicArn: this.topicArn,
queueUrl: this.queueUrl,
queueName: this.queueName,
subscriptionArn: this.subscriptionArn,
deadLetterQueueUrl: this.deadLetterQueueUrl,
}
Expand Down

0 comments on commit 9038e87

Please sign in to comment.