Skip to content

Commit 498506f

Browse files
committed
Auto merge of #2067 - Turbo87:login-tests, r=locks
tests: Add basic "successful/failed login" test cases This PR adds two basic application tests for the login button and the `login` route. Please note that this does not test the `github-login` and `github-authorize` routes yet, which we will do in a follow-up PR. r? @locks /cc @carols10cents
2 parents 159977b + f9007c1 commit 498506f

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

app/routes/login.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Route from '@ember/routing/route';
22
import { inject as service } from '@ember/service';
3+
import window from 'ember-window-mock';
34

45
/**
56
* This route will open a popup window directed at the `github-login` route.

app/templates/application.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565
</RlDropdownContainer>
6666
<span class="sep">|</span>
6767
{{#if this.session.currentUser}}
68-
<RlDropdownContainer class="dropdown-container">
69-
<RlDropdownToggle class="dropdown">
70-
<UserAvatar @user={{this.session.currentUser}} @size="small" />
68+
<RlDropdownContainer class="dropdown-container" data-test-user-menu>
69+
<RlDropdownToggle class="dropdown" data-test-toggle>
70+
<UserAvatar @user={{this.session.currentUser}} @size="small" data-test-avatar />
7171
{{ this.session.currentUser.name }}
7272
<span class='arrow'></span>
7373
</RlDropdownToggle>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<p id="flash" class={{if this.message "shown"}} ...attributes>
1+
<p id="flash" class={{if this.message "shown"}} data-test-flash-message ...attributes>
22
{{this.message}}
33
</p>

tests/acceptance/login-test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { module, test } from 'qunit';
2+
import { setupApplicationTest } from 'ember-qunit';
3+
import { visit, currentURL, click, waitFor } from '@ember/test-helpers';
4+
import { defer } from 'rsvp';
5+
import window, { setupWindowMock } from 'ember-window-mock';
6+
import setupMirage from '../helpers/setup-mirage';
7+
8+
module('Acceptance | Login', function(hooks) {
9+
setupApplicationTest(hooks);
10+
setupWindowMock(hooks);
11+
setupMirage(hooks);
12+
13+
test('successful login', async function(assert) {
14+
let deferred = defer();
15+
let fakeWindow = { closed: false };
16+
window.open = (url, target, features) => {
17+
assert.equal(url, '/github_login');
18+
assert.equal(target, 'Authorization');
19+
assert.equal(features, 'width=1000,height=450,toolbar=0,scrollbars=1,status=1,resizable=1,location=1,menuBar=0');
20+
deferred.resolve();
21+
return fakeWindow;
22+
};
23+
24+
await visit('/');
25+
assert.equal(currentURL(), '/');
26+
27+
await click('[data-test-login-link]');
28+
assert.equal(currentURL(), '/');
29+
30+
// wait for `window.open()` to be called
31+
await deferred.promise;
32+
33+
// simulate the response from the `github-authorize` route
34+
window.github_response = JSON.stringify({
35+
ok: true,
36+
data: {
37+
user: {
38+
id: 42,
39+
login: 'johnnydee',
40+
name: 'John Doe',
41+
email: 'john@doe.name',
42+
avatar: 'https://avatars2.githubusercontent.com/u/12345?v=4',
43+
url: 'https://github.com/johnnydee',
44+
},
45+
},
46+
});
47+
48+
// simulate that the window has been closed by the `github-authorize` route
49+
fakeWindow.closed = true;
50+
51+
// wait for the user menu to show up after the successful login
52+
await waitFor('[data-test-user-menu]');
53+
54+
assert.dom('[data-test-user-menu] [data-test-toggle]').hasText('John Doe');
55+
});
56+
57+
test('failed login', async function(assert) {
58+
let deferred = defer();
59+
let fakeWindow = { closed: false };
60+
window.open = () => {
61+
deferred.resolve();
62+
return fakeWindow;
63+
};
64+
65+
await visit('/');
66+
assert.equal(currentURL(), '/');
67+
68+
await click('[data-test-login-link]');
69+
assert.equal(currentURL(), '/');
70+
71+
// wait for `window.open()` to be called
72+
await deferred.promise;
73+
74+
// simulate the response from the `github-authorize` route
75+
window.github_response = JSON.stringify({
76+
ok: false,
77+
data: {
78+
errors: [{ detail: 'Forbidden' }],
79+
},
80+
});
81+
82+
// simulate that the window has been closed by the `github-authorize` route
83+
fakeWindow.closed = true;
84+
85+
// wait for the error message to show up after the failed login
86+
await waitFor('[data-test-flash-message].shown');
87+
88+
assert.dom('[data-test-flash-message]').hasText('Failed to log in: Forbidden');
89+
});
90+
});

0 commit comments

Comments
 (0)