2017-08-22 10:53:26 +03:00
|
|
|
import EmberError from '@ember/error';
|
2017-05-29 21:50:03 +03:00
|
|
|
import FeatureService, {feature} from 'ghost-admin/services/feature';
|
|
|
|
import Pretender from 'pretender';
|
2021-02-05 12:12:26 +03:00
|
|
|
import ghostPaths from 'ghost-admin/utils/ghost-paths';
|
2017-05-29 21:50:03 +03:00
|
|
|
import wait from 'ember-test-helpers/wait';
|
|
|
|
import {describe, it} from 'mocha';
|
2019-01-02 12:58:55 +03:00
|
|
|
import {expect} from 'chai';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {setupTest} from 'ember-mocha';
|
2016-02-02 20:05:25 +03:00
|
|
|
|
2017-03-14 20:09:06 +03:00
|
|
|
function stubSettings(server, labs, validSave = true) {
|
2016-02-09 22:39:15 +03:00
|
|
|
let settings = [
|
|
|
|
{
|
|
|
|
id: '1',
|
2020-06-23 23:04:46 +03:00
|
|
|
type: 'labs',
|
2016-02-09 22:39:15 +03:00
|
|
|
key: 'labs',
|
|
|
|
value: JSON.stringify(labs)
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-02-05 12:12:26 +03:00
|
|
|
server.get(`${ghostPaths().apiRoot}/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
|
|
|
});
|
|
|
|
|
2021-02-05 12:12:26 +03:00
|
|
|
server.put(`${ghostPaths().apiRoot}/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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
function stubUser(server, accessibility, validSave = true) {
|
|
|
|
let users = [{
|
|
|
|
id: '1',
|
|
|
|
// Add extra properties for the validations
|
|
|
|
name: 'Test User',
|
|
|
|
email: 'test@example.com',
|
|
|
|
accessibility: JSON.stringify(accessibility),
|
|
|
|
roles: [{
|
|
|
|
id: 1,
|
|
|
|
name: 'Owner',
|
|
|
|
description: 'Owner'
|
|
|
|
}]
|
|
|
|
}];
|
|
|
|
|
2021-02-05 12:12:26 +03:00
|
|
|
server.get(`${ghostPaths().apiRoot}/users/me/`, function () {
|
2017-03-03 19:14:33 +03:00
|
|
|
return [200, {'Content-Type': 'application/json'}, JSON.stringify({users})];
|
|
|
|
});
|
|
|
|
|
2021-02-05 12:12:26 +03:00
|
|
|
server.put(`${ghostPaths().apiRoot}/users/1/`, function (request) {
|
2017-03-03 19:14:33 +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({
|
2017-03-03 19:14:33 +03:00
|
|
|
testFlag: feature('testFlag'),
|
2018-01-11 17:16:42 +03:00
|
|
|
testUserFlag: feature('testUserFlag', {user: true})
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
describe('Integration: Service: feature', function () {
|
2019-05-13 15:43:53 +03:00
|
|
|
setupTest();
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
let server;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
server = new Pretender();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
server.shutdown();
|
|
|
|
});
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('loads labs and user settings correctly', async function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSettings(server, {testFlag: true});
|
2017-03-03 19:14:33 +03:00
|
|
|
stubUser(server, {testUserFlag: true});
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
addTestFlag();
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2016-11-24 01:50:57 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return service.fetch().then(() => {
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2017-03-03 19:14:33 +03:00
|
|
|
expect(service.get('testUserFlag')).to.be.true;
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('returns false for set flag with config false and labs false', async function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSettings(server, {testFlag: false});
|
2017-03-03 19:14:33 +03:00
|
|
|
stubUser(server, {});
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
addTestFlag();
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2016-11-24 01:50:57 +03:00
|
|
|
service.get('config').set('testFlag', false);
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return service.fetch().then(() => {
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(service.get('labs.testFlag')).to.be.false;
|
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-02-23 18:00:17 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-02-23 18:00:17 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('returns true for set flag with config true and labs false', async function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSettings(server, {testFlag: false});
|
2017-03-03 19:14:33 +03:00
|
|
|
stubUser(server, {});
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
addTestFlag();
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2016-11-24 01:50:57 +03:00
|
|
|
service.get('config').set('testFlag', true);
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return service.fetch().then(() => {
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(service.get('labs.testFlag')).to.be.false;
|
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('returns true for set flag with config false and labs true', async function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSettings(server, {testFlag: true});
|
2017-03-03 19:14:33 +03:00
|
|
|
stubUser(server, {});
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
addTestFlag();
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2016-11-24 01:50:57 +03:00
|
|
|
service.get('config').set('testFlag', false);
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return service.fetch().then(() => {
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(service.get('labs.testFlag')).to.be.true;
|
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('returns true for set flag with config true and labs true', async function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSettings(server, {testFlag: true});
|
2017-03-03 19:14:33 +03:00
|
|
|
stubUser(server, {});
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
addTestFlag();
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2016-11-24 01:50:57 +03:00
|
|
|
service.get('config').set('testFlag', true);
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return service.fetch().then(() => {
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(service.get('labs.testFlag')).to.be.true;
|
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('returns false for set flag with accessibility false', async function () {
|
2017-03-03 19:14:33 +03:00
|
|
|
stubSettings(server, {});
|
|
|
|
stubUser(server, {testUserFlag: false});
|
|
|
|
|
|
|
|
addTestFlag();
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2017-03-03 19:14:33 +03:00
|
|
|
|
|
|
|
return service.fetch().then(() => {
|
|
|
|
expect(service.get('accessibility.testUserFlag')).to.be.false;
|
|
|
|
expect(service.get('testUserFlag')).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('returns true for set flag with accessibility true', async function () {
|
2017-03-03 19:14:33 +03:00
|
|
|
stubSettings(server, {});
|
|
|
|
stubUser(server, {testUserFlag: true});
|
|
|
|
|
|
|
|
addTestFlag();
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2017-03-03 19:14:33 +03:00
|
|
|
|
|
|
|
return service.fetch().then(() => {
|
|
|
|
expect(service.get('accessibility.testUserFlag')).to.be.true;
|
|
|
|
expect(service.get('testUserFlag')).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('saves labs setting correctly', async function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSettings(server, {testFlag: false});
|
2017-03-03 19:14:33 +03:00
|
|
|
stubUser(server, {testUserFlag: false});
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
addTestFlag();
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2016-11-24 01:50:57 +03:00
|
|
|
service.get('config').set('testFlag', false);
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return service.fetch().then(() => {
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
run(() => {
|
|
|
|
service.set('testFlag', true);
|
|
|
|
});
|
|
|
|
|
|
|
|
return wait().then(() => {
|
|
|
|
expect(server.handlers[1].numberOfCalls).to.equal(1);
|
2016-05-05 17:03:09 +03:00
|
|
|
expect(service.get('testFlag')).to.be.true;
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('saves accessibility setting correctly', async function () {
|
2017-03-03 19:14:33 +03:00
|
|
|
stubSettings(server, {});
|
|
|
|
stubUser(server, {testUserFlag: false});
|
|
|
|
|
|
|
|
addTestFlag();
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2017-03-03 19:14:33 +03:00
|
|
|
|
|
|
|
return service.fetch().then(() => {
|
|
|
|
expect(service.get('testUserFlag')).to.be.false;
|
|
|
|
|
|
|
|
run(() => {
|
|
|
|
service.set('testUserFlag', true);
|
|
|
|
});
|
|
|
|
|
|
|
|
return wait().then(() => {
|
|
|
|
expect(server.handlers[3].numberOfCalls).to.equal(1);
|
|
|
|
expect(service.get('testUserFlag')).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('notifies for server errors on labs save', async function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSettings(server, {testFlag: false}, false);
|
2017-03-03 19:14:33 +03:00
|
|
|
stubUser(server, {});
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
addTestFlag();
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2016-11-24 01:50:57 +03:00
|
|
|
service.get('config').set('testFlag', false);
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return service.fetch().then(() => {
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-02-02 20:05:25 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
run(() => {
|
|
|
|
service.set('testFlag', true);
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
2016-02-02 20:05:25 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
return wait().then(() => {
|
|
|
|
expect(
|
|
|
|
server.handlers[1].numberOfCalls,
|
|
|
|
'PUT call is made'
|
|
|
|
).to.equal(1);
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(
|
|
|
|
service.get('notifications.alerts').length,
|
|
|
|
'number of alerts shown'
|
|
|
|
).to.equal(1);
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-02-09 22:39:15 +03:00
|
|
|
});
|
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('notifies for server errors on accessibility save', async function () {
|
2017-03-03 19:14:33 +03:00
|
|
|
stubSettings(server, {});
|
|
|
|
stubUser(server, {testUserFlag: false}, false);
|
|
|
|
|
|
|
|
addTestFlag();
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2017-03-03 19:14:33 +03:00
|
|
|
|
|
|
|
return service.fetch().then(() => {
|
|
|
|
expect(service.get('testUserFlag')).to.be.false;
|
|
|
|
|
|
|
|
run(() => {
|
|
|
|
service.set('testUserFlag', true);
|
|
|
|
});
|
|
|
|
|
|
|
|
return wait().then(() => {
|
|
|
|
expect(
|
|
|
|
server.handlers[3].numberOfCalls,
|
|
|
|
'PUT call is made'
|
|
|
|
).to.equal(1);
|
|
|
|
|
|
|
|
expect(
|
|
|
|
service.get('notifications.alerts').length,
|
|
|
|
'number of alerts shown'
|
|
|
|
).to.equal(1);
|
|
|
|
|
|
|
|
expect(service.get('testUserFlag')).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
it('notifies for validation errors', async function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSettings(server, {testFlag: false}, true, false);
|
2017-03-03 19:14:33 +03:00
|
|
|
stubUser(server, {});
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
addTestFlag();
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let session = this.owner.lookup('service:session');
|
|
|
|
await session.populateUser();
|
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let service = this.owner.lookup('service:feature');
|
2016-11-24 01:50:57 +03:00
|
|
|
service.get('config').set('testFlag', false);
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return service.fetch().then(() => {
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(service.get('testFlag')).to.be.false;
|
2016-02-09 22:39:15 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
run(() => {
|
|
|
|
expect(() => {
|
|
|
|
service.set('testFlag', true);
|
|
|
|
}, EmberError, 'threw validation error');
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
2016-02-09 22:39:15 +03:00
|
|
|
});
|
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
|
|
|
});
|