Files
threshold-ui/ui/admin/tests/acceptance/auth-methods/create-test.js
T

223 lines
7.0 KiB
JavaScript
Raw Normal View History

2020-08-13 15:41:33 -04:00
import { module, test } from 'qunit';
2020-09-21 11:40:33 -04:00
import { visit, currentURL, click, find, fillIn } from '@ember/test-helpers';
2020-08-13 15:41:33 -04:00
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
2020-09-21 11:40:33 -04:00
import { Response } from 'miragejs';
import {
authenticateSession,
2020-11-16 15:08:59 -05:00
// // These are left here intentionally for future reference.
// //currentSession,
// //invalidateSession,
2020-09-21 11:40:33 -04:00
} from 'ember-simple-auth/test-support';
import { enableFeature } from 'ember-feature-flags/test-support';
2020-08-13 15:41:33 -04:00
2021-08-30 19:16:58 -07:00
module('Acceptance | auth-methods | create ', function (hooks) {
2020-08-13 15:41:33 -04:00
setupApplicationTest(hooks);
setupMirage(hooks);
2020-09-21 11:40:33 -04:00
const instances = {
scopes: {
global: null,
org: null,
2021-09-02 09:48:28 -07:00
project: null,
2020-09-21 11:40:33 -04:00
},
authMethod: null,
2021-08-30 19:16:58 -07:00
orgScope: null,
2020-09-21 11:40:33 -04:00
};
2021-08-30 19:16:58 -07:00
2020-09-21 11:40:33 -04:00
const urls = {
orgScope: null,
authMethods: null,
newAuthMethod: null,
authMethod: null,
};
2020-08-13 15:41:33 -04:00
hooks.beforeEach(function () {
2020-09-21 11:40:33 -04:00
authenticateSession({});
2021-09-02 09:48:28 -07:00
instances.orgScope = this.server.create(
'scope',
{
type: 'org',
scope: { id: 'global', type: 'global' },
},
'withChildren'
);
instances.scopes.project = this.server.create('scope', {
type: 'project',
scope: { id: instances.orgScope.id, type: instances.orgScope.type },
2020-09-21 11:40:33 -04:00
});
2021-08-30 19:16:58 -07:00
2020-09-21 11:40:33 -04:00
instances.authMethod = this.server.create('auth-method', {
2021-08-30 19:16:58 -07:00
scope: instances.orgScope,
2020-09-21 11:40:33 -04:00
});
2020-08-13 15:41:33 -04:00
// Generate route URLs for resources
2021-09-02 09:48:28 -07:00
urls.orgScope = `/scopes/${instances.orgScope.id}`;
urls.authMethods = `${urls.orgScope}/auth-methods`;
urls.newAuthMethod = `${urls.authMethods}/new?type=password`;
2020-09-21 11:40:33 -04:00
urls.authMethod = `${urls.authMethods}/${instances.authMethod.id}`;
2020-08-13 15:41:33 -04:00
});
2021-09-02 08:15:04 -07:00
test('Users can create new auth method', async function (assert) {
2020-09-21 11:40:33 -04:00
assert.expect(1);
const authMethodsCount = this.server.db.authMethods.length;
await visit(urls.newAuthMethod);
await fillIn('[name="name"]', 'AuthMethod name');
await fillIn('[name="description"]', 'description');
await click('form [type="submit"]:not(:disabled)');
assert.equal(this.server.db.authMethods.length, authMethodsCount + 1);
2020-08-13 16:08:45 -04:00
});
2021-09-02 08:15:04 -07:00
test('Users can navigate to new auth-methods route with proper authorization', async function (assert) {
2021-08-30 19:16:58 -07:00
assert.expect(2);
instances.orgScope.authorized_collection_actions['auth-methods'] = [
'create',
'list',
];
2021-09-02 09:48:28 -07:00
await visit(urls.orgScope);
2021-08-30 19:16:58 -07:00
assert.ok(
instances.orgScope.authorized_collection_actions['auth-methods'].includes(
'create'
)
);
assert.ok(find(`[href="${urls.authMethods}"]`));
});
2021-09-02 08:15:04 -07:00
test('Users cannot navigate to new auth-methods route without proper authorization', async function (assert) {
2021-08-30 19:16:58 -07:00
assert.expect(2);
instances.orgScope.authorized_collection_actions['auth-methods'] = [];
2021-09-02 09:48:28 -07:00
await visit(urls.orgScope);
2021-08-30 19:16:58 -07:00
assert.notOk(
instances.orgScope.authorized_collection_actions['auth-methods'].includes(
'create'
)
);
assert.notOk(find(`[href="${urls.authMethods}"]`));
});
2020-08-13 16:08:45 -04:00
test('can cancel new auth method creation', async function (assert) {
2020-09-21 11:40:33 -04:00
assert.expect(2);
const authMethodsCount = this.server.db.authMethods.length;
await visit(urls.newAuthMethod);
await fillIn('[name="name"]', 'AuthMethod name');
await fillIn('[name="description"]', 'description');
await click('form button:not([type="submit"])');
assert.equal(this.server.db.authMethods.length, authMethodsCount);
assert.equal(currentURL(), urls.authMethods);
2020-08-13 16:08:45 -04:00
});
2021-03-26 14:55:19 -04:00
test('user can make primary an auth method', async function (assert) {
assert.expect(2);
assert.notOk(
2021-08-30 19:16:58 -07:00
instances.orgScope.primaryAuthMethodId,
2021-03-26 14:55:19 -04:00
'Primary auth method is not yet set.'
);
enableFeature('primary-auth-method');
2021-03-26 14:55:19 -04:00
await visit(urls.authMethod);
await click(
'.rose-layout-page-actions .rose-dropdown-content [type="button"]:first-child'
);
2021-08-30 19:16:58 -07:00
const scope = this.server.schema.scopes.find(instances.orgScope.id);
2021-03-26 14:55:19 -04:00
assert.equal(
scope.primaryAuthMethodId,
instances.authMethod.id,
'Primary auth method is set.'
);
});
test('user is notified of error on make primary an auth method', async function (assert) {
assert.expect(2);
this.server.patch('/scopes/:id', () => {
return new Response(
400,
{},
{
status: 400,
code: 'fail',
message: 'Sorry!',
}
);
});
assert.notOk(
2021-08-30 19:16:58 -07:00
instances.orgScope.primaryAuthMethodId,
2021-03-26 14:55:19 -04:00
'Primary auth method is not yet set.'
);
enableFeature('primary-auth-method');
2021-03-26 14:55:19 -04:00
await visit(urls.authMethod);
await click(
'.rose-layout-page-actions .rose-dropdown-content [type="button"]:first-child'
);
assert.ok(find('.rose-notification'));
});
test('user can remove as primary an auth method', async function (assert) {
assert.expect(2);
2021-08-30 19:16:58 -07:00
instances.orgScope.update({
2021-03-26 14:55:19 -04:00
primaryAuthMethodId: instances.authMethod.id,
});
assert.ok(
2021-08-30 19:16:58 -07:00
instances.orgScope.primaryAuthMethodId,
2021-03-26 14:55:19 -04:00
'Primary auth method is set.'
);
enableFeature('primary-auth-method');
2021-03-26 14:55:19 -04:00
await visit(urls.authMethod);
await click(
'.rose-layout-page-actions .rose-dropdown-content [type="button"]:first-child'
);
2021-08-30 19:16:58 -07:00
const scope = this.server.schema.scopes.find(instances.orgScope.id);
2021-03-26 14:55:19 -04:00
assert.notOk(scope.primaryAuthMethodId, 'Primary auth method is unset.');
});
test('user is notified of error on remove as primary an auth method', async function (assert) {
assert.expect(2);
this.server.patch('/scopes/:id', () => {
return new Response(
400,
{},
{
status: 400,
code: 'fail',
message: 'Sorry!',
}
);
});
2021-08-30 19:16:58 -07:00
instances.orgScope.update({
2021-03-26 14:55:19 -04:00
primaryAuthMethodId: instances.authMethod.id,
});
assert.ok(
2021-08-30 19:16:58 -07:00
instances.orgScope.primaryAuthMethodId,
2021-03-26 14:55:19 -04:00
'Primary auth method is set.'
);
enableFeature('primary-auth-method');
2021-03-26 14:55:19 -04:00
await visit(urls.authMethod);
await click(
'.rose-layout-page-actions .rose-dropdown-content [type="button"]:first-child'
);
assert.ok(find('.rose-notification'));
});
test('user can make and remove primary auth methods from index', async function (assert) {
assert.expect(3);
assert.notOk(
2021-08-30 19:16:58 -07:00
instances.orgScope.primaryAuthMethodId,
'Primary auth method is not yet set.'
);
enableFeature('primary-auth-method');
await visit(urls.authMethods);
await click(
'.rose-table-body .rose-table-row:first-child .rose-dropdown-content [type="button"]:first-child'
);
2021-08-30 19:16:58 -07:00
let scope = this.server.schema.scopes.find(instances.orgScope.id);
assert.equal(
scope.primaryAuthMethodId,
instances.authMethod.id,
'Primary auth method is set.'
);
await click(
'.rose-table-body .rose-table-row:first-child .rose-dropdown-content [type="button"]:first-child'
);
2021-08-30 19:16:58 -07:00
scope = this.server.schema.scopes.find(instances.orgScope.id);
assert.notOk(scope.primaryAuthMethodId, 'Primary auth method is unset.');
});
2020-08-13 15:41:33 -04:00
});