Files
threshold-ui/ui/admin/tests/unit/controllers/scopes/scope/targets/index-test.js
T
hashiccandZhihe Li 63b810d53f Migrate from ember-cli-mirage to miragejs (#3088)
* chore: 🤖 import explicit mirage references

* chore: 🤖 add mirage setupMirage helper

* chore: 🤖 working example with application adapter test

* chore: 🤖 use internal setupMirage test helper from api addon

* chore: 🤖 conditionally load mirage

* chore: 🤖 filter mirage folder inclusion in production builds

* chore: 🤖 drop ember-mirage and use our own test helpers

* chore: 🤖 collapse reference to import declaration

* docs: ✏️ add comment better explaining the mirage test env

* chore: 🤖 update configuration and manage mirage-related deps

* fix: 🐛 restore ipc mock and add missing checkOS handler

* chore: 🤖 add comment explaining omitting mirage deps in build

* chore: 🤖 allow mirage.enabled to include deps and handlers

* chore: 🤖 re-enable mirage for core dummy app

* Add missing copyright headers

* chore: 🤖 use embroider macros to conditionally start mirage

* docs: ✏️ update api readme for mirage changes

* chore: 🤖 replace remaining ember-cli-mirage references

* chore: 🤖 add mirage to test builds

* fix: 🐛 ensure that auto mirage start doesn't happen in test env

* chore: 🤖 use const instead of var

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>

* refactor: 💡 use cleaner logic for excluded api build modules

* fix: 🐛 handle issue where importSync was too eager

This was fixed by bumping the @embroider/macros dependency that includes
a fix to this issue:
https://github.com/embroider-build/embroider/pull/2511

* refactor: 💡 move mirage from addon to app directory

* refactor: 💡 update mirage test helper to be a factory

* chore: 🤖 exclude mirage directory when necessary at build time

* chore: 🤖 add miragejs as admin dev dependency

* refactor: 💡 update reference to startMirage

* chore: 🤖 update local mirage test helper

* chore: 🤖 change import path for mirage on desktop

* chore: 🤖 update imports in tests for startMirage

* chore: 🤖 update startMirage imports for api addon tests

* chore: 🤖 add miragejs dev dep to desktop

* docs: ✏️ update README

* chore: 🤖 set up setupMirage for core tests

* chore: 🤖 fix copyright header

* Add missing copyright headers

---------

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>
2026-02-03 13:54:32 -05:00

343 lines
11 KiB
JavaScript

/**
* Copyright IBM Corp. 2021, 2026
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { visit, waitUntil } from '@ember/test-helpers';
import { setupMirage } from 'admin/tests/helpers/mirage';
import { setupSqlite } from 'api/test-support/helpers/sqlite';
import { setupIntl } from 'ember-intl/test-support';
import { authenticateSession } from 'ember-simple-auth/test-support';
import {
TYPE_TARGET_TCP,
TYPE_TARGET_SSH,
TYPE_TARGET_RDP,
} from 'api/models/target';
module('Unit | Controller | scopes/scope/targets/index', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
setupSqlite(hooks);
setupIntl(hooks, 'en-us');
let intl;
let store;
let controller;
let getTargetCount;
let getAliasCount;
let features;
const instances = {
scopes: {
global: null,
org: null,
project: null,
},
target: null,
hostSet: null,
credential: null,
alias: null,
};
const urls = {
orgScope: null,
projectScope: null,
targets: null,
target: null,
alias: null,
};
hooks.beforeEach(async function () {
intl = this.owner.lookup('service:intl');
store = this.owner.lookup('service:store');
controller = this.owner.lookup('controller:scopes/scope/targets/index');
features = this.owner.lookup('service:features');
instances.scopes.global = this.server.create(
'scope',
{ id: 'global' },
'withGlobalAuth',
);
await authenticateSession({
isGlobal: true,
account_id: this.server.schema.accounts.first().id,
});
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
instances.scopes.project = this.server.create('scope', {
type: 'project',
scope: { id: instances.scopes.org.id, type: 'org' },
});
instances.target = this.server.create('target', {
scope: instances.scopes.project,
});
instances.hostSet = this.server.create('host-set', {
scope: instances.scopes.project,
});
instances.credential = this.server.create('credential', {
scope: instances.scopes.project,
});
instances.alias = this.server.create('alias', {
scope: instances.scopes.global,
});
urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`;
urls.projectScope = `/scopes/${instances.scopes.project.id}`;
urls.targets = `${urls.projectScope}/targets`;
urls.target = `${urls.targets}/${instances.target.id}`;
urls.alias = `${urls.target}/${instances.alias.id}`;
getTargetCount = () => this.server.schema.targets.all().models.length;
getAliasCount = () => this.server.schema.aliases.all().models.length;
});
test('it exists', function (assert) {
assert.ok(controller);
});
test('availableSessionOptions returns expected object', function (assert) {
let controller = this.owner.lookup('controller:scopes/scope/targets/index');
assert.deepEqual(controller.availableSessionOptions, [
{ id: 'yes', name: 'Has active sessions' },
{ id: 'no', name: 'No active sessions' },
]);
});
test('targetTypeOptions returns expected object', function (assert) {
features.enable('rdp-target');
features.enable('ssh-target');
let controller = this.owner.lookup('controller:scopes/scope/targets/index');
assert.deepEqual(controller.targetTypeOptions, [
{ id: TYPE_TARGET_TCP, name: 'Generic TCP' },
{ id: TYPE_TARGET_SSH, name: 'SSH' },
{ id: TYPE_TARGET_RDP, name: 'RDP' },
]);
});
test('filters returns expected entries', function (assert) {
let controller = this.owner.lookup('controller:scopes/scope/targets/index');
assert.ok(controller.filters.allFilters);
assert.ok(controller.filters.selectedFilters);
});
test('handleSearchInput action sets expected values correctly', async function (assert) {
let controller = this.owner.lookup('controller:scopes/scope/targets/index');
const searchValue = 'test';
controller.handleSearchInput({ target: { value: searchValue } });
await waitUntil(() => controller.search === searchValue);
assert.strictEqual(controller.page, 1);
assert.strictEqual(controller.search, searchValue);
});
test('applyFilter action sets expected values correctly', async function (assert) {
let controller = this.owner.lookup('controller:scopes/scope/targets/index');
const selectedItems = ['yes'];
controller.applyFilter('availableSessions', selectedItems);
assert.strictEqual(controller.page, 1);
assert.deepEqual(controller.availableSessions, selectedItems);
});
test('cancel action rolls-back changes on the specified model', async function (assert) {
await visit(urls.targets);
const target = await store.findRecord('target', instances.target.id);
target.name = 'test';
assert.strictEqual(target.name, 'test');
await controller.cancel(target);
assert.notEqual(target.name, 'test');
});
test('save action saves changes on the specified model', async function (assert) {
await visit(urls.targets);
const target = await store.findRecord('target', instances.target.id);
target.name = 'test';
await controller.save(target);
assert.strictEqual(target.name, 'test');
});
test('saveWithAddress action saves address and removes host sources on the specified model', async function (assert) {
await visit(urls.targets);
const target = await store.findRecord('target', instances.target.id);
await target.addHostSources([instances.hostSet.id]);
target.address = '127.0.0.1';
assert.deepEqual(target.host_sources, [
{ host_source_id: instances.hostSet.id },
]);
await controller.saveWithAddress(target);
assert.deepEqual(target.host_sources, []);
assert.strictEqual(target.address, '127.0.0.1');
});
test('delete action destroys specified model', async function (assert) {
await visit(urls.orgScope);
const target = await store.findRecord('target', instances.target.id);
const targetCount = getTargetCount();
await controller.delete(target);
assert.strictEqual(getTargetCount(), targetCount - 1);
});
test('removeHostSource action removes specified host-set from model', async function (assert) {
await visit(urls.targets);
const target = await store.findRecord('target', instances.target.id);
await target.addHostSources([instances.hostSet.id]);
assert.deepEqual(target.host_sources, [
{ host_source_id: instances.hostSet.id },
]);
await controller.removeHostSource(target, instances.hostSet);
assert.deepEqual(target.host_sources, []);
});
test('removeInjectedApplicationCredentialSource action removes specified credential from model', async function (assert) {
await visit(urls.targets);
const target = await store.findRecord('target', instances.target.id);
await target.addInjectedApplicationCredentialSources([
instances.credential.id,
]);
assert.deepEqual(target.injected_application_credential_source_ids, [
{ value: instances.credential.id },
]);
await controller.removeInjectedApplicationCredentialSource(
target,
instances.credential,
);
assert.deepEqual(target.injected_application_credential_source_ids, []);
});
test('removeBrokeredCredentialSource action removes specified credential from model', async function (assert) {
await visit(urls.targets);
const target = await store.findRecord('target', instances.target.id);
await target.addBrokeredCredentialSources([instances.credential.id]);
assert.deepEqual(target.brokered_credential_source_ids, [
{ value: instances.credential.id },
]);
await controller.removeBrokeredCredentialSource(
target,
instances.credential,
);
assert.deepEqual(target.brokered_credential_source_ids, []);
});
test('deleteAlias action destroys specified model', async function (assert) {
await visit(urls.target);
const alias = await store.findRecord('alias', instances.alias.id);
const aliasCount = getAliasCount();
await controller.deleteAlias(alias);
assert.strictEqual(getAliasCount(), aliasCount - 1);
});
test('clearAlias action removes destination_id from model', async function (assert) {
await visit(urls.alias);
const alias = await store.findRecord('alias', instances.alias.id);
alias.destination_id = 'www.test.com';
assert.strictEqual(alias.destination_id, 'www.test.com');
await controller.clearAlias(alias);
assert.notOk(alias.destination_id);
});
test('saveAlias action saves changes on the specified model', async function (assert) {
await visit(urls.alias);
const alias = await store.findRecord('alias', instances.alias.id);
alias.name = 'test';
await controller.saveAlias(alias);
assert.strictEqual(alias.name, 'test');
});
test('cancelAlias action rolls-back changes on the specified model', async function (assert) {
await visit(urls.alias);
const alias = await store.findRecord('alias', instances.alias.id);
alias.name = 'test';
assert.strictEqual(alias.name, 'test');
await controller.cancelAlias(alias);
assert.notEqual(alias.name, 'test');
});
test('cancelWorkerFilter action rolls-back changes on the specified model', async function (assert) {
await visit(urls.target);
const target = await store.findRecord('target', instances.target.id);
target.egress_worker_filter = 'test';
assert.strictEqual(target.egress_worker_filter, 'test');
await controller.cancelWorkerFilter(target);
assert.notEqual(target.egress_worker_filter, 'test');
});
test('saveWorkerFilter action saves changes on the specified model', async function (assert) {
await visit(urls.target);
const target = await store.findRecord('target', instances.target.id);
target.egress_worker_filter = 'test';
await controller.saveWorkerFilter(target);
assert.strictEqual(target.egress_worker_filter, 'test');
});
test('messageDescription returns correct translation with list authorization', async function (assert) {
await visit(urls.targets);
assert.strictEqual(
controller.messageDescription,
intl.t('resources.target.description'),
);
});
test('messageDescription returns correct translation with create authorization', async function (assert) {
instances.scopes.project.authorized_collection_actions.targets = ['create'];
await visit(urls.targets);
assert.strictEqual(
controller.messageDescription,
intl.t('descriptions.create-but-not-list', {
resource: intl.t('resources.target.title_plural'),
}),
);
});
test('messageDescription returns correct translation with no authorization', async function (assert) {
instances.scopes.project.authorized_collection_actions.targets = [];
await visit(urls.targets);
assert.strictEqual(
controller.messageDescription,
intl.t('descriptions.neither-list-nor-create', {
resource: intl.t('resources.target.title_plural'),
}),
);
});
});