2018-08-30 19:30:36 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
2021-10-06 13:12:21 +03:00
|
|
|
const configUtils = require('../../../../../utils/configUtils');
|
2020-03-25 20:33:03 +03:00
|
|
|
const imageTransform = require('@tryghost/image-transform');
|
2021-06-15 17:36:27 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-10-06 13:12:21 +03:00
|
|
|
const normalize = require('../../../../../../core/server/web/api/middleware/normalize-image');
|
2018-08-30 19:30:36 +03:00
|
|
|
|
|
|
|
describe('normalize', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let res;
|
|
|
|
let req;
|
2018-08-30 19:30:36 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = {
|
|
|
|
file: {
|
|
|
|
name: 'test',
|
2018-09-06 12:42:42 +03:00
|
|
|
path: '/test/path',
|
|
|
|
ext: '.jpg'
|
2018-08-30 19:30:36 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-25 20:33:03 +03:00
|
|
|
sinon.stub(imageTransform, 'resizeFromPath');
|
|
|
|
sinon.stub(logging, 'error');
|
2018-08-30 19:30:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-08-30 19:30:36 +03:00
|
|
|
configUtils.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do manipulation by default', function (done) {
|
2020-03-25 20:33:03 +03:00
|
|
|
imageTransform.resizeFromPath.resolves();
|
2018-08-30 19:30:36 +03:00
|
|
|
|
2018-09-06 12:42:42 +03:00
|
|
|
normalize(req, res, function () {
|
2020-03-25 20:33:03 +03:00
|
|
|
imageTransform.resizeFromPath.calledOnce.should.be.true();
|
2018-08-30 19:30:36 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-25 20:33:03 +03:00
|
|
|
it('should add files array to request object with original and resized files', function (done) {
|
|
|
|
imageTransform.resizeFromPath.resolves();
|
2018-08-30 19:30:36 +03:00
|
|
|
|
2018-09-06 12:42:42 +03:00
|
|
|
normalize(req, res, function () {
|
2018-08-30 19:30:36 +03:00
|
|
|
req.files.length.should.be.equal(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not do manipulation without resize flag set', function (done) {
|
|
|
|
configUtils.set({
|
|
|
|
imageOptimization: {
|
2019-07-05 14:40:43 +03:00
|
|
|
resize: false
|
2018-08-30 19:30:36 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-06 12:42:42 +03:00
|
|
|
normalize(req, res, function () {
|
2020-03-25 20:33:03 +03:00
|
|
|
imageTransform.resizeFromPath.called.should.be.false();
|
2018-08-30 19:30:36 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-25 20:33:03 +03:00
|
|
|
it('should not create files array when resizing fails', function (done) {
|
|
|
|
imageTransform.resizeFromPath.rejects();
|
2018-08-30 19:30:36 +03:00
|
|
|
|
2019-07-05 14:40:43 +03:00
|
|
|
normalize(req, res, () => {
|
2020-03-25 20:33:03 +03:00
|
|
|
logging.error.calledOnce.should.be.true();
|
2018-08-30 19:30:36 +03:00
|
|
|
req.file.should.not.be.equal(undefined);
|
|
|
|
should.not.exist(req.files);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-09-06 12:42:42 +03:00
|
|
|
|
|
|
|
['.gif', '.svg', '.svgz'].forEach(function (extension) {
|
2020-03-25 20:33:03 +03:00
|
|
|
it(`should skip resizing when file extension is ${extension}`, function (done) {
|
2018-09-06 12:42:42 +03:00
|
|
|
req.file.ext = extension;
|
|
|
|
normalize(req, res, function () {
|
|
|
|
req.file.should.not.be.equal(undefined);
|
|
|
|
should.not.exist(req.files);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-08-30 19:30:36 +03:00
|
|
|
});
|