|
| 1 | +import { HttpService } from '@nestjs/axios'; |
| 2 | +import { ConfigService } from '@nestjs/config'; |
| 3 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 4 | + |
| 5 | +import { S3 } from '@aws-sdk/client-s3'; |
| 6 | +import { Upload } from '@aws-sdk/lib-storage'; |
| 7 | +import type { FileUpload } from 'graphql-upload/processRequest.mjs'; |
| 8 | +import { of } from 'rxjs'; |
| 9 | +import { Readable } from 'stream'; |
| 10 | + |
| 11 | +import { CustomBadRequestException } from 'src/common/exceptions'; |
| 12 | + |
| 13 | +import { UploadService } from './upload.service'; |
| 14 | + |
| 15 | +jest.mock('@aws-sdk/client-s3'); |
| 16 | +jest.mock('@aws-sdk/lib-storage'); |
| 17 | + |
| 18 | +describe('UploadService', () => { |
| 19 | + let service: UploadService; |
| 20 | + let mockS3Send: jest.Mock; |
| 21 | + let mockHttpService: { get: jest.Mock }; |
| 22 | + |
| 23 | + const mockConfigService = { |
| 24 | + get: jest.fn((key: string) => { |
| 25 | + const config = { |
| 26 | + AWS_S3_REGION: 'ap-northeast-2', |
| 27 | + AWS_S3_ACCESS_KEY: 'test-access-key', |
| 28 | + AWS_S3_SECRET_KEY: 'test-secret-key', |
| 29 | + AWS_S3_BUCKET_NAME: 'test-bucket', |
| 30 | + }; |
| 31 | + return config[key]; |
| 32 | + }), |
| 33 | + }; |
| 34 | + |
| 35 | + const mockFileUpload: FileUpload = { |
| 36 | + filename: 'test-file.png', |
| 37 | + mimetype: 'image/png', |
| 38 | + encoding: '7bit', |
| 39 | + createReadStream: () => Readable.from(Buffer.from('test')), |
| 40 | + }; |
| 41 | + |
| 42 | + beforeAll(async () => { |
| 43 | + mockS3Send = jest.fn(); |
| 44 | + (S3 as jest.Mock).mockImplementation(() => ({ |
| 45 | + send: mockS3Send, |
| 46 | + })); |
| 47 | + |
| 48 | + mockHttpService = { |
| 49 | + get: jest.fn(), |
| 50 | + }; |
| 51 | + |
| 52 | + const module: TestingModule = await Test.createTestingModule({ |
| 53 | + providers: [ |
| 54 | + UploadService, |
| 55 | + { |
| 56 | + provide: ConfigService, |
| 57 | + useValue: mockConfigService, |
| 58 | + }, |
| 59 | + { |
| 60 | + provide: HttpService, |
| 61 | + useValue: mockHttpService, |
| 62 | + }, |
| 63 | + ], |
| 64 | + }).compile(); |
| 65 | + |
| 66 | + service = module.get<UploadService>(UploadService); |
| 67 | + }); |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + jest.clearAllMocks(); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('getLinkByKey', () => { |
| 74 | + it('should return correct S3 URL', () => { |
| 75 | + const key = 'folder/test-file.png'; |
| 76 | + const result = service.getLinkByKey(key); |
| 77 | + |
| 78 | + expect(result).toBe( |
| 79 | + 'https://test-bucket.s3.amazonaws.com/folder/test-file.png', |
| 80 | + ); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('uploadFileToS3', () => { |
| 85 | + it('should upload file successfully', async () => { |
| 86 | + const mockUploadDone = jest.fn().mockResolvedValue({}); |
| 87 | + (Upload as unknown as jest.Mock).mockImplementation(() => ({ |
| 88 | + done: mockUploadDone, |
| 89 | + })); |
| 90 | + |
| 91 | + const result = await service.uploadFileToS3({ |
| 92 | + folderName: 'testFolder', |
| 93 | + file: mockFileUpload, |
| 94 | + }); |
| 95 | + |
| 96 | + expect(result).toHaveProperty('key'); |
| 97 | + expect(result.key).toContain('testFolder/'); |
| 98 | + expect(result.key).toContain('test-file.png'); |
| 99 | + expect(mockUploadDone).toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should throw CustomBadRequestException on upload failure', async () => { |
| 103 | + (Upload as unknown as jest.Mock).mockImplementation(() => ({ |
| 104 | + done: jest.fn().mockRejectedValue(new Error('Upload failed')), |
| 105 | + })); |
| 106 | + |
| 107 | + await expect( |
| 108 | + service.uploadFileToS3({ |
| 109 | + folderName: 'testFolder', |
| 110 | + file: mockFileUpload, |
| 111 | + }), |
| 112 | + ).rejects.toThrow(CustomBadRequestException); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('deleteS3Object', () => { |
| 117 | + it('should delete file successfully', async () => { |
| 118 | + mockS3Send |
| 119 | + .mockResolvedValueOnce({ Contents: [{ Key: 'folder/file.png' }] }) |
| 120 | + .mockResolvedValueOnce({}); |
| 121 | + |
| 122 | + const result = await service.deleteS3Object('folder/file.png'); |
| 123 | + |
| 124 | + expect(result).toEqual({ success: true }); |
| 125 | + expect(mockS3Send).toHaveBeenCalledTimes(2); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should throw CustomBadRequestException when file does not exist', async () => { |
| 129 | + mockS3Send.mockResolvedValueOnce({ Contents: [] }); |
| 130 | + |
| 131 | + await expect( |
| 132 | + service.deleteS3Object('nonexistent/file.png'), |
| 133 | + ).rejects.toThrow(CustomBadRequestException); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should throw CustomBadRequestException on delete failure', async () => { |
| 137 | + mockS3Send |
| 138 | + .mockResolvedValueOnce({ Contents: [{ Key: 'folder/file.png' }] }) |
| 139 | + .mockRejectedValueOnce(new Error('Delete failed')); |
| 140 | + |
| 141 | + await expect(service.deleteS3Object('folder/file.png')).rejects.toThrow( |
| 142 | + CustomBadRequestException, |
| 143 | + ); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('listS3Object', () => { |
| 148 | + it('should list and fetch S3 objects', async () => { |
| 149 | + const mockContents = [ |
| 150 | + { Key: 'folder/file1.png' }, |
| 151 | + { Key: 'folder/file2.png' }, |
| 152 | + ]; |
| 153 | + |
| 154 | + mockS3Send.mockResolvedValueOnce({ Contents: mockContents }); |
| 155 | + mockHttpService.get |
| 156 | + .mockReturnValueOnce(of({ data: 'file1-content' })) |
| 157 | + .mockReturnValueOnce(of({ data: 'file2-content' })); |
| 158 | + |
| 159 | + const result = await service.listS3Object('folder'); |
| 160 | + |
| 161 | + expect(result).toEqual(['file1-content', 'file2-content']); |
| 162 | + expect(mockHttpService.get).toHaveBeenCalledTimes(2); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments