mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
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
63 lines
1.6 KiB
JavaScript
63 lines
1.6 KiB
JavaScript
const assert = require('assert');
|
|
const sinon = require('sinon');
|
|
|
|
const models = require('../../../../../core/server/models');
|
|
|
|
const FrontendDataService = require('../../../../../core/server/services/frontend-data-service/frontend-data-service');
|
|
|
|
describe('Frontend Data Service', function () {
|
|
let service, modelStub, fakeModel;
|
|
|
|
before(function () {
|
|
models.init();
|
|
});
|
|
|
|
beforeEach(function () {
|
|
fakeModel = {
|
|
toJSON: () => {
|
|
return {api_keys: [{secret: 'xyz'}]};
|
|
}
|
|
};
|
|
|
|
modelStub = sinon.stub(models.Integration, 'getInternalFrontendKey');
|
|
|
|
service = new FrontendDataService({
|
|
IntegrationModel: models.Integration
|
|
});
|
|
});
|
|
|
|
afterEach(function () {
|
|
sinon.restore();
|
|
});
|
|
|
|
it('returns null if anything goes wrong', async function () {
|
|
modelStub.returns();
|
|
|
|
const key = await service.getFrontendKey();
|
|
|
|
sinon.assert.calledOnce(modelStub);
|
|
assert.equal(key, null);
|
|
});
|
|
|
|
it('returns the key from a model response', async function () {
|
|
modelStub.returns(fakeModel);
|
|
|
|
const key = await service.getFrontendKey();
|
|
|
|
sinon.assert.calledOnce(modelStub);
|
|
assert.equal(key, 'xyz');
|
|
});
|
|
|
|
it('returns the key from cache the second time', async function () {
|
|
modelStub.returns(fakeModel);
|
|
|
|
let key = await service.getFrontendKey();
|
|
|
|
sinon.assert.calledOnce(modelStub);
|
|
assert.equal(key, 'xyz');
|
|
|
|
key = await service.getFrontendKey();
|
|
sinon.assert.calledOnce(modelStub);
|
|
assert.equal(key, 'xyz');
|
|
});
|
|
});
|