0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/test/unit/services/settings/loader.test.js
Naz ba964c549f Moved route settings "getter" to the backend
refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings

- Frontend is not meant to know about the underlying source of the "routes" configuration, so any reads/edits/validations are being moved into a backend service. This should also simplify the coupling of the backend with the frontend where the latter will get a JSON blob with all needed configuration during the boot
- Nother problem the "get" method had was hiding an underlying function it was doing - reading the file from the filesystem SYNCRONOUSLY. It might be a thing we need to do during the "web" app initialization, but there's no clear need to do this in a sync fassion during the bootup for example. Also having a more explicit name should help :)
2021-09-28 04:59:41 +13:00

129 lines
4.8 KiB
JavaScript

const sinon = require('sinon');
const should = require('should');
const rewire = require('rewire');
const fs = require('fs-extra');
const path = require('path');
const configUtils = require('../../../utils/configUtils');
const errors = require('@tryghost/errors');
const loadSettings = rewire('../../../../core/server/services/route-settings/loader');
describe('UNIT > Settings Service loader:', function () {
beforeEach(function () {
configUtils.set('paths:contentPath', path.join(__dirname, '../../../utils/fixtures/'));
});
afterEach(function () {
sinon.restore();
configUtils.restore();
});
describe('Settings Loader', function () {
const yamlStubFile = {
routes: null,
collections: {
'/': {
permalink: '/{slug}/',
template: ['home', 'index']
}
},
taxonomies: {tag: '/tag/{slug}/', author: '/author/{slug}/'}
};
let yamlParserStub;
let validateStub;
beforeEach(function () {
yamlParserStub = sinon.stub();
validateStub = sinon.stub();
});
it('reads a settings object for routes.yaml file', function () {
const settingsStubFile = {
routes: null,
collections: {
'/': {
permalink: '/{slug}/',
template: ['home', 'index']
}
},
resources: {
tag: '/tag/{slug}/',
author: '/author/{slug}/'
}
};
const fsReadFileStub = sinon.stub(fs, 'readFileSync').returns(settingsStubFile);
const result = loadSettings.loadSettingsSync();
should.exist(result);
result.should.be.an.Object().with.properties('routes', 'collections', 'taxonomies');
fsReadFileStub.calledOnce.should.be.true();
});
it('can find yaml settings file and returns a settings object', function () {
const fsReadFileSpy = sinon.spy(fs, 'readFileSync');
const expectedSettingsFile = path.join(__dirname, '../../../utils/fixtures/settings/goodroutes.yaml');
yamlParserStub.returns(yamlStubFile);
validateStub.returns({routes: {}, collections: {}, taxonomies: {}});
loadSettings.__set__('yamlParser', yamlParserStub);
loadSettings.__set__('validate', validateStub);
const setting = loadSettings.loadSettingsSync('goodroutes');
should.exist(setting);
setting.should.be.an.Object().with.properties('routes', 'collections', 'taxonomies');
fsReadFileSpy.calledOnce.should.be.true();
fsReadFileSpy.calledWith(expectedSettingsFile).should.be.true();
yamlParserStub.callCount.should.be.eql(1);
});
it('can handle errors from YAML parser', function (done) {
yamlParserStub.throws(new errors.GhostError({
message: 'could not parse yaml file',
context: 'bad indentation of a mapping entry at line 5, column 10'
}));
loadSettings.__set__('yamlParser', yamlParserStub);
try {
loadSettings.loadSettingsSync('goodroutes');
done(new Error('Loader should fail'));
} catch (err) {
should.exist(err);
err.message.should.be.eql('could not parse yaml file');
err.context.should.be.eql('bad indentation of a mapping entry at line 5, column 10');
yamlParserStub.calledOnce.should.be.true();
done();
}
});
it('throws error if file can\'t be accessed', function (done) {
const expectedSettingsFile = path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml');
const fsError = new Error('no permission');
fsError.code = 'EPERM';
const originalFn = fs.readFileSync;
const fsReadFileStub = sinon.stub(fs, 'readFileSync').callsFake(function (filePath, options) {
if (filePath.match(/routes\.yaml/)) {
throw fsError;
}
return originalFn(filePath, options);
});
yamlParserStub = sinon.spy();
loadSettings.__set__('yamlParser', yamlParserStub);
try {
loadSettings.loadSettingsSync('routes');
done(new Error('Loader should fail'));
} catch (err) {
err.message.should.match(/Error trying to load YAML setting for routes from/);
fsReadFileStub.calledWith(expectedSettingsFile).should.be.true();
yamlParserStub.calledOnce.should.be.false();
done();
}
});
});
});