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

176 lines
5.3 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
2022-07-27 10:25:01 -07:00
import { visit, currentURL, fillIn, click, find } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
2021-03-19 12:58:59 -04:00
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import sinon from 'sinon';
2022-07-27 10:25:01 -07:00
import { 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';
2022-07-27 10:25:01 -07:00
module('Acceptance | clusterUrl', 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: '/',
2022-07-27 10:25:01 -07:00
clusterUrl: '/cluster-url',
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,
};
2023-03-07 16:55:24 -05:00
const setupMockIpc = (test) => {
test.owner.register('service:browser/window', WindowMockIPC);
mockIPC = test.owner.lookup('service:browser/window').mockIPC;
};
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`;
});
hooks.afterEach(function () {
2021-03-19 12:58:59 -04:00
sinon.restore();
});
test('visiting index', async function (assert) {
assert.expect(1);
2022-07-27 10:25:01 -07:00
await visit(urls.clusterUrl);
2021-03-19 12:58:59 -04:00
await a11yAudit();
2022-07-27 10:25:01 -07:00
assert.strictEqual(currentURL(), urls.clusterUrl);
});
2022-07-27 10:25:01 -07:00
test('visiting index without a clusterUrl specified redirects to clusterUrl route', async function (assert) {
assert.expect(2);
2023-03-07 16:55:24 -05:00
setupMockIpc(this);
await visit(urls.index);
2021-03-19 12:58:59 -04:00
await a11yAudit();
2022-07-27 10:25:01 -07:00
assert.notOk(mockIPC.clusterUrl);
assert.strictEqual(currentURL(), urls.clusterUrl);
});
2021-03-19 12:58:59 -04:00
2022-07-27 10:25:01 -07:00
test('can set clusterUrl', async function (assert) {
2021-03-31 15:02:17 -04:00
assert.expect(3);
2023-03-07 16:55:24 -05:00
setupMockIpc(this);
2022-07-27 10:25:01 -07:00
assert.notOk(mockIPC.clusterUrl);
await visit(urls.clusterUrl);
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"]');
assert.strictEqual(currentURL(), urls.authenticate.methods.global);
2022-07-27 10:25:01 -07:00
assert.strictEqual(mockIPC.clusterUrl, currentOrigin);
2021-03-19 12:58:59 -04:00
});
2022-07-27 10:25:01 -07:00
test('can reset clusterUrl before authentication', async function (assert) {
2021-03-31 15:02:17 -04:00
assert.expect(4);
2023-03-07 16:55:24 -05:00
setupMockIpc(this);
2022-07-27 10:25:01 -07:00
assert.notOk(mockIPC.clusterUrl);
await visit(urls.clusterUrl);
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"]');
assert.strictEqual(currentURL(), urls.authenticate.methods.global);
2022-07-27 10:25:01 -07:00
assert.strictEqual(mockIPC.clusterUrl, currentOrigin);
2021-03-31 15:02:17 -04:00
await click('.change-origin a');
2022-07-27 10:25:01 -07:00
assert.strictEqual(currentURL(), urls.clusterUrl);
2021-03-19 12:58:59 -04:00
});
2022-07-27 10:25:01 -07:00
test('captures error on clusterUrl update', async function (assert) {
2021-03-31 15:02:17 -04:00
assert.expect(2);
2023-03-07 16:55:24 -05:00
setupMockIpc(this);
2022-07-27 10:25:01 -07:00
assert.notOk(mockIPC.clusterUrl);
2022-08-11 15:37:44 -07:00
sinon
.stub(this.owner.lookup('service:clusterUrl'), 'setClusterUrl')
.throws();
2022-07-27 10:25:01 -07:00
await visit(urls.clusterUrl);
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
});
2022-07-27 10:25:01 -07:00
test('clusterUrl set automatically when autoOrigin is true', async function (assert) {
2021-03-19 12:58:59 -04:00
assert.expect(1);
config.autoOrigin = true;
2022-07-27 10:25:01 -07:00
await visit(urls.clusterUrl);
assert.strictEqual(find('[name="host"]').value, currentOrigin);
2021-03-19 12:58:59 -04:00
config.autoOrigin = false;
});
2022-07-27 10:25:01 -07:00
test('clusterUrl is *not* set automatically when autoOrigin is false', async function (assert) {
2021-04-01 13:04:28 -04:00
assert.expect(2);
assert.notOk(config.autoOrigin, 'autoOrigin is disabled');
2022-07-27 10:25:01 -07:00
await visit(urls.clusterUrl);
2021-04-01 13:04:28 -04:00
assert.notOk(find('[name="host"]').value, 'Origin field is empty');
});
});