Skip to content

Instantly share code, notes, and snippets.

@f1lander
Last active March 3, 2020 01:33
Show Gist options
  • Save f1lander/204ae46878f930f08d5456af622083e8 to your computer and use it in GitHub Desktop.
Save f1lander/204ae46878f930f08d5456af622083e8 to your computer and use it in GitHub Desktop.
const { S3 } = require('aws-sdk')
jest.mock('aws-sdk');
// see the previous gist file I created here
// https://gist.github.com/f1lander/287cf20fb8dbfcce24a5c3e867346788
import s3Utils from './s3-utils';
// I set this for debug
jest.setTimeout(45000);
describe('S3 Utils', () => {
afterAll(() => {
jest.clearAllMocks();
});
test('should return headObject result', async () => {
const response = {
"x-amz-id-2": "ef8yU9AS1ed4OpIszj7UDNEHGran",
"x-amz-request-id": "318BC8BC143432E5",
"x-amz-version-id": "3HL4kqtJlcpXroDTDmjVBH40Nrjfkd",
"Date": "Wed, 28 Oct 2009 22:32:00 GMT",
"Last-Modified": "Sun, 1 Jan 2006 12:00:00 GMT",
"ETag": "fba9dede5f27731c9771645a39863328",
"Content-Length": 434234,
"Content-Type": "text/plain",
"Connection": "close",
"Server": "AmazonS3",
};
const s3HeadObject = jest.fn().mockReturnValue({
promise: jest.fn().mockResolvedValue(response)
})
S3.mockImplementation(() => ({
headObject: s3HeadObject
}))
const s3 = new s3Utils();
var params = {
Bucket: "bucket",
Key: "HappyFaceCopy.jpg"
};
const result = await s3.headObject(params);
expect(result).toEqual(response);
});
test('should return copyObject result', async () => {
const response = {
CopyObjectResult: {
ETag: "6805f2cfc46c0f04559748bb039d69ae",
LastModified: "Wed, 28 Oct 2009 22:32:00 GMT",
}
}
const _copyObject = jest.fn().mockReturnValue({
promise: jest.fn().mockResolvedValue(response)
})
S3.mockImplementation(() => ({
copyObject: _copyObject
}))
const s3 = new s3Utils();
var params = {
Bucket: "destinationbucket",
CopySource: "/sourcebucket/HappyFacejpg",
Key: "HappyFaceCopyjpg"
};
const result = await s3.copyObject(params);
expect(result).toEqual(response);
});
test('should return getSignedUrl result', async () => {
const response = {
AcceptRanges: "bytes",
ContentLength: 3191,
ContentType: "image/jpeg",
ETag: "6805f2cfc46c0f04559748bb039d69ae",
LastModified: "Wed, 28 Oct 2009 22:32:00 GMT",
Metadata: {
},
VersionId: "null"
}
const _getSignedUrl = jest.fn().mockReturnValue(response);
S3.mockImplementation(() => ({
getSignedUrl: _getSignedUrl
}));
const s3 = new s3Utils();
var params = {
Bucket: "bucket",
Key: "HappyFaceCopy.jpg"
};
const result = s3.getSignedUrl(params);
expect(result).toEqual(response);
});
test('should return createPresignedPost result', async () => {
const response = {
url: "http://github.com"
}
const _createPresignedPost = jest.fn().mockReturnValue(response);
S3.mockImplementation(() => ({
createPresignedPost: _createPresignedPost
}));
const s3 = new s3Utils();
var params = {
Bucket: "bucket",
Key: "HappyFaceCopy.jpg"
};
const result = s3.createPresignedPost(params);
expect(result).toEqual(response);
});
test('should return getObjectTagging result', async () => {
const response = {
TagSet: [
{ Key: 'av-status', Value: undefined },
],
};
const _getObjectTagging = jest.fn().mockReturnValue({
promise: jest.fn().mockResolvedValue(response)
})
S3.mockImplementation(() => ({
getObjectTagging: _getObjectTagging
}));
const s3 = new s3Utils();
var params = {
Bucket: "bucket",
Key: "HappyFaceCopy.jpg"
};
const result = await s3.getTaggingObjects(params);
expect(result).toEqual(response);
});
test('should return deleteObject result', async () => {
const response = {
"x-amz-id-2": "LriYPLdmOdAiIfgSm/F1YsViT1LW94/xUQxMsF7xiEb1a0wiIOIxl+zbwZ163pt7",
"x-amz-request-id": "0A49CE4060975EAC",
"Date": "Wed, 12 Oct 2009 17:50:00 GMT",
"Content-Length": 0,
"Connection": "close",
"Server": "AmazonS3"
}
const _deleteObject = jest.fn().mockReturnValue({
promise: jest.fn().mockResolvedValue(response)
});
S3.mockImplementation(() => ({
deleteObject: _deleteObject
}));
const s3 = new s3Utils();
var params = {
Bucket: "bucket",
Key: "HappyFaceCopy.jpg"
};
const result = await s3.deleteObject(params);
expect(result).toEqual(response);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment