mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
409dc3b534
refs: https://github.com/TryGhost/Team/issues/1599
refs: f3d5d9cf6b
- this commit adds the concept of a frontend data service, intended for passing data to the frontend from the server in a clean way. This is the start of a new & improved pattern, to hopefully reduce coupling
- the newly added internal frontend key is then exposed through this pattern so that the frontend can make use of it
- the first use case is so that portal can use it to talk to the content API instead of having weird endpoints for portal
- this key will also be used by other internal scripts in future, it's public and therefore safe to expose, but it's meant for internal use only and therefore is not exposed in a generic way e.g. as a helper
101 lines
3.3 KiB
JavaScript
101 lines
3.3 KiB
JavaScript
const should = require('should');
|
|
const url = require('url');
|
|
const sinon = require('sinon');
|
|
const models = require('../../../../core/server/models');
|
|
const testUtils = require('../../../utils');
|
|
const {knex} = require('../../../../core/server/data/db');
|
|
|
|
describe('Unit: models/integration', function () {
|
|
before(function () {
|
|
models.init();
|
|
});
|
|
|
|
afterEach(function () {
|
|
sinon.restore();
|
|
});
|
|
|
|
describe('permittedOptions', function () {
|
|
let basePermittedOptionsReturnVal;
|
|
let basePermittedOptionsStub;
|
|
|
|
beforeEach(function () {
|
|
basePermittedOptionsReturnVal = ['super', 'doopa'];
|
|
basePermittedOptionsStub = sinon.stub(models.Base.Model, 'permittedOptions')
|
|
.returns(basePermittedOptionsReturnVal);
|
|
});
|
|
|
|
it('returns the base permittedOptions result', function () {
|
|
const returnedOptions = models.Integration.permittedOptions();
|
|
should.deepEqual(returnedOptions, basePermittedOptionsReturnVal);
|
|
});
|
|
|
|
it('returns the base permittedOptions result plus "filter" when methodName is findOne', function () {
|
|
const returnedOptions = models.Integration.permittedOptions('findOne');
|
|
should.deepEqual(returnedOptions, basePermittedOptionsReturnVal.concat('filter'));
|
|
});
|
|
});
|
|
|
|
describe('findOne', function () {
|
|
const mockDb = require('mock-knex');
|
|
let tracker;
|
|
|
|
before(function () {
|
|
mockDb.mock(knex);
|
|
tracker = mockDb.getTracker();
|
|
});
|
|
|
|
after(function () {
|
|
mockDb.unmock(knex);
|
|
});
|
|
|
|
it('generates correct query (allows use of options.filter)', function () {
|
|
const queries = [];
|
|
tracker.install();
|
|
|
|
tracker.on('query', (query) => {
|
|
queries.push(query);
|
|
query.response([]);
|
|
});
|
|
|
|
return models.Integration.findOne({
|
|
id: '123'
|
|
}, {
|
|
filter: 'type:[custom,builtin]'
|
|
}).then(() => {
|
|
queries.length.should.eql(1);
|
|
queries[0].sql.should.eql('select `integrations`.* from `integrations` where `integrations`.`type` in (?, ?) and `integrations`.`id` = ? limit ?');
|
|
queries[0].bindings.should.eql(['custom', 'builtin', '123', 1]);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('getInternalFrontendKey', function () {
|
|
const mockDb = require('mock-knex');
|
|
let tracker;
|
|
|
|
before(function () {
|
|
mockDb.mock(knex);
|
|
tracker = mockDb.getTracker();
|
|
});
|
|
|
|
after(function () {
|
|
mockDb.unmock(knex);
|
|
});
|
|
|
|
it('generates correct query', function () {
|
|
const queries = [];
|
|
tracker.install();
|
|
|
|
tracker.on('query', (query) => {
|
|
queries.push(query);
|
|
query.response([]);
|
|
});
|
|
|
|
return models.Integration.getInternalFrontendKey().then(() => {
|
|
queries.length.should.eql(1);
|
|
queries[0].sql.should.eql('select `integrations`.* from `integrations` where `integrations`.`slug` = ? limit ?');
|
|
queries[0].bindings.should.eql(['ghost-internal-frontend', 1]);
|
|
});
|
|
});
|
|
});
|
|
});
|