Refactor account and user abilities (#1694)

* refactor: 💡 add abilities to user and remove from account
This commit is contained in:
lisbet-alvarez authored and GitHub committed 2023-05-22 11:16:48 -07:00
1 parent fe81a626fa
commit 379f317d8b
5 files changed
+269 -102

No files matched your search

@@ -3,10 +3,10 @@
* SPDX-License-Identifier: MPL-2.0
*/
import AccountAbility from 'api/abilities/account';
import UserAbility from 'api/abilities/user';
import { inject as service } from '@ember/service';
export default class OverrideAccountAbility extends AccountAbility {
export default class OverrideUserAbility extends UserAbility {
// =service
@service features;
@@ -16,10 +16,14 @@ export default class OverrideAccountAbility extends AccountAbility {
*/
get canAddAccount() {
return this.features.isEnabled('ldap-auth-methods') || !this.model.isLDAP;
return this.features.isEnabled('ldap-auth-methods') || !this.account.isLDAP
? super.canAddAccount
: false;
}
get canRemoveAccount() {
return this.features.isEnabled('ldap-auth-methods') || !this.model.isLDAP;
return this.features.isEnabled('ldap-auth-methods') || !this.account.isLDAP
? super.canRemoveAccount
: false;
}
}
@@ -1,80 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import {
TYPE_AUTH_METHOD_LDAP,
TYPE_AUTH_METHOD_OIDC,
TYPE_AUTH_METHOD_PASSWORD,
} from 'api/models/auth-method';
module('Unit | Abilities | account', function (hooks) {
setupTest(hooks);
let canService;
let store;
let features;
hooks.beforeEach(function () {
canService = this.owner.lookup('service:can');
store = this.owner.lookup('service:store');
features = this.owner.lookup('service:features');
});
test('can add ldap account to user when feature flag enabled', function (assert) {
assert.expect(1);
features.enable('ldap-auth-methods');
const account = store.createRecord('account', {
type: TYPE_AUTH_METHOD_LDAP,
});
assert.true(canService.can('addAccount account', account));
});
test('can add non-ldap account to user', function (assert) {
assert.expect(2);
const account = store.createRecord('account', {
type: TYPE_AUTH_METHOD_OIDC,
});
assert.true(canService.can('addAccount account', account));
account.type = TYPE_AUTH_METHOD_PASSWORD;
assert.true(canService.can('addAccount account', account));
});
test('cannot add ldap account to user when feature flag disabled', function (assert) {
assert.expect(1);
const account = store.createRecord('account', {
type: TYPE_AUTH_METHOD_LDAP,
});
assert.false(canService.can('addAccount account', account));
});
test('can remove ldap account from user when feature flag enabled', function (assert) {
assert.expect(1);
features.enable('ldap-auth-methods');
const account = store.createRecord('account', {
type: TYPE_AUTH_METHOD_LDAP,
});
assert.true(canService.can('removeAccount account', account));
});
test('cannot remove ldap account from user when feature flag disabled', function (assert) {
assert.expect(1);
const account = store.createRecord('account', {
type: TYPE_AUTH_METHOD_LDAP,
});
assert.false(canService.can('removeAccount account', account));
});
test('can remove non-ldap account from user', function (assert) {
assert.expect(2);
const account = store.createRecord('account', {
type: TYPE_AUTH_METHOD_OIDC,
});
assert.true(canService.can('removeAccount account', account));
(account.type = TYPE_AUTH_METHOD_PASSWORD),
assert.true(canService.can('removeAccount account', account));
});
});
+109
View File
@@ -0,0 +1,109 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import {
TYPE_AUTH_METHOD_LDAP,
TYPE_AUTH_METHOD_OIDC,
TYPE_AUTH_METHOD_PASSWORD,
} from 'api/models/auth-method';
module('Unit | Abilities | user', function (hooks) {
setupTest(hooks);
let canService;
let store;
let features;
const instances = {
account: null,
user: null,
};
hooks.beforeEach(function () {
canService = this.owner.lookup('service:can');
store = this.owner.lookup('service:store');
features = this.owner.lookup('service:features');
instances.user = store.createRecord('user', {
authorized_actions: ['add-accounts', 'remove-accounts'],
});
instances.account = store.createRecord('account');
});
test('can add ldap account to user when feature flag enabled', function (assert) {
assert.expect(1);
features.enable('ldap-auth-methods');
instances.account.type = TYPE_AUTH_METHOD_LDAP;
assert.true(
canService.can('addAccount user', instances.user, {
account: instances.account,
})
);
});
test('can add non-ldap account to user', function (assert) {
assert.expect(2);
instances.account.type = TYPE_AUTH_METHOD_OIDC;
assert.true(
canService.can('addAccount user', instances.user, {
account: instances.account,
})
);
instances.account.type = TYPE_AUTH_METHOD_PASSWORD;
assert.true(
canService.can('addAccount user', instances.user, {
account: instances.account,
})
);
});
test('cannot add ldap account to user when feature flag disabled', function (assert) {
assert.expect(1);
instances.account.type = TYPE_AUTH_METHOD_LDAP;
assert.false(
canService.can('addAccount user', instances.user, {
account: instances.account,
})
);
});
test('can remove ldap account from user when feature flag enabled', function (assert) {
assert.expect(1);
features.enable('ldap-auth-methods');
instances.account.type = TYPE_AUTH_METHOD_LDAP;
assert.true(
canService.can('removeAccount user', instances.user, {
account: instances.account,
})
);
});
test('cannot remove ldap account from user when feature flag disabled', function (assert) {
assert.expect(1);
instances.account.type = TYPE_AUTH_METHOD_LDAP;
assert.false(
canService.can('removeAccount user', instances.user, {
account: instances.account,
})
);
});
test('can remove non-ldap account from user', function (assert) {
assert.expect(2);
instances.account.type = TYPE_AUTH_METHOD_OIDC;
assert.true(
canService.can('removeAccount user', instances.user, {
account: instances.account,
})
);
instances.account.type = TYPE_AUTH_METHOD_PASSWORD;
assert.true(
canService.can('removeAccount user', instances.user, {
account: instances.account,
})
);
});
});