diff --git a/ui/desktop/tests/acceptance/origin-test.js b/ui/desktop/tests/acceptance/origin-test.js index 8830cc483..32c633497 100644 --- a/ui/desktop/tests/acceptance/origin-test.js +++ b/ui/desktop/tests/acceptance/origin-test.js @@ -2,24 +2,27 @@ import { module, test } from 'qunit'; import { visit, currentURL, - //fillIn, - //click, - //find, + fillIn, + click, + find, //findAll, //getRootElement //setupOnerror, } from '@ember/test-helpers'; +import { run, later } from '@ember/runloop'; import { setupApplicationTest } from 'ember-qunit'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; //import { Response } from 'miragejs'; -//import a11yAudit from 'ember-a11y-testing/test-support/audit'; +import a11yAudit from 'ember-a11y-testing/test-support/audit'; +import sinon from 'sinon'; import { - //currentSession, - //authenticateSession, + currentSession, + // authenticateSession, invalidateSession, } from 'ember-simple-auth/test-support'; +import config from '../../config/environment'; -module('Acceptance | authentication', function (hooks) { +module('Acceptance | origin', function (hooks) { setupApplicationTest(hooks); setupMirage(hooks); @@ -43,6 +46,7 @@ module('Acceptance | authentication', function (hooks) { const stubs = { global: null, org: null, + ipcService: null, }; const urls = { @@ -54,7 +58,9 @@ module('Acceptance | authentication', function (hooks) { }, authenticate: { global: null, - org: null, + methods: { + global: null, + }, }, targets: null, }; @@ -97,8 +103,11 @@ module('Acceptance | authentication', function (hooks) { urls.scopes.global = `/scopes/${instances.scopes.global.id}`; urls.scopes.org = `/scopes/${instances.scopes.org.id}`; - urls.authenticate.global = `${urls.scopes.global}/authenticate/${instances.authMethods.global.id}`; + urls.authenticate.global = `${urls.scopes.global}/authenticate`; + urls.authenticate.methods.global = `${urls.authenticate.global}/${instances.authMethods.global.id}`; urls.authenticate.org = `${urls.scopes.global}/authenticate/${instances.authMethods.org.id}`; + urls.projects = `${urls.scopes.org}/projects`; + urls.targets = `${urls.projects}/targets`; class MockIPC { origin = null; @@ -115,6 +124,8 @@ module('Acceptance | authentication', function (hooks) { this.origin = origin; return this.origin; } + + resetOrigin() {} } mockIPC = new MockIPC(); @@ -132,13 +143,117 @@ module('Acceptance | authentication', function (hooks) { hooks.afterEach(function () { window.removeEventListener('message', messageHandler); + sinon.restore(); + }); + + test('visiting index', async function (assert) { + assert.expect(1); + await visit(urls.origin); + await a11yAudit(); + assert.equal(currentURL(), urls.origin); }); test('visiting index without an origin specified redirects to origin route', async function (assert) { assert.expect(2); await visit(urls.index); - //await a11yAudit(); + await a11yAudit(); assert.notOk(mockIPC.origin); assert.equal(currentURL(), urls.origin); }); + + test('can set origin', async function (assert) { + assert.expect(2); + assert.notOk(mockIPC.origin); + await visit(urls.origin); + await a11yAudit(); + await fillIn('[name="host"]', window.location.origin); + await click('[type="submit"]'); + assert.equal(mockIPC.origin, window.location.origin); + }); + + test('captures error on origin update', async function (assert) { + assert.expect(2); + assert.notOk(mockIPC.origin); + sinon.stub(this.owner.lookup('service:origin'), 'setOrigin').throws(); + await visit(urls.origin); + await a11yAudit(); + await fillIn('[name="host"]', window.location.origin); + await click('[type="submit"]'); + assert.ok(find('.rose-notification.is-error')); + }); + + test('can reset origin before authentication', async function (assert) { + assert.expect(4); + assert.notOk(mockIPC.origin); + await visit(urls.origin); + await fillIn('[name="host"]', window.location.origin); + await click('[type="submit"]'); + assert.equal(mockIPC.origin, window.location.origin); + assert.equal(currentURL(), urls.authenticate.methods.global); + await click('.change-origin a'); + assert.equal(currentURL(), urls.origin); + }); + + test('can set origin', async function (assert) { + const originService = this.owner.lookup('service:origin'); + assert.expect(2); + assert.notOk(originService.rendererOrigin); + await visit(urls.origin); + await a11yAudit(); + await fillIn('[name="host"]', window.location.origin); + await click('[type="submit"]'); + assert.equal(originService.rendererOrigin, window.location.origin); + }); + + test('origin set automatically in dev mode', async function (assert) { + assert.expect(1); + config.autoOrigin = true; + await visit(urls.origin); + await fillIn('[name="host"]', window.location.origin); + await click('[type="submit"]'); + assert.equal( + this.owner.lookup('controller:origin').origin, + window.location.origin + ); + config.autoOrigin = false; + }); + + test('captures error on origin update', async function (assert) { + assert.expect(2); + assert.notOk(mockIPC.origin); + sinon.stub(this.owner.lookup('service:origin'), 'setOrigin').throws(); + await visit(urls.origin); + await a11yAudit(); + await fillIn('[name="host"]', window.location.origin); + await click('[type="submit"]'); + assert.ok(find('.rose-notification.is-error')); + }); + + test('can reset origin on error', async function (assert) { + assert.expect(7); + const originService = this.owner.lookup('service:origin'); + await visit(urls.origin); + await a11yAudit(); + await fillIn('[name="host"]', window.location.origin); + await click('[type="submit"]'); + assert.ok(originService.rendererOrigin, window.location.origin); + await visit(urls.authenticate.methods.global); + await fillIn('[name="identification"]', 'test'); + await fillIn('[name="password"]', 'test'); + this.server.get('/targets', () => new Response(500)); + later(async () => { + run.cancelTimers(); + assert.ok(currentSession().isAuthenticated); + assert.ok(find('.rose-message')); + assert.equal( + find('main section button').textContent.trim(), + 'Disconnect' + ); + await click('main section button'); + assert.notOk(originService.rendererOrigin); + assert.notOk(currentSession().isAuthenticated); + assert.equal(currentURL(), urls.authenticate.methods.global); + }, 750); + await click('[type="submit"]'); + }); });