2013-11-03 12:13:19 -05:00
|
|
|
/*globals describe, before, beforeEach, afterEach, it */
|
2013-11-07 08:26:47 -05:00
|
|
|
var testUtils = require('../../utils'),
|
2013-11-03 12:13:19 -05:00
|
|
|
should = require('should'),
|
2014-02-05 03:40:30 -05:00
|
|
|
_ = require('lodash'),
|
2013-11-07 04:34:18 -05:00
|
|
|
request = require('request');
|
2013-11-03 12:13:19 -05:00
|
|
|
|
|
|
|
request = request.defaults({jar:true})
|
|
|
|
|
|
|
|
describe('Settings API', function () {
|
|
|
|
|
|
|
|
var user = testUtils.DataGenerator.forModel.users[0],
|
|
|
|
csrfToken = '';
|
|
|
|
|
|
|
|
before(function (done) {
|
|
|
|
testUtils.clearData()
|
|
|
|
.then(function () {
|
2013-11-24 09:29:36 -05:00
|
|
|
return testUtils.initData();
|
|
|
|
})
|
2013-11-03 12:13:19 -05:00
|
|
|
.then(function () {
|
2013-11-05 10:02:57 -05:00
|
|
|
return testUtils.insertDefaultFixtures();
|
2013-11-03 12:13:19 -05:00
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
request.get(testUtils.API.getSigninURL(), function (error, response, body) {
|
2013-11-05 10:02:57 -05:00
|
|
|
response.should.have.status(200);
|
2013-11-03 12:13:19 -05:00
|
|
|
var pattern_meta = /<meta.*?name="csrf-param".*?content="(.*?)".*?>/i;
|
|
|
|
pattern_meta.should.exist;
|
|
|
|
csrfToken = body.match(pattern_meta)[1];
|
2013-11-11 05:37:09 -05:00
|
|
|
setTimeout((function () {
|
|
|
|
request.post({uri: testUtils.API.getSigninURL(),
|
2013-11-24 09:29:36 -05:00
|
|
|
headers: {'X-CSRF-Token': csrfToken}}, function (error, response, body) {
|
2013-11-05 10:02:57 -05:00
|
|
|
response.should.have.status(200);
|
2013-11-24 09:29:36 -05: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 10:02:57 -05:00
|
|
|
}).form({email: user.email, password: user.password});
|
|
|
|
}), 2000);
|
2013-11-03 12:13:19 -05:00
|
|
|
});
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: currently includes values of type=core
|
|
|
|
it('can retrieve all settings', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('settings/'), function (error, response, body) {
|
|
|
|
response.should.have.status(200);
|
2013-11-07 04:34:18 -05:00
|
|
|
should.not.exist(response.headers['x-cache-invalidate']);
|
2013-11-03 12:13:19 -05:00
|
|
|
response.should.be.json;
|
|
|
|
var jsonResponse = JSON.parse(body);
|
|
|
|
jsonResponse.should.exist;
|
|
|
|
|
2013-11-07 04:34:18 -05:00
|
|
|
testUtils.API.checkResponse(jsonResponse, 'settings');
|
2013-11-03 12:13:19 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('can retrieve a setting', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('settings/title/'), function (error, response, body) {
|
|
|
|
response.should.have.status(200);
|
2013-11-07 04:34:18 -05:00
|
|
|
should.not.exist(response.headers['x-cache-invalidate']);
|
2013-11-03 12:13:19 -05:00
|
|
|
response.should.be.json;
|
|
|
|
var jsonResponse = JSON.parse(body);
|
|
|
|
|
|
|
|
jsonResponse.should.exist;
|
2013-11-07 04:34:18 -05:00
|
|
|
testUtils.API.checkResponseValue(jsonResponse, ['key','value']);
|
2013-11-03 12:13:19 -05:00
|
|
|
jsonResponse.key.should.eql('title');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2013-11-07 04:34:18 -05:00
|
|
|
should.not.exist(response.headers['x-cache-invalidate']);
|
2013-11-03 12:13:19 -05:00
|
|
|
response.should.be.json;
|
|
|
|
var jsonResponse = JSON.parse(body);
|
|
|
|
jsonResponse.should.exist;
|
2013-11-07 04:34:18 -05:00
|
|
|
testUtils.API.checkResponseValue(jsonResponse, ['error']);
|
2013-11-03 12:13:19 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can edit settings', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('settings'), function (error, response, body) {
|
|
|
|
var jsonResponse = JSON.parse(body),
|
|
|
|
changedValue = 'Ghost changed';
|
|
|
|
jsonResponse.should.exist;
|
|
|
|
jsonResponse.title = changedValue;
|
|
|
|
|
|
|
|
request.put({uri: testUtils.API.getApiURL('settings/'),
|
|
|
|
headers: {'X-CSRF-Token': csrfToken},
|
|
|
|
json: jsonResponse}, function (error, response, putBody) {
|
|
|
|
response.should.have.status(200);
|
2013-11-07 04:34:18 -05:00
|
|
|
response.headers['x-cache-invalidate'].should.eql('/*');
|
2013-11-03 12:13:19 -05:00
|
|
|
response.should.be.json;
|
|
|
|
putBody.should.exist;
|
|
|
|
putBody.title.should.eql(changedValue);
|
2013-11-07 04:34:18 -05:00
|
|
|
testUtils.API.checkResponse(putBody, 'settings');
|
2013-11-03 12:13:19 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-14 05:00:11 -05:00
|
|
|
it('can\'t edit settings with invalid CSRF token', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('settings'), function (error, response, body) {
|
|
|
|
var jsonResponse = JSON.parse(body),
|
|
|
|
changedValue = 'Ghost changed';
|
|
|
|
jsonResponse.should.exist;
|
|
|
|
jsonResponse.title = changedValue;
|
|
|
|
|
|
|
|
request.put({uri: testUtils.API.getApiURL('settings/'),
|
|
|
|
headers: {'X-CSRF-Token': 'invalid-token'},
|
|
|
|
json: jsonResponse}, function (error, response, putBody) {
|
|
|
|
response.should.have.status(403);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-03 12:13:19 -05:00
|
|
|
it('can\'t edit non existent setting', function (done) {
|
|
|
|
request.get(testUtils.API.getApiURL('settings'), function (error, response, body) {
|
|
|
|
var jsonResponse = JSON.parse(body),
|
|
|
|
newValue = 'new value';
|
|
|
|
jsonResponse.should.exist;
|
|
|
|
jsonResponse.testvalue = newValue;
|
|
|
|
|
|
|
|
request.put({uri: testUtils.API.getApiURL('settings/'),
|
|
|
|
headers: {'X-CSRF-Token': csrfToken},
|
|
|
|
json: jsonResponse}, function (error, response, putBody) {
|
|
|
|
response.should.have.status(404);
|
2013-11-07 04:34:18 -05:00
|
|
|
should.not.exist(response.headers['x-cache-invalidate']);
|
2013-11-03 12:13:19 -05:00
|
|
|
response.should.be.json;
|
2013-11-07 04:34:18 -05:00
|
|
|
testUtils.API.checkResponseValue(putBody, ['error']);
|
2013-11-03 12:13:19 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|