|
| 1 | +'use strict' |
| 2 | + |
| 3 | +import { |
| 4 | + CompleteMultipartUploadCommand, |
| 5 | + CopyObjectCommand, |
| 6 | + CreateMultipartUploadCommand, |
| 7 | + S3Client, |
| 8 | + UploadPartCopyCommand, |
| 9 | +} from '@aws-sdk/client-s3' |
| 10 | +import { ObjectBackup } from '../storage/backend/s3/backup' |
| 11 | + |
| 12 | +jest.mock('@aws-sdk/client-s3', () => { |
| 13 | + const originalModule = jest.requireActual('@aws-sdk/client-s3') |
| 14 | + return { |
| 15 | + ...originalModule, |
| 16 | + S3Client: jest.fn().mockImplementation(() => ({ |
| 17 | + send: jest.fn(), |
| 18 | + })), |
| 19 | + } |
| 20 | +}) |
| 21 | + |
| 22 | +const encodeCopySourceByPathToken = (bucket: string, key: string) => |
| 23 | + `${encodeURIComponent(bucket)}/${key |
| 24 | + .split('/') |
| 25 | + .map((pathToken) => encodeURIComponent(pathToken)) |
| 26 | + .join('/')}` |
| 27 | + |
| 28 | +describe('ObjectBackup', () => { |
| 29 | + let mockSend: jest.Mock |
| 30 | + let client: S3Client |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + jest.clearAllMocks() |
| 34 | + mockSend = jest.fn() |
| 35 | + ;(S3Client as jest.Mock).mockImplementation(() => ({ |
| 36 | + send: mockSend, |
| 37 | + })) |
| 38 | + client = new S3Client({}) as unknown as S3Client |
| 39 | + }) |
| 40 | + |
| 41 | + test('singleCopy preserves path separators for Unicode source keys', async () => { |
| 42 | + mockSend.mockResolvedValue({}) |
| 43 | + |
| 44 | + const sourceKey = 'folder one/일이삼/子目录/🙂?#%.png' |
| 45 | + const destinationKey = 'backup/folder/복사본.png' |
| 46 | + |
| 47 | + const backup = new ObjectBackup(client, { |
| 48 | + sourceBucket: 'source-bucket', |
| 49 | + sourceKey, |
| 50 | + destinationBucket: 'backup-bucket', |
| 51 | + destinationKey, |
| 52 | + size: 1024, |
| 53 | + }) |
| 54 | + |
| 55 | + await backup.backup() |
| 56 | + |
| 57 | + expect(mockSend).toHaveBeenCalledTimes(1) |
| 58 | + const command = mockSend.mock.calls[0][0] as CopyObjectCommand |
| 59 | + expect(command).toBeInstanceOf(CopyObjectCommand) |
| 60 | + expect(command.input.CopySource).toBe(encodeCopySourceByPathToken('source-bucket', sourceKey)) |
| 61 | + expect(command.input.CopySource).toContain('source-bucket/folder%20one/') |
| 62 | + expect(command.input.CopySource).not.toContain('%2Fsource-bucket%2F') |
| 63 | + expect(command.input.CopySource).not.toContain('source-bucket%2F') |
| 64 | + }) |
| 65 | + |
| 66 | + test('multipartCopy preserves path separators for Unicode source keys', async () => { |
| 67 | + mockSend.mockImplementation((command: unknown) => { |
| 68 | + if (command instanceof CreateMultipartUploadCommand) { |
| 69 | + return Promise.resolve({ UploadId: 'upload-id' }) |
| 70 | + } |
| 71 | + |
| 72 | + if (command instanceof UploadPartCopyCommand) { |
| 73 | + return Promise.resolve({ |
| 74 | + CopyPartResult: { |
| 75 | + ETag: `"etag-${command.input.PartNumber}"`, |
| 76 | + }, |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + if (command instanceof CompleteMultipartUploadCommand) { |
| 81 | + return Promise.resolve({}) |
| 82 | + } |
| 83 | + |
| 84 | + return Promise.resolve({}) |
| 85 | + }) |
| 86 | + |
| 87 | + const sourceKey = 'folder one/일이삼/子目录/🙂?#%.png' |
| 88 | + const destinationKey = 'backup/folder/복사본.png' |
| 89 | + const partSize = 5 * 1024 * 1024 * 1024 |
| 90 | + const backup = new ObjectBackup(client, { |
| 91 | + sourceBucket: 'source-bucket', |
| 92 | + sourceKey, |
| 93 | + destinationBucket: 'backup-bucket', |
| 94 | + destinationKey, |
| 95 | + size: partSize + 1024, |
| 96 | + }) |
| 97 | + |
| 98 | + await backup.backup() |
| 99 | + |
| 100 | + const uploadPartCommands = mockSend.mock.calls |
| 101 | + .map(([command]) => command) |
| 102 | + .filter( |
| 103 | + (command): command is UploadPartCopyCommand => command instanceof UploadPartCopyCommand |
| 104 | + ) |
| 105 | + |
| 106 | + expect(uploadPartCommands).toHaveLength(2) |
| 107 | + for (const command of uploadPartCommands) { |
| 108 | + expect(command.input.CopySource).toBe(encodeCopySourceByPathToken('source-bucket', sourceKey)) |
| 109 | + expect(command.input.CopySource).toContain('source-bucket/folder%20one/') |
| 110 | + expect(command.input.CopySource).not.toContain('%2Fsource-bucket%2F') |
| 111 | + expect(command.input.CopySource).not.toContain('source-bucket%2F') |
| 112 | + } |
| 113 | + }) |
| 114 | +}) |
0 commit comments