mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 00:11:49 +03:00
c646e78fff
no issue Having `session.user` return a promise made dealing with it in components difficult because you always had to remember it returned a promise rather than a model and had to handle the async behaviour. It also meant that you couldn't use any current user properties directly inside getters which made refactors to Glimmer/Octane idioms harder to reason about. `session.user` was a cached computed property so it really made no sense for it to be a promise - it was loaded on first access and then always returned instantly but with a fulfilled promise rather than the underlying model. Refactoring to a synchronous property that is loaded as part of the authentication flows (we load the current user to check that we're logged in - we may as well make use of that!) means one less thing to be aware of/remember and provides a nicer migration process to Glimmer components. As part of the refactor, the auth flows and pre-load of required data across other services was also simplified to make it easier to find and follow. - refactored app setup and `session.user` - added `session.populateUser()` that fetches a user model from the current user endpoint and sets it on `session.user` - removed knowledge of app setup from the `cookie` authenticator and moved it into = `session.postAuthPreparation()`, this means we have the same post-authentication setup no matter which authenticator is used so we have more consistent behaviour in tests which don't use the `cookie` authenticator - switched `session` service to native class syntax to get the expected `super()` behaviour - updated `handleAuthentication()` so it populate's `session.user` and performs post-auth setup before transitioning (handles sign-in after app load) - updated `application` route to remove duplicated knowledge of app preload behaviour that now lives in `session.postAuthPreparation()` (handles already-authed app load) - removed out-of-date attempt at pre-loading data from setup controller as that's now handled automatically via `session.handleAuthentication` - updated app code to not treat `session.user` as a promise - predominant usage was router `beforeModel` hooks that transitioned users without valid permissions, this sets us up for an easier removal of the `current-user-settings` mixin in the future
359 lines
11 KiB
JavaScript
359 lines
11 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 wait from 'ember-test-helpers/wait';
|
|
import {describe, it} from 'mocha';
|
|
import {expect} from 'chai';
|
|
import {run} from '@ember/runloop';
|
|
import {setupTest} from 'ember-mocha';
|
|
|
|
function stubSettings(server, labs, validSave = true) {
|
|
let settings = [
|
|
{
|
|
id: '1',
|
|
type: 'labs',
|
|
key: 'labs',
|
|
value: JSON.stringify(labs)
|
|
}
|
|
];
|
|
|
|
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.get('config').set('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.get('config').set('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.get('config').set('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.get('config').set('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.get('config').set('testFlag', false);
|
|
|
|
return service.fetch().then(() => {
|
|
expect(service.get('testFlag')).to.be.false;
|
|
|
|
run(() => {
|
|
service.set('testFlag', true);
|
|
});
|
|
|
|
return wait().then(() => {
|
|
expect(server.handlers[1].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 wait().then(() => {
|
|
expect(server.handlers[3].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.get('config').set('testFlag', false);
|
|
|
|
return service.fetch().then(() => {
|
|
expect(service.get('testFlag')).to.be.false;
|
|
|
|
run(() => {
|
|
service.set('testFlag', true);
|
|
});
|
|
|
|
return wait().then(() => {
|
|
expect(
|
|
server.handlers[1].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 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;
|
|
});
|
|
});
|
|
});
|
|
|
|
it('notifies for validation errors', async function () {
|
|
stubSettings(server, {testFlag: false}, true, false);
|
|
stubUser(server, {});
|
|
|
|
addTestFlag();
|
|
|
|
let session = this.owner.lookup('service:session');
|
|
await session.populateUser();
|
|
|
|
let service = this.owner.lookup('service:feature');
|
|
service.get('config').set('testFlag', false);
|
|
|
|
return service.fetch().then(() => {
|
|
expect(service.get('testFlag')).to.be.false;
|
|
|
|
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;
|
|
});
|
|
});
|
|
});
|
|
});
|