-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: durability checks for create & complete multipart
- Loading branch information
1 parent
7876916
commit c018013
Showing
14 changed files
with
620 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
packages/storage/__tests__/providers/s3/utils/client/S3/utils/integrityHelpers.test.ts
This file was deleted.
Oops, something went wrong.
143 changes: 143 additions & 0 deletions
143
packages/storage/__tests__/providers/s3/utils/client/s3Data/completeMultipartUpload.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { HttpResponse } from '@aws-amplify/core/internals/aws-client-utils'; | ||
|
||
import { s3TransferHandler } from '../../../../../../src/providers/s3/utils/client/runtime/s3TransferHandler/fetch'; | ||
import { completeMultipartUpload } from '../../../../../../src/providers/s3/utils/client/s3data'; | ||
import { validateObjectUrl } from '../../../../../../src/providers/s3/utils/validateObjectUrl'; | ||
import { validateMultipartUploadXML } from '../../../../../../src/providers/s3/utils/validateMultipartUploadXML'; | ||
import { | ||
DEFAULT_RESPONSE_HEADERS, | ||
defaultConfig, | ||
expectedMetadata, | ||
} from '../S3/cases/shared'; | ||
import { IntegrityError } from '../../../../../../src/errors/IntegrityError'; | ||
|
||
jest.mock('../../../../../../src/providers/s3/utils/validateObjectUrl'); | ||
jest.mock( | ||
'../../../../../../src/providers/s3/utils/validateMultipartUploadXML', | ||
); | ||
jest.mock( | ||
'../../../../../../src/providers/s3/utils/client/runtime/s3TransferHandler/fetch', | ||
); | ||
|
||
const mockS3TransferHandler = s3TransferHandler as jest.Mock; | ||
const mockBinaryResponse = ({ | ||
status, | ||
headers, | ||
body, | ||
}: { | ||
status: number; | ||
headers: Record<string, string>; | ||
body: string; | ||
}): HttpResponse => { | ||
const responseBody = { | ||
json: async (): Promise<any> => { | ||
throw new Error( | ||
'Parsing response to JSON is not implemented. Please use response.text() instead.', | ||
); | ||
}, | ||
blob: async () => new Blob([body], { type: 'plain/text' }), | ||
text: async () => body, | ||
} as HttpResponse['body']; | ||
|
||
return { | ||
statusCode: status, | ||
headers, | ||
body: responseBody, | ||
} as any; | ||
}; | ||
|
||
const completeMultipartUploadSuccessResponse = { | ||
status: 200, | ||
headers: { | ||
...DEFAULT_RESPONSE_HEADERS, | ||
'x-amz-version-id': 'versionId', | ||
etag: 'etag', | ||
}, | ||
body: '', | ||
}; | ||
|
||
describe('completeMultipartUploadSerializer', () => { | ||
const mockValidateObjectUrl = jest.mocked(validateObjectUrl); | ||
const mockValidateMultipartUploadXML = jest.mocked( | ||
validateMultipartUploadXML, | ||
); | ||
beforeEach(() => { | ||
mockS3TransferHandler.mockReset(); | ||
}); | ||
|
||
it('should pass when objectUrl and multipartUploadXML is durable', async () => { | ||
expect.assertions(1); | ||
mockS3TransferHandler.mockResolvedValue( | ||
mockBinaryResponse(completeMultipartUploadSuccessResponse as any), | ||
); | ||
const output = await completeMultipartUpload(defaultConfig, { | ||
Bucket: 'bucket', | ||
Key: 'key', | ||
UploadId: 'uploadId', | ||
MultipartUpload: { | ||
Parts: [ | ||
{ | ||
ETag: 'etag', | ||
PartNumber: 1, | ||
}, | ||
], | ||
}, | ||
}); | ||
console.log(output); | ||
expect(output).toEqual({ | ||
$metadata: expect.objectContaining(expectedMetadata), | ||
}); | ||
}); | ||
|
||
it('should fail when objectUrl is NOT durable', async () => { | ||
expect.assertions(1); | ||
mockS3TransferHandler.mockResolvedValue( | ||
mockBinaryResponse(completeMultipartUploadSuccessResponse as any), | ||
); | ||
const integrityError = new IntegrityError(); | ||
mockValidateObjectUrl.mockImplementationOnce(() => { | ||
throw integrityError; | ||
}); | ||
expect( | ||
completeMultipartUpload(defaultConfig, { | ||
Bucket: 'bucket', | ||
Key: 'key', | ||
UploadId: 'uploadId', | ||
MultipartUpload: { | ||
Parts: [ | ||
{ | ||
ETag: 'etag', | ||
PartNumber: 1, | ||
}, | ||
], | ||
}, | ||
}), | ||
).rejects.toThrow(integrityError); | ||
}); | ||
|
||
it('should fail when multipartUploadXML is NOT durable', async () => { | ||
expect.assertions(1); | ||
mockS3TransferHandler.mockResolvedValue( | ||
mockBinaryResponse(completeMultipartUploadSuccessResponse as any), | ||
); | ||
const integrityError = new IntegrityError(); | ||
mockValidateMultipartUploadXML.mockImplementationOnce(() => { | ||
throw integrityError; | ||
}); | ||
expect( | ||
completeMultipartUpload(defaultConfig, { | ||
Bucket: 'bucket', | ||
Key: 'key', | ||
UploadId: 'uploadId', | ||
MultipartUpload: { | ||
Parts: [ | ||
{ | ||
ETag: 'etag', | ||
PartNumber: 1, | ||
}, | ||
], | ||
}, | ||
}), | ||
).rejects.toThrow(integrityError); | ||
}); | ||
}); |
92 changes: 92 additions & 0 deletions
92
packages/storage/__tests__/providers/s3/utils/client/s3Data/createMultipartUpload.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { HttpResponse } from '@aws-amplify/core/internals/aws-client-utils'; | ||
|
||
import { s3TransferHandler } from '../../../../../../src/providers/s3/utils/client/runtime/s3TransferHandler/fetch'; | ||
import { createMultipartUpload } from '../../../../../../src/providers/s3/utils/client/s3data'; | ||
import { validateObjectUrl } from '../../../../../../src/providers/s3/utils/validateObjectUrl'; | ||
import { | ||
DEFAULT_RESPONSE_HEADERS, | ||
defaultConfig, | ||
expectedMetadata, | ||
} from '../S3/cases/shared'; | ||
import { IntegrityError } from '../../../../../../src/errors/IntegrityError'; | ||
|
||
jest.mock('../../../../../../src/providers/s3/utils/validateObjectUrl'); | ||
jest.mock( | ||
'../../../../../../src/providers/s3/utils/client/runtime/s3TransferHandler/fetch', | ||
); | ||
|
||
const mockS3TransferHandler = s3TransferHandler as jest.Mock; | ||
const mockBinaryResponse = ({ | ||
status, | ||
headers, | ||
body, | ||
}: { | ||
status: number; | ||
headers: Record<string, string>; | ||
body: string; | ||
}): HttpResponse => { | ||
const responseBody = { | ||
json: async (): Promise<any> => { | ||
throw new Error( | ||
'Parsing response to JSON is not implemented. Please use response.text() instead.', | ||
); | ||
}, | ||
blob: async () => new Blob([body], { type: 'plain/text' }), | ||
text: async () => body, | ||
} as HttpResponse['body']; | ||
|
||
return { | ||
statusCode: status, | ||
headers, | ||
body: responseBody, | ||
} as any; | ||
}; | ||
|
||
const createMultipartUploadSuccessResponse = { | ||
status: 200, | ||
headers: { | ||
...DEFAULT_RESPONSE_HEADERS, | ||
'x-amz-version-id': 'versionId', | ||
etag: 'etag', | ||
}, | ||
body: '', | ||
}; | ||
|
||
describe('createMultipartUploadSerializer', () => { | ||
const mockIsValidObjectUrl = jest.mocked(validateObjectUrl); | ||
beforeEach(() => { | ||
mockS3TransferHandler.mockReset(); | ||
}); | ||
|
||
it('should pass when objectUrl is durable', async () => { | ||
expect.assertions(1); | ||
mockS3TransferHandler.mockResolvedValue( | ||
mockBinaryResponse(createMultipartUploadSuccessResponse as any), | ||
); | ||
const output = await createMultipartUpload(defaultConfig, { | ||
Bucket: 'bucket', | ||
Key: 'key', | ||
}); | ||
console.log(output); | ||
expect(output).toEqual({ | ||
$metadata: expect.objectContaining(expectedMetadata), | ||
}); | ||
}); | ||
|
||
it('should fail when objectUrl is NOT durable', async () => { | ||
expect.assertions(1); | ||
mockS3TransferHandler.mockResolvedValue( | ||
mockBinaryResponse(createMultipartUploadSuccessResponse as any), | ||
); | ||
const integrityError = new IntegrityError(); | ||
mockIsValidObjectUrl.mockImplementationOnce(() => { | ||
throw integrityError; | ||
}); | ||
expect( | ||
createMultipartUpload(defaultConfig, { | ||
Bucket: 'bucket', | ||
Key: 'key', | ||
}), | ||
).rejects.toThrow(integrityError); | ||
}); | ||
}); |
71 changes: 71 additions & 0 deletions
71
packages/storage/__tests__/providers/s3/utils/client/utils/integrityHelpers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
bothNilOrEqual, | ||
isEqual, | ||
isNil, | ||
isObject, | ||
} from '../../../../../../src/providers/s3/utils/client/utils/integrityHelpers'; | ||
|
||
describe('isNil', () => { | ||
it.each([ | ||
['undefined', undefined, true], | ||
['null', null, true], | ||
['object', {}, false], | ||
['string', 'string', false], | ||
['empty string', '', false], | ||
['false', false, false], | ||
])('should correctly evaluate %s', (_, input, expected) => { | ||
expect(isNil(input)).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe('bothNilorEqual', () => { | ||
it.each([ | ||
['both undefined', undefined, undefined, true], | ||
['both null', null, null, true], | ||
['null and undefined', null, undefined, true], | ||
['both equal', 'mock', 'mock', true], | ||
['undefined and falsy', undefined, '', false], | ||
['truthy and null', 'mock', null, false], | ||
['different strings', 'mock-1', 'mock-2', false], | ||
])( | ||
'should correctly compare %s', | ||
(_, original: any, output: any, expected) => { | ||
expect(bothNilOrEqual(original, output)).toBe(expected); | ||
}, | ||
); | ||
}); | ||
|
||
describe('Integrity Helpers Tests', () => { | ||
describe('isObjectLike', () => { | ||
// Generate all test cases for isObjectLike function here | ||
test.each([ | ||
[{}, true], | ||
[{ a: 1 }, true], | ||
[[1, 2, 3], false], | ||
[null, false], | ||
[undefined, false], | ||
['', false], | ||
[1, false], | ||
])('isObjectLike(%p) = %p', (value, expected) => { | ||
expect(isObject(value)).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe('isEqual', () => { | ||
test.each([ | ||
[1, 1, true], | ||
[1, 2, false], | ||
[1, '1', false], | ||
['1', '1', true], | ||
['1', '2', false], | ||
[{ a: 1 }, { a: 1 }, true], | ||
[{ a: 1 }, { a: 2 }, false], | ||
[{ a: 1 }, { b: 1 }, false], | ||
[[1, 2], [1, 2], true], | ||
[[1, 2], [2, 1], false], | ||
[[1, 2], [1, 2, 3], false], | ||
])('isEqual(%p, %p) = %p', (a, b, expected) => { | ||
expect(isEqual(a, b)).toBe(expected); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.