Files
threshold-ui/ui/desktop/tests/acceptance/origin-test.js
T

193 lines
5.7 KiB
JavaScript
Raw Normal View History

import { module, test } from 'qunit';
import {
visit,
currentURL,
2021-03-19 12:58:59 -04:00
fillIn,
click,
find,
//findAll,
//getRootElement
//setupOnerror,
} from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
//import { Response } from 'miragejs';
2021-03-19 12:58:59 -04:00
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import sinon from 'sinon';
import {
2021-03-19 12:58:59 -04:00
currentSession,
// authenticateSession,
invalidateSession,
} from 'ember-simple-auth/test-support';
import { setupBrowserFakes } from 'ember-browser-services/test-support';
import WindowMockIPC from '../helpers/window-mock-ipc';
2021-03-19 12:58:59 -04:00
import config from '../../config/environment';
2021-03-19 12:58:59 -04:00
module('Acceptance | origin', function (hooks) {
setupApplicationTest(hooks);
setupBrowserFakes(hooks, { window: true });
setupMirage(hooks);
2021-04-01 10:26:59 -04:00
const currentOrigin = window.location.origin;
let mockIPC;
const instances = {
scopes: {
global: null,
org: null,
2020-11-16 15:08:59 -05:00
project: null,
},
authMethods: {
global: null,
2020-11-16 15:08:59 -05:00
org: null,
},
hostCatalog: null,
2020-11-16 15:08:59 -05:00
target: null,
};
const stubs = {
global: null,
2020-11-16 15:08:59 -05:00
org: null,
2021-03-19 12:58:59 -04:00
ipcService: null,
};
const urls = {
index: '/',
origin: '/origin',
scopes: {
global: null,
org: null,
},
authenticate: {
global: null,
2021-03-19 12:58:59 -04:00
methods: {
global: null,
},
},
2021-03-22 20:12:25 -04:00
projects: null,
2020-11-16 15:08:59 -05:00
targets: null,
};
hooks.beforeEach(function () {
invalidateSession();
// create scopes
instances.scopes.global = this.server.create('scope', { id: 'global' });
stubs.global = { id: 'global', type: 'global' };
2020-11-16 15:08:59 -05:00
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: stubs.global,
});
stubs.org = { id: instances.scopes.org.id, type: 'org' };
2020-11-16 15:08:59 -05:00
instances.scopes.project = this.server.create('scope', {
type: 'project',
scope: stubs.org,
});
stubs.project = { id: instances.scopes.project.id, type: 'project' };
2020-11-16 15:08:59 -05:00
instances.authMethods.global = this.server.create('auth-method', {
scope: instances.scopes.global,
});
instances.hostCatalog = this.server.create(
'host-catalog',
{ scope: instances.scopes.project },
'withChildren'
);
instances.target = this.server.create(
'target',
{ scope: instances.scopes.project },
'withAssociations'
2020-11-16 15:08:59 -05:00
);
urls.scopes.global = `/scopes/${instances.scopes.global.id}`;
2021-03-19 12:58:59 -04:00
urls.authenticate.global = `${urls.scopes.global}/authenticate`;
urls.authenticate.methods.global = `${urls.authenticate.global}/${instances.authMethods.global.id}`;
2021-03-22 20:12:25 -04:00
urls.projects = `${urls.scopes.global}/projects`;
2021-03-19 12:58:59 -04:00
urls.targets = `${urls.projects}/targets`;
// Mock the postMessage interface used by IPC.
this.owner.register('service:browser/window', WindowMockIPC);
mockIPC = this.owner.lookup('service:browser/window').mockIPC;
});
hooks.afterEach(function () {
2021-03-19 12:58:59 -04:00
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);
2021-03-19 12:58:59 -04:00
await a11yAudit();
assert.notOk(mockIPC.origin);
assert.equal(currentURL(), urls.origin);
});
2021-03-19 12:58:59 -04:00
test('can set origin', async function (assert) {
2021-03-31 15:02:17 -04:00
assert.expect(3);
2021-03-19 12:58:59 -04:00
assert.notOk(mockIPC.origin);
await visit(urls.origin);
2021-04-01 10:26:59 -04:00
await fillIn('[name="host"]', currentOrigin);
2021-03-19 12:58:59 -04:00
await click('[type="submit"]');
2021-03-31 15:02:17 -04:00
assert.equal(currentURL(), urls.authenticate.methods.global);
2021-04-01 10:26:59 -04:00
assert.equal(mockIPC.origin, currentOrigin);
2021-03-19 12:58:59 -04:00
});
2021-03-31 15:02:17 -04:00
test('can reset origin before authentication', async function (assert) {
assert.expect(4);
2021-03-19 12:58:59 -04:00
assert.notOk(mockIPC.origin);
await visit(urls.origin);
2021-04-01 10:26:59 -04:00
await fillIn('[name="host"]', currentOrigin);
2021-03-19 12:58:59 -04:00
await click('[type="submit"]');
2021-03-31 15:02:17 -04:00
assert.equal(currentURL(), urls.authenticate.methods.global);
2021-04-01 10:26:59 -04:00
assert.equal(mockIPC.origin, currentOrigin);
2021-03-31 15:02:17 -04:00
await click('.change-origin a');
assert.equal(currentURL(), urls.origin);
2021-03-19 12:58:59 -04:00
});
2021-03-31 15:02:17 -04:00
test('captures error on origin update', async function (assert) {
assert.expect(2);
2021-03-19 12:58:59 -04:00
assert.notOk(mockIPC.origin);
2021-03-31 15:02:17 -04:00
sinon.stub(this.owner.lookup('service:origin'), 'setOrigin').throws();
2021-03-19 12:58:59 -04:00
await visit(urls.origin);
2021-04-01 10:26:59 -04:00
await fillIn('[name="host"]', currentOrigin);
2021-03-19 12:58:59 -04:00
await click('[type="submit"]');
2021-03-31 15:02:17 -04:00
assert.ok(find('.rose-notification.is-error'));
2021-03-19 12:58:59 -04:00
});
2021-04-01 13:04:28 -04:00
test('origin set automatically when autoOrigin is true', async function (assert) {
2021-03-19 12:58:59 -04:00
assert.expect(1);
config.autoOrigin = true;
await visit(urls.origin);
2021-04-01 13:04:28 -04:00
assert.equal(find('[name="host"]').value, currentOrigin);
2021-03-19 12:58:59 -04:00
config.autoOrigin = false;
});
2021-04-01 13:04:28 -04:00
test('origin is *not* set automatically when autoOrigin is false', async function (assert) {
assert.expect(2);
assert.notOk(config.autoOrigin, 'autoOrigin is disabled');
await visit(urls.origin);
assert.notOk(find('[name="host"]').value, 'Origin field is empty');
});
2021-03-19 12:58:59 -04:00
test('can reset origin on error', async function (assert) {
2021-03-22 20:12:25 -04:00
assert.expect(4);
2021-03-31 18:00:42 -04:00
this.server.get('/targets', () => new Response(500));
2021-03-19 12:58:59 -04:00
await visit(urls.origin);
2021-04-01 10:26:59 -04:00
await fillIn('[name="host"]', currentOrigin);
await click('[type="submit"]', 'Set origin');
await click('[type="submit"]', 'Authenticate');
2021-03-31 18:00:42 -04:00
assert.ok(currentSession().isAuthenticated);
assert.equal(find('main section button').textContent.trim(), 'Disconnect');
2021-03-22 20:12:25 -04:00
await click('main section button');
2021-03-31 18:00:42 -04:00
assert.notOk(currentSession().isAuthenticated);
assert.equal(currentURL(), urls.authenticate.methods.global);
2021-03-19 12:58:59 -04:00
});
});