|
| 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