refactor: 💡 account and auth-method abilities (#1691)

* refactor: 💡 account and auth-method abilities

* refactor: 💡 add update & delete abilities & unit tests
This commit is contained in:
lisbet-alvarez authored and GitHub committed 2023-05-17 14:31:12 -07:00
1 parent e8a10b3131
commit 6dd0423923
14 files changed
+551 -51

No files matched your search

@@ -0,0 +1,114 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import {
TYPE_AUTH_METHOD_PASSWORD,
TYPE_AUTH_METHOD_OIDC,
TYPE_AUTH_METHOD_LDAP,
} from 'api/models/auth-method';
module('Unit | Abilities | Auth Method', function (hooks) {
setupTest(hooks);
let canService;
let store;
hooks.beforeEach(function () {
canService = this.owner.lookup('service:can');
store = this.owner.lookup('service:store');
});
test('can read known auth method types when authorized', function (assert) {
assert.expect(4);
const authMethod = store.createRecord('auth-method', {
authorized_actions: ['read'],
type: TYPE_AUTH_METHOD_OIDC,
});
assert.true(canService.can('read auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_PASSWORD;
assert.true(canService.can('read auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_LDAP;
assert.true(canService.can('read auth-method', authMethod));
authMethod.type = 'no-such-type';
assert.false(canService.can('read auth-method', authMethod));
});
test('cannot read auth methods when unauthorized', function (assert) {
assert.expect(4);
const authMethod = store.createRecord('auth-method', {
authorized_actions: [],
type: TYPE_AUTH_METHOD_OIDC,
});
assert.false(canService.can('read auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_PASSWORD;
assert.false(canService.can('read auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_LDAP;
assert.false(canService.can('read auth-method', authMethod));
authMethod.type = 'no-such-type';
assert.false(canService.can('read auth-method', authMethod));
});
test('can delete known auth method types when authorized', function (assert) {
assert.expect(4);
const authMethod = store.createRecord('auth-method', {
authorized_actions: ['delete'],
type: TYPE_AUTH_METHOD_OIDC,
});
assert.true(canService.can('delete auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_PASSWORD;
assert.true(canService.can('delete auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_LDAP;
assert.true(canService.can('delete auth-method', authMethod));
authMethod.type = 'no-such-type';
assert.false(canService.can('delete auth-method', authMethod));
});
test('cannot delete auth methods when unauthorized', function (assert) {
assert.expect(4);
const authMethod = store.createRecord('auth-method', {
authorized_actions: ['delete'],
type: TYPE_AUTH_METHOD_OIDC,
});
assert.true(canService.can('delete auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_PASSWORD;
assert.true(canService.can('delete auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_LDAP;
assert.true(canService.can('delete auth-method', authMethod));
authMethod.type = 'no-such-type';
assert.false(canService.can('delete auth-method', authMethod));
});
test('can update known auth method types when authorized', function (assert) {
assert.expect(4);
const authMethod = store.createRecord('auth-method', {
authorized_actions: ['update'],
type: TYPE_AUTH_METHOD_OIDC,
});
assert.true(canService.can('update auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_PASSWORD;
assert.true(canService.can('update auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_LDAP;
assert.true(canService.can('update auth-method', authMethod));
authMethod.type = 'no-such-type';
assert.false(canService.can('update auth-method', authMethod));
});
test('cannot update auth methods when unauthorized', function (assert) {
assert.expect(4);
const authMethod = store.createRecord('auth-method', {
authorized_actions: [],
type: TYPE_AUTH_METHOD_OIDC,
});
assert.false(canService.can('update auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_PASSWORD;
assert.false(canService.can('update auth-method', authMethod));
authMethod.type = TYPE_AUTH_METHOD_LDAP;
assert.false(canService.can('update auth-method', authMethod));
authMethod.type = 'no-such-type';
assert.false(canService.can('update auth-method', authMethod));
});
});