2013-09-06 20:07:25 +04:00
|
|
|
/*globals describe, before, beforeEach, afterEach, it */
|
2013-11-07 17:26:47 +04:00
|
|
|
var testUtils = require('../../utils'),
|
2013-09-06 20:07:25 +04:00
|
|
|
should = require('should'),
|
2013-11-03 21:13:19 +04:00
|
|
|
_ = require('underscore'),
|
2013-11-07 13:34:18 +04:00
|
|
|
request = require('request');
|
2013-11-03 21:13:19 +04:00
|
|
|
|
2013-12-10 07:38:25 +04:00
|
|
|
request = request.defaults({jar:true});
|
2013-09-06 20:07:25 +04:00
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
describe('Post API', function () {
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
var user = testUtils.DataGenerator.forModel.users[0],
|
2013-11-03 21:13:19 +04:00
|
|
|
csrfToken = '';
|
2013-08-25 00:51:58 +04:00
|
|
|
|
|
|
|
before(function (done) {
|
2013-10-08 05:39:33 +04:00
|
|
|
testUtils.clearData()
|
|
|
|
.then(function () {
|
2013-11-24 18:29:36 +04:00
|
|
|
return testUtils.initData();
|
|
|
|
})
|
2013-08-25 00:51:58 +04:00
|
|
|
.then(function () {
|
2013-11-05 19:02:57 +04:00
|
|
|
return testUtils.insertDefaultFixtures();
|
2013-08-25 00:51:58 +04:00
|
|
|
})
|
|
|
|
.then(function () {
|
2013-11-03 21:13:19 +04:00
|
|
|
request.get(testUtils.API.getSigninURL(), function (error, response, body) {
|
2013-11-05 19:02:57 +04:00
|
|
|
response.should.have.status(200);
|
2013-11-03 21:13:19 +04:00
|
|
|
var pattern_meta = /<meta.*?name="csrf-param".*?content="(.*?)".*?>/i;
|
|
|
|
pattern_meta.should.exist;
|
|
|
|
csrfToken = body.match(pattern_meta)[1];
|
2013-11-11 14:37:09 +04:00
|
|
|
setTimeout((function () {
|
|
|
|
request.post({uri: testUtils.API.getSigninURL(),
|
2013-11-24 18:29:36 +04:00
|
|
|
headers: {'X-CSRF-Token': csrfToken}}, function (error, response, body) {
|
2013-11-05 19:02:57 +04:00
|
|
|
response.should.have.status(200);
|
2013-11-24 18:29:36 +04:00
|
|
|
request.get(testUtils.API.getAdminURL(), function (error, response, body) {
|
|
|
|
response.should.have.status(200);
|
|
|
|
csrfToken = body.match(pattern_meta)[1];
|
|
|
|
done();
|
|
|
|
});
|
2013-11-05 19:02:57 +04:00
|
|
|
}).form({email: user.email, password: user.password});
|
|
|
|
}), 2000);
|
2013-11-03 21:13:19 +04:00
|
|
|
});
|
2013-08-25 00:51:58 +04:00
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2013-12-20 02:51:28 +04:00
|
|
|
// ## Browse
|
|
|
|
|
|
|
|
it('retrieves all published posts only by default', function (done) {
|
2013-11-03 21:13:19 +04:00
|
|
|
request.get(testUtils.API.getApiURL('posts/'), function (error, response, body) {
|
|
|
|
response.should.have.status(200);
|
2013-11-07 13:34:18 +04:00
|
|
|
should.not.exist(response.headers['x-cache-invalidate']);
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.be.json;
|
|
|
|
var jsonResponse = JSON.parse(body);
|
|
|
|
jsonResponse.posts.should.exist;
|
2013-11-07 13:34:18 +04:00
|
|
|
testUtils.API.checkResponse(jsonResponse, 'posts');
|
2013-11-03 21:13:19 +04:00
|
|
|
jsonResponse.posts.should.have.length(5);
|
2013-11-07 13:34:18 +04:00
|
|
|
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
2013-11-03 21:13:19 +04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-20 02:51:28 +04:00
|
|
|
it('can retrieve all published posts and pages', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('posts/?staticPages=all'), 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, 'posts');
|
|
|
|
jsonResponse.posts.should.have.length(6);
|
|
|
|
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test bits of the API we don't use in the app yet to ensure the API behaves properly
|
|
|
|
|
|
|
|
it('can retrieve all status posts and pages', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('posts/?staticPages=all&status=all'), 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, 'posts');
|
|
|
|
jsonResponse.posts.should.have.length(8);
|
|
|
|
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can retrieve just published pages', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('posts/?staticPages=true'), 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, 'posts');
|
|
|
|
jsonResponse.posts.should.have.length(1);
|
|
|
|
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can retrieve just draft posts', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('posts/?status=draft'), 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, 'posts');
|
|
|
|
jsonResponse.posts.should.have.length(1);
|
|
|
|
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// ## Read
|
|
|
|
|
2013-11-03 21:13:19 +04:00
|
|
|
it('can retrieve a post', function (done) {
|
2013-12-20 02:51:28 +04:00
|
|
|
request.get(testUtils.API.getApiURL('posts/1/'), function (error, response, body) {
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.have.status(200);
|
2013-11-07 13:34:18 +04:00
|
|
|
should.not.exist(response.headers['x-cache-invalidate']);
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.be.json;
|
|
|
|
var jsonResponse = JSON.parse(body);
|
|
|
|
jsonResponse.should.exist;
|
2013-11-07 13:34:18 +04:00
|
|
|
testUtils.API.checkResponse(jsonResponse, 'post');
|
2013-12-10 07:38:25 +04:00
|
|
|
jsonResponse.page.should.eql(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can retrieve a static page', function (done) {
|
2013-12-20 02:51:28 +04:00
|
|
|
request.get(testUtils.API.getApiURL('posts/7/'), function (error, response, body) {
|
2013-12-10 07:38:25 +04:00
|
|
|
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, 'post');
|
|
|
|
jsonResponse.page.should.eql(1);
|
2013-11-03 21:13:19 +04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-20 02:51:28 +04:00
|
|
|
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.checkResponseValue(jsonResponse, ['error']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can\'t retrieve a draft post', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('posts/5/'), 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.checkResponseValue(jsonResponse, ['error']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can\'t retrieve a draft page', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('posts/8/'), 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.checkResponseValue(jsonResponse, ['error']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// ## Add
|
|
|
|
|
2013-11-03 21:13:19 +04:00
|
|
|
it('can create a new draft, publish post, update post', function (done) {
|
|
|
|
var newTitle = 'My Post',
|
|
|
|
changedTitle = 'My Post changed',
|
|
|
|
publishedState = 'published',
|
2013-11-11 14:37:09 +04:00
|
|
|
newPost = {status: 'draft', title: newTitle, markdown: 'my post'};
|
2013-11-03 21:13:19 +04:00
|
|
|
|
|
|
|
request.post({uri: testUtils.API.getApiURL('posts/'),
|
2013-11-11 14:37:09 +04:00
|
|
|
headers: {'X-CSRF-Token': csrfToken},
|
|
|
|
json: newPost}, function (error, response, draftPost) {
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.have.status(200);
|
2013-11-07 13:34:18 +04:00
|
|
|
//TODO: do drafts really need a x-cache-invalidate header
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.be.json;
|
|
|
|
draftPost.should.exist;
|
|
|
|
draftPost.title.should.eql(newTitle);
|
|
|
|
draftPost.status = publishedState;
|
2013-11-07 13:34:18 +04:00
|
|
|
testUtils.API.checkResponse(draftPost, 'post');
|
2013-11-03 21:13:19 +04:00
|
|
|
request.put({uri: testUtils.API.getApiURL('posts/' + draftPost.id + '/'),
|
2013-11-11 14:37:09 +04:00
|
|
|
headers: {'X-CSRF-Token': csrfToken},
|
|
|
|
json: draftPost}, function (error, response, publishedPost) {
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.have.status(200);
|
2013-11-07 13:34:18 +04:00
|
|
|
response.headers['x-cache-invalidate'].should.eql('/, /page/*, /rss/, /rss/*, /' + publishedPost.slug + '/');
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.be.json;
|
|
|
|
publishedPost.should.exist;
|
|
|
|
publishedPost.title.should.eql(newTitle);
|
|
|
|
publishedPost.status.should.eql(publishedState);
|
2013-11-07 13:34:18 +04:00
|
|
|
testUtils.API.checkResponse(publishedPost, 'post');
|
2013-11-03 21:13:19 +04:00
|
|
|
request.put({uri: testUtils.API.getApiURL('posts/' + publishedPost.id + '/'),
|
2013-11-11 14:37:09 +04:00
|
|
|
headers: {'X-CSRF-Token': csrfToken},
|
|
|
|
json: publishedPost}, function (error, response, updatedPost) {
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.have.status(200);
|
2013-11-07 13:34:18 +04:00
|
|
|
response.headers['x-cache-invalidate'].should.eql('/, /page/*, /rss/, /rss/*, /' + updatedPost.slug + '/');
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.be.json;
|
|
|
|
updatedPost.should.exist;
|
|
|
|
updatedPost.title.should.eql(newTitle);
|
2013-11-07 13:34:18 +04:00
|
|
|
testUtils.API.checkResponse(updatedPost, 'post');
|
2013-11-03 21:13:19 +04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-20 02:51:28 +04:00
|
|
|
// ## edit
|
2013-11-24 18:29:36 +04:00
|
|
|
|
|
|
|
it('can edit a post', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('posts/1/'), function (error, response, body) {
|
|
|
|
var jsonResponse = JSON.parse(body),
|
|
|
|
changedValue = 'My new Title';
|
|
|
|
jsonResponse.should.exist;
|
|
|
|
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, 'post');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-20 02:51:28 +04:00
|
|
|
// ## delete
|
|
|
|
|
2013-11-24 18:29:36 +04:00
|
|
|
it('can\'t edit non existent post', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('posts/1/'), function (error, response, body) {
|
|
|
|
var jsonResponse = JSON.parse(body),
|
|
|
|
changedValue = 'My new Title';
|
|
|
|
jsonResponse.title.exist;
|
|
|
|
jsonResponse.testvalue = changedValue;
|
|
|
|
jsonResponse.id = 99;
|
|
|
|
request.put({uri: testUtils.API.getApiURL('posts/99/'),
|
|
|
|
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.checkResponseValue(putBody, ['error']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-03 21:13:19 +04:00
|
|
|
it('can delete a post', function (done) {
|
|
|
|
var deletePostId = 1;
|
2013-11-11 14:37:09 +04:00
|
|
|
request.del({uri: testUtils.API.getApiURL('posts/' + deletePostId + '/'),
|
|
|
|
headers: {'X-CSRF-Token': csrfToken}}, function (error, response, body) {
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.have.status(200);
|
|
|
|
response.should.be.json;
|
|
|
|
var jsonResponse = JSON.parse(body);
|
|
|
|
jsonResponse.should.exist;
|
2013-11-07 13:34:18 +04:00
|
|
|
response.headers['x-cache-invalidate'].should.eql('/, /page/*, /rss/, /rss/*, /' + jsonResponse.slug + '/');
|
|
|
|
testUtils.API.checkResponseValue(jsonResponse, ['id', 'slug']);
|
2013-11-03 21:13:19 +04:00
|
|
|
jsonResponse.id.should.eql(deletePostId);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can\'t delete a non existent post', function (done) {
|
|
|
|
request.del({uri: testUtils.API.getApiURL('posts/99/'),
|
2013-11-11 14:37:09 +04:00
|
|
|
headers: {'X-CSRF-Token': csrfToken}}, function (error, response, body) {
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.have.status(404);
|
2013-11-07 13:34:18 +04:00
|
|
|
should.not.exist(response.headers['x-cache-invalidate']);
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.be.json;
|
|
|
|
var jsonResponse = JSON.parse(body);
|
|
|
|
jsonResponse.should.exist;
|
2013-11-07 13:34:18 +04:00
|
|
|
testUtils.API.checkResponseValue(jsonResponse, ['error']);
|
2013-11-03 21:13:19 +04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can delete a new draft', function (done) {
|
|
|
|
var newTitle = 'My Post',
|
|
|
|
publishedState = 'draft',
|
|
|
|
newPost = {status: publishedState, title: newTitle, markdown: 'my post'};
|
|
|
|
|
|
|
|
request.post({uri: testUtils.API.getApiURL('posts/'),
|
2013-11-11 14:37:09 +04:00
|
|
|
headers: {'X-CSRF-Token': csrfToken},
|
|
|
|
json: newPost}, function (error, response, draftPost) {
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.have.status(200);
|
2013-11-07 13:34:18 +04:00
|
|
|
//TODO: do drafts really need a x-cache-invalidate header
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.be.json;
|
|
|
|
draftPost.should.exist;
|
|
|
|
draftPost.title.should.eql(newTitle);
|
|
|
|
draftPost.status = publishedState;
|
2013-11-07 13:34:18 +04:00
|
|
|
testUtils.API.checkResponse(draftPost, 'post');
|
2013-11-03 21:13:19 +04:00
|
|
|
request.del({uri: testUtils.API.getApiURL('posts/' + draftPost.id + '/'),
|
2013-11-11 14:37:09 +04:00
|
|
|
headers: {'X-CSRF-Token': csrfToken}}, function (error, response, body) {
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.have.status(200);
|
2013-11-07 13:34:18 +04:00
|
|
|
//TODO: do drafts really need a x-cache-invalidate header
|
2013-11-03 21:13:19 +04:00
|
|
|
response.should.be.json;
|
|
|
|
var jsonResponse = JSON.parse(body);
|
|
|
|
jsonResponse.should.exist;
|
2013-11-07 13:34:18 +04:00
|
|
|
testUtils.API.checkResponseValue(jsonResponse, ['id', 'slug']);
|
2013-11-03 21:13:19 +04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-11 14:37:09 +04:00
|
|
|
});
|