forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TryGhost#5985 from ErisDS/frontend-split
Further split up frontend controller & improve tests
- Loading branch information
Showing
10 changed files
with
742 additions
and
739 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function handleError(next) { | ||
return function handleError(err) { | ||
// If we've thrown an error message of type: 'NotFound' then we found no path match. | ||
if (err.errorType === 'NotFoundError') { | ||
return next(); | ||
} | ||
|
||
return next(err); | ||
}; | ||
} | ||
|
||
module.exports = handleError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var api = require('../../api'); | ||
|
||
function fetchData(options) { | ||
return api.settings.read('postsPerPage').then(function then(response) { | ||
var postPP = response.settings[0], | ||
postsPerPage = parseInt(postPP.value, 10); | ||
|
||
// No negative posts per page, must be number | ||
if (!isNaN(postsPerPage) && postsPerPage > 0) { | ||
options.limit = postsPerPage; | ||
} | ||
options.include = 'author,tags,fields'; | ||
return api.posts.browse(options); | ||
}); | ||
} | ||
|
||
module.exports = fetchData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var _ = require('lodash'); | ||
|
||
/** | ||
* formats variables for handlebars in multi-post contexts. | ||
* If extraValues are available, they are merged in the final value | ||
* @return {Object} containing page variables | ||
*/ | ||
function formatPageResponse(posts, page, extraValues) { | ||
extraValues = extraValues || {}; | ||
|
||
var resp = { | ||
posts: posts, | ||
pagination: page.meta.pagination | ||
}; | ||
return _.extend(resp, extraValues); | ||
} | ||
|
||
/** | ||
* similar to formatPageResponse, but for single post pages | ||
* @return {Object} containing page variables | ||
*/ | ||
function formatResponse(post) { | ||
return { | ||
post: post | ||
}; | ||
} | ||
|
||
module.exports = { | ||
channel: formatPageResponse, | ||
single: formatResponse | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// TODO: figure out how to remove the need for this | ||
// Add Request context parameter to the data object | ||
// to be passed down to the templates | ||
function setRequestIsSecure(req, data) { | ||
(Array.isArray(data) ? data : [data]).forEach(function forEach(d) { | ||
d.secure = req.secure; | ||
}); | ||
} | ||
|
||
module.exports = setRequestIsSecure; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var api = require('../../api'), | ||
config = require('../../config'); | ||
|
||
/** | ||
* Returns the paths object of the active theme via way of a promise. | ||
* @return {Promise} The promise resolves with the value of the paths. | ||
*/ | ||
function getActiveThemePaths() { | ||
return api.settings.read({ | ||
key: 'activeTheme', | ||
context: { | ||
internal: true | ||
} | ||
}).then(function then(response) { | ||
var activeTheme = response.settings[0], | ||
paths = config.paths.availableThemes[activeTheme.value]; | ||
|
||
return paths; | ||
}); | ||
} | ||
|
||
module.exports = getActiveThemePaths; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/*globals describe, beforeEach, afterEach, it*/ | ||
/*jshint expr:true*/ | ||
var should = require('should'), | ||
sinon = require('sinon'), | ||
errors = require('../../../../server/errors'), | ||
|
||
// Stuff we are testing | ||
handleError = require('../../../../server/controllers/frontend/error'), | ||
|
||
sandbox = sinon.sandbox.create(); | ||
|
||
// To stop jshint complaining | ||
should.equal(true, true); | ||
|
||
describe('handleError', function () { | ||
var next; | ||
beforeEach(function () { | ||
next = sandbox.spy(); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('should call next with no args for 404 errors', function () { | ||
var notFoundError = new errors.NotFoundError('Something wasn\'t found'); | ||
handleError(next)(notFoundError); | ||
|
||
next.calledOnce.should.be.true; | ||
next.firstCall.args.should.be.empty; | ||
}); | ||
|
||
it('should call next with error for other errors', function () { | ||
var otherError = new errors.MethodNotAllowedError('Something wasn\'t allowed'); | ||
|
||
handleError(next)(otherError); | ||
|
||
next.calledOnce.should.be.true; | ||
next.firstCall.args.should.have.lengthOf(1); | ||
next.firstCall.args[0].should.be.an.Object; | ||
next.firstCall.args[0].should.be.instanceof(Error); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*globals describe, beforeEach, afterEach, it*/ | ||
/*jshint expr:true*/ | ||
var should = require('should'), | ||
sinon = require('sinon'), | ||
Promise = require('bluebird'), | ||
|
||
// Stuff we are testing | ||
api = require('../../../../server/api'), | ||
fetchData = require('../../../../server/controllers/frontend/fetch-data'), | ||
|
||
sandbox = sinon.sandbox.create(); | ||
|
||
describe('fetchData', function () { | ||
var apiSettingsStub, | ||
apiPostsStub; | ||
|
||
beforeEach(function () { | ||
apiPostsStub = sandbox.stub(api.posts, 'browse').returns(new Promise.resolve({})); | ||
apiSettingsStub = sandbox.stub(api.settings, 'read'); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('valid postsPerPage', function () { | ||
beforeEach(function () { | ||
apiSettingsStub.withArgs('postsPerPage').returns(Promise.resolve({ | ||
settings: [{ | ||
key: 'postsPerPage', | ||
value: '10' | ||
}] | ||
})); | ||
}); | ||
|
||
it('Adds limit & includes to options by default', function (done) { | ||
fetchData({}).then(function () { | ||
apiSettingsStub.calledOnce.should.be.true; | ||
apiPostsStub.calledOnce.should.be.true; | ||
apiPostsStub.firstCall.args[0].should.be.an.Object; | ||
apiPostsStub.firstCall.args[0].should.have.property('include'); | ||
apiPostsStub.firstCall.args[0].should.have.property('limit', 10); | ||
done(); | ||
}).catch(done); | ||
}); | ||
|
||
it('Throws error if no options are passed', function (done) { | ||
fetchData().then(function () { | ||
done('Should have thrown an error here'); | ||
}).catch(function (err) { | ||
should.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('invalid postsPerPage', function () { | ||
beforeEach(function () { | ||
apiSettingsStub.withArgs('postsPerPage').returns(Promise.resolve({ | ||
settings: [{ | ||
key: 'postsPerPage', | ||
value: '-1' | ||
}] | ||
})); | ||
}); | ||
|
||
it('Will not add limit if postsPerPage is not valid', function (done) { | ||
fetchData({}).then(function () { | ||
apiSettingsStub.calledOnce.should.be.true; | ||
apiPostsStub.calledOnce.should.be.true; | ||
apiPostsStub.firstCall.args[0].should.be.an.Object; | ||
apiPostsStub.firstCall.args[0].should.have.property('include'); | ||
apiPostsStub.firstCall.args[0].should.not.have.property('limit'); | ||
|
||
done(); | ||
}).catch(done); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.