2021-10-25 13:53:22 +03:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
|
|
|
const localUtils = require('./utils');
|
|
|
|
const testUtils = require('../../utils');
|
|
|
|
const config = require('../../../core/shared/config');
|
|
|
|
|
|
|
|
describe('Media API', function () {
|
|
|
|
// NOTE: holds paths to media that need to be cleaned up after the tests are run
|
|
|
|
const media = [];
|
|
|
|
let request;
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
await testUtils.startGhost();
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
await localUtils.doAuth(request);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
media.forEach(function (image) {
|
|
|
|
fs.removeSync(config.get('paths').appRoot + image);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can upload a MP4', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('media/upload'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.field('purpose', 'video')
|
|
|
|
.field('ref', 'https://ghost.org/sample_640x360.mp4')
|
|
|
|
.attach('file', path.join(__dirname, '/../../utils/fixtures/media/sample_640x360.mp4'))
|
2021-11-03 16:38:59 +03:00
|
|
|
.attach('thumbnail', path.join(__dirname, '/../../utils/fixtures/images/ghost-logo.png'))
|
2021-10-25 13:53:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
res.body.media[0].url.should.match(new RegExp(`${config.get('url')}/content/media/\\d+/\\d+/sample_640x360.mp4`));
|
2021-11-04 18:03:45 +03:00
|
|
|
res.body.media[0].thumbnail_url.should.match(new RegExp(`${config.get('url')}/content/media/\\d+/\\d+/sample_640x360_thumb.png`));
|
2021-10-25 13:53:22 +03:00
|
|
|
res.body.media[0].ref.should.equal('https://ghost.org/sample_640x360.mp4');
|
|
|
|
|
|
|
|
media.push(res.body.media[0].url.replace(config.get('url'), ''));
|
2021-11-03 16:38:59 +03:00
|
|
|
media.push(res.body.media[0].thumbnail_url.replace(config.get('url'), ''));
|
2021-10-25 13:53:22 +03:00
|
|
|
});
|
|
|
|
|
2021-11-05 16:06:28 +03:00
|
|
|
it('Can upload a WebM without a thumbnail', async function () {
|
2021-10-25 13:53:22 +03:00
|
|
|
const res = await request.post(localUtils.API.getApiQuery('media/upload'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.field('purpose', 'video')
|
|
|
|
.field('ref', 'https://ghost.org/sample_640x360.webm')
|
|
|
|
.attach('file', path.join(__dirname, '/../../utils/fixtures/media/sample_640x360.webm'))
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
res.body.media[0].url.should.match(new RegExp(`${config.get('url')}/content/media/\\d+/\\d+/sample_640x360.webm`));
|
2021-11-05 16:06:28 +03:00
|
|
|
should(res.body.media[0].thumbnail_url).eql(null);
|
2021-10-25 13:53:22 +03:00
|
|
|
res.body.media[0].ref.should.equal('https://ghost.org/sample_640x360.webm');
|
|
|
|
|
|
|
|
media.push(res.body.media[0].url.replace(config.get('url'), ''));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can upload an Ogg', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('media/upload'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.field('purpose', 'video')
|
|
|
|
.field('ref', 'https://ghost.org/sample_640x360.ogv')
|
|
|
|
.attach('file', path.join(__dirname, '/../../utils/fixtures/media/sample_640x360.ogv'))
|
2021-11-03 16:38:59 +03:00
|
|
|
.attach('thumbnail', path.join(__dirname, '/../../utils/fixtures/images/ghost-logo.png'))
|
2021-10-25 13:53:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
res.body.media[0].url.should.match(new RegExp(`${config.get('url')}/content/media/\\d+/\\d+/sample_640x360.ogv`));
|
|
|
|
res.body.media[0].ref.should.equal('https://ghost.org/sample_640x360.ogv');
|
|
|
|
|
|
|
|
media.push(res.body.media[0].url.replace(config.get('url'), ''));
|
|
|
|
});
|
2021-11-02 13:56:28 +03:00
|
|
|
|
|
|
|
it('Rejects non-media file type', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('media/upload'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.attach('file', path.join(__dirname, '/../../utils/fixtures/images/favicon_16x_single.ico'))
|
2021-11-03 16:38:59 +03:00
|
|
|
.attach('thumbnail', path.join(__dirname, '/../../utils/fixtures/images/ghost-logo.png'))
|
2021-11-02 13:56:28 +03:00
|
|
|
.expect(415);
|
|
|
|
|
|
|
|
res.body.errors[0].message.should.match(/select a valid media file/gi);
|
|
|
|
});
|
2021-10-25 13:53:22 +03:00
|
|
|
});
|