2016-01-19 17:25:36 +03:00
|
|
|
import {
|
|
|
|
describeModule,
|
|
|
|
it
|
|
|
|
} from 'ember-mocha';
|
|
|
|
import Pretender from 'pretender';
|
|
|
|
import wait from 'ember-test-helpers/wait';
|
2016-05-24 15:06:59 +03:00
|
|
|
import FeatureService, {feature} from 'ghost-admin/services/feature';
|
2016-02-02 20:05:25 +03:00
|
|
|
import Ember from 'ember';
|
2016-05-24 15:06:59 +03:00
|
|
|
import { errorOverride, errorReset } from 'ghost-admin/tests/helpers/adapter-error';
|
2016-02-02 20:05:25 +03:00
|
|
|
|
2016-02-23 18:00:17 +03:00
|
|
|
const {RSVP, merge, run} = Ember;
|
2016-02-09 22:39:15 +03:00
|
|
|
const EmberError = Ember.Error;
|
|
|
|
|
|
|
|
function stubSettings(server, labs, validSave = true, validSettings = true) {
|
|
|
|
let settings = [
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
type: 'blog',
|
|
|
|
key: 'labs',
|
|
|
|
value: JSON.stringify(labs)
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
if (validSettings) {
|
|
|
|
settings.push({
|
|
|
|
id: '2',
|
|
|
|
type: 'blog',
|
|
|
|
key: 'postsPerPage',
|
|
|
|
value: 1
|
|
|
|
});
|
|
|
|
}
|
2016-01-19 17:25:36 +03:00
|
|
|
|
|
|
|
server.get('/ghost/api/v0.1/settings/', function () {
|
2016-02-09 22:39:15 +03:00
|
|
|
return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings})];
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
server.put('/ghost/api/v0.1/settings/', function (request) {
|
2016-02-09 22:39:15 +03:00
|
|
|
let statusCode = (validSave) ? 200 : 400;
|
|
|
|
let response = (validSave) ? request.requestBody : JSON.stringify({
|
|
|
|
errors: [{
|
|
|
|
message: 'Test Error'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
return [statusCode, {'Content-Type': 'application/json'}, response];
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addTestFlag() {
|
|
|
|
FeatureService.reopen({
|
|
|
|
testFlag: feature('testFlag')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describeModule(
|
|
|
|
'service:feature',
|
|
|
|
'Integration: Service: feature',
|
|
|
|
{
|
|
|
|
integration: true
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
let server;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
server = new Pretender();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
server.shutdown();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('loads labs settings correctly', function (done) {
|
|
|
|
stubSettings(server, {testFlag: true});
|
2016-05-05 17:03:09 +03:00
|
|
|
addTestFlag();
|
2016-01-19 17:25:36 +03:00
|
|
|
|
|
|
|
let service = this.subject();
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
service.fetch().then(() => {
|
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2016-02-23 18:00:17 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-19 17:25:36 +03:00
|
|
|
it('returns false for set flag with config false and labs false', function (done) {
|
|
|
|
stubSettings(server, {testFlag: false});
|
|
|
|
addTestFlag();
|
|
|
|
|
|
|
|
let service = this.subject();
|
|
|
|
service.get('config').set('testFlag', false);
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
service.fetch().then(() => {
|
|
|
|
expect(service.get('labs.testFlag')).to.be.false;
|
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-01-19 17:25:36 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true for set flag with config true and labs false', function (done) {
|
|
|
|
stubSettings(server, {testFlag: false});
|
|
|
|
addTestFlag();
|
|
|
|
|
|
|
|
let service = this.subject();
|
|
|
|
service.get('config').set('testFlag', true);
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
service.fetch().then(() => {
|
|
|
|
expect(service.get('labs.testFlag')).to.be.false;
|
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2016-01-19 17:25:36 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true for set flag with config false and labs true', function (done) {
|
|
|
|
stubSettings(server, {testFlag: true});
|
|
|
|
addTestFlag();
|
|
|
|
|
|
|
|
let service = this.subject();
|
|
|
|
service.get('config').set('testFlag', false);
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
service.fetch().then(() => {
|
|
|
|
expect(service.get('labs.testFlag')).to.be.true;
|
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2016-01-19 17:25:36 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true for set flag with config true and labs true', function (done) {
|
|
|
|
stubSettings(server, {testFlag: true});
|
|
|
|
addTestFlag();
|
|
|
|
|
|
|
|
let service = this.subject();
|
|
|
|
service.get('config').set('testFlag', true);
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
service.fetch().then(() => {
|
|
|
|
expect(service.get('labs.testFlag')).to.be.true;
|
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2016-01-19 17:25:36 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('saves correctly', function (done) {
|
|
|
|
stubSettings(server, {testFlag: false});
|
|
|
|
addTestFlag();
|
|
|
|
|
|
|
|
let service = this.subject();
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
service.fetch().then(() => {
|
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
run(() => {
|
|
|
|
service.set('testFlag', true);
|
|
|
|
});
|
2016-02-02 20:05:25 +03:00
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
return wait().then(() => {
|
|
|
|
expect(server.handlers[1].numberOfCalls).to.equal(1);
|
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2016-01-19 17:25:36 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-02-02 20:05:25 +03:00
|
|
|
|
2016-02-09 22:39:15 +03:00
|
|
|
it('notifies for server errors', function (done) {
|
|
|
|
stubSettings(server, {testFlag: false}, false);
|
|
|
|
addTestFlag();
|
|
|
|
|
|
|
|
let service = this.subject();
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
service.fetch().then(() => {
|
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
run(() => {
|
|
|
|
service.set('testFlag', true);
|
|
|
|
});
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
return wait().then(() => {
|
|
|
|
expect(server.handlers[1].numberOfCalls).to.equal(1);
|
|
|
|
expect(service.get('notifications.notifications').length).to.equal(1);
|
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-02-09 22:39:15 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('notifies for validation errors', function (done) {
|
|
|
|
stubSettings(server, {testFlag: false}, true, false);
|
|
|
|
addTestFlag();
|
|
|
|
|
|
|
|
let service = this.subject();
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
service.fetch().then(() => {
|
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
run(() => {
|
|
|
|
expect(() => {
|
|
|
|
service.set('testFlag', true);
|
|
|
|
}, EmberError, 'threw validation error');
|
|
|
|
});
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
return wait().then(() => {
|
|
|
|
// ensure validation is happening before the API is hit
|
|
|
|
expect(server.handlers[1].numberOfCalls).to.equal(0);
|
|
|
|
expect(service.get('testFlag')).to.be.false;
|
|
|
|
done();
|
|
|
|
});
|
2016-02-09 22:39:15 +03:00
|
|
|
});
|
|
|
|
});
|
2016-01-19 17:25:36 +03:00
|
|
|
}
|
|
|
|
);
|