Ghost/ghost/admin/tests/integration/services/feature-test.js
Kevin Ansfield 9bdb25d184
Fixed hosting management screen not loading after sign-in process (#15763)
refs https://github.com/TryGhost/Team/issues/2110

- dynamically defined properties on the config service did not have
autotracking set up properly if they were accessed in any way before the
property was defined, this caused problems in a number of areas because
we have both "unauthed" and "authed" sets of config and when not logged
in we had parts of the app checking for authed config properties that
don't exist until after sign-in and subsequent config re-fetch
- renamed `config` service to `configManager` and updated to only
contain methods for fetching config data
- added a `config` instance initializer that sets up a `TrackedObject`
instance with some custom properties/methods and registers it on
`config:main`
- uses application instance initializer rather than a standard
initializer because standard initializers are only called once when
setting up the test suite so we'd end up with config leaking across
tests
- added an `@inject` decorator that when used takes the property name
and injects whatever is registered at `${propertyName}:main`, this
allows us to use dependency injection for any object rather than just
services or controllers
- using `application.inject()` in the initializer was initially used but
that only works for objects that extend from `EmberObject`, the
injections weren't available in native-class glimmer components so this
decorator keeps the injection syntax consistent
  - swapped all `@service config` uses to `@inject config`
2022-11-03 11:14:36 +00:00

383 lines
12 KiB
JavaScript

import EmberError from '@ember/error';
import FeatureService, {feature} from 'ghost-admin/services/feature';
import Pretender from 'pretender';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {run} from '@ember/runloop';
import {settled} from '@ember/test-helpers';
import {setupTest} from 'ember-mocha';
function stubSettings(server, labs, validSave = true) {
const site = [];
const settings = [
{
id: '1',
type: 'labs',
key: 'labs',
value: JSON.stringify(labs)
}
];
server.get(`${ghostPaths().apiRoot}/site/`, function () {
return [200, {'Content-Type': 'application/json'}, JSON.stringify({site})];
});
server.get(`${ghostPaths().apiRoot}/settings/`, function () {
return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings})];
});
server.put(`${ghostPaths().apiRoot}/settings/`, function (request) {
let statusCode = (validSave) ? 200 : 400;
let response = (validSave) ? request.requestBody : JSON.stringify({
errors: [{
message: 'Test Error'
}]
});
return [statusCode, {'Content-Type': 'application/json'}, response];
});
}
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'
}]
}];
server.get(`${ghostPaths().apiRoot}/users/me/`, function () {
return [200, {'Content-Type': 'application/json'}, JSON.stringify({users})];
});
server.put(`${ghostPaths().apiRoot}/users/1/`, function (request) {
let statusCode = (validSave) ? 200 : 400;
let response = (validSave) ? request.requestBody : JSON.stringify({
errors: [{
message: 'Test Error'
}]
});
return [statusCode, {'Content-Type': 'application/json'}, response];
});
}
function addTestFlag() {
FeatureService.reopen({
testFlag: feature('testFlag'),
testUserFlag: feature('testUserFlag', {user: true})
});
}
describe('Integration: Service: feature', function () {
setupTest();
let server;
beforeEach(function () {
server = new Pretender();
});
afterEach(function () {
server.shutdown();
});
it('loads labs and user settings correctly', async function () {
stubSettings(server, {testFlag: true});
stubUser(server, {testUserFlag: true});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
return service.fetch().then(() => {
expect(service.get('testFlag')).to.be.true;
expect(service.get('testUserFlag')).to.be.true;
});
});
it('returns false for set flag with config false and labs false', async function () {
stubSettings(server, {testFlag: false});
stubUser(server, {});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
service.config.testFlag = false;
return service.fetch().then(() => {
expect(service.get('labs.testFlag')).to.be.false;
expect(service.get('testFlag')).to.be.false;
});
});
it('returns true for set flag with config true and labs false', async function () {
stubSettings(server, {testFlag: false});
stubUser(server, {});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
service.config.testFlag = true;
return service.fetch().then(() => {
expect(service.get('labs.testFlag')).to.be.false;
expect(service.get('testFlag')).to.be.true;
});
});
it('returns true for set flag with config false and labs true', async function () {
stubSettings(server, {testFlag: true});
stubUser(server, {});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
service.config.testFlag = false;
return service.fetch().then(() => {
expect(service.get('labs.testFlag')).to.be.true;
expect(service.get('testFlag')).to.be.true;
});
});
it('returns true for set flag with config true and labs true', async function () {
stubSettings(server, {testFlag: true});
stubUser(server, {});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
service.config.testFlag = true;
return service.fetch().then(() => {
expect(service.get('labs.testFlag')).to.be.true;
expect(service.get('testFlag')).to.be.true;
});
});
it('returns false for set flag with accessibility false', async function () {
stubSettings(server, {});
stubUser(server, {testUserFlag: false});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
return service.fetch().then(() => {
expect(service.get('accessibility.testUserFlag')).to.be.false;
expect(service.get('testUserFlag')).to.be.false;
});
});
it('returns true for set flag with accessibility true', async function () {
stubSettings(server, {});
stubUser(server, {testUserFlag: true});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
return service.fetch().then(() => {
expect(service.get('accessibility.testUserFlag')).to.be.true;
expect(service.get('testUserFlag')).to.be.true;
});
});
it('saves labs setting correctly', async function () {
stubSettings(server, {testFlag: false});
stubUser(server, {testUserFlag: false});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
service.config.testFlag = false;
return service.fetch().then(() => {
expect(service.get('testFlag')).to.be.false;
run(() => {
service.set('testFlag', true);
});
return settled().then(() => {
expect(server.handlers[2].numberOfCalls).to.equal(1);
expect(service.get('testFlag')).to.be.true;
});
});
});
it('saves accessibility setting correctly', async function () {
stubSettings(server, {});
stubUser(server, {testUserFlag: false});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
return service.fetch().then(() => {
expect(service.get('testUserFlag')).to.be.false;
run(() => {
service.set('testUserFlag', true);
});
return settled().then(() => {
expect(server.handlers[4].numberOfCalls).to.equal(1);
expect(service.get('testUserFlag')).to.be.true;
});
});
});
it('notifies for server errors on labs save', async function () {
stubSettings(server, {testFlag: false}, false);
stubUser(server, {});
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
service.config.testFlag = false;
return service.fetch().then(() => {
expect(service.get('testFlag')).to.be.false;
run(() => {
service.set('testFlag', true);
});
return settled().then(() => {
expect(
server.handlers[2].numberOfCalls,
'PUT call is made'
).to.equal(1);
expect(
service.get('notifications.alerts').length,
'number of alerts shown'
).to.equal(1);
expect(service.get('testFlag')).to.be.false;
});
});
});
it('notifies for server errors on accessibility save', async function () {
stubSettings(server, {});
stubUser(server, {testUserFlag: false}, false);
addTestFlag();
let session = this.owner.lookup('service:session');
await session.populateUser();
let service = this.owner.lookup('service:feature');
return service.fetch().then(() => {
expect(service.get('testUserFlag')).to.be.false;
run(() => {
service.set('testUserFlag', true);
});
return settled().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;
});
});
});
it('notifies for validation errors', async function () {
stubSettings(server, {testFlag: false}, true, false);
stubUser(server, {});
addTestFlag();
let sessionService = this.owner.lookup('service:session');
await sessionService.populateUser();
let featureService = this.owner.lookup('service:feature');
featureService.config.testFlag = false;
return featureService.fetch().then(() => {
expect(featureService.get('testFlag'), 'testFlag before set').to.be.false;
run(() => {
expect(() => {
featureService.set('testFlag', true);
}, EmberError, 'threw validation error');
});
return settled().then(() => {
// ensure validation is happening before the API is hit
expect(server.handlers[2].numberOfCalls).to.equal(0);
expect(featureService.get('testFlag')).to.be.false;
});
});
});
it('has correct labs flags when accessed before and after settings load', async function () {
stubSettings(server, {testFlag: true});
stubUser(server, {});
addTestFlag();
const settingsService = this.owner.lookup('service:settings');
const featureService = this.owner.lookup('service:feature');
expect(featureService.testFlag, 'testFlag before settings fetch').to.be.false;
await settingsService.fetch();
expect(featureService.settings.labs, 'feature.settings.labs after settings fetch').to.equal('{"testFlag":true}');
expect(featureService.labs, 'feature.labs after settings fetch').to.deep.equal({testFlag: true});
expect(featureService.testFlag, 'feature.testFlag after settings fetch').to.be.true;
});
});