Files
threshold-ui/addons/api/tests/unit/abilities/user-test.js
T

147 lines
4.5 KiB
JavaScript
Raw Normal View History

/**
2026-01-28 17:09:06 -08:00
* Copyright IBM Corp. 2021, 2026
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
2023-05-22 11:16:48 -07:00
import {
TYPE_AUTH_METHOD_LDAP,
TYPE_AUTH_METHOD_OIDC,
TYPE_AUTH_METHOD_PASSWORD,
} from 'api/models/auth-method';
2021-10-01 17:00:36 -04:00
module('Unit | Abilities | User', function (hooks) {
setupTest(hooks);
2026-02-24 13:48:34 -05:00
let abilitiesService;
2023-05-22 11:16:48 -07:00
let store;
const instances = {
account: null,
user: null,
};
hooks.beforeEach(function () {
2026-02-24 13:48:34 -05:00
abilitiesService = this.owner.lookup('service:abilities');
2023-05-22 11:16:48 -07:00
store = this.owner.lookup('service:store');
instances.user = store.createRecord('user');
instances.account = store.createRecord('account');
});
test('can add accounts to user when authorized and given a known account type', function (assert) {
instances.user.authorized_actions = ['add-accounts'];
2026-02-24 13:48:34 -05:00
assert.true(abilitiesService.can('addAccounts user', instances.user));
2023-05-22 11:16:48 -07:00
instances.account.type = TYPE_AUTH_METHOD_OIDC;
assert.true(
2026-02-24 13:48:34 -05:00
abilitiesService.can('addAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = TYPE_AUTH_METHOD_PASSWORD;
assert.true(
2026-02-24 13:48:34 -05:00
abilitiesService.can('addAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = TYPE_AUTH_METHOD_LDAP;
assert.true(
2026-02-24 13:48:34 -05:00
abilitiesService.can('addAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = 'no-such-type';
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('addAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
});
test('cannot add accounts to user when unauthorized', function (assert) {
instances.user.authorized_actions = [];
2026-02-24 13:48:34 -05:00
assert.false(abilitiesService.can('addAccounts user', instances.user));
2023-05-22 11:16:48 -07:00
instances.account.type = TYPE_AUTH_METHOD_OIDC;
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('addAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = TYPE_AUTH_METHOD_PASSWORD;
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('addAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = TYPE_AUTH_METHOD_LDAP;
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('addAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = 'no-such-type';
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('addAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
});
test('can remove accounts from user when authorized and given a known account type', function (assert) {
instances.user.authorized_actions = ['remove-accounts'];
2026-02-24 13:48:34 -05:00
assert.true(abilitiesService.can('removeAccounts user', instances.user));
2023-05-22 11:16:48 -07:00
instances.account.type = TYPE_AUTH_METHOD_OIDC;
assert.true(
2026-02-24 13:48:34 -05:00
abilitiesService.can('removeAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = TYPE_AUTH_METHOD_PASSWORD;
assert.true(
2026-02-24 13:48:34 -05:00
abilitiesService.can('removeAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = TYPE_AUTH_METHOD_LDAP;
assert.true(
2026-02-24 13:48:34 -05:00
abilitiesService.can('removeAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = 'no-such-type';
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('removeAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
});
2023-05-22 11:16:48 -07:00
test('cannot remove accounts from user when unauthorized', function (assert) {
instances.user.authorized_actions = [];
2026-02-24 13:48:34 -05:00
assert.false(abilitiesService.can('removeAccounts user', instances.user));
2023-05-22 11:16:48 -07:00
instances.account.type = TYPE_AUTH_METHOD_OIDC;
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('addAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = TYPE_AUTH_METHOD_PASSWORD;
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('removeAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = TYPE_AUTH_METHOD_LDAP;
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('removeAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
instances.account.type = 'no-such-type';
assert.false(
2026-02-24 13:48:34 -05:00
abilitiesService.can('removeAccount user', instances.user, {
2023-05-22 11:16:48 -07:00
account: instances.account,
}),
2023-05-22 11:16:48 -07:00
);
});
});