Merge pull request #1418 from sebgie/issue#1397

This commit is contained in:
Hannah Wolfe 2013-11-07 16:11:09 +00:00
commit ab639f7026
5 changed files with 78 additions and 49 deletions

View File

@ -2,15 +2,10 @@
var testUtils = require('../../utils'),
should = require('should'),
_ = require('underscore'),
request = require('request'),
expectedPostsProperties = ['posts', 'page', 'limit', 'pages', 'total'],
expectedPostProperties = ['id', 'uuid', 'title', 'slug', 'markdown', 'html', 'meta_title', 'meta_description',
'featured', 'image', 'status', 'language', 'author_id', 'created_at', 'created_by', 'updated_at', 'updated_by',
'published_at', 'published_by', 'page', 'author', 'user', 'tags'];
request = require('request');
request = request.defaults({jar:true})
describe('Post API', function () {
var user = testUtils.DataGenerator.forModel.users[0],
@ -55,12 +50,13 @@ describe('Post API', function () {
it('can retrieve all posts', function (done) {
request.get(testUtils.API.getApiURL('posts/'), function (error, response, body) {
response.should.have.status(200);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.posts.should.exist;
testUtils.API.checkResponse (jsonResponse, expectedPostsProperties);
testUtils.API.checkResponse(jsonResponse, 'posts');
jsonResponse.posts.should.have.length(5);
testUtils.API.checkResponse (jsonResponse.posts[0], expectedPostProperties);
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
done();
});
});
@ -68,10 +64,11 @@ describe('Post API', function () {
it('can retrieve a post', function (done) {
request.get(testUtils.API.getApiURL('posts/1/'), function (error, response, body) {
response.should.have.status(200);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, expectedPostProperties);
testUtils.API.checkResponse(jsonResponse, 'post');
done();
});
});
@ -86,28 +83,31 @@ describe('Post API', function () {
headers: {'X-CSRF-Token': csrfToken},
json: newPost}, function (error, response, draftPost) {
response.should.have.status(200);
//TODO: do drafts really need a x-cache-invalidate header
response.should.be.json;
draftPost.should.exist;
draftPost.title.should.eql(newTitle);
draftPost.status = publishedState;
testUtils.API.checkResponse (draftPost, expectedPostProperties);
testUtils.API.checkResponse(draftPost, 'post');
request.put({uri: testUtils.API.getApiURL('posts/' + draftPost.id + '/'),
headers: {'X-CSRF-Token': csrfToken},
json: draftPost}, function (error, response, publishedPost) {
response.should.have.status(200);
response.headers['x-cache-invalidate'].should.eql('/, /page/*, /rss/, /rss/*, /' + publishedPost.slug + '/');
response.should.be.json;
publishedPost.should.exist;
publishedPost.title.should.eql(newTitle);
publishedPost.status.should.eql(publishedState);
testUtils.API.checkResponse (publishedPost, expectedPostProperties);
testUtils.API.checkResponse(publishedPost, 'post');
request.put({uri: testUtils.API.getApiURL('posts/' + publishedPost.id + '/'),
headers: {'X-CSRF-Token': csrfToken},
json: publishedPost}, function (error, response, updatedPost) {
response.should.have.status(200);
response.headers['x-cache-invalidate'].should.eql('/, /page/*, /rss/, /rss/*, /' + updatedPost.slug + '/');
response.should.be.json;
updatedPost.should.exist;
updatedPost.title.should.eql(newTitle);
testUtils.API.checkResponse (updatedPost, expectedPostProperties);
testUtils.API.checkResponse(updatedPost, 'post');
done();
});
});
@ -122,7 +122,8 @@ describe('Post API', function () {
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, ['id', 'slug']);
response.headers['x-cache-invalidate'].should.eql('/, /page/*, /rss/, /rss/*, /' + jsonResponse.slug + '/');
testUtils.API.checkResponseValue(jsonResponse, ['id', 'slug']);
jsonResponse.id.should.eql(deletePostId);
done();
});
@ -132,10 +133,11 @@ describe('Post API', function () {
request.del({uri: testUtils.API.getApiURL('posts/99/'),
headers: {'X-CSRF-Token': csrfToken}}, function (error, response, body) {
response.should.have.status(404);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, ['error']);
testUtils.API.checkResponseValue(jsonResponse, ['error']);
done();
});
});
@ -149,18 +151,20 @@ describe('Post API', function () {
headers: {'X-CSRF-Token': csrfToken},
json: newPost}, function (error, response, draftPost) {
response.should.have.status(200);
//TODO: do drafts really need a x-cache-invalidate header
response.should.be.json;
draftPost.should.exist;
draftPost.title.should.eql(newTitle);
draftPost.status = publishedState;
testUtils.API.checkResponse (draftPost, expectedPostProperties);
testUtils.API.checkResponse(draftPost, 'post');
request.del({uri: testUtils.API.getApiURL('posts/' + draftPost.id + '/'),
headers: {'X-CSRF-Token': csrfToken}}, function (error, response, body) {
response.should.have.status(200);
//TODO: do drafts really need a x-cache-invalidate header
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, ['id', 'slug']);
testUtils.API.checkResponseValue(jsonResponse, ['id', 'slug']);
done();
});
});
@ -170,10 +174,11 @@ describe('Post API', function () {
it('can\'t retrieve non existent post', function (done) {
request.get(testUtils.API.getApiURL('posts/99/'), function (error, response, body) {
response.should.have.status(404);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, ['error']);
testUtils.API.checkResponseValue(jsonResponse, ['error']);
done();
});
});
@ -183,18 +188,18 @@ describe('Post API', function () {
var jsonResponse = JSON.parse(body),
changedValue = 'My new Title';
jsonResponse.should.exist;
//jsonResponse.websiteshould.be.empty;
jsonResponse.title = changedValue;
request.put({uri: testUtils.API.getApiURL('posts/1/'),
headers: {'X-CSRF-Token': csrfToken},
json: jsonResponse}, function (error, response, putBody) {
response.should.have.status(200);
response.headers['x-cache-invalidate'].should.eql('/, /page/*, /rss/, /rss/*, /' + putBody.slug + '/');
response.should.be.json;
putBody.should.exist;
putBody.title.should.eql(changedValue);
testUtils.API.checkResponse (putBody, expectedPostProperties);
testUtils.API.checkResponse(putBody, 'post');
done();
});
});
@ -211,8 +216,9 @@ describe('Post API', function () {
headers: {'X-CSRF-Token': csrfToken},
json: jsonResponse}, function (error, response, putBody) {
response.should.have.status(404);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
testUtils.API.checkResponse (putBody, ['error']);
testUtils.API.checkResponseValue(putBody, ['error']);
done();
});
});

View File

@ -2,10 +2,7 @@
var testUtils = require('../../utils'),
should = require('should'),
_ = require('underscore'),
request = require('request'),
// TODO: remove databaseVersion
expectedProperties = ['databaseVersion', 'title', 'description', 'email', 'logo', 'cover', 'defaultLang',
'postsPerPage', 'forceI18n', 'activeTheme', 'activePlugins', 'installedPlugins', 'availableThemes'];
request = require('request');
request = request.defaults({jar:true})
@ -54,22 +51,24 @@ describe('Settings API', function () {
it('can retrieve all settings', function (done) {
request.get(testUtils.API.getApiURL('settings/'), function (error, response, body) {
response.should.have.status(200);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, expectedProperties);
testUtils.API.checkResponse(jsonResponse, 'settings');
done();
});
});
it('can retrieve a setting', function (done) {
request.get(testUtils.API.getApiURL('settings/title/'), function (error, response, body) {
response.should.have.status(200);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, ['key','value']);
testUtils.API.checkResponseValue(jsonResponse, ['key','value']);
jsonResponse.key.should.eql('title');
done();
});
@ -78,10 +77,11 @@ describe('Settings API', function () {
it('can\'t retrieve non existent setting', function (done) {
request.get(testUtils.API.getApiURL('settings/testsetting/'), function (error, response, body) {
response.should.have.status(404);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, ['error']);
testUtils.API.checkResponseValue(jsonResponse, ['error']);
done();
});
});
@ -97,10 +97,11 @@ describe('Settings API', function () {
headers: {'X-CSRF-Token': csrfToken},
json: jsonResponse}, function (error, response, putBody) {
response.should.have.status(200);
response.headers['x-cache-invalidate'].should.eql('/*');
response.should.be.json;
putBody.should.exist;
putBody.title.should.eql(changedValue);
testUtils.API.checkResponse (putBody, expectedProperties);
testUtils.API.checkResponse(putBody, 'settings');
done();
});
});
@ -117,8 +118,9 @@ describe('Settings API', function () {
headers: {'X-CSRF-Token': csrfToken},
json: jsonResponse}, function (error, response, putBody) {
response.should.have.status(404);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
testUtils.API.checkResponse (putBody, ['error']);
testUtils.API.checkResponseValue(putBody, ['error']);
done();
});
});

View File

@ -2,9 +2,7 @@
var testUtils = require('../../utils'),
should = require('should'),
_ = require('underscore'),
request = require('request'),
expectedProperties = ['id', 'uuid', 'name', 'slug', 'description', 'parent_id',
'meta_title', 'meta_description', 'created_at', 'created_by', 'updated_at', 'updated_by'];
request = require('request');
request = request.defaults({jar:true})
@ -52,11 +50,12 @@ describe('Tag API', function () {
it('can retrieve all tags', function (done) {
request.get(testUtils.API.getApiURL('tags/'), function (error, response, body) {
response.should.have.status(200);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
jsonResponse.should.have.length(5);
testUtils.API.checkResponse (jsonResponse[0], expectedProperties);
testUtils.API.checkResponse(jsonResponse[0], 'tag');
done();
});
});

View File

@ -2,10 +2,7 @@
var testUtils = require('../../utils'),
should = require('should'),
_ = require('underscore'),
request = require('request'),
expectedProperties = ['id', 'uuid', 'name', 'slug', 'email', 'image', 'cover', 'bio', 'website',
'location', 'accessibility', 'status', 'language', 'meta_title', 'meta_description',
'created_at', 'updated_at'];
request = require('request');
request = request.defaults({jar:true})
@ -53,11 +50,12 @@ describe('User API', function () {
it('can retrieve all users', function (done) {
request.get(testUtils.API.getApiURL('users/'), function (error, response, body) {
response.should.have.status(200);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse[0].should.exist;
testUtils.API.checkResponse (jsonResponse[0], expectedProperties);
testUtils.API.checkResponse(jsonResponse[0], 'user');
done();
});
});
@ -65,11 +63,12 @@ describe('User API', function () {
it('can retrieve a user', function (done) {
request.get(testUtils.API.getApiURL('users/me/'), function (error, response, body) {
response.should.have.status(200);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, expectedProperties);
testUtils.API.checkResponse(jsonResponse, 'user');
done();
});
});
@ -77,11 +76,12 @@ describe('User API', function () {
it('can\'t retrieve non existent user', function (done) {
request.get(testUtils.API.getApiURL('users/99/'), function (error, response, body) {
response.should.have.status(404);
should.not.exist(response.headers['x-cache-invalidate']);
response.should.be.json;
var jsonResponse = JSON.parse(body);
jsonResponse.should.exist;
testUtils.API.checkResponse (jsonResponse, ['error']);
testUtils.API.checkResponseValue(jsonResponse, ['error']);
done();
});
});
@ -97,11 +97,12 @@ describe('User API', function () {
headers: {'X-CSRF-Token': csrfToken},
json: jsonResponse}, function (error, response, putBody) {
response.should.have.status(200);
response.headers['x-cache-invalidate'].should.eql('/*');
response.should.be.json;
putBody.should.exist;
putBody.website.should.eql(changedValue);
testUtils.API.checkResponse (putBody, expectedProperties);
testUtils.API.checkResponse(putBody, 'user');
done();
});
});

View File

@ -3,7 +3,22 @@ var _ = require('underscore'),
ApiRouteBase = '/ghost/api/v0.1/',
host = 'localhost',
port = '2369';
schema = "http://"
schema = "http://",
expectedProperties = {
posts: ['posts', 'page', 'limit', 'pages', 'total'],
post: ['id', 'uuid', 'title', 'slug', 'markdown', 'html', 'meta_title', 'meta_description',
'featured', 'image', 'status', 'language', 'author_id', 'created_at', 'created_by', 'updated_at', 'updated_by',
'published_at', 'published_by', 'page', 'author', 'user', 'tags'],
// TODO: remove databaseVersion
settings: ['databaseVersion', 'title', 'description', 'email', 'logo', 'cover', 'defaultLang',
'postsPerPage', 'forceI18n', 'activeTheme', 'activePlugins', 'installedPlugins', 'availableThemes'],
tag: ['id', 'uuid', 'name', 'slug', 'description', 'parent_id',
'meta_title', 'meta_description', 'created_at', 'created_by', 'updated_at', 'updated_by'],
user: ['id', 'uuid', 'name', 'slug', 'email', 'image', 'cover', 'bio', 'website',
'location', 'accessibility', 'status', 'language', 'meta_title', 'meta_description',
'created_at', 'updated_at']
};
function getApiURL (route) {
var baseURL = url.resolve(schema + host + ':' + port, ApiRouteBase);
@ -14,13 +29,19 @@ function getSigninURL () {
}
// make sure the API only returns expected properties only
function checkResponse (jsonResponse, expectedProperties) {
Object.keys(jsonResponse).length.should.eql(expectedProperties.length);
for(var i=0; i<expectedProperties.length; i = i + 1) {
jsonResponse.should.have.property(expectedProperties[i]);
function checkResponse (jsonResponse, objectType) {
checkResponseValue(jsonResponse, expectedProperties[objectType]);
}
function checkResponseValue (jsonResponse, properties) {
Object.keys(jsonResponse).length.should.eql(properties.length);
for(var i=0; i<properties.length; i = i + 1) {
jsonResponse.should.have.property(properties[i]);
}
}
module.exports.getApiURL = getApiURL;
module.exports.getSigninURL = getSigninURL;
module.exports.checkResponse = checkResponse;
module.exports = {
getApiURL: getApiURL,
getSigninURL: getSigninURL,
checkResponse: checkResponse,
checkResponseValue: checkResponseValue,
};