2022-10-25 16:29:33 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const AWS = require('aws-sdk');
|
|
|
|
const awsProvider = require('../index');
|
|
|
|
|
|
|
|
jest.mock('aws-sdk');
|
|
|
|
|
|
|
|
const S3InstanceMock = {
|
|
|
|
upload: jest.fn((params, callback) => callback(null, {})),
|
|
|
|
};
|
|
|
|
|
|
|
|
AWS.S3.mockReturnValue(S3InstanceMock);
|
|
|
|
|
|
|
|
describe('AWS-S3 provider', () => {
|
|
|
|
const providerInstance = awsProvider.init({});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('upload', () => {
|
|
|
|
test('Should add url to file object', async () => {
|
|
|
|
S3InstanceMock.upload.mockImplementationOnce((params, callback) =>
|
2022-10-25 16:34:05 +02:00
|
|
|
callback(null, { Location: 'https://validurl.test/tmp/test.json' })
|
2022-10-25 16:29:33 +02:00
|
|
|
);
|
|
|
|
const file = {
|
|
|
|
path: '/tmp/',
|
|
|
|
hash: 'test',
|
2022-10-25 16:34:05 +02:00
|
|
|
ext: '.json',
|
2022-10-25 16:29:33 +02:00
|
|
|
mime: 'application/json',
|
|
|
|
buffer: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
await providerInstance.upload(file);
|
|
|
|
|
|
|
|
expect(S3InstanceMock.upload).toBeCalled();
|
|
|
|
expect(file.url).toBeDefined();
|
2022-10-25 16:34:05 +02:00
|
|
|
expect(file.url).toEqual('https://validurl.test/tmp/test.json');
|
2022-10-25 16:29:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should add to the url the https protocol as it is missing', async () => {
|
|
|
|
S3InstanceMock.upload.mockImplementationOnce((params, callback) =>
|
2022-10-25 16:34:05 +02:00
|
|
|
callback(null, { Location: 'uri.test/tmp/test.json' })
|
2022-10-25 16:29:33 +02:00
|
|
|
);
|
|
|
|
const file = {
|
|
|
|
path: '/tmp/',
|
|
|
|
hash: 'test',
|
2022-10-25 16:34:05 +02:00
|
|
|
ext: '.json',
|
2022-10-25 16:29:33 +02:00
|
|
|
mime: 'application/json',
|
|
|
|
buffer: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
await providerInstance.upload(file);
|
|
|
|
|
|
|
|
expect(S3InstanceMock.upload).toBeCalled();
|
|
|
|
expect(file.url).toBeDefined();
|
2022-10-25 16:34:05 +02:00
|
|
|
expect(file.url).toEqual('https://uri.test/tmp/test.json');
|
2022-10-25 16:29:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should prepend the baseUrl to the url of the file object', async () => {
|
|
|
|
const providerInstance = awsProvider.init({ baseUrl: 'https://cdn.test' });
|
|
|
|
|
|
|
|
S3InstanceMock.upload.mockImplementationOnce((params, callback) =>
|
|
|
|
callback(null, { Location: 'https://validurl.test' })
|
|
|
|
);
|
|
|
|
const file = {
|
|
|
|
path: 'tmp/test',
|
|
|
|
hash: 'test',
|
|
|
|
ext: '.json',
|
|
|
|
mime: 'application/json',
|
|
|
|
buffer: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
await providerInstance.upload(file);
|
|
|
|
|
|
|
|
expect(S3InstanceMock.upload).toBeCalled();
|
|
|
|
expect(file.url).toBeDefined();
|
|
|
|
expect(file.url).toEqual('https://cdn.test/tmp/test/test.json');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should prepend the baseUrl and rootPath to the url of the file object', async () => {
|
2022-10-25 16:34:05 +02:00
|
|
|
const providerInstance = awsProvider.init({
|
|
|
|
baseUrl: 'https://cdn.test',
|
|
|
|
rootPath: 'dir/dir2',
|
|
|
|
});
|
2022-10-25 16:29:33 +02:00
|
|
|
|
|
|
|
S3InstanceMock.upload.mockImplementationOnce((params, callback) =>
|
|
|
|
callback(null, { Location: 'https://validurl.test' })
|
|
|
|
);
|
|
|
|
const file = {
|
|
|
|
path: 'tmp/test',
|
|
|
|
hash: 'test',
|
|
|
|
ext: '.json',
|
|
|
|
mime: 'application/json',
|
|
|
|
buffer: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
await providerInstance.upload(file);
|
|
|
|
|
|
|
|
expect(S3InstanceMock.upload).toBeCalled();
|
|
|
|
expect(file.url).toBeDefined();
|
2022-10-25 16:34:05 +02:00
|
|
|
expect(file.url).toEqual('https://cdn.test/dir/dir2/tmp/test/test.json');
|
2022-10-25 16:29:33 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|