From 9038e87d94e32f789daef481517f6dd2570120f8 Mon Sep 17 00:00:00 2001 From: CarlosGamero <101278162+CarlosGamero@users.noreply.github.com> Date: Wed, 24 Apr 2024 20:25:57 +0200 Subject: [PATCH] Bugfix -> SNS consumer queue name is always undefined (#128) * Fixing queueName on SNS consumer * Adding tests to cover the bugfix * SNS patch version * Fixing typo Co-authored-by: Igor Savin --------- Co-authored-by: Igor Savin --- packages/sns/lib/sns/AbstractSnsSqsConsumer.ts | 1 + packages/sns/lib/utils/snsInitter.ts | 9 ++++++++- packages/sns/package.json | 2 +- .../sns/test/consumers/SnsSqsPermissionConsumer.spec.ts | 4 ++++ packages/sns/test/consumers/SnsSqsPermissionConsumer.ts | 1 + 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/sns/lib/sns/AbstractSnsSqsConsumer.ts b/packages/sns/lib/sns/AbstractSnsSqsConsumer.ts index 987796fa..faf58ca3 100644 --- a/packages/sns/lib/sns/AbstractSnsSqsConsumer.ts +++ b/packages/sns/lib/sns/AbstractSnsSqsConsumer.ts @@ -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 diff --git a/packages/sns/lib/utils/snsInitter.ts b/packages/sns/lib/utils/snsInitter.ts index a2cb30a2..79c7d360 100644 --- a/packages/sns/lib/utils/snsInitter.ts +++ b/packages/sns/lib/utils/snsInitter.ts @@ -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', @@ -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) @@ -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, } } diff --git a/packages/sns/package.json b/packages/sns/package.json index 6fad0398..31b7112d 100644 --- a/packages/sns/package.json +++ b/packages/sns/package.json @@ -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", diff --git a/packages/sns/test/consumers/SnsSqsPermissionConsumer.spec.ts b/packages/sns/test/consumers/SnsSqsPermissionConsumer.spec.ts index e7255b8b..661d286c 100644 --- a/packages/sns/test/consumers/SnsSqsPermissionConsumer.spec.ts +++ b/packages/sns/test/consumers/SnsSqsPermissionConsumer.spec.ts @@ -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', @@ -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, @@ -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-"}}}]}', @@ -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, diff --git a/packages/sns/test/consumers/SnsSqsPermissionConsumer.ts b/packages/sns/test/consumers/SnsSqsPermissionConsumer.ts index 31df4f3a..6d8019a1 100644 --- a/packages/sns/test/consumers/SnsSqsPermissionConsumer.ts +++ b/packages/sns/test/consumers/SnsSqsPermissionConsumer.ts @@ -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, }