Files
threshold-ui/addons/api/tests/unit/models/worker-test.js
T

139 lines
4.4 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
2022-07-28 14:33:23 -04:00
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
2022-08-09 15:34:42 -04:00
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
2022-07-28 14:33:23 -04:00
module('Unit | Model | worker', function (hooks) {
setupTest(hooks);
2022-08-09 15:34:42 -04:00
setupMirage(hooks);
2022-07-28 14:33:23 -04:00
2022-08-26 11:50:53 -04:00
test('it has a `createWorkerLed` method that targets a specific POST API', async function (assert) {
2022-08-09 15:34:42 -04:00
assert.expect(2);
2022-08-26 11:50:53 -04:00
const scopeId = 'global';
const workerGeneratedAuthToken = 'token';
2024-03-13 15:52:55 -04:00
this.server.post('/workers:create:worker-led', (schema, request) => {
2022-08-09 15:34:42 -04:00
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
2022-08-26 11:50:53 -04:00
scope_id: scopeId,
worker_generated_auth_token: workerGeneratedAuthToken,
2022-08-09 15:34:42 -04:00
});
return { id: '123abc' };
});
const store = this.owner.lookup('service:store');
2022-08-26 11:50:53 -04:00
const model = store.createRecord('worker', {
type: 'pki',
scope: {
scope_id: scopeId,
type: 'global',
},
});
2022-08-26 11:50:53 -04:00
await model.createWorkerLed(workerGeneratedAuthToken);
2022-08-09 15:34:42 -04:00
const worker = store.peekRecord('worker', '123abc');
2022-08-09 15:34:42 -04:00
assert.ok(worker);
2022-07-28 14:33:23 -04:00
});
test('it has a `configTagList` method that returns an array of key/value pair objects', function (assert) {
const store = this.owner.lookup('service:store');
const model = store.createRecord('worker', {
config_tags: {
tag1: ['value1', 'value2'],
tag2: ['value3'],
},
});
const expected = [
{ key: 'tag1', value: 'value1', type: 'config' },
{ key: 'tag1', value: 'value2', type: 'config' },
{ key: 'tag2', value: 'value3', type: 'config' },
];
assert.deepEqual(model.configTagList, expected);
});
test('it has a `configTagList` method that returns null if there are no config tags', function (assert) {
const store = this.owner.lookup('service:store');
const model = store.createRecord('worker');
assert.deepEqual(model.configTagList, null);
});
test('it has a `apiTagList` method that returns an array of key/value pair objects', function (assert) {
const store = this.owner.lookup('service:store');
const model = store.createRecord('worker', {
api_tags: {
tag1: ['value1', 'value2'],
tag2: ['value3'],
},
});
const expected = [
{ key: 'tag1', value: 'value1', type: 'api' },
{ key: 'tag1', value: 'value2', type: 'api' },
{ key: 'tag2', value: 'value3', type: 'api' },
];
assert.deepEqual(model.apiTagList, expected);
});
test('it has a `apiTagList` method that returns null if there are no api tags', function (assert) {
const store = this.owner.lookup('service:store');
const model = store.createRecord('worker');
assert.deepEqual(model.apiTagList, null);
});
test('it has a `allTags` method that returns an array of key/value pair objects with tag type', function (assert) {
const store = this.owner.lookup('service:store');
const model = store.createRecord('worker', {
config_tags: {
tag1: ['value1', 'value2'],
tag2: ['value3'],
},
api_tags: {
tag1: ['value1', 'value2'],
tag2: ['value3'],
},
});
const expected = [
{ key: 'tag1', value: 'value1', type: 'config' },
{ key: 'tag1', value: 'value2', type: 'config' },
{ key: 'tag2', value: 'value3', type: 'config' },
{ key: 'tag1', value: 'value1', type: 'api' },
{ key: 'tag1', value: 'value2', type: 'api' },
{ key: 'tag2', value: 'value3', type: 'api' },
];
assert.deepEqual(model.allTags, expected);
});
test('it has a `allTags` method that returns an empty array if there are no config or api tags', function (assert) {
const store = this.owner.lookup('service:store');
const model = store.createRecord('worker');
assert.deepEqual(model.allTags, []);
});
test('tagCount returns the total number of tags', function (assert) {
const store = this.owner.lookup('service:store');
const model = store.createRecord('worker', {
canonical_tags: {
tag1: ['value1', 'value2'],
tag2: ['value3'],
},
});
assert.strictEqual(model.tagCount, 3);
});
test('tagCount returns 0 if there are no tags', function (assert) {
const store = this.owner.lookup('service:store');
const model = store.createRecord('worker');
assert.strictEqual(model.tagCount, 0);
});
2022-07-28 14:33:23 -04:00
});