Files
threshold-ui/ui/admin/tests/unit/abilities/credential-test.js
T

82 lines
3.2 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2022-12-12 16:37:01 -08:00
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Abilities | credential', function (hooks) {
setupTest(hooks);
2023-04-03 20:28:31 -07:00
let featuresService;
2022-12-16 15:06:19 -05:00
hooks.beforeEach(function () {
// Enable json-credentials feature by default
2023-04-03 20:28:31 -07:00
featuresService = this.owner.lookup('service:features');
2022-12-16 15:06:19 -05:00
});
test('can read credentials, including JSON credentials, when authorized and json-credentials feature is enabled', function (assert) {
2022-12-12 16:37:01 -08:00
assert.expect(3);
2023-04-03 20:28:31 -07:00
featuresService.enable('json-credentials');
2022-12-12 16:37:01 -08:00
const service = this.owner.lookup('service:can');
2022-12-16 15:06:19 -05:00
const store = this.owner.lookup('service:store');
const credential = store.createRecord('credential', {
2022-12-12 16:37:01 -08:00
authorized_actions: ['read'],
2022-12-15 09:11:01 -08:00
type: 'json',
2022-12-16 15:06:19 -05:00
});
assert.true(service.can('read credential', credential));
2022-12-15 09:11:01 -08:00
credential.type = 'username_password';
2022-12-16 15:06:19 -05:00
assert.true(service.can('read credential', credential));
2022-12-15 09:11:01 -08:00
credential.type = 'ssh_private_key';
2022-12-16 15:06:19 -05:00
assert.true(service.can('read credential', credential));
2022-12-12 16:37:01 -08:00
});
2022-12-16 15:06:19 -05:00
test('cannot read credentials, including JSON credentials, when unauthorized and json-credentials feature is enabled', function (assert) {
2022-12-12 16:37:01 -08:00
assert.expect(3);
2023-04-03 20:28:31 -07:00
featuresService.enable('json-credentials');
2022-12-12 16:37:01 -08:00
const service = this.owner.lookup('service:can');
2022-12-16 15:06:19 -05:00
const store = this.owner.lookup('service:store');
const credential = store.createRecord('credential', {
2022-12-12 16:37:01 -08:00
authorized_actions: [],
2022-12-15 09:11:01 -08:00
type: 'json',
2022-12-16 15:06:19 -05:00
});
assert.false(service.can('read credential', credential));
2022-12-15 09:11:01 -08:00
credential.type = 'username_password';
2022-12-16 15:06:19 -05:00
assert.false(service.can('read credential', credential));
2022-12-15 09:11:01 -08:00
credential.type = 'ssh_private_key';
2022-12-16 15:06:19 -05:00
assert.false(service.can('read credential', credential));
2022-12-12 16:37:01 -08:00
});
2022-12-16 15:06:19 -05:00
test('cannot read credentials, including JSON credentials, when unauthorized and json-credentials feature is disabled', function (assert) {
2023-04-03 20:28:31 -07:00
assert.expect(4);
2022-12-12 16:37:01 -08:00
const service = this.owner.lookup('service:can');
2022-12-16 15:06:19 -05:00
const store = this.owner.lookup('service:store');
const credential = store.createRecord('credential', {
2022-12-12 16:37:01 -08:00
authorized_actions: [],
2022-12-15 09:11:01 -08:00
type: 'json',
2022-12-16 15:06:19 -05:00
});
2023-04-03 20:28:31 -07:00
assert.false(featuresService.isEnabled('json-credentials'));
2022-12-16 15:06:19 -05:00
assert.false(service.can('read credential', credential));
2022-12-15 09:11:01 -08:00
credential.type = 'username_password';
2022-12-16 15:06:19 -05:00
assert.false(service.can('read credential', credential));
2022-12-15 09:11:01 -08:00
credential.type = 'ssh_private_key';
2022-12-16 15:06:19 -05:00
assert.false(service.can('read credential', credential));
2022-12-12 16:37:01 -08:00
});
2022-12-16 15:06:19 -05:00
test('can read credentials, excepting JSON credentials, when authorized and json-credentials feature is disabled', function (assert) {
2023-04-03 20:28:31 -07:00
assert.expect(4);
2022-12-12 16:37:01 -08:00
const service = this.owner.lookup('service:can');
2022-12-16 15:06:19 -05:00
const store = this.owner.lookup('service:store');
const credential = store.createRecord('credential', {
2022-12-12 16:37:01 -08:00
authorized_actions: ['read'],
2022-12-15 09:11:01 -08:00
type: 'json',
2022-12-16 15:06:19 -05:00
});
2023-04-03 20:28:31 -07:00
assert.false(featuresService.isEnabled('json-credentials'));
2022-12-16 15:06:19 -05:00
assert.false(service.can('read credential', credential));
2022-12-15 09:11:01 -08:00
credential.type = 'username_password';
2022-12-16 15:06:19 -05:00
assert.true(service.can('read credential', credential));
2022-12-15 09:11:01 -08:00
credential.type = 'ssh_private_key';
2022-12-16 15:06:19 -05:00
assert.true(service.can('read credential', credential));
2022-12-12 16:37:01 -08:00
});
});