Skip to content

Commit

Permalink
Add JSON API tests & cleanup
Browse files Browse the repository at this point in the history
first 10 % of TryGhost#2124
- added initial version of JSON API tests
- renamed error.errorCode to error.code
- renamed tags.all to tags.browse for consistency
  • Loading branch information
sebgie committed Feb 27, 2014
1 parent 7a6f481 commit 6fd3e20
Show file tree
Hide file tree
Showing 19 changed files with 277 additions and 43 deletions.
5 changes: 4 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ var path = require('path'),
},

integration: {
src: ['core/test/integration/**/model*_spec.js']
src: [
'core/test/integration/**/model*_spec.js',
'core/test/integration/**/api*_spec.js'
]
},

api: {
Expand Down
7 changes: 4 additions & 3 deletions core/server/api/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ api.settings = require('./settings');
db = {
'exportContent': function (req, res) {
/*jslint unparam:true*/

return dataExport().then(function (exportedData) {
// Save the exported data to the file system for download
var fileName = path.join(config().paths.exportPath, 'exported-' + (new Date().getTime()) + '.json');
Expand Down Expand Up @@ -56,7 +57,7 @@ db = {
* - If there is no path
* - If the name doesn't have json in it
*/
return when.reject({errorCode: 500, message: 'Please select a .json file to import.'});
return when.reject({code: 500, message: 'Please select a .json file to import.'});

This comment has been minimized.

Copy link
@ErisDS

ErisDS Feb 27, 2014

👍

}

return api.settings.read({ key: 'databaseVersion' }).then(function (setting) {
Expand Down Expand Up @@ -121,15 +122,15 @@ db = {
}).then(function () {
return when.resolve({message: 'Posts, tags and other data successfully imported'});
}).otherwise(function importFailure(error) {
return when.reject({errorCode: 500, message: error.message || error});
return when.reject({code: 500, message: error.message || error});
});
},
'deleteAllContent': function () {
return when(dataProvider.deleteAllContent())
.then(function () {
return when.resolve({message: 'Successfully deleted all content from your blog.'});
}, function (error) {
return when.reject({errorCode: 500, message: error.message || error});
return when.reject({code: 500, message: error.message || error});
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion core/server/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ requestHandler = function (apiMethod) {
}
});
}, function (error) {
var errorCode = error.errorCode || 500,
var errorCode = error.code || 500,
errorMsg = {error: _.isString(error) ? error : (_.isObject(error) ? error.message : 'Unknown API Error')};
res.json(errorCode, errorMsg);
});
Expand Down
25 changes: 12 additions & 13 deletions core/server/api/posts.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var when = require('when'),
_ = require('lodash'),
dataProvider = require('../models'),
permissions = require('../permissions'),
canThis = permissions.canThis,
canThis = require('../permissions').canThis,
filteredUserAttributes = require('./users').filteredAttributes,
posts;

Expand All @@ -15,7 +14,7 @@ posts = {
options = options || {};

// **returns:** a promise for a page of posts in a json object
//return dataProvider.Post.findPage(options);

return dataProvider.Post.findPage(options).then(function (result) {
var i = 0,
omitted = result;
Expand Down Expand Up @@ -43,7 +42,7 @@ posts = {
omitted.user = _.omit(omitted.user, filteredUserAttributes);
return omitted;
}
return when.reject({errorCode: 404, message: 'Post not found'});
return when.reject({code: 404, message: 'Post not found'});

});
},
Expand All @@ -53,7 +52,7 @@ posts = {
if (slug) {
return slug;
}
return when.reject({errorCode: 500, message: 'Could not generate slug'});
return when.reject({code: 500, message: 'Could not generate slug'});
});
},

Expand All @@ -63,7 +62,7 @@ posts = {
edit: function edit(postData) {
// **returns:** a promise for the resulting post in a json object
if (!this.user) {
return when.reject({errorCode: 403, message: 'You do not have permission to edit this post.'});
return when.reject({code: 403, message: 'You do not have permission to edit this post.'});
}
var self = this;
return canThis(self.user).edit.post(postData.id).then(function () {
Expand All @@ -74,17 +73,17 @@ posts = {
omitted.user = _.omit(omitted.user, filteredUserAttributes);
return omitted;
}
return when.reject({errorCode: 404, message: 'Post not found'});
return when.reject({code: 404, message: 'Post not found'});
}).otherwise(function (error) {
return dataProvider.Post.findOne({id: postData.id, status: 'all'}).then(function (result) {
if (!result) {
return when.reject({errorCode: 404, message: 'Post not found'});
return when.reject({code: 404, message: 'Post not found'});
}
return when.reject({message: error.message});
});
});
}, function () {
return when.reject({errorCode: 403, message: 'You do not have permission to edit this post.'});
return when.reject({code: 403, message: 'You do not have permission to edit this post.'});
});
},

Expand All @@ -94,13 +93,13 @@ posts = {
add: function add(postData) {
// **returns:** a promise for the resulting post in a json object
if (!this.user) {
return when.reject({errorCode: 403, message: 'You do not have permission to add posts.'});
return when.reject({code: 403, message: 'You do not have permission to add posts.'});
}

return canThis(this.user).create.post().then(function () {
return dataProvider.Post.add(postData);
}, function () {
return when.reject({errorCode: 403, message: 'You do not have permission to add posts.'});
return when.reject({code: 403, message: 'You do not have permission to add posts.'});
});
},

Expand All @@ -110,7 +109,7 @@ posts = {
destroy: function destroy(args) {
// **returns:** a promise for a json response with the id of the deleted post
if (!this.user) {
return when.reject({errorCode: 403, message: 'You do not have permission to remove posts.'});
return when.reject({code: 403, message: 'You do not have permission to remove posts.'});
}

return canThis(this.user).remove.post(args.id).then(function () {
Expand All @@ -121,7 +120,7 @@ posts = {
});
});
}, function () {
return when.reject({errorCode: 403, message: 'You do not have permission to remove posts.'});
return when.reject({code: 403, message: 'You do not have permission to remove posts.'});
});
}
};
Expand Down
6 changes: 3 additions & 3 deletions core/server/api/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ settings = {
if (settingsCache) {
return when(settingsCache[options.key]).then(function (setting) {
if (!setting) {
return when.reject({errorCode: 404, message: 'Unable to find setting: ' + options.key});
return when.reject({code: 404, message: 'Unable to find setting: ' + options.key});
}
var res = {};
res.key = options.key;
Expand Down Expand Up @@ -167,15 +167,15 @@ settings = {
}).otherwise(function (error) {
return dataProvider.Settings.read(key.key).then(function (result) {
if (!result) {
return when.reject({errorCode: 404, message: 'Unable to find setting: ' + key});
return when.reject({code: 404, message: 'Unable to find setting: ' + key});
}
return when.reject({message: error.message});
});
});
}
return dataProvider.Settings.read(key).then(function (setting) {
if (!setting) {
return when.reject({errorCode: 404, message: 'Unable to find setting: ' + key});
return when.reject({code: 404, message: 'Unable to find setting: ' + key});
}
if (!_.isString(value)) {
value = JSON.stringify(value);
Expand Down
8 changes: 5 additions & 3 deletions core/server/api/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ var dataProvider = require('../models'),


tags = {
// #### All
// #### Browse

// **takes:** Nothing yet
all: function browse() {
browse: function browse() {
// **returns:** a promise for all tags which have previously been used in a json object
return dataProvider.Tag.findAll();
return dataProvider.Tag.findAll().then(function (result) {
return result.toJSON();
});
}
};

Expand Down
10 changes: 3 additions & 7 deletions core/server/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var when = require('when'),

// ## Users
users = {
// #### Browse

// #### Browse
// **takes:** options object
browse: function browse(options) {
// **returns:** a promise for a collection of users in a json object
Expand All @@ -31,7 +31,6 @@ users = {
},

// #### Read

// **takes:** an identifier (id or slug?)
read: function read(args) {
// **returns:** a promise for a single user in a json object
Expand All @@ -45,12 +44,11 @@ users = {
return omitted;
}

return when.reject({errorCode: 404, message: 'User not found'});
return when.reject({code: 404, message: 'User not found'});
});
},

// #### Edit

// **takes:** a json object representing a user
edit: function edit(userData) {
// **returns:** a promise for the resulting user in a json object
Expand All @@ -60,12 +58,11 @@ users = {
var omitted = _.omit(result.toJSON(), filteredAttributes);
return omitted;
}
return when.reject({errorCode: 404, message: 'User not found'});
return when.reject({code: 404, message: 'User not found'});
});
},

// #### Add

// **takes:** a json object representing a user
add: function add(userData) {

Expand All @@ -83,7 +80,6 @@ users = {
},

// #### Change Password

// **takes:** a json object representing a user
changePassword: function changePassword(userData) {
// **returns:** on success, returns a promise for the resulting user in a json object
Expand Down
2 changes: 1 addition & 1 deletion core/server/apps/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var proxy = {
},
api: {
posts: _.pick(api.posts, 'browse', 'read'),
tags: api.tags,
tags: _.pick(api.tags, 'browse'),
notifications: _.pick(api.notifications, 'add'),
settings: _.pick(api.settings, 'read')
}
Expand Down
2 changes: 1 addition & 1 deletion core/server/controllers/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function formatPageResponse(posts, page) {
function handleError(next) {
return function (err) {
var e = new Error(err.message);
e.status = err.errorCode;
e.status = err.code;
return next(e);
};
}
Expand Down
2 changes: 1 addition & 1 deletion core/server/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var Filters = function () {

// Register a new filter callback function
Filters.prototype.registerFilter = function (name, priority, fn) {
// Curry the priority optional parameter to a default of 5
// Carry the priority optional parameter to a default of 5
if (_.isFunction(priority)) {
fn = priority;
priority = null;
Expand Down
2 changes: 1 addition & 1 deletion core/server/middleware/ghost-busboy.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function ghostBusBoy(req, res, next) {

busboy.on('limit', function () {
hasError = true;
res.send(413, { errorCode: 413, message: 'File size limit breached.' });
res.send(413, {code: 413, message: 'File size limit breached.'});
});

busboy.on('error', function (error) {
Expand Down
2 changes: 1 addition & 1 deletion core/server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function (server) {
server.get('/ghost/api/v0.1/users/:id/', api.requestHandler(api.users.read));
server.put('/ghost/api/v0.1/users/:id/', api.requestHandler(api.users.edit));
// #### Tags
server.get('/ghost/api/v0.1/tags/', api.requestHandler(api.tags.all));
server.get('/ghost/api/v0.1/tags/', api.requestHandler(api.tags.browse));
// #### Notifications
server.del('/ghost/api/v0.1/notifications/:id', api.requestHandler(api.notifications.destroy));
server.post('/ghost/api/v0.1/notifications/', api.requestHandler(api.notifications.add));
Expand Down
49 changes: 49 additions & 0 deletions core/test/integration/api/api_notifications_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*globals describe, before, beforeEach, afterEach, it */
var testUtils = require('../../utils'),
should = require('should'),

// Stuff we are testing
DataGenerator = require('../../utils/fixtures/data-generator'),
NotificationsAPI = require('../../../server/api/notifications');

describe('Notifications API', function () {

before(function (done) {
testUtils.clearData().then(function () {
done();
}, done);
});

beforeEach(function (done) {
testUtils.initData()
.then(function () {
return testUtils.insertDefaultFixtures();
})
.then(function () {
done();
}, done);
});

afterEach(function (done) {
testUtils.clearData().then(function () {
done();
}, done);
});

it('can browse', function (done) {
var msg = {
type: 'error', // this can be 'error', 'success', 'warn' and 'info'
message: 'This is an error', // A string. Should fit in one line.
status: 'persistent', // or 'passive'
id: 'auniqueid' // A unique ID
};
NotificationsAPI.add(msg).then(function (notification){
NotificationsAPI.browse().then(function (results) {
should.exist(results);
results.length.should.be.above(0);
testUtils.API.checkResponse(results[0], 'notification');
done();
});
});
});
});
Loading

0 comments on commit 6fd3e20

Please sign in to comment.