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

271 lines
8.5 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import {
visit,
currentURL,
fillIn,
click,
find,
getRootElement,
} from '@ember/test-helpers';
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 {
currentSession,
authenticateSession,
invalidateSession,
} from 'ember-simple-auth/test-support';
import WindowMockIPC from '../helpers/window-mock-ipc';
module('Acceptance | authentication', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
const instances = {
scopes: {
global: null,
org: null,
emptyOrg: null,
project: null,
},
authMethods: {
global: null,
org: null,
},
user: null,
target: null,
session: null,
};
const stubs = {
global: null,
org: null,
};
const urls = {
index: '/',
clusterUrl: '/cluster-url',
scopes: {
global: null,
org: null,
},
authenticate: {
global: null,
methods: {
global: null,
},
},
targets: null,
sessions: null,
};
const setDefaultClusterUrl = (test) => {
const windowOrigin = window.location.origin;
const clusterUrl = test.owner.lookup('service:clusterUrl');
clusterUrl.rendererClusterUrl = windowOrigin;
};
hooks.beforeEach(function () {
instances.user = this.server.create('user', {
scope: instances.scopes.global,
});
invalidateSession();
// create scopes
instances.scopes.global = this.server.create('scope', { id: 'global' });
stubs.global = { id: 'global', type: 'global' };
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: stubs.global,
});
stubs.org = { id: instances.scopes.org.id, type: 'org' };
instances.scopes.project = this.server.create('scope', {
type: 'project',
scope: stubs.org,
});
stubs.project = { id: instances.scopes.project.id, type: 'project' };
// create other resources
instances.scopes.emptyOrg = this.server.create('scope', {
type: 'org',
scope: stubs.global,
});
instances.authMethods.global = this.server.create('auth-method', {
scope: instances.scopes.global,
type: 'password',
});
instances.authMethods.org = this.server.create('auth-method', {
scope: instances.scopes.org,
type: 'password',
});
instances.target = this.server.create(
'target',
{ scope: instances.scopes.project },
'withAssociations',
);
instances.session = this.server.create(
'session',
{
scope: instances.scopes.project,
target: instances.target,
status: 'active',
user: instances.user,
},
'withAssociations',
);
urls.scopes.global = `/scopes/${instances.scopes.global.id}`;
urls.scopes.org = `/scopes/${instances.scopes.org.id}`;
urls.authenticate.global = `${urls.scopes.global}/authenticate`;
urls.authenticate.methods.global = `${urls.authenticate.global}/${instances.authMethods.global.id}`;
urls.projects = `${urls.scopes.global}/projects`;
urls.targets = `${urls.projects}/targets`;
urls.sessions = `${urls.projects}/sessions`;
// Mock the postMessage interface used by IPC.
this.owner.register('service:browser/window', WindowMockIPC);
setDefaultClusterUrl(this);
});
test('visiting index while unauthenticated redirects to global authenticate method', async function (assert) {
assert.expect(2);
await visit(urls.index);
await a11yAudit();
assert.notOk(currentSession().isAuthenticated);
assert.strictEqual(currentURL(), urls.authenticate.methods.global);
});
test('visiting authenticate route without clusterUrl redirects to clusterUrl index', async function (assert) {
assert.expect(1);
this.owner.lookup('service:clusterUrl').rendererClusterUrl = null;
await visit(urls.authenticate.global);
await a11yAudit();
assert.strictEqual(currentURL(), urls.clusterUrl);
});
test('visiting authenticate route when the scope cannot be loaded is allowed', async function (assert) {
assert.expect(1);
this.server.get('/scopes/global', () => {
return new Response(404);
});
await visit(urls.authenticate.global);
await a11yAudit();
assert.strictEqual(currentURL(), urls.authenticate.methods.global);
});
test('failed authentication shows a notification message', async function (assert) {
assert.expect(3);
await visit(urls.authenticate.methods.global);
assert.notOk(currentSession().isAuthenticated);
await fillIn('[name="identification"]', 'error');
await click('[type="submit"]');
assert.ok(find('.rose-notification.is-error'));
assert.notOk(currentSession().isAuthenticated);
});
test('can reset clusterUrl before authentication', async function (assert) {
assert.expect(1);
await visit(urls.authenticate.methods.global);
await click('.change-origin a');
assert.strictEqual(currentURL(), urls.clusterUrl);
});
test('signing out redirects to first global authenticate method', async function (assert) {
assert.expect(3);
await visit(urls.authenticate.methods.global);
await fillIn('[name="identification"]', 'test');
await fillIn('[name="password"]', 'test');
await click('[type="submit"]');
assert.ok(currentSession().isAuthenticated);
await click('.rose-header-utilities .rose-dropdown summary');
assert.strictEqual(
find(
'.rose-header-utilities .rose-dropdown-content button',
).textContent.trim(),
'Sign Out',
);
await click('.rose-header-utilities .rose-dropdown-content button');
assert.notOk(currentSession().isAuthenticated);
});
test('401 responses result in deauthentication', async function (assert) {
assert.expect(3);
authenticateSession({
scope: {
id: instances.scopes.global.id,
type: instances.scopes.global.type,
},
});
await visit(urls.sessions);
assert.ok(
currentSession().isAuthenticated,
'Session begins authenticated, before encountering 401',
);
assert.ok(currentURL(), urls.targets);
this.server.get('/sessions', () => new Response(401));
await visit(urls.targets);
await visit(urls.sessions);
assert.notOk(
currentSession().isAuthenticated,
'Session is unauthenticated, after encountering 401',
);
});
test('color theme is applied from session data', async function (assert) {
assert.expect(12);
authenticateSession({
scope: {
id: instances.scopes.global.id,
type: instances.scopes.global.type,
},
});
await visit(urls.scopes.org);
// system default
assert.notOk(currentSession().get('data.theme'));
assert.notOk(getRootElement().classList.contains('rose-theme-light'));
assert.notOk(getRootElement().classList.contains('rose-theme-dark'));
// toggle light mode
await click('[name="theme"][value="light"]');
assert.strictEqual(currentSession().get('data.theme'), 'light');
assert.ok(getRootElement().classList.contains('rose-theme-light'));
assert.notOk(getRootElement().classList.contains('rose-theme-dark'));
// toggle dark mode
await click('[name="theme"][value="dark"]');
assert.strictEqual(currentSession().get('data.theme'), 'dark');
assert.notOk(getRootElement().classList.contains('rose-theme-light'));
assert.ok(getRootElement().classList.contains('rose-theme-dark'));
// toggle system default
await click('[name="theme"][value="system-default-theme"]');
assert.strictEqual(
currentSession().get('data.theme'),
'system-default-theme',
);
assert.notOk(getRootElement().classList.contains('rose-theme-light'));
assert.notOk(getRootElement().classList.contains('rose-theme-dark'));
});
test('org scopes with no auth methods are not visible in dropdown', async function (assert) {
await visit(urls.authenticate.methods.global);
await click('.rose-dropdown-trigger');
assert.dom('.rose-dropdown-content a').exists({ count: 2 });
});
test('change cluster url is visible when no auth methods are available', async function (assert) {
this.server.get('/auth-methods', () => new Response(200));
await visit(urls.authenticate.methods.global);
assert.dom('[data-test-no-auth-methods]').includesText('No Auth Methods');
assert.dom('.change-origin').exists();
});
});