0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

Fixed oembed tests being network dependent

no issue

- some tests started failing locally because example.com was not resolvable
- mocked dns lookup for all tests so that we're always testing against expected public/private IP address blocks
This commit is contained in:
Kevin Ansfield 2020-11-30 21:44:15 +00:00
parent da5586e5aa
commit 5c5bf2d3b0
2 changed files with 33 additions and 4 deletions

View file

@ -26,6 +26,13 @@ describe('Oembed API', function () {
});
});
beforeEach(function () {
// ensure sure we're not network dependent
sinon.stub(dnsPromises, 'lookup').callsFake(function () {
return Promise.resolve({address: '123.123.123.123'});
});
});
afterEach(function () {
sinon.restore();
nock.cleanAll();
@ -490,11 +497,13 @@ describe('Oembed API', function () {
});
it('skips fetching url that resolves to private IP', function (done) {
dnsPromises.lookup.restore();
sinon.stub(dnsPromises, 'lookup').callsFake(function (hostname) {
if (hostname === 'page.com') {
return Promise.resolve({address: '192.168.0.1'});
} else {
return Promise.resolve({address: '123.123.123.123'});
}
return dnsPromises.lookup.wrappedMethod.apply(this, arguments);
});
const pageMock = nock('http://page.com')
@ -525,11 +534,13 @@ describe('Oembed API', function () {
});
it('aborts fetching if a redirect resolves to private IP', function (done) {
sinon.stub(dnsPromises, 'lookup').callsFake(function (hostname) {
dnsPromises.lookup.restore();
sinon.stub(dnsPromises, 'lookup').callsFake(async function (hostname) {
if (hostname === 'page.com') {
return Promise.resolve({address: '192.168.0.1'});
} else {
return Promise.resolve({address: '123.123.123.123'});
}
return dnsPromises.lookup.wrappedMethod.apply(this, arguments);
});
const redirectMock = nock('http://redirect.com')
@ -565,11 +576,13 @@ describe('Oembed API', function () {
});
it('skips fetching <link rel="alternate"> if it resolves to a private IP', function (done) {
dnsPromises.lookup.restore();
sinon.stub(dnsPromises, 'lookup').callsFake(function (hostname) {
if (hostname === 'oembed.com') {
return Promise.resolve({address: '192.168.0.1'});
} else {
return Promise.resolve({address: '123.123.123.123'});
}
return dnsPromises.lookup.wrappedMethod.apply(this, arguments);
});
const pageMock = nock('http://page.com')

View file

@ -1,10 +1,14 @@
const nock = require('nock');
const sinon = require('sinon');
const should = require('should');
const supertest = require('supertest');
const testUtils = require('../../../../utils/index');
const config = require('../../../../../core/shared/config/index');
const localUtils = require('./utils');
// for sinon stubs
const dnsPromises = require('dns').promises;
const ghost = testUtils.startGhost;
describe('Oembed API (v2)', function () {
@ -22,6 +26,18 @@ describe('Oembed API (v2)', function () {
});
});
beforeEach(function () {
// ensure sure we're not network dependent
sinon.stub(dnsPromises, 'lookup').callsFake(function () {
return Promise.resolve({address: '123.123.123.123'});
});
});
afterEach(function () {
sinon.restore();
nock.cleanAll();
});
it('can fetch an embed', function (done) {
let requestMock = nock('https://www.youtube.com')
.get('/oembed')