mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Fixed check for a theme's custom error.hbs:
Closes #2513 - Checks for property `error.hbs` on active theme - Added unit test to ensure `error` view is rendered when activeTheme has a custom error template. - Removed unused variable, `userErrorTemplatePath` from errorHandler - Refactored errorHandler.`updateActiveTheme` to take one argument, the new active theme, and to then check if the active theme has an error.hbs - Changed errorHandler unit test to use rewire for mocking config.
This commit is contained in:
parent
a458165cf0
commit
345fe1a6d4
3 changed files with 52 additions and 8 deletions
|
@ -9,7 +9,6 @@ var _ = require('lodash'),
|
|||
|
||||
// Paths for views
|
||||
defaultErrorTemplatePath = path.resolve(config().paths.adminViews, 'user-error.hbs'),
|
||||
userErrorTemplatePath = path.resolve(config().paths.themePath, 'error.hbs'),
|
||||
userErrorTemplateExists = false;
|
||||
|
||||
// This is not useful but required for jshint
|
||||
|
@ -19,9 +18,8 @@ colors.setTheme({silly: 'rainbow'});
|
|||
* Basic error handling helpers
|
||||
*/
|
||||
errors = {
|
||||
updateActiveTheme: function (activeTheme, hasErrorTemplate) {
|
||||
userErrorTemplatePath = path.resolve(config().paths.themePath, activeTheme, 'error.hbs');
|
||||
userErrorTemplateExists = hasErrorTemplate;
|
||||
updateActiveTheme: function (activeTheme) {
|
||||
userErrorTemplateExists = config().paths.availableThemes[activeTheme].hasOwnProperty('error.hbs');
|
||||
},
|
||||
|
||||
throwError: function (err) {
|
||||
|
|
|
@ -118,7 +118,7 @@ function activateTheme(activeTheme) {
|
|||
expressServer.set('theme view engine', hbs.express3(hbsOptions));
|
||||
|
||||
// Update user error template
|
||||
errors.updateActiveTheme(activeTheme, config().paths.availableThemes[activeTheme].hasOwnProperty('error'));
|
||||
errors.updateActiveTheme(activeTheme);
|
||||
}
|
||||
|
||||
// ### ManageAdminAndTheme Middleware
|
||||
|
|
|
@ -4,10 +4,11 @@ var testUtils = require('../utils'),
|
|||
when = require('when'),
|
||||
sinon = require('sinon'),
|
||||
express = require('express'),
|
||||
rewire = require('rewire')
|
||||
|
||||
// Stuff we are testing
|
||||
colors = require('colors'),
|
||||
errors = require('../../server/errorHandling'),
|
||||
errors = rewire('../../server/errorHandling'),
|
||||
// storing current environment
|
||||
currentEnv = process.env.NODE_ENV;
|
||||
|
||||
|
@ -197,10 +198,31 @@ describe('Error handling', function () {
|
|||
});
|
||||
|
||||
describe('Rendering', function () {
|
||||
var sandbox;
|
||||
var sandbox,
|
||||
originalConfig;
|
||||
|
||||
before(function () {
|
||||
errors.updateActiveTheme('casper', false);
|
||||
originalConfig = errors.__get__('config');
|
||||
errors.__set__('config', function () {
|
||||
return {
|
||||
'paths': {
|
||||
'themePath': '/content/themes',
|
||||
'availableThemes': {
|
||||
'casper': {
|
||||
'assets': null,
|
||||
'default.hbs': '/content/themes/casper/default.hbs',
|
||||
'index.hbs': '/content/themes/casper/index.hbs',
|
||||
'page.hbs': '/content/themes/casper/page.hbs',
|
||||
'tag.hbs': '/content/themes/casper/tag.hbs'
|
||||
},
|
||||
'theme-with-error': {
|
||||
'error.hbs':''
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
errors.updateActiveTheme('casper');
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
|
@ -211,6 +233,9 @@ describe('Error handling', function () {
|
|||
sandbox.restore();
|
||||
});
|
||||
|
||||
after(function () {
|
||||
errors.__set__('config', originalConfig);
|
||||
});
|
||||
|
||||
it('Renders end-of-middleware 404 errors correctly', function (done) {
|
||||
var req = {method: 'GET'},
|
||||
|
@ -321,5 +346,26 @@ describe('Error handling', function () {
|
|||
err.code = 500;
|
||||
errors.error500(err, req, res, null);
|
||||
});
|
||||
|
||||
it('Renders custom error template if one exists', function(done){
|
||||
var code = 404,
|
||||
error = {message:'Custom view test'},
|
||||
req = {
|
||||
session: null
|
||||
},
|
||||
res = {
|
||||
status: function(code) {
|
||||
return this;
|
||||
},
|
||||
render: function(view, model, fn){
|
||||
view.should.eql('error');
|
||||
errors.updateActiveTheme('casper');
|
||||
done();
|
||||
}
|
||||
},
|
||||
next = null;
|
||||
errors.updateActiveTheme('theme-with-error');
|
||||
errors.renderErrorPage(code, error, req, res, next);
|
||||
})
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue