-
Notifications
You must be signed in to change notification settings - Fork 603
/
authentication-test.js
194 lines (164 loc) · 6.04 KB
/
authentication-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { currentURL, visit, fillIn, click } from '@ember/test-helpers';
import Pretender from 'pretender';
import {
invalidateSession,
authenticateSession,
currentSession,
} from 'ember-simple-auth/test-support';
import config from '../../config/environment';
module('Acceptance: Authentication', function (hooks) {
setupApplicationTest(hooks);
let server;
hooks.afterEach(function () {
if (server) {
server.shutdown();
}
});
test('logging in with correct credentials works', async function (assert) {
server = new Pretender(function () {
this.post(`${config.apiHost}/token`, () => [
200,
{ 'Content-Type': 'application/json' },
'{ "access_token": "secret token!", "account_id": 1 }',
]);
this.get(`${config.apiHost}/accounts/1`, () => [
200,
{ 'Content-Type': 'application/json' },
'{ "data": { "type": "accounts", "id": "1", "attributes": { "login": "letme", "name": "Some person" } } }',
]);
});
await invalidateSession();
await visit('/login');
await fillIn('[data-test-identification]', 'identification');
await fillIn('[data-test-password]', 'password');
await click('button[type="submit"]');
assert.equal(currentURL(), '/');
});
test('logging in with incorrect credentials shows an error', async function (assert) {
server = new Pretender(function () {
this.post(`${config.apiHost}/token`, () => [
400,
{ 'Content-Type': 'application/json' },
'{ "error": "invalid_grant" }',
]);
});
await invalidateSession();
await visit('/login');
await fillIn('[data-test-identification]', 'identification');
await fillIn('[data-test-password]', 'wrong-password!');
await click('button[type="submit"]');
assert.equal(currentURL(), '/login');
assert.ok(document.querySelector('[data-test-error-message]'));
});
module('the protected route', function () {
test('cannot be visited when the session is not authenticated', async function (assert) {
await invalidateSession();
await visit('/protected');
assert.equal(currentURL(), '/login');
});
test('can be visited when the session is authenticated', async function (assert) {
server = new Pretender(function () {
this.get(`${config.apiHost}/posts`, () => [
200,
{ 'Content-Type': 'application/json' },
'{"data":[]}',
]);
});
await visit('/');
await authenticateSession({ userId: 1, otherData: 'some-data' });
await visit('/protected');
let session = currentSession();
assert.equal(currentURL(), '/protected');
assert.equal(session.get('data.authenticated.userId'), 1);
assert.equal(session.get('data.authenticated.otherData'), 'some-data');
});
});
module('the auth-error route', function () {
test('invalidates the session', async function (assert) {
server = new Pretender(function () {
this.get(`${config.apiHost}/posts/3`, () => [
401,
{ 'Content-Type': 'application/json' },
'',
]);
});
await authenticateSession({});
try {
await visit('/auth-error');
} catch (e) {
// ignore the error
}
let session = currentSession();
assert.notOk(session.get('isAuthenticated'));
});
});
module('the protected route in the engine', function () {
test('cannot be visited when the session is not authenticated', async function (assert) {
await invalidateSession();
await visit('/engine/protected');
assert.equal(currentURL(), '/login');
});
test('can be visited when the session is authenticated', async function (assert) {
server = new Pretender(function () {
this.get(`${config.apiHost}/posts`, () => [
200,
{ 'Content-Type': 'application/json' },
'{"data":[]}',
]);
});
await authenticateSession({ userId: 1, otherData: 'some-data' });
await visit('/engine/protected');
assert.equal(currentURL(), '/engine/protected');
let session = currentSession();
assert.equal(session.get('data.authenticated.userId'), 1);
assert.equal(session.get('data.authenticated.otherData'), 'some-data');
});
test('can invalidate the session', async function (assert) {
server = new Pretender(function () {
this.get(`${config.apiHost}/posts`, () => [
200,
{ 'Content-Type': 'application/json' },
'{"data":[]}',
]);
});
await authenticateSession({ userId: 1, otherData: 'some-data' });
await visit('/engine/protected');
await click('[data-test-logout-button]');
let session = currentSession();
assert.notOk(session.get('isAuthenticated'));
});
});
module('the open-only route in the engine', function () {
test('cannot be visited when the session is authenticated', async function (assert) {
server = new Pretender(function () {
this.get(`${config.apiHost}/posts`, () => [
200,
{ 'Content-Type': 'application/json' },
'{"data":[]}',
]);
});
await authenticateSession({ userId: 1, otherData: 'some-data' });
await visit('/engine/open-only');
assert.equal(currentURL(), '/');
});
test('can be visited when the session is not authenticated', async function (assert) {
await invalidateSession();
await visit('/engine/open-only');
assert.equal(currentURL(), '/engine/open-only');
});
});
module('the login route', function () {
test('can be visited when the session is not authenticated', async function (assert) {
await invalidateSession();
await visit('/login');
assert.equal(currentURL(), '/login');
});
test('cannot be visited when the session is authenticated', async function (assert) {
await authenticateSession();
await visit('/login');
assert.equal(currentURL(), '/');
});
});
});