Skip to content

Commit 28d9078

Browse files
authored
Merge pull request #1996 from simplabs/deprecate-old-testing
Deprecate the old testing API
2 parents a0a5d09 + 516c773 commit 28d9078

File tree

6 files changed

+206
-124
lines changed

6 files changed

+206
-124
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,9 @@ module('Acceptance | super secret url', function(hooks) {
785785

786786
### ember-cli-qunit 4.1.0 and earlier
787787

788+
__Deprecation warning:__ Support for ember-cli-qunit 4.1.0 and earlier is
789+
deprecated and will be removed in the next major version of ember-simple-auth.
790+
788791
For apps using earlier versions of ember-cli-qunit, you can use the
789792
test helpers with the following signature:
790793

test-support/helpers/ember-simple-auth.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { deprecate } from '@ember/application/deprecations';
12
import Test from 'ember-simple-auth/authenticators/test';
23

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

13+
function deprecateOldTestingApi() {
14+
deprecate('Ember Simple Auth: The legacy testing API is deprecated; switch to the new testing helpers available from "ember-simple-auth/test-support".', false, {
15+
id: `ember-simple-auth.testing.legacy-testing-api`,
16+
until: '3.0.0'
17+
});
18+
}
19+
1220
export function authenticateSession(app, sessionData) {
21+
deprecateOldTestingApi();
1322
const { __container__: container } = app;
1423
const session = container.lookup('service:session');
1524
ensureAuthenticator(app, container);
@@ -18,10 +27,12 @@ export function authenticateSession(app, sessionData) {
1827
}
1928

2029
export function currentSession(app) {
30+
deprecateOldTestingApi();
2131
return app.__container__.lookup('service:session');
2232
}
2333

2434
export function invalidateSession(app) {
35+
deprecateOldTestingApi();
2536
const session = app.__container__.lookup('service:session');
2637
if (session.get('isAuthenticated')) {
2738
session.invalidate();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { tryInvoke } from '@ember/utils';
2+
import {
3+
describe,
4+
it,
5+
beforeEach,
6+
afterEach
7+
} from 'mocha';
8+
import { expect } from 'chai';
9+
import startApp from '../helpers/start-app';
10+
import Pretender from 'pretender';
11+
import {
12+
invalidateSession,
13+
authenticateSession,
14+
currentSession
15+
} from '../helpers/ember-simple-auth';
16+
import destroyApp from '../helpers/destroy-app';
17+
import config from '../../config/environment';
18+
19+
describe('Acceptance: Authentication - legacy style helpers', function() {
20+
let application;
21+
let server;
22+
23+
beforeEach(function() {
24+
application = startApp();
25+
});
26+
27+
afterEach(function() {
28+
tryInvoke(server, 'shutdown');
29+
destroyApp(application);
30+
});
31+
32+
describe('the protected route', function() {
33+
it('cannot be visited when the session is not authenticated', function() {
34+
invalidateSession(application);
35+
visit('/protected');
36+
37+
return andThen(() => {
38+
expect(currentPath()).to.eq('login');
39+
});
40+
});
41+
42+
it('can be visited when the session is authenticated', function() {
43+
server = new Pretender(function() {
44+
this.get(`${config.apiHost}/posts`, () => [200, { 'Content-Type': 'application/json' }, '{"data":[]}']);
45+
});
46+
authenticateSession(application, { userId: 1, otherData: 'some-data' });
47+
visit('/protected');
48+
49+
return andThen(() => {
50+
expect(currentPath()).to.eq('protected');
51+
let session = currentSession(application);
52+
expect(session.get('data.authenticated.userId')).to.eql(1);
53+
expect(session.get('data.authenticated.otherData')).to.eql('some-data');
54+
});
55+
});
56+
});
57+
58+
describe('the login route', function() {
59+
it('can be visited when the session is not authenticated', function() {
60+
invalidateSession(application);
61+
visit('/login');
62+
63+
return andThen(() => {
64+
expect(currentPath()).to.eq('login');
65+
});
66+
});
67+
68+
it('cannot be visited when the session is authenticated', function() {
69+
authenticateSession(application);
70+
visit('/login');
71+
72+
return andThen(() => {
73+
expect(currentPath()).to.eq('index');
74+
});
75+
});
76+
});
77+
});
Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,91 @@
11
import { tryInvoke } from '@ember/utils';
2+
import {
3+
currentURL,
4+
setupContext,
5+
setupApplicationContext,
6+
teardownApplicationContext,
7+
teardownContext,
8+
visit
9+
} from '@ember/test-helpers';
10+
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
211
import {
312
describe,
413
it,
514
beforeEach,
615
afterEach
716
} from 'mocha';
817
import { expect } from 'chai';
9-
import startApp from '../helpers/start-app';
1018
import Pretender from 'pretender';
1119
import {
1220
invalidateSession,
1321
authenticateSession,
1422
currentSession
15-
} from '../helpers/ember-simple-auth';
16-
import destroyApp from '../helpers/destroy-app';
23+
} from 'ember-simple-auth/test-support';
1724
import config from '../../config/environment';
1825

1926
describe('Acceptance: Authentication', function() {
20-
let application;
27+
let context;
2128
let server;
2229

2330
beforeEach(function() {
24-
application = startApp();
31+
context = {};
32+
return setupContext(context).then(() => setupApplicationContext(context));
2533
});
2634

2735
afterEach(function() {
2836
tryInvoke(server, 'shutdown');
29-
destroyApp(application);
37+
return teardownApplicationContext(context).then(() => teardownContext(context));
3038
});
3139

3240
describe('the protected route', function() {
33-
it('cannot be visited when the session is not authenticated', function() {
34-
invalidateSession(application);
35-
visit('/protected');
41+
if (!hasEmberVersion(2, 4)) {
42+
// guard against running test module on unsupported version (before 2.4)
43+
return;
44+
}
3645

37-
return andThen(() => {
38-
expect(currentPath()).to.eq('login');
39-
});
46+
it('cannot be visited when the session is not authenticated', function() {
47+
return invalidateSession()
48+
.then(() => visit('/protected'))
49+
.then(() => {
50+
expect(currentURL()).to.eq('/login');
51+
});
4052
});
4153

4254
it('can be visited when the session is authenticated', function() {
4355
server = new Pretender(function() {
4456
this.get(`${config.apiHost}/posts`, () => [200, { 'Content-Type': 'application/json' }, '{"data":[]}']);
4557
});
46-
authenticateSession(application, { userId: 1, otherData: 'some-data' });
47-
visit('/protected');
48-
49-
return andThen(() => {
50-
expect(currentPath()).to.eq('protected');
51-
let session = currentSession(application);
52-
expect(session.get('data.authenticated.userId')).to.eql(1);
53-
expect(session.get('data.authenticated.otherData')).to.eql('some-data');
54-
});
58+
return authenticateSession({ userId: 1, otherData: 'some-data' })
59+
.then(() => visit('/protected'))
60+
.then(() => {
61+
let session = currentSession();
62+
expect(currentURL()).to.eq('/protected');
63+
expect(session.get('data.authenticated.userId')).to.eql(1);
64+
expect(session.get('data.authenticated.otherData')).to.eql('some-data');
65+
});
5566
});
5667
});
5768

5869
describe('the login route', function() {
59-
it('can be visited when the session is not authenticated', function() {
60-
invalidateSession(application);
61-
visit('/login');
70+
if (!hasEmberVersion(2, 4)) {
71+
// guard against running test module on unsupported version (before 2.4)
72+
return;
73+
}
6274

63-
return andThen(() => {
64-
expect(currentPath()).to.eq('login');
65-
});
75+
it('can be visited when the session is not authenticated', function() {
76+
return invalidateSession()
77+
.then(() => visit('/login'))
78+
.then(() => {
79+
expect(currentURL()).to.eq('/login');
80+
});
6681
});
6782

6883
it('cannot be visited when the session is authenticated', function() {
69-
authenticateSession(application);
70-
visit('/login');
71-
72-
return andThen(() => {
73-
expect(currentPath()).to.eq('index');
74-
});
84+
return authenticateSession()
85+
.then(() => visit('/login'))
86+
.then(() => {
87+
expect(currentURL()).to.eq('/');
88+
});
7589
});
7690
});
7791
});

tests/acceptance/new-style-helpers-test.js

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)