mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 00:39:49 +00:00
Merge pull request #14654 from strapi/fix/S3-DO-file-url-check-protocol
fix(aws-provider): add https protocol when missing in file url
This commit is contained in:
commit
bf890beab6
10
packages/providers/upload-aws-s3/jest.config.js
Normal file
10
packages/providers/upload-aws-s3/jest.config.js
Normal file
@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const baseConfig = require('../../../jest.base-config');
|
||||
const pkg = require('./package.json');
|
||||
|
||||
module.exports = {
|
||||
...baseConfig,
|
||||
displayName: pkg.name,
|
||||
roots: [__dirname],
|
||||
};
|
@ -0,0 +1,60 @@
|
||||
'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) =>
|
||||
callback(null, { Location: 'https://validurl.test' })
|
||||
);
|
||||
const file = {
|
||||
path: '/tmp/',
|
||||
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://validurl.test');
|
||||
});
|
||||
|
||||
test('Should add to the url the https protocol as it is missing', async () => {
|
||||
S3InstanceMock.upload.mockImplementationOnce((params, callback) =>
|
||||
callback(null, { Location: 'uri.test' })
|
||||
);
|
||||
const file = {
|
||||
path: '/tmp/',
|
||||
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://uri.test');
|
||||
});
|
||||
});
|
||||
});
|
@ -9,6 +9,11 @@
|
||||
const _ = require('lodash');
|
||||
const AWS = require('aws-sdk');
|
||||
|
||||
function assertUrlProtocol(url) {
|
||||
// Regex to test protocol like "http://", "https://"
|
||||
return /^\w*:\/\//.test(url);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init(config) {
|
||||
const S3 = new AWS.S3({
|
||||
@ -34,7 +39,12 @@ module.exports = {
|
||||
}
|
||||
|
||||
// set the bucket file url
|
||||
file.url = data.Location;
|
||||
if (assertUrlProtocol(data.Location)) {
|
||||
file.url = data.Location;
|
||||
} else {
|
||||
// Default protocol to https protocol
|
||||
file.url = `https://${data.Location}`;
|
||||
}
|
||||
|
||||
resolve();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user