Skip to content

Commit

Permalink
SQS publisher tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosGamero committed Oct 14, 2024
1 parent 563c171 commit 4ad7c3b
Showing 1 changed file with 164 additions and 62 deletions.
226 changes: 164 additions & 62 deletions packages/sqs/test/publishers/SqsPermissionPublisher.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SQSClient } from '@aws-sdk/client-sqs'
import { ListQueueTagsCommand, type SQSClient } from '@aws-sdk/client-sqs'
import { waitAndRetry } from '@lokalise/node-core'
import type { AwilixContainer } from 'awilix'
import { Consumer } from 'sqs-consumer'
Expand Down Expand Up @@ -59,86 +59,188 @@ describe('SqsPermissionPublisher', () => {
)
})

it('updates existing queue when one with different attributes exist', async () => {
await assertQueue(sqsClient, {
QueueName: queueName,
Attributes: {
KmsMasterKeyId: 'somevalue',
},
})

const newPublisher = new SqsPermissionPublisher(diContainer.cradle, {
creationConfig: {
queue: {
QueueName: queueName,
Attributes: {
KmsMasterKeyId: 'othervalue',
describe('attributes update', () => {
it('updates existing queue when one with different attributes exist', async () => {
await assertQueue(sqsClient, {
QueueName: queueName,
Attributes: {
KmsMasterKeyId: 'somevalue',
},
})

const newPublisher = new SqsPermissionPublisher(diContainer.cradle, {
creationConfig: {
queue: {
QueueName: queueName,
Attributes: {
KmsMasterKeyId: 'othervalue',
},
},
updateAttributesIfExists: true,
},
updateAttributesIfExists: true,
},
deletionConfig: {
deleteIfExists: false,
},
logMessages: true,
})
deletionConfig: {
deleteIfExists: false,
},
logMessages: true,
})

const sqsSpy = vi.spyOn(sqsClient, 'send')
const sqsSpy = vi.spyOn(sqsClient, 'send')

await newPublisher.init()
expect(newPublisher.queueProps.url).toBe(
`http://sqs.eu-west-1.localstack:4566/000000000000/${queueName}`,
)
await newPublisher.init()
expect(newPublisher.queueProps.url).toBe(
`http://sqs.eu-west-1.localstack:4566/000000000000/${queueName}`,
)

const updateCall = sqsSpy.mock.calls.find((entry) => {
return entry[0].constructor.name === 'SetQueueAttributesCommand'
})
expect(updateCall).toBeDefined()

const updateCall = sqsSpy.mock.calls.find((entry) => {
return entry[0].constructor.name === 'SetQueueAttributesCommand'
const attributes = await getQueueAttributes(sqsClient, newPublisher.queueProps.url)

expect(attributes.result?.attributes!.KmsMasterKeyId).toBe('othervalue')
})
expect(updateCall).toBeDefined()

const attributes = await getQueueAttributes(sqsClient, newPublisher.queueProps.url)
it('does not update existing queue when attributes did not change', async () => {
await assertQueue(sqsClient, {
QueueName: queueName,
Attributes: {
KmsMasterKeyId: 'somevalue',
},
})

const newPublisher = new SqsPermissionPublisher(diContainer.cradle, {
creationConfig: {
queue: {
QueueName: queueName,
Attributes: {
KmsMasterKeyId: 'somevalue',
},
},
updateAttributesIfExists: true,
},
deletionConfig: {
deleteIfExists: false,
},
logMessages: true,
})

const sqsSpy = vi.spyOn(sqsClient, 'send')

expect(attributes.result?.attributes!.KmsMasterKeyId).toBe('othervalue')
})
await newPublisher.init()
expect(newPublisher.queueProps.url).toBe(
`http://sqs.eu-west-1.localstack:4566/000000000000/${queueName}`,
)

it('does not update existing queue when attributes did not change', async () => {
await assertQueue(sqsClient, {
QueueName: queueName,
Attributes: {
KmsMasterKeyId: 'somevalue',
},
const updateCall = sqsSpy.mock.calls.find((entry) => {
return entry[0].constructor.name === 'SetQueueAttributesCommand'
})
expect(updateCall).toBeUndefined()

const attributes = await getQueueAttributes(sqsClient, newPublisher.queueProps.url)

expect(attributes.result?.attributes!.KmsMasterKeyId).toBe('somevalue')
})
})

const newPublisher = new SqsPermissionPublisher(diContainer.cradle, {
creationConfig: {
queue: {
QueueName: queueName,
Attributes: {
KmsMasterKeyId: 'somevalue',
describe('tags update', () => {
const getTags = (queueUrl: string) =>
sqsClient.send(new ListQueueTagsCommand({ QueueUrl: queueUrl }))

it('updates existing queue tags when update is forced', async () => {
const initialTags = {
project: 'some-project',
service: 'some-service',
leftover: 'some-leftover',
}
const newTags = {
project: 'some-project',
service: 'changed-service',
cc: 'some-cc',
}

const assertResult = await assertQueue(sqsClient, {
QueueName: queueName,
tags: initialTags,
})
const preTags = await getTags(assertResult.queueUrl)
expect(preTags.Tags).toEqual(initialTags)

const newPublisher = new SqsPermissionPublisher(diContainer.cradle, {
creationConfig: {
queue: {
QueueName: queueName,
tags: newTags,
},
forceTagUpdate: true,
},
updateAttributesIfExists: true,
},
deletionConfig: {
deleteIfExists: false,
},
logMessages: true,
deletionConfig: {
deleteIfExists: false,
},
logMessages: true,
})

const sqsSpy = vi.spyOn(sqsClient, 'send')

await newPublisher.init()
expect(newPublisher.queueProps.url).toBe(
`http://sqs.eu-west-1.localstack:4566/000000000000/${queueName}`,
)

const updateCall = sqsSpy.mock.calls.find((entry) => {
return entry[0].constructor.name === 'TagQueueCommand'
})
expect(updateCall).toBeDefined()

const postTags = await getTags(assertResult.queueUrl)
expect(postTags.Tags).toEqual({
...newTags,
leftover: 'some-leftover',
})
})

const sqsSpy = vi.spyOn(sqsClient, 'send')
it('does not update existing queue tags when update is not forced', async () => {
const initialTags = {
project: 'some-project',
service: 'some-service',
leftover: 'some-leftover',
}

const assertResult = await assertQueue(sqsClient, {
QueueName: queueName,
tags: initialTags,
})
const preTags = await getTags(assertResult.queueUrl)
expect(preTags.Tags).toEqual(initialTags)

const newPublisher = new SqsPermissionPublisher(diContainer.cradle, {
creationConfig: {
queue: {
QueueName: queueName,
tags: { service: 'changed-service' },
},
},
deletionConfig: {
deleteIfExists: false,
},
logMessages: true,
})

await newPublisher.init()
expect(newPublisher.queueProps.url).toBe(
`http://sqs.eu-west-1.localstack:4566/000000000000/${queueName}`,
)
const sqsSpy = vi.spyOn(sqsClient, 'send')

const updateCall = sqsSpy.mock.calls.find((entry) => {
return entry[0].constructor.name === 'SetQueueAttributesCommand'
})
expect(updateCall).toBeUndefined()
await newPublisher.init()
expect(newPublisher.queueProps.url).toBe(
`http://sqs.eu-west-1.localstack:4566/000000000000/${queueName}`,
)

const attributes = await getQueueAttributes(sqsClient, newPublisher.queueProps.url)
const updateCall = sqsSpy.mock.calls.find((entry) => {
return entry[0].constructor.name === 'TagQueueCommand'
})
expect(updateCall).toBeUndefined()

expect(attributes.result?.attributes!.KmsMasterKeyId).toBe('somevalue')
const postTags = await getTags(assertResult.queueUrl)
expect(postTags.Tags).toEqual(initialTags)
})
})
})

Expand Down

0 comments on commit 4ad7c3b

Please sign in to comment.