2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2021-02-19 16:42:16 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupTest } from 'ember-qunit';
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
const assertAuthorize = function (assertion, params = {}, token, $, adapter) {
|
2021-11-11 12:02:29 +00:00
|
|
|
const rpc = adapter.rpc;
|
|
|
|
const env = adapter.env;
|
|
|
|
const settings = adapter.settings;
|
|
|
|
adapter.env = {
|
2022-09-15 08:43:17 +00:00
|
|
|
var: (str) => $[str],
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
adapter.settings = {
|
2022-09-15 08:43:17 +00:00
|
|
|
findBySlug: (_) => token,
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
adapter.rpc = function (request, respond) {
|
2021-11-11 12:02:29 +00:00
|
|
|
request(
|
|
|
|
{
|
|
|
|
requestForAuthorize: (request, params) => {
|
|
|
|
assertion(request, params);
|
2022-09-15 08:43:17 +00:00
|
|
|
},
|
2021-11-11 12:02:29 +00:00
|
|
|
},
|
|
|
|
() => {},
|
|
|
|
params,
|
|
|
|
params
|
2022-09-15 08:43:17 +00:00
|
|
|
);
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
adapter.authorize({}, { modelName: 'permission' }, 1, {});
|
2021-11-11 12:02:29 +00:00
|
|
|
adapter.rpc = rpc;
|
|
|
|
adapter.env = env;
|
|
|
|
adapter.settings = settings;
|
2022-09-15 08:43:17 +00:00
|
|
|
};
|
|
|
|
module('Unit | Adapter | permission', function (hooks) {
|
2021-02-19 16:42:16 +00:00
|
|
|
setupTest(hooks);
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
test('it exists', function (assert) {
|
2021-02-19 16:42:16 +00:00
|
|
|
let adapter = this.owner.lookup('adapter:permission');
|
|
|
|
assert.ok(adapter);
|
|
|
|
});
|
2021-11-11 12:02:29 +00:00
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
test(`authorize adds the tokens default namespace if one isn't specified`, function (assert) {
|
2021-11-11 12:02:29 +00:00
|
|
|
const adapter = this.owner.lookup('adapter:permission');
|
|
|
|
const expected = 'test';
|
|
|
|
const token = {
|
2022-09-15 08:43:17 +00:00
|
|
|
Namespace: expected,
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
const env = {
|
2022-09-15 08:43:17 +00:00
|
|
|
CONSUL_NSPACES_ENABLED: true,
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
const cases = [
|
|
|
|
undefined,
|
|
|
|
{
|
2022-09-15 08:43:17 +00:00
|
|
|
ns: undefined,
|
2021-11-11 12:02:29 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-15 08:43:17 +00:00
|
|
|
ns: '',
|
|
|
|
},
|
2021-11-11 12:02:29 +00:00
|
|
|
];
|
|
|
|
assert.expect(cases.length);
|
2022-09-15 08:43:17 +00:00
|
|
|
cases.forEach((params) => {
|
|
|
|
assertAuthorize(
|
|
|
|
(request, params) => {
|
|
|
|
assert.equal(params.ns, expected);
|
|
|
|
},
|
|
|
|
params,
|
|
|
|
token,
|
|
|
|
env,
|
|
|
|
adapter
|
|
|
|
);
|
|
|
|
});
|
2021-11-11 12:02:29 +00:00
|
|
|
});
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
test(`authorize doesn't add the tokens default namespace if one is specified`, function (assert) {
|
2021-11-11 12:02:29 +00:00
|
|
|
assert.expect(1);
|
|
|
|
const adapter = this.owner.lookup('adapter:permission');
|
|
|
|
const notExpected = 'test';
|
|
|
|
const expected = 'default';
|
|
|
|
const token = {
|
2022-09-15 08:43:17 +00:00
|
|
|
Namespace: notExpected,
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
const env = {
|
2022-09-15 08:43:17 +00:00
|
|
|
CONSUL_NSPACES_ENABLED: true,
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
assertAuthorize(
|
|
|
|
(request, params) => {
|
2022-09-15 08:43:17 +00:00
|
|
|
assert.equal(params.ns, expected);
|
2021-11-11 12:02:29 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-15 08:43:17 +00:00
|
|
|
ns: expected,
|
2021-11-11 12:02:29 +00:00
|
|
|
},
|
|
|
|
token,
|
|
|
|
env,
|
|
|
|
adapter
|
2022-09-15 08:43:17 +00:00
|
|
|
);
|
2021-11-11 12:02:29 +00:00
|
|
|
});
|
2022-09-15 08:43:17 +00:00
|
|
|
test(`authorize adds the tokens default partition if one isn't specified`, function (assert) {
|
2021-11-11 12:02:29 +00:00
|
|
|
const adapter = this.owner.lookup('adapter:permission');
|
|
|
|
const expected = 'test';
|
|
|
|
const token = {
|
2022-09-15 08:43:17 +00:00
|
|
|
Partition: expected,
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
const env = {
|
2022-09-15 08:43:17 +00:00
|
|
|
CONSUL_PARTITIONS_ENABLED: true,
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
const cases = [
|
|
|
|
undefined,
|
|
|
|
{
|
2022-09-15 08:43:17 +00:00
|
|
|
partition: undefined,
|
2021-11-11 12:02:29 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-15 08:43:17 +00:00
|
|
|
partition: '',
|
|
|
|
},
|
2021-11-11 12:02:29 +00:00
|
|
|
];
|
|
|
|
assert.expect(cases.length);
|
2022-09-15 08:43:17 +00:00
|
|
|
cases.forEach((params) => {
|
|
|
|
assertAuthorize(
|
|
|
|
(request, params) => {
|
|
|
|
assert.equal(params.partition, expected);
|
|
|
|
},
|
|
|
|
params,
|
|
|
|
token,
|
|
|
|
env,
|
|
|
|
adapter
|
|
|
|
);
|
|
|
|
});
|
2021-11-11 12:02:29 +00:00
|
|
|
});
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
test(`authorize doesn't add the tokens default partition if one is specified`, function (assert) {
|
2021-11-11 12:02:29 +00:00
|
|
|
assert.expect(1);
|
|
|
|
const adapter = this.owner.lookup('adapter:permission');
|
|
|
|
const notExpected = 'test';
|
|
|
|
const expected = 'default';
|
|
|
|
const token = {
|
2022-09-15 08:43:17 +00:00
|
|
|
Partition: notExpected,
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
const env = {
|
2022-09-15 08:43:17 +00:00
|
|
|
CONSUL_PARTITIONS_ENABLED: true,
|
2021-11-11 12:02:29 +00:00
|
|
|
};
|
|
|
|
assertAuthorize(
|
|
|
|
(request, params) => {
|
2022-09-15 08:43:17 +00:00
|
|
|
assert.equal(params.partition, expected);
|
2021-11-11 12:02:29 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-15 08:43:17 +00:00
|
|
|
partition: expected,
|
2021-11-11 12:02:29 +00:00
|
|
|
},
|
|
|
|
token,
|
|
|
|
env,
|
|
|
|
adapter
|
2022-09-15 08:43:17 +00:00
|
|
|
);
|
2021-11-11 12:02:29 +00:00
|
|
|
});
|
2021-02-19 16:42:16 +00:00
|
|
|
});
|