2018-10-05 21:46:33 +03:00
|
|
|
import Service from '@ember/service';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import {beforeEach, describe, it} from 'mocha';
|
|
|
|
import {expect} from 'chai';
|
|
|
|
import {setupTest} from 'ember-mocha';
|
|
|
|
|
|
|
|
const mockAjax = Service.extend({
|
|
|
|
skipSessionDeletion: false,
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.post = sinon.stub().resolves();
|
|
|
|
this.del = sinon.stub().resolves();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-12 11:11:10 +03:00
|
|
|
const mockConfig = Service.extend({
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.fetchAuthenticated = sinon.stub().resolves();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockFeature = Service.extend({
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.fetch = sinon.stub().resolves();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockSettings = Service.extend({
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.fetch = sinon.stub().resolves();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockTour = Service.extend({
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.fetchViewed = sinon.stub().resolves();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-05 21:46:33 +03:00
|
|
|
const mockGhostPaths = Service.extend({
|
2019-08-12 11:56:21 +03:00
|
|
|
apiRoot: '/ghost/api/canary/admin'
|
2018-10-05 21:46:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Unit: Authenticator: cookie', () => {
|
2019-05-13 15:43:53 +03:00
|
|
|
setupTest();
|
2018-10-05 21:46:33 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-05-13 15:43:53 +03:00
|
|
|
this.owner.register('service:ajax', mockAjax);
|
2019-08-12 11:11:10 +03:00
|
|
|
this.owner.register('service:config', mockConfig);
|
|
|
|
this.owner.register('service:feature', mockFeature);
|
|
|
|
this.owner.register('service:settings', mockSettings);
|
|
|
|
this.owner.register('service:tour', mockTour);
|
2019-05-13 15:43:53 +03:00
|
|
|
this.owner.register('service:ghost-paths', mockGhostPaths);
|
2018-10-05 21:46:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#restore', function () {
|
|
|
|
it('returns a resolving promise', function () {
|
2019-05-13 15:43:53 +03:00
|
|
|
return this.owner.lookup('authenticator:cookie').restore();
|
2018-10-05 21:46:33 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#authenticate', function () {
|
|
|
|
it('posts the username and password to the sessionEndpoint and returns the promise', function () {
|
2019-05-13 15:43:53 +03:00
|
|
|
let authenticator = this.owner.lookup('authenticator:cookie');
|
2018-10-05 21:46:33 +03:00
|
|
|
let post = authenticator.ajax.post;
|
|
|
|
|
2019-08-12 11:11:10 +03:00
|
|
|
let config = this.owner.lookup('service:config');
|
|
|
|
let feature = this.owner.lookup('service:feature');
|
|
|
|
let settings = this.owner.lookup('service:settings');
|
|
|
|
let tour = this.owner.lookup('service:tour');
|
|
|
|
|
2018-10-05 21:46:33 +03:00
|
|
|
return authenticator.authenticate('AzureDiamond', 'hunter2').then(() => {
|
2019-08-12 11:56:21 +03:00
|
|
|
expect(post.args[0][0]).to.equal('/ghost/api/canary/admin/session');
|
2018-10-05 21:46:33 +03:00
|
|
|
expect(post.args[0][1]).to.deep.include({
|
|
|
|
data: {
|
|
|
|
username: 'AzureDiamond',
|
|
|
|
password: 'hunter2'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(post.args[0][1]).to.deep.include({
|
|
|
|
dataType: 'text'
|
|
|
|
});
|
|
|
|
expect(post.args[0][1]).to.deep.include({
|
|
|
|
contentType: 'application/json;charset=utf-8'
|
|
|
|
});
|
2019-08-12 11:11:10 +03:00
|
|
|
|
|
|
|
// ensure our pre-loading calls have been made
|
|
|
|
expect(config.fetchAuthenticated.calledOnce, 'config.fetchAuthenticated called').to.be.true;
|
|
|
|
expect(feature.fetch.calledOnce, 'feature.fetch called').to.be.true;
|
|
|
|
expect(settings.fetch.calledOnce, 'settings.fetch called').to.be.true;
|
|
|
|
expect(tour.fetchViewed.calledOnce, 'tour.fetchViewed called').to.be.true;
|
2018-10-05 21:46:33 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#invalidate', function () {
|
|
|
|
it('makes a delete request to the sessionEndpoint', function () {
|
2019-05-13 15:43:53 +03:00
|
|
|
let authenticator = this.owner.lookup('authenticator:cookie');
|
2018-10-05 21:46:33 +03:00
|
|
|
let del = authenticator.ajax.del;
|
|
|
|
|
|
|
|
return authenticator.invalidate().then(() => {
|
2019-08-12 11:56:21 +03:00
|
|
|
expect(del.args[0][0]).to.equal('/ghost/api/canary/admin/session');
|
2018-10-05 21:46:33 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|