2018-10-12 23:41:39 +03:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
|
|
|
const localUtils = require('./utils');
|
2019-09-20 18:02:45 +03:00
|
|
|
const testUtils = require('../../utils');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../core/shared/config');
|
2018-10-12 23:41:39 +03:00
|
|
|
|
|
|
|
const ghost = testUtils.startGhost;
|
|
|
|
|
2019-02-24 07:18:45 +03:00
|
|
|
describe('Images API', function () {
|
2018-10-12 23:41:39 +03:00
|
|
|
const images = [];
|
|
|
|
let request;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
return ghost()
|
|
|
|
.then(function () {
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return localUtils.doAuth(request);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
images.forEach(function (image) {
|
|
|
|
fs.removeSync(config.get('paths').appRoot + image);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
it('Can upload a png', function (done) {
|
2019-02-25 12:51:32 +03:00
|
|
|
request.post(localUtils.API.getApiQuery('images/upload'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
2019-02-25 12:51:32 +03:00
|
|
|
.field('purpose', 'image')
|
|
|
|
.field('ref', 'https://ghost.org/ghost-logo.png')
|
2019-09-20 18:02:45 +03:00
|
|
|
.attach('file', path.join(__dirname, '/../../utils/fixtures/images/ghost-logo.png'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.expect(201)
|
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2019-02-25 12:51:32 +03:00
|
|
|
res.body.images[0].url.should.match(new RegExp(`${config.get('url')}/content/images/\\d+/\\d+/ghost-logo.png`));
|
|
|
|
res.body.images[0].ref.should.equal('https://ghost.org/ghost-logo.png');
|
|
|
|
images.push(res.body.images[0].url.replace(config.get('url'), ''));
|
2019-02-04 17:16:24 +03:00
|
|
|
done();
|
|
|
|
});
|
2018-10-12 23:41:39 +03:00
|
|
|
});
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
it('Can upload a jpg', function (done) {
|
2019-02-25 12:51:32 +03:00
|
|
|
request.post(localUtils.API.getApiQuery('images/upload'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
2019-09-20 18:02:45 +03:00
|
|
|
.attach('file', path.join(__dirname, '/../../utils/fixtures/images/ghosticon.jpg'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.expect(201)
|
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2019-02-25 12:51:32 +03:00
|
|
|
res.body.images[0].url.should.match(new RegExp(`${config.get('url')}/content/images/\\d+/\\d+/ghosticon.jpg`));
|
|
|
|
should(res.body.images[0].ref).equal(null);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2019-02-25 12:51:32 +03:00
|
|
|
images.push(res.body.images[0].url.replace(config.get('url'), ''));
|
2019-02-04 17:16:24 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-10-12 23:41:39 +03:00
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
it('Can upload a gif', function (done) {
|
2019-02-25 12:51:32 +03:00
|
|
|
request.post(localUtils.API.getApiQuery('images/upload'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
2019-09-20 18:02:45 +03:00
|
|
|
.attach('file', path.join(__dirname, '/../../utils/fixtures/images/loadingcat.gif'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.expect(201)
|
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2019-02-25 12:51:32 +03:00
|
|
|
res.body.images[0].url.should.match(new RegExp(`${config.get('url')}/content/images/\\d+/\\d+/loadingcat.gif`));
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2019-02-25 12:51:32 +03:00
|
|
|
images.push(res.body.images[0].url.replace(config.get('url'), ''));
|
2019-02-04 17:16:24 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-10-12 23:41:39 +03:00
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
it('Can upload a square profile image', function (done) {
|
2019-02-25 12:51:32 +03:00
|
|
|
request.post(localUtils.API.getApiQuery('images/upload'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
2019-09-20 18:02:45 +03:00
|
|
|
.attach('file', path.join(__dirname, '/../../utils/fixtures/images/loadingcat_square.gif'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.expect(201)
|
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2019-02-25 12:51:32 +03:00
|
|
|
res.body.images[0].url.should.match(new RegExp(`${config.get('url')}/content/images/\\d+/\\d+/loadingcat_square.gif`));
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2019-02-25 12:51:32 +03:00
|
|
|
images.push(res.body.images[0].url.replace(config.get('url'), ''));
|
2019-02-04 17:16:24 +03:00
|
|
|
done();
|
|
|
|
});
|
2018-10-12 23:41:39 +03:00
|
|
|
});
|
|
|
|
});
|