|
| 1 | +import { ImageConsumer } from '../../../../src/common/image/application/image.consumer'; |
| 2 | +import { ImageService } from '../../../../src/common/image/application/image.service'; |
| 3 | +import { Message } from 'aws-sdk/clients/sqs'; |
| 4 | +import { BadRequestException } from '@nestjs/common/exceptions'; |
| 5 | +import { FakeConfigService } from '../../../mock/fake.config.service'; |
| 6 | +import { FakeCloudStorage } from '../../../mock/fake.cloud-storage'; |
| 7 | +import { FakeImageRepository } from '../../../mock/fake.image.repository'; |
| 8 | + |
| 9 | +describe('ImageConsumer', () => { |
| 10 | + let consumer: ImageConsumer; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + consumer = new ImageConsumer( |
| 14 | + new ImageService(new FakeCloudStorage(), new FakeImageRepository()), |
| 15 | + new FakeConfigService(), |
| 16 | + ); |
| 17 | + }); |
| 18 | + |
| 19 | + test('consumeMessage', async () => { |
| 20 | + // given |
| 21 | + const uuid = 'test-uuid'; |
| 22 | + const env = `test`; |
| 23 | + |
| 24 | + const message = { |
| 25 | + Body: JSON.stringify({ |
| 26 | + version: '2.0', |
| 27 | + id: 'test-id', |
| 28 | + source: 'aws:s3', |
| 29 | + account: '123456789012', |
| 30 | + time: '2023-10-01T12:00:00Z', |
| 31 | + region: 'us-east-1', |
| 32 | + resources: ['arn:aws:s3:::example-bucket'], |
| 33 | + detail: { |
| 34 | + version: '2.0', |
| 35 | + bucket: { |
| 36 | + name: 'example-bucket', |
| 37 | + }, |
| 38 | + object: { |
| 39 | + key: `${env}/images/${uuid}/image.jpg`, |
| 40 | + size: 1024, |
| 41 | + etag: 'd41d8cd98f00b204e9800998ecf8427e', |
| 42 | + sequencer: '0055AED6DCD90281E5', |
| 43 | + }, |
| 44 | + requester: 'arn:aws:iam::123456789012:user/test-user', |
| 45 | + reason: 'PutObject', |
| 46 | + }, |
| 47 | + }), |
| 48 | + } as unknown as Message; |
| 49 | + |
| 50 | + // when |
| 51 | + const result = await consumer.consumeMessage(message); |
| 52 | + |
| 53 | + // then |
| 54 | + console.table(result); |
| 55 | + expect(result).toEqual({ |
| 56 | + imageUrl: `https://image.mgmg.life/${env}/images/${uuid}/image.jpg`, |
| 57 | + uuid: uuid, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + test('consumeMessage with empty message body', async () => { |
| 62 | + // given |
| 63 | + const message = { |
| 64 | + Body: '', |
| 65 | + } as unknown as Message; |
| 66 | + |
| 67 | + // when && then |
| 68 | + await expect(consumer.consumeMessage(message)).rejects.toThrow( |
| 69 | + BadRequestException, |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + test('Message 안 object key에 대해 /images 와 파일명 사이에 uuid가 존재하지 않으면 BadRequestException 발생 ', async () => { |
| 74 | + // given |
| 75 | + const env = 'test'; |
| 76 | + const message = { |
| 77 | + Body: JSON.stringify({ |
| 78 | + version: '2.0', |
| 79 | + id: 'test-id', |
| 80 | + source: 'aws:s3', |
| 81 | + account: '123456789012', |
| 82 | + time: '2023-10-01T12:00:00Z', |
| 83 | + region: 'us-east-1', |
| 84 | + resources: ['arn:aws:s3:::example-bucket'], |
| 85 | + detail: { |
| 86 | + version: '2.0', |
| 87 | + bucket: { |
| 88 | + name: 'example-bucket', |
| 89 | + }, |
| 90 | + object: { |
| 91 | + key: `${env}/images//image.jpg`, |
| 92 | + size: 1024, |
| 93 | + etag: 'd41d8cd98f00b204e9800998ecf8427e', |
| 94 | + sequencer: '0055AED6DCD90281E5', |
| 95 | + }, |
| 96 | + requester: 'arn:aws:iam::123456789012:user/test-user', |
| 97 | + reason: 'PutObject', |
| 98 | + }, |
| 99 | + }), |
| 100 | + } as unknown as Message; |
| 101 | + |
| 102 | + // when && then |
| 103 | + await expect(consumer.consumeMessage(message)).rejects.toThrow( |
| 104 | + BadRequestException, |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
0 commit comments