|
1 | 1 | 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'; |
2 | 11 | import { |
3 | 12 | describe, |
4 | 13 | it, |
5 | 14 | beforeEach, |
6 | 15 | afterEach |
7 | 16 | } from 'mocha'; |
8 | 17 | import { expect } from 'chai'; |
9 | | -import startApp from '../helpers/start-app'; |
10 | 18 | import Pretender from 'pretender'; |
11 | 19 | import { |
12 | 20 | invalidateSession, |
13 | 21 | authenticateSession, |
14 | 22 | currentSession |
15 | | -} from '../helpers/ember-simple-auth'; |
16 | | -import destroyApp from '../helpers/destroy-app'; |
| 23 | +} from 'ember-simple-auth/test-support'; |
17 | 24 | import config from '../../config/environment'; |
18 | 25 |
|
19 | 26 | describe('Acceptance: Authentication', function() { |
20 | | - let application; |
| 27 | + let context; |
21 | 28 | let server; |
22 | 29 |
|
23 | 30 | beforeEach(function() { |
24 | | - application = startApp(); |
| 31 | + context = {}; |
| 32 | + return setupContext(context).then(() => setupApplicationContext(context)); |
25 | 33 | }); |
26 | 34 |
|
27 | 35 | afterEach(function() { |
28 | 36 | tryInvoke(server, 'shutdown'); |
29 | | - destroyApp(application); |
| 37 | + return teardownApplicationContext(context).then(() => teardownContext(context)); |
30 | 38 | }); |
31 | 39 |
|
32 | 40 | 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 | + } |
36 | 45 |
|
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 | + }); |
40 | 52 | }); |
41 | 53 |
|
42 | 54 | it('can be visited when the session is authenticated', function() { |
43 | 55 | server = new Pretender(function() { |
44 | 56 | this.get(`${config.apiHost}/posts`, () => [200, { 'Content-Type': 'application/json' }, '{"data":[]}']); |
45 | 57 | }); |
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 | + }); |
55 | 66 | }); |
56 | 67 | }); |
57 | 68 |
|
58 | 69 | 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 | + } |
62 | 74 |
|
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 | + }); |
66 | 81 | }); |
67 | 82 |
|
68 | 83 | 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 | + }); |
75 | 89 | }); |
76 | 90 | }); |
77 | 91 | }); |
0 commit comments