Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ module('Acceptance | super secret url', function(hooks) {

### ember-cli-qunit 4.1.0 and earlier

__Deprecation warning:__ Support for ember-cli-qunit 4.1.0 and earlier is
deprecated and will be removed in the next major version of ember-simple-auth.

For apps using earlier versions of ember-cli-qunit, you can use the
test helpers with the following signature:

Expand Down
11 changes: 11 additions & 0 deletions test-support/helpers/ember-simple-auth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deprecate } from '@ember/application/deprecations';
import Test from 'ember-simple-auth/authenticators/test';

const TEST_CONTAINER_KEY = 'authenticator:test';
Expand All @@ -9,7 +10,15 @@ function ensureAuthenticator(app, container) {
}
}

function deprecateOldTestingApi() {
deprecate('Ember Simple Auth: The legacy testing API is deprecated; switch to the new testing helpers available from "ember-simple-auth/test-support".', false, {
id: `ember-simple-auth.testing.legacy-testing-api`,
until: '3.0.0'
});
}

export function authenticateSession(app, sessionData) {
deprecateOldTestingApi();
const { __container__: container } = app;
const session = container.lookup('service:session');
ensureAuthenticator(app, container);
Expand All @@ -18,10 +27,12 @@ export function authenticateSession(app, sessionData) {
}

export function currentSession(app) {
deprecateOldTestingApi();
return app.__container__.lookup('service:session');
}

export function invalidateSession(app) {
deprecateOldTestingApi();
const session = app.__container__.lookup('service:session');
if (session.get('isAuthenticated')) {
session.invalidate();
Expand Down
77 changes: 77 additions & 0 deletions tests/acceptance/authentication-legacy-style-helpers-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { tryInvoke } from '@ember/utils';
import {
describe,
it,
beforeEach,
afterEach
} from 'mocha';
import { expect } from 'chai';
import startApp from '../helpers/start-app';
import Pretender from 'pretender';
import {
invalidateSession,
authenticateSession,
currentSession
} from '../helpers/ember-simple-auth';
import destroyApp from '../helpers/destroy-app';
import config from '../../config/environment';

describe('Acceptance: Authentication - legacy style helpers', function() {
let application;
let server;

beforeEach(function() {
application = startApp();
});

afterEach(function() {
tryInvoke(server, 'shutdown');
destroyApp(application);
});

describe('the protected route', function() {
it('cannot be visited when the session is not authenticated', function() {
invalidateSession(application);
visit('/protected');

return andThen(() => {
expect(currentPath()).to.eq('login');
});
});

it('can be visited when the session is authenticated', function() {
server = new Pretender(function() {
this.get(`${config.apiHost}/posts`, () => [200, { 'Content-Type': 'application/json' }, '{"data":[]}']);
});
authenticateSession(application, { userId: 1, otherData: 'some-data' });
visit('/protected');

return andThen(() => {
expect(currentPath()).to.eq('protected');
let session = currentSession(application);
expect(session.get('data.authenticated.userId')).to.eql(1);
expect(session.get('data.authenticated.otherData')).to.eql('some-data');
});
});
});

describe('the login route', function() {
it('can be visited when the session is not authenticated', function() {
invalidateSession(application);
visit('/login');

return andThen(() => {
expect(currentPath()).to.eq('login');
});
});

it('cannot be visited when the session is authenticated', function() {
authenticateSession(application);
visit('/login');

return andThen(() => {
expect(currentPath()).to.eq('index');
});
});
});
});
80 changes: 47 additions & 33 deletions tests/acceptance/authentication-test.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,91 @@
import { tryInvoke } from '@ember/utils';
import {
currentURL,
setupContext,
setupApplicationContext,
teardownApplicationContext,
teardownContext,
visit
} from '@ember/test-helpers';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import {
describe,
it,
beforeEach,
afterEach
} from 'mocha';
import { expect } from 'chai';
import startApp from '../helpers/start-app';
import Pretender from 'pretender';
import {
invalidateSession,
authenticateSession,
currentSession
} from '../helpers/ember-simple-auth';
import destroyApp from '../helpers/destroy-app';
} from 'ember-simple-auth/test-support';
import config from '../../config/environment';

describe('Acceptance: Authentication', function() {
let application;
let context;
let server;

beforeEach(function() {
application = startApp();
context = {};
return setupContext(context).then(() => setupApplicationContext(context));
});

afterEach(function() {
tryInvoke(server, 'shutdown');
destroyApp(application);
return teardownApplicationContext(context).then(() => teardownContext(context));
});

describe('the protected route', function() {
it('cannot be visited when the session is not authenticated', function() {
invalidateSession(application);
visit('/protected');
if (!hasEmberVersion(2, 4)) {
// guard against running test module on unsupported version (before 2.4)
return;
}

return andThen(() => {
expect(currentPath()).to.eq('login');
});
it('cannot be visited when the session is not authenticated', function() {
return invalidateSession()
.then(() => visit('/protected'))
.then(() => {
expect(currentURL()).to.eq('/login');
});
});

it('can be visited when the session is authenticated', function() {
server = new Pretender(function() {
this.get(`${config.apiHost}/posts`, () => [200, { 'Content-Type': 'application/json' }, '{"data":[]}']);
});
authenticateSession(application, { userId: 1, otherData: 'some-data' });
visit('/protected');

return andThen(() => {
expect(currentPath()).to.eq('protected');
let session = currentSession(application);
expect(session.get('data.authenticated.userId')).to.eql(1);
expect(session.get('data.authenticated.otherData')).to.eql('some-data');
});
return authenticateSession({ userId: 1, otherData: 'some-data' })
.then(() => visit('/protected'))
.then(() => {
let session = currentSession();
expect(currentURL()).to.eq('/protected');
expect(session.get('data.authenticated.userId')).to.eql(1);
expect(session.get('data.authenticated.otherData')).to.eql('some-data');
});
});
});

describe('the login route', function() {
it('can be visited when the session is not authenticated', function() {
invalidateSession(application);
visit('/login');
if (!hasEmberVersion(2, 4)) {
// guard against running test module on unsupported version (before 2.4)
return;
}

return andThen(() => {
expect(currentPath()).to.eq('login');
});
it('can be visited when the session is not authenticated', function() {
return invalidateSession()
.then(() => visit('/login'))
.then(() => {
expect(currentURL()).to.eq('/login');
});
});

it('cannot be visited when the session is authenticated', function() {
authenticateSession(application);
visit('/login');

return andThen(() => {
expect(currentPath()).to.eq('index');
});
return authenticateSession()
.then(() => visit('/login'))
.then(() => {
expect(currentURL()).to.eq('/');
});
});
});
});
91 changes: 0 additions & 91 deletions tests/acceptance/new-style-helpers-test.js

This file was deleted.

Loading