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

445 lines
14 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
2020-09-16 14:33:04 -04:00
import {
visit,
currentURL,
fillIn,
click,
2020-11-16 15:08:59 -05:00
getRootElement,
2020-09-16 14:33:04 -04:00
} from '@ember/test-helpers';
import { setupApplicationTest } from 'admin/tests/helpers';
2025-08-20 10:38:33 -07:00
import { setupSqlite } from 'api/test-support/helpers/sqlite';
2025-06-05 11:00:25 -07:00
import { setupIntl } from 'ember-intl/test-support';
2020-07-30 17:03:59 -04:00
import { Response } from 'miragejs';
2020-07-24 17:49:42 -04:00
import {
currentSession,
2020-07-24 18:08:37 -04:00
authenticateSession,
2020-07-24 19:12:20 -04:00
invalidateSession,
2020-07-24 17:49:42 -04:00
} from 'ember-simple-auth/test-support';
2021-05-07 14:32:37 -04:00
import Service from '@ember/service';
2023-05-23 12:37:35 -07:00
import {
TYPE_AUTH_METHOD_PASSWORD,
TYPE_AUTH_METHOD_OIDC,
} from 'api/models/auth-method';
2025-04-14 14:27:02 -07:00
import * as commonSelectors from 'admin/tests/helpers/selectors';
2020-07-29 15:47:23 -04:00
module('Acceptance | authentication', function (hooks) {
setupApplicationTest(hooks);
2025-08-20 10:38:33 -07:00
setupSqlite(hooks);
2025-06-05 11:00:25 -07:00
setupIntl(hooks, 'en-us');
2020-07-24 19:12:20 -04:00
let indexURL;
let globalScope;
let orgScope1;
let orgScope2;
let orgScopeID;
2020-07-24 19:12:20 -04:00
let scopesURL;
let orgScopeURL;
2020-07-24 17:49:42 -04:00
let scope;
2020-09-09 12:24:01 -04:00
let globalAuthMethod;
let globalAccount;
2020-09-09 12:24:01 -04:00
let globalAuthMethodID;
2021-05-07 14:32:37 -04:00
let authMethod;
let account;
let authMethodID;
2021-05-07 14:32:37 -04:00
let authMethodOIDC;
let authMethodOIDCID;
let globalAuthenticateURL;
2020-07-24 18:08:37 -04:00
let authenticateURL;
let authMethodAuthenticateURL;
2021-05-07 14:32:37 -04:00
let authMethodOIDCAuthenticateURL;
let authMethodGlobalAuthenticateURL;
2020-10-03 12:57:21 -04:00
let changePasswordURL;
let orgsURL;
2020-10-29 16:58:15 -04:00
let orgEditURL;
let newProjectURL;
let projectsURL;
let usersURL;
let groupsURL;
let rolesURL;
let authMethodsURL;
2020-10-29 16:58:15 -04:00
let hostCatalogsURL;
let sessionsURL;
let targetsURL;
hooks.beforeEach(async function () {
await invalidateSession();
2020-07-24 19:12:20 -04:00
indexURL = '/';
globalScope = this.server.schema.scopes.find('global');
globalAuthMethod = this.server.schema.authMethods.first();
globalAccount = this.server.schema.accounts.first();
orgScope1 = this.server.create(
'scope',
{
type: 'org',
scope: { id: globalScope.id, type: globalScope.type },
},
'withChildren',
);
orgScope2 = this.server.create(
2020-07-29 15:47:23 -04:00
'scope',
{
type: 'org',
scope: { id: globalScope.id, type: globalScope.type },
},
'withChildren',
2020-07-29 15:47:23 -04:00
);
// create an empty org with no auth methods
this.server.create(
'scope',
{
type: 'org',
scope: { id: globalScope.id, type: globalScope.type },
},
'withChildren',
);
scope = { id: orgScope1.id, type: orgScope1.type };
authMethod = this.server.create('auth-method', {
scope: orgScope1,
2023-05-23 12:37:35 -07:00
type: TYPE_AUTH_METHOD_PASSWORD,
2020-09-16 14:33:04 -04:00
});
account = this.server.create('account', {
scope: orgScope1,
authMethod,
});
2021-05-07 14:32:37 -04:00
authMethodOIDC = this.server.create('auth-method', {
scope: orgScope2,
2023-05-23 12:37:35 -07:00
type: TYPE_AUTH_METHOD_OIDC,
2021-05-07 14:32:37 -04:00
});
this.server.create('account', {
scope: orgScope2,
authMethod: authMethodOIDC,
});
orgScopeID = orgScope1.id;
2020-09-09 12:24:01 -04:00
globalAuthMethodID = globalAuthMethod.id;
authMethodID = authMethod.id;
2021-05-07 14:32:37 -04:00
authMethodOIDCID = authMethodOIDC.id;
2020-07-24 19:12:20 -04:00
scopesURL = `/scopes`;
orgScopeURL = `/scopes/${orgScopeID}`;
globalAuthenticateURL = `/scopes/global/authenticate`;
2020-07-24 18:08:37 -04:00
authenticateURL = `/scopes/${orgScopeID}/authenticate`;
2020-09-09 12:24:01 -04:00
authMethodGlobalAuthenticateURL = `/scopes/global/authenticate/${globalAuthMethodID}`;
authMethodAuthenticateURL = `/scopes/${orgScopeID}/authenticate/${authMethodID}`;
authMethodOIDCAuthenticateURL = `/scopes/${orgScope2.id}/authenticate/${authMethodOIDCID}`;
2020-10-03 12:57:21 -04:00
changePasswordURL = `/account/change-password`;
2020-10-27 16:50:10 -04:00
orgsURL = `/scopes/global/scopes`;
2020-10-29 16:58:15 -04:00
orgEditURL = `/scopes/${orgScopeID}/edit`;
newProjectURL = `/scopes/${orgScopeID}/scopes/new`;
2020-10-27 16:50:10 -04:00
projectsURL = `/scopes/${orgScopeID}/scopes`;
usersURL = `/scopes/${orgScopeID}/users`;
groupsURL = `/scopes/${orgScopeID}/groups`;
rolesURL = `/scopes/${orgScopeID}/roles`;
authMethodsURL = `/scopes/${orgScopeID}/auth-methods`;
2020-10-29 16:58:15 -04:00
hostCatalogsURL = `/scopes/${orgScopeID}/host-catalogs`;
sessionsURL = `/scopes/${orgScopeID}/sessions`;
targetsURL = `/scopes/${orgScopeID}/targets`;
});
2020-07-29 15:47:23 -04:00
test('visiting auth methods authenticate route redirects to first auth method', async function (assert) {
2020-07-24 18:08:37 -04:00
await visit(authenticateURL);
assert.strictEqual(currentURL(), authMethodAuthenticateURL);
2020-07-24 18:08:37 -04:00
});
2020-07-30 17:03:59 -04:00
test('visiting auth method when the scope cannot be loaded is still allowed', async function (assert) {
2023-08-14 17:03:59 -04:00
this.server.get(authMethodAuthenticateURL, () => {
2020-09-09 12:24:01 -04:00
return new Response(404);
2020-07-30 17:03:59 -04:00
});
await visit(authMethodAuthenticateURL);
assert.strictEqual(currentURL(), authMethodAuthenticateURL);
2020-07-30 17:03:59 -04:00
});
test('visiting any authentication parent route while unauthenticated redirects to first global authenticate method', async function (assert) {
2020-07-24 19:12:20 -04:00
await visit(indexURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-07-24 19:12:20 -04:00
await visit(scopesURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-10-03 12:57:21 -04:00
assert.notOk(currentSession().isAuthenticated);
});
test('visiting change password while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(changePasswordURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-10-03 12:16:54 -04:00
assert.notOk(currentSession().isAuthenticated);
});
test('visiting orgs while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(orgsURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-07-24 19:12:20 -04:00
assert.notOk(currentSession().isAuthenticated);
});
2020-10-29 16:58:15 -04:00
test('visiting org edit while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(orgEditURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-10-29 16:58:15 -04:00
assert.notOk(currentSession().isAuthenticated);
});
test('visiting projects while unauthenticated redirects to first global authenticate method', async function (assert) {
2020-07-24 19:12:20 -04:00
await visit(projectsURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-10-29 16:58:15 -04:00
assert.notOk(currentSession().isAuthenticated);
});
test('visiting new project while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(newProjectURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-07-24 19:12:20 -04:00
assert.notOk(currentSession().isAuthenticated);
});
test('visiting users while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(usersURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
assert.notOk(currentSession().isAuthenticated);
});
test('visiting roles while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(rolesURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
assert.notOk(currentSession().isAuthenticated);
});
test('visiting groups while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(groupsURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
assert.notOk(currentSession().isAuthenticated);
});
2020-10-29 16:58:15 -04:00
test('visiting host catalogs while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(hostCatalogsURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-10-29 16:58:15 -04:00
assert.notOk(currentSession().isAuthenticated);
});
test('visiting targets while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(targetsURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-10-29 16:58:15 -04:00
assert.notOk(currentSession().isAuthenticated);
});
test('visiting sessions while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(sessionsURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
2020-10-29 16:58:15 -04:00
assert.notOk(currentSession().isAuthenticated);
});
test('visiting auth methods while unauthenticated redirects to first global authenticate method', async function (assert) {
await visit(authMethodsURL);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
assert.notOk(currentSession().isAuthenticated);
});
2020-07-29 15:47:23 -04:00
test('visiting auth method authenticate route', async function (assert) {
await visit(authMethodAuthenticateURL);
assert.strictEqual(currentURL(), authMethodAuthenticateURL);
});
test('visiting any authentication parent route while already authenticated with an org redirects to projects', async function (assert) {
await authenticateSession({ scope, account_id: account.id });
2020-07-24 19:12:20 -04:00
await visit(indexURL);
assert.strictEqual(currentURL(), projectsURL);
2020-07-24 19:12:20 -04:00
await visit(scopesURL);
assert.strictEqual(currentURL(), projectsURL);
2020-07-24 19:12:20 -04:00
await visit(orgScopeURL);
assert.strictEqual(currentURL(), projectsURL);
2020-07-24 18:08:37 -04:00
await visit(authenticateURL);
assert.strictEqual(currentURL(), projectsURL);
2020-07-24 18:08:37 -04:00
await visit(authMethodAuthenticateURL);
assert.strictEqual(currentURL(), projectsURL);
2020-07-24 19:12:20 -04:00
assert.ok(currentSession().isAuthenticated);
2020-07-24 18:08:37 -04:00
});
test('visiting index or scopes routes while already authenticated with global redirects to orgs', async function (assert) {
await authenticateSession({
2020-09-16 14:33:04 -04:00
scope: { id: globalScope.id, type: globalScope.type },
account_id: globalAccount.id,
2020-09-16 14:33:04 -04:00
});
await visit(indexURL);
assert.strictEqual(currentURL(), orgsURL);
await visit(scopesURL);
assert.strictEqual(currentURL(), orgsURL);
await visit(globalAuthenticateURL);
assert.strictEqual(currentURL(), orgsURL);
assert.ok(currentSession().isAuthenticated);
});
2020-07-29 15:47:23 -04:00
test('failed authentication shows a notification message', async function (assert) {
await visit(authMethodAuthenticateURL);
2020-07-24 17:49:42 -04:00
assert.notOk(currentSession().isAuthenticated);
await fillIn(commonSelectors.FIELD_IDENTIFICATION, 'error');
await click(commonSelectors.SAVE_BTN);
assert.dom(commonSelectors.ALERT_TOAST);
2020-07-24 17:49:42 -04:00
assert.notOk(currentSession().isAuthenticated);
});
test('successful authentication with the global scope redirects to orgs', async function (assert) {
await visit(authMethodGlobalAuthenticateURL);
assert.notOk(currentSession().isAuthenticated);
assert.strictEqual(currentURL(), authMethodGlobalAuthenticateURL);
await fillIn(commonSelectors.FIELD_IDENTIFICATION, 'test');
await fillIn(
commonSelectors.FIELD_PASSWORD,
commonSelectors.FIELD_PASSWORD_VALUE,
);
await click(commonSelectors.SAVE_BTN);
assert.strictEqual(currentURL(), orgsURL);
assert.ok(currentSession().isAuthenticated);
});
test('successful authentication with an org scope redirects to projects', async function (assert) {
await visit(authMethodAuthenticateURL);
2020-07-24 17:49:42 -04:00
assert.notOk(currentSession().isAuthenticated);
assert.strictEqual(currentURL(), authMethodAuthenticateURL);
await fillIn(commonSelectors.FIELD_IDENTIFICATION, 'test');
await fillIn(
commonSelectors.FIELD_PASSWORD,
commonSelectors.FIELD_PASSWORD_VALUE,
);
await click(commonSelectors.SAVE_BTN);
assert.strictEqual(currentURL(), projectsURL);
2020-07-24 17:49:42 -04:00
assert.ok(currentSession().isAuthenticated);
});
2020-09-08 17:35:02 -04:00
test('401 responses result in deauthentication', async function (assert) {
await authenticateSession({
2020-09-16 14:33:04 -04:00
scope: { id: globalScope.id, type: globalScope.type },
account_id: globalAccount.id,
2020-09-16 14:33:04 -04:00
});
2020-09-08 17:35:02 -04:00
await visit(orgsURL);
2020-09-16 14:33:04 -04:00
assert.ok(
currentSession().isAuthenticated,
'Session begins authenticated, before encountering 401',
2020-09-16 14:33:04 -04:00
);
this.server.get('/users', () => new Response(401));
await visit(usersURL);
assert.notOk(
currentSession().isAuthenticated,
'Session is unauthenticated, after encountering 401',
);
2020-09-08 17:35:02 -04:00
});
2020-11-04 13:51:37 -05:00
test('color theme is applied from session data', async function (assert) {
await authenticateSession({
2020-11-16 15:08:59 -05:00
scope: { id: globalScope.id, type: globalScope.type },
account_id: globalAccount.id,
2020-11-16 15:08:59 -05:00
});
currentSession().set('data.theme', 'light');
2020-11-04 13:51:37 -05:00
await visit(orgsURL);
// light mode
assert.strictEqual(currentSession().get('data.theme'), 'light');
2020-11-04 13:51:37 -05:00
assert.ok(getRootElement().classList.contains('rose-theme-light'));
assert.notOk(getRootElement().classList.contains('rose-theme-dark'));
2025-04-14 14:27:02 -07:00
// open dropdown
await click(commonSelectors.SIDEBAR_USER_DROPDOWN);
// toggle system default
await click(commonSelectors.TOGGLE_THEME_DEFAULT);
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'));
2020-11-04 13:51:37 -05:00
// toggle dark mode
await click(commonSelectors.TOGGLE_THEME_DARK);
assert.strictEqual(currentSession().get('data.theme'), 'dark');
2020-11-04 13:51:37 -05:00
assert.notOk(getRootElement().classList.contains('rose-theme-light'));
assert.ok(getRootElement().classList.contains('rose-theme-dark'));
2020-11-04 13:51:37 -05:00
// toggle system default
await click(commonSelectors.TOGGLE_THEME_DEFAULT);
2023-01-19 16:53:02 -08:00
assert.strictEqual(
currentSession().get('data.theme'),
'system-default-theme',
2023-01-19 16:53:02 -08:00
);
2020-11-04 13:51:37 -05:00
assert.notOk(getRootElement().classList.contains('rose-theme-light'));
assert.notOk(getRootElement().classList.contains('rose-theme-dark'));
});
2021-05-07 14:32:37 -04:00
// TODO: figure out a sustainable test strategy for polling routes
test('OIDC authentication opens a new window and closes it upon completion', async function (assert) {
assert.expect(3);
// Register mock window service
this.owner.register(
'service:browser/window',
class extends Service {
open(/* url */) {
assert.ok(true, 'New window was opened.');
return {
close() {
assert.ok(true, 'New window was closed.');
assert.ok(currentSession().isAuthenticated);
},
};
}
},
2021-05-07 14:32:37 -04:00
);
await visit(authMethodOIDCAuthenticateURL);
await click(commonSelectors.SAVE_BTN);
2021-05-07 14:32:37 -04:00
});
// TODO: test OIDC retry and cancel
test('org scopes with no auth methods are not visible in dropdown', async function (assert) {
await visit(authMethodGlobalAuthenticateURL);
await click(commonSelectors.AUTH_SCOPE_DROPDOWN);
assert
.dom(commonSelectors.AUTH_SCOPE_DROPDOWN_OPTIONS)
.exists({ count: 3 });
});
});