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

218 lines
7.5 KiB
JavaScript
Raw Normal View History

import { module, test } from 'qunit';
2020-09-16 14:33:04 -04:00
import {
visit,
currentURL,
fillIn,
click,
find,
findAll,
setupOnerror,
} from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
2020-07-30 17:03:59 -04:00
import { Response } from 'miragejs';
import a11yAudit from 'ember-a11y-testing/test-support/audit';
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';
2020-07-29 15:47:23 -04:00
module('Acceptance | authentication', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
2020-07-24 19:12:20 -04:00
let indexURL;
let globalScope;
let orgScope;
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 authMethod;
2020-09-09 12:24:01 -04:00
let globalAuthMethodID;
let authMethodID;
let globalAuthenticateURL;
2020-07-24 18:08:37 -04:00
let authenticateURL;
let authMethodAuthenticateURL;
let authMethodGlobalAuthenticateURL;
let orgsURL;
let projectsURL;
hooks.beforeEach(function () {
2020-07-24 19:12:20 -04:00
invalidateSession();
indexURL = '/';
globalScope = this.server.create('scope', { id: 'global' });
2020-07-29 15:47:23 -04:00
orgScope = this.server.create(
'scope',
{
type: 'org',
scope: { id: globalScope.id, type: globalScope.type },
},
'withChildren'
);
2020-07-24 17:49:42 -04:00
scope = { id: orgScope.id, type: orgScope.type };
2020-09-16 14:33:04 -04:00
globalAuthMethod = this.server.create('auth-method', {
scope: globalScope,
});
2020-09-09 12:24:01 -04:00
authMethod = this.server.create('auth-method', { scope: orgScope });
orgScopeID = orgScope.id;
2020-09-09 12:24:01 -04:00
globalAuthMethodID = globalAuthMethod.id;
authMethodID = authMethod.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}`;
orgsURL = `/scopes/global/orgs`;
projectsURL = `/scopes/${orgScopeID}/projects`;
});
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
assert.expect(1);
await visit(authenticateURL);
await a11yAudit();
assert.equal(currentURL(), authMethodAuthenticateURL);
});
test('visiting auth methods authenticate route when there no methods shows a message', async function (assert) {
assert.expect(2);
2020-09-16 14:33:04 -04:00
authMethod.destroy();
await visit(authenticateURL);
await a11yAudit();
assert.equal(currentURL(), authenticateURL);
assert.ok(find('.rose-message'));
});
2020-07-30 17:03:59 -04:00
test('visiting auth method when the scope cannot be loaded is still allowed', async function (assert) {
assert.expect(1);
2020-09-09 12:24:01 -04:00
this.server.get('/scopes', () => {
return new Response(404);
2020-07-30 17:03:59 -04:00
});
await visit(authMethodAuthenticateURL);
await a11yAudit();
assert.equal(currentURL(), authMethodAuthenticateURL);
});
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
assert.expect(3);
await visit(indexURL);
assert.equal(currentURL(), authMethodGlobalAuthenticateURL);
2020-07-24 19:12:20 -04:00
await visit(scopesURL);
assert.equal(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) {
assert.expect(2);
await visit(orgsURL);
assert.equal(currentURL(), authMethodGlobalAuthenticateURL);
2020-07-24 19:12:20 -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
assert.expect(2);
await visit(projectsURL);
assert.equal(currentURL(), authMethodGlobalAuthenticateURL);
2020-07-24 19:12:20 -04:00
assert.notOk(currentSession().isAuthenticated);
});
2020-07-29 15:47:23 -04:00
test('visiting auth method authenticate route', async function (assert) {
assert.expect(1);
await visit(authMethodAuthenticateURL);
await a11yAudit();
assert.equal(currentURL(), authMethodAuthenticateURL);
});
test('visiting any authentication parent route while already authenticated with an org redirects to projects', async function (assert) {
2020-07-24 19:12:20 -04:00
assert.expect(6);
authenticateSession({ scope });
await visit(indexURL);
assert.equal(currentURL(), projectsURL);
await visit(scopesURL);
assert.equal(currentURL(), projectsURL);
await visit(orgScopeURL);
assert.equal(currentURL(), projectsURL);
2020-07-24 18:08:37 -04:00
await visit(authenticateURL);
assert.equal(currentURL(), projectsURL);
await visit(authMethodAuthenticateURL);
assert.equal(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) {
assert.expect(4);
2020-09-16 14:33:04 -04:00
authenticateSession({
scope: { id: globalScope.id, type: globalScope.type },
});
await visit(indexURL);
assert.equal(currentURL(), orgsURL);
await visit(scopesURL);
assert.equal(currentURL(), orgsURL);
await visit(globalAuthenticateURL);
assert.equal(currentURL(), orgsURL);
assert.ok(currentSession().isAuthenticated);
});
2020-07-29 15:47:23 -04:00
test('failed authentication shows a notification message', async function (assert) {
2020-07-24 17:49:42 -04:00
assert.expect(3);
await visit(authMethodAuthenticateURL);
2020-07-24 17:49:42 -04:00
assert.notOk(currentSession().isAuthenticated);
await fillIn('[name="identification"]', 'error');
await click('[type="submit"]');
assert.ok(find('.rose-notification.is-error'));
2020-07-24 17:49:42 -04:00
assert.notOk(currentSession().isAuthenticated);
});
2020-07-29 15:47:23 -04:00
test('successful authentication redirects to projects', async function (assert) {
2020-07-24 17:49:42 -04:00
assert.expect(4);
await visit(authMethodAuthenticateURL);
2020-07-24 17:49:42 -04:00
assert.notOk(currentSession().isAuthenticated);
assert.equal(currentURL(), authMethodAuthenticateURL);
await fillIn('[name="identification"]', 'test');
await fillIn('[name="password"]', 'test');
await click('[type="submit"]');
assert.equal(currentURL(), projectsURL);
2020-07-24 17:49:42 -04:00
assert.ok(currentSession().isAuthenticated);
});
test('deauthentication redirects to first global authenticate method', async function (assert) {
2020-07-24 17:49:42 -04:00
assert.expect(3);
await visit(authMethodAuthenticateURL);
await fillIn('[name="identification"]', 'test');
await fillIn('[name="password"]', 'test');
await click('[type="submit"]');
assert.ok(currentSession().isAuthenticated);
// Open header utilities dropdown
await click('.rose-header-utilities .rose-dropdown summary');
// Find and click on last element in dropdown - should be deauthenticate button
2020-09-16 14:33:04 -04:00
const menu = findAll(
'.rose-header-utilities .rose-dropdown .rose-dropdown-content button'
);
await click(menu[menu.length - 1]);
2020-07-24 17:49:42 -04:00
assert.notOk(currentSession().isAuthenticated);
assert.equal(currentURL(), authMethodGlobalAuthenticateURL);
});
2020-09-08 17:35:02 -04:00
test('401 responses result in deauthentication', async function (assert) {
assert.expect(2);
2020-09-16 14:33:04 -04:00
authenticateSession({
scope: { id: globalScope.id, type: globalScope.type },
});
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-08 17:35:02 -04:00
this.server.get('/scopes', () => new Response(401));
setupOnerror(() => {
2020-09-16 14:33:04 -04:00
assert.notOk(
currentSession().isAuthenticated,
'Session is unauthenticated, after encountering 401'
);
2020-09-08 17:35:02 -04:00
});
await visit(projectsURL);
});
});