2015-11-04 18:20:11 +03:00
|
|
|
/* jshint expr:true */
|
|
|
|
import {
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
beforeEach,
|
|
|
|
afterEach
|
|
|
|
} from 'mocha';
|
2016-11-14 16:16:51 +03:00
|
|
|
import {expect} from 'chai';
|
2016-06-30 13:21:47 +03:00
|
|
|
import $ from 'jquery';
|
|
|
|
import run from 'ember-runloop';
|
2015-11-04 18:20:11 +03:00
|
|
|
import startApp from '../helpers/start-app';
|
2015-11-30 20:21:39 +03:00
|
|
|
import destroyApp from '../helpers/destroy-app';
|
2016-11-14 16:16:51 +03:00
|
|
|
import {authenticateSession, invalidateSession} from 'ghost-admin/tests/helpers/ember-simple-auth';
|
2015-11-04 18:20:11 +03:00
|
|
|
import Mirage from 'ember-cli-mirage';
|
2016-05-24 15:06:59 +03:00
|
|
|
import windowProxy from 'ghost-admin/utils/window-proxy';
|
|
|
|
import ghostPaths from 'ghost-admin/utils/ghost-paths';
|
2016-01-25 14:11:29 +03:00
|
|
|
|
|
|
|
const Ghost = ghostPaths();
|
2015-11-04 18:20:11 +03:00
|
|
|
|
|
|
|
describe('Acceptance: Authentication', function () {
|
|
|
|
let application,
|
|
|
|
originalReplaceLocation;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
application = startApp();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2015-11-30 20:21:39 +03:00
|
|
|
destroyApp(application);
|
2015-11-04 18:20:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('general page', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
originalReplaceLocation = windowProxy.replaceLocation;
|
|
|
|
windowProxy.replaceLocation = function (url) {
|
|
|
|
visit(url);
|
|
|
|
};
|
|
|
|
|
|
|
|
server.loadFixtures();
|
2015-10-28 14:36:45 +03:00
|
|
|
let role = server.create('role', {name: 'Administrator'});
|
2016-11-14 16:16:51 +03:00
|
|
|
server.create('user', {roles: [role], slug: 'test-user'});
|
2015-11-04 18:20:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
windowProxy.replaceLocation = originalReplaceLocation;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invalidates session on 401 API response', function () {
|
2016-05-14 04:02:55 +03:00
|
|
|
// return a 401 when attempting to retrieve users
|
2016-11-14 16:16:51 +03:00
|
|
|
server.get('/users/', () => {
|
2015-11-04 18:20:11 +03:00
|
|
|
return new Mirage.Response(401, {}, {
|
|
|
|
errors: [
|
|
|
|
{message: 'Access denied.', errorType: 'UnauthorizedError'}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
authenticateSession(application);
|
|
|
|
visit('/team');
|
|
|
|
|
|
|
|
andThen(() => {
|
|
|
|
expect(currentURL(), 'url after 401').to.equal('/signin');
|
|
|
|
});
|
|
|
|
});
|
2016-05-14 04:02:55 +03:00
|
|
|
|
|
|
|
it('doesn\'t show navigation menu on invalid url when not authenticated', function () {
|
|
|
|
invalidateSession(application);
|
|
|
|
|
|
|
|
visit('/');
|
|
|
|
|
|
|
|
andThen(() => {
|
|
|
|
expect(currentURL(), 'current url').to.equal('/signin');
|
|
|
|
expect(find('nav.gh-nav').length, 'nav menu presence').to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
visit('/signin/invalidurl/');
|
|
|
|
|
|
|
|
andThen(() => {
|
|
|
|
expect(currentURL(), 'url after invalid url').to.equal('/signin/invalidurl/');
|
|
|
|
expect(currentPath(), 'path after invalid url').to.equal('error404');
|
|
|
|
expect(find('nav.gh-nav').length, 'nav menu presence').to.equal(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows nav menu on invalid url when authenticated', function () {
|
|
|
|
authenticateSession(application);
|
|
|
|
visit('/signin/invalidurl/');
|
|
|
|
|
|
|
|
andThen(() => {
|
|
|
|
expect(currentURL(), 'url after invalid url').to.equal('/signin/invalidurl/');
|
|
|
|
expect(currentPath(), 'path after invalid url').to.equal('error404');
|
|
|
|
expect(find('nav.gh-nav').length, 'nav menu presence').to.equal(1);
|
|
|
|
});
|
|
|
|
});
|
2015-11-04 18:20:11 +03:00
|
|
|
});
|
|
|
|
|
2016-09-26 16:04:20 +03:00
|
|
|
describe.skip('editor', function () {
|
2016-06-11 19:52:36 +03:00
|
|
|
let origDebounce = run.debounce;
|
|
|
|
let origThrottle = run.throttle;
|
2015-11-04 18:20:11 +03:00
|
|
|
|
|
|
|
// we don't want the autosave interfering in this test
|
|
|
|
beforeEach(function () {
|
2016-06-11 19:52:36 +03:00
|
|
|
run.debounce = function () { };
|
|
|
|
run.throttle = function () { };
|
2015-11-04 18:20:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('displays re-auth modal attempting to save with invalid session', function () {
|
2015-10-28 14:36:45 +03:00
|
|
|
let role = server.create('role', {name: 'Administrator'});
|
2016-11-14 16:16:51 +03:00
|
|
|
server.create('user', {roles: [role]});
|
2015-11-04 18:20:11 +03:00
|
|
|
|
|
|
|
// simulate an invalid session when saving the edited post
|
|
|
|
server.put('/posts/:id/', (db, request) => {
|
2015-10-28 14:36:45 +03:00
|
|
|
let post = db.posts.find(request.params.id);
|
|
|
|
let [attrs] = JSON.parse(request.requestBody).posts;
|
2015-11-04 18:20:11 +03:00
|
|
|
|
|
|
|
if (attrs.markdown === 'Edited post body') {
|
|
|
|
return new Mirage.Response(401, {}, {
|
|
|
|
errors: [
|
|
|
|
{message: 'Access denied.', errorType: 'UnauthorizedError'}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
posts: [post]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.loadFixtures();
|
|
|
|
authenticateSession(application);
|
|
|
|
|
|
|
|
visit('/editor');
|
|
|
|
|
|
|
|
// create the post
|
|
|
|
fillIn('#entry-title', 'Test Post');
|
2016-09-26 16:04:20 +03:00
|
|
|
fillIn('.__mobiledoc-editor', 'Test post body');
|
2015-11-04 18:20:11 +03:00
|
|
|
click('.js-publish-button');
|
|
|
|
|
|
|
|
andThen(() => {
|
|
|
|
// we shouldn't have a modal at this point
|
|
|
|
expect(find('.modal-container #login').length, 'modal exists').to.equal(0);
|
|
|
|
// we also shouldn't have any alerts
|
|
|
|
expect(find('.gh-alert').length, 'no of alerts').to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// update the post
|
2016-09-26 16:04:20 +03:00
|
|
|
fillIn('.__mobiledoc-editor', 'Edited post body');
|
2015-11-04 18:20:11 +03:00
|
|
|
click('.js-publish-button');
|
|
|
|
|
|
|
|
andThen(() => {
|
|
|
|
// we should see a re-auth modal
|
2015-11-18 13:50:48 +03:00
|
|
|
expect(find('.fullscreen-modal #login').length, 'modal exists').to.equal(1);
|
2015-11-04 18:20:11 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// don't clobber debounce/throttle for future tests
|
|
|
|
afterEach(function () {
|
2016-06-11 19:52:36 +03:00
|
|
|
run.debounce = origDebounce;
|
|
|
|
run.throttle = origThrottle;
|
2015-11-04 18:20:11 +03:00
|
|
|
});
|
|
|
|
});
|
2016-01-25 14:11:29 +03:00
|
|
|
|
|
|
|
it('adds auth headers to jquery ajax', function (done) {
|
|
|
|
let role = server.create('role', {name: 'Administrator'});
|
2016-11-14 16:16:51 +03:00
|
|
|
server.create('user', {roles: [role]});
|
2016-01-25 14:11:29 +03:00
|
|
|
|
|
|
|
server.post('/uploads', (db, request) => {
|
|
|
|
return request;
|
|
|
|
});
|
|
|
|
server.loadFixtures();
|
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
/* eslint-disable camelcase */
|
2016-01-25 14:11:29 +03:00
|
|
|
authenticateSession(application, {
|
|
|
|
access_token: 'test_token',
|
|
|
|
expires_in: 3600,
|
|
|
|
token_type: 'Bearer'
|
|
|
|
});
|
2016-11-14 16:16:51 +03:00
|
|
|
/* eslint-enable camelcase */
|
2016-01-25 14:11:29 +03:00
|
|
|
|
|
|
|
// necessary to visit a page to fully boot the app in testing
|
|
|
|
visit('/').andThen(() => {
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: `${Ghost.apiRoot}/uploads/`,
|
|
|
|
data: {test: 'Test'}
|
|
|
|
}).then((request) => {
|
|
|
|
expect(request.requestHeaders.Authorization, 'Authorization header')
|
|
|
|
.to.exist;
|
|
|
|
expect(request.requestHeaders.Authorization, 'Authotization header content')
|
|
|
|
.to.equal('Bearer test_token');
|
|
|
|
}).always(() => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-11-04 18:20:11 +03:00
|
|
|
});
|