2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
2021-10-06 13:12:21 +03:00
|
|
|
const urlUtils = require('../../../../../core/shared/url-utils');
|
2017-09-05 15:13:22 +03:00
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
// Stuff we are testing
|
2021-10-06 13:12:21 +03:00
|
|
|
const storageUtils = require('../../../../../core/server/adapters/storage/utils');
|
2017-09-05 15:13:22 +03:00
|
|
|
|
|
|
|
describe('storage utils', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let urlForStub;
|
|
|
|
let urlGetSubdirStub;
|
2017-09-05 15:13:22 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
urlForStub = sinon.stub();
|
2017-09-05 15:13:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2017-09-05 15:13:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('fn: getLocalFileStoragePath', function () {
|
|
|
|
it('should return local file storage path for absolute URL', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const url = 'http://myblog.com/content/images/2017/07/ghost-logo.png';
|
|
|
|
let result;
|
2017-09-05 15:13:22 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlGetSubdirStub.returns('');
|
|
|
|
|
|
|
|
result = storageUtils.getLocalFileStoragePath(url);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal('/2017/07/ghost-logo.png');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return local file storage path for absolute URL with subdirectory', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const url = 'http://myblog.com/blog/content/images/2017/07/ghost-logo.png';
|
|
|
|
let result;
|
2017-09-05 15:13:22 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlGetSubdirStub.returns('/blog');
|
|
|
|
|
|
|
|
result = storageUtils.getLocalFileStoragePath(url);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal('/2017/07/ghost-logo.png');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return local file storage path for relative URL', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const filePath = '/content/images/2017/07/ghost-logo.png';
|
|
|
|
let result;
|
2017-09-05 15:13:22 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlGetSubdirStub.returns('');
|
|
|
|
|
|
|
|
result = storageUtils.getLocalFileStoragePath(filePath);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal('/2017/07/ghost-logo.png');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return local file storage path for relative URL with subdirectory', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const filePath = '/blog/content/images/2017/07/ghost-logo.png';
|
|
|
|
let result;
|
2017-09-05 15:13:22 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlGetSubdirStub.returns('/blog');
|
|
|
|
|
|
|
|
result = storageUtils.getLocalFileStoragePath(filePath);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal('/2017/07/ghost-logo.png');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not sanitize URL if not local file storage', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const url = 'http://example-blog.com/ghost-logo.png';
|
|
|
|
let result;
|
2017-09-05 15:13:22 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-09-05 15:13:22 +03:00
|
|
|
urlGetSubdirStub.returns('');
|
|
|
|
|
|
|
|
result = storageUtils.getLocalFileStoragePath(url);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal('http://example-blog.com/ghost-logo.png');
|
|
|
|
});
|
|
|
|
});
|
2017-11-09 13:11:54 +03:00
|
|
|
|
|
|
|
describe('fn: isLocalImage', function () {
|
|
|
|
it('should return true when absolute URL and local file', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const url = 'http://myblog.com/content/images/2017/07/ghost-logo.png';
|
|
|
|
let result;
|
2017-11-09 13:11:54 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlGetSubdirStub.returns('');
|
|
|
|
|
|
|
|
result = storageUtils.isLocalImage(url);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true when absolute URL with subdirectory and local file', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const url = 'http://myblog.com/blog/content/images/2017/07/ghost-logo.png';
|
|
|
|
let result;
|
2017-11-09 13:11:54 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlGetSubdirStub.returns('/blog');
|
|
|
|
|
|
|
|
result = storageUtils.isLocalImage(url);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true when relative URL and local file', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const url = '/content/images/2017/07/ghost-logo.png';
|
|
|
|
let result;
|
2017-11-09 13:11:54 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlGetSubdirStub.returns('');
|
|
|
|
|
|
|
|
result = storageUtils.isLocalImage(url);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true when relative URL and local file', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const url = '/blog/content/images/2017/07/ghost-logo.png';
|
|
|
|
let result;
|
2017-11-09 13:11:54 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlGetSubdirStub.returns('/blog');
|
|
|
|
|
|
|
|
result = storageUtils.isLocalImage(url);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false when no local file', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const url = 'http://somewebsite.com/ghost-logo.png';
|
|
|
|
let result;
|
2017-11-09 13:11:54 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
urlForStub = sinon.stub(urlUtils, 'urlFor');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlForStub.withArgs('home').returns('http://myblog.com/');
|
2019-06-18 16:13:55 +03:00
|
|
|
urlGetSubdirStub = sinon.stub(urlUtils, 'getSubdir');
|
2017-11-09 13:11:54 +03:00
|
|
|
urlGetSubdirStub.returns('');
|
|
|
|
|
|
|
|
result = storageUtils.isLocalImage(url);
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.equal(false);
|
|
|
|
});
|
|
|
|
});
|
2017-09-05 15:13:22 +03:00
|
|
|
});
|