0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/test/unit/api/oembed_spec.js
Kevin Ansfield ca20f3a6b0 Added /oembed API endpoint
refs https://github.com/TryGhost/Ghost/issues/9623
- add `oembed-parser` module for checking provider availability for a url and fetching data from the provider
  - require it in the `overrides.js` file before the general Promise override so that the `promise-wrt` sub-dependency doesn't attempt to extend the Bluebird promise implementation
- add `/oembed` authenticated endpoint
  - takes `?url=` query parameter to match against known providers
  - adds safeguard against oembed-parser's providers list not recognising http+https and www+non-www
  - responds with `ValidationError` if no provider is found
  - responds with oembed response from matched provider's oembed endpoint if match is found
2018-06-12 16:18:01 +01:00

60 lines
2.3 KiB
JavaScript

const common = require('../../../server/lib/common');
const nock = require('nock');
const OembedAPI = require('../../../server/api/oembed');
const should = require('should');
describe('API: oembed', function () {
describe('fn: read', function () {
// https://oembed.com/providers.json only has schemes for https://reddit.com
it('finds match for unlisted http scheme', function (done) {
let requestMock = nock('https://www.reddit.com')
.get('/oembed')
.query(true)
.reply(200, {
html: 'test'
});
OembedAPI.read({url: 'http://www.reddit.com/r/pics/comments/8qi5oq/breathtaking_picture_of_jupiter_with_its_moon_io/'})
.then((results) => {
should.exist(results);
should.exist(results.html);
done();
}).catch(done);
});
it('returns error for missing url', function (done) {
OembedAPI.read({url: ''})
.then(() => {
done(new Error('Fetch oembed without url should error'));
}).catch((err) => {
(err instanceof common.errors.BadRequestError).should.eql(true);
done();
});
});
it('returns error for unsupported provider', function (done) {
OembedAPI.read({url: 'http://example.com/unknown'})
.then(() => {
done(new Error('Fetch oembed with unknown url provider should error'));
}).catch((err) => {
(err instanceof common.errors.ValidationError).should.eql(true);
done();
});
});
it('returns error for fetch failure', function (done) {
let requestMock = nock('https://www.youtube.com')
.get('/oembed')
.query(true)
.reply(500);
OembedAPI.read({url: 'https://www.youtube.com/watch?v=E5yFcdPAGv0'})
.then(() => {
done(new Error('Fetch oembed with external failure should error'));
}).catch((err) => {
(err instanceof common.errors.InternalServerError).should.eql(true);
done();
});
});
});
});