Skip to content

Commit

Permalink
test: πŸ’ add missing controller unit tests in admin-ui (#2553)
Browse files Browse the repository at this point in the history
* test: πŸ’ add missing controller unit tests in admin-ui

βœ… Closes: https://hashicorp.atlassian.net/browse/ICU-14740
  • Loading branch information
lisbet-alvarez authored Nov 12, 2024
1 parent 7c18141 commit 57568f9
Show file tree
Hide file tree
Showing 77 changed files with 946 additions and 81 deletions.
2 changes: 0 additions & 2 deletions ui/admin/app/controllers/scopes/scope/auth-methods/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeAuthMethodsNewController extends Controller {
@controller('scopes/scope/auth-methods/index') authMethods;

// =services

// =attributes

queryParams = ['type'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { notifySuccess, notifyError } from 'core/decorators/notify';

export default class ScopesScopeStorageBucketsIndexController extends Controller {
// =services

@service router;

// =actions
Expand Down Expand Up @@ -63,7 +64,7 @@ export default class ScopesScopeStorageBucketsIndexController extends Controller
}

/**
* Updates credentil type
* Updates credential type
* @param {object} storageBucket
* @param {string} credentialType
*/
Expand All @@ -74,7 +75,7 @@ export default class ScopesScopeStorageBucketsIndexController extends Controller

/**
* Changes the plugin type.
* @param {*} pluginType
* @param {string} pluginType
*/
@action
async changePluginType(pluginType) {
Expand Down
76 changes: 76 additions & 0 deletions ui/admin/tests/unit/controllers/account/change-password-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { authenticateSession } from 'ember-simple-auth/test-support';
import { TYPE_AUTH_METHOD_PASSWORD } from 'api/models/auth-method';

module('Unit | Controller | account/change-password', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

let controller;
let store;

const instances = {
scopes: {
global: null,
},
authMethod: null,
account: null,
};

const urls = {
globalScope: null,
};

hooks.beforeEach(async function () {
authenticateSession({});
controller = this.owner.lookup('controller:account/change-password');
store = this.owner.lookup('service:store');

instances.scopes.global = this.server.create('scope', {
id: 'global',
type: 'global',
});
instances.authMethod = this.server.create('auth-method', {
scope: instances.scopes.global,
type: TYPE_AUTH_METHOD_PASSWORD,
});
instances.account = this.server.create('account', {
scope: instances.scopes.global,
authMethod: instances.authMethod,
});

urls.globalScope = `/scopes/global`;
});

test('it exists', function (assert) {
assert.ok(controller);
});

test('changePassword action makes the correct API call with correct values', async function (assert) {
assert.expect(2);
const password = 'current password';
const newPassword = 'new password';
this.server.post(
'/accounts/:idMethod',
(_, { params: { idMethod }, requestBody }) => {
const attrs = JSON.parse(requestBody);
assert.strictEqual(attrs.current_password, password);
assert.strictEqual(attrs.new_password, newPassword);
const id = idMethod.split(':')[0];
return { id };
},
);
await visit(urls.globalScope);
const account = await store.findRecord('account', instances.account.id);

await controller.changePassword(account, password, newPassword);
});
});
47 changes: 46 additions & 1 deletion ui/admin/tests/unit/controllers/application-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,57 @@

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { authenticateSession } from 'ember-simple-auth/test-support';

module('Unit | Controller | application', function (hooks) {
setupTest(hooks);

let controller;
let featureEdition;
let featuresService;
let session;

hooks.beforeEach(async function () {
authenticateSession({});
controller = this.owner.lookup('controller:application');
featureEdition = this.owner.lookup('service:featureEdition');
featuresService = this.owner.lookup('service:features');
session = this.owner.lookup('service:session');
});

test('it exists', function (assert) {
let controller = this.owner.lookup('controller:application');
assert.ok(controller);
});

test('invalidateSession action de-authenticates a user', async function (assert) {
assert.true(session.isAuthenticated);

await controller.invalidateSession();

assert.false(session.isAuthenticated);
});

test('toggleTheme action sets theme to specified value', function (assert) {
assert.notOk(session.data.theme);

controller.toggleTheme('light');

assert.strictEqual(session.data.theme, 'light');
});

test('toggleEdition action sets edition to specified value', function (assert) {
assert.notOk(featureEdition.edition);

controller.toggleEdition('oss');

assert.strictEqual(featureEdition.edition, 'oss');
});

test('toggleFeature action toggles specified feature', function (assert) {
assert.false(featuresService.isEnabled('ssh-target'));

controller.toggleFeature('ssh-target');

assert.true(featuresService.isEnabled('ssh-target'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { module, test } from 'qunit';
import { setupTest } from 'admin/tests/helpers';
import { setupTest } from 'ember-qunit';

module(
'Unit | Controller | scopes/scope/aliases/alias/index',
Expand All @@ -16,6 +16,7 @@ module(
'controller:scopes/scope/aliases/alias/index',
);
assert.ok(controller);
assert.ok(controller.aliases);
});
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
*/

import { module, test } from 'qunit';
import { setupTest } from 'admin/tests/helpers';
import { setupTest } from 'ember-qunit';

module('Unit | Controller | scopes/scope/aliases/new', function (hooks) {
setupTest(hooks);

test('it exists', function (assert) {
let controller = this.owner.lookup('controller:scopes/scope/aliases/new');
assert.ok(controller);
assert.ok(controller.aliases);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module(
'controller:scopes/scope/auth-methods/auth-method/accounts/account/index',
);
assert.ok(controller);
assert.ok(controller.accounts);
});
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,77 @@

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { authenticateSession } from 'ember-simple-auth/test-support';
import { TYPE_AUTH_METHOD_PASSWORD } from 'api/models/auth-method';

module(
'Unit | Controller | scopes/scope/auth-methods/auth-method/accounts/account/set-password',
function (hooks) {
setupTest(hooks);
setupMirage(hooks);

test('it exists', function (assert) {
let controller = this.owner.lookup(
let controller;
let store;

const instances = {
scopes: {
global: null,
},
authMethod: null,
account: null,
};

const urls = {
globalScope: null,
accounts: null,
};

hooks.beforeEach(async function () {
authenticateSession({});
controller = this.owner.lookup(
'controller:scopes/scope/auth-methods/auth-method/accounts/account/set-password',
);
store = this.owner.lookup('service:store');

instances.scopes.global = this.server.create('scope', {
id: 'global',
type: 'global',
});
instances.authMethod = this.server.create('auth-method', {
scope: instances.scopes.global,
type: TYPE_AUTH_METHOD_PASSWORD,
});
instances.account = this.server.create('account', {
scope: instances.scopes.global,
authMethod: instances.authMethod,
});

urls.globalScope = `/scopes/global`;
});

test('it exists', function (assert) {
assert.ok(controller);
assert.ok(controller.accounts);
});

test('setPassword action makes the correct API call with correct values', async function (assert) {
assert.expect(1);
const password = 'current password';
this.server.post(
'/accounts/:idMethod',
(_, { params: { idMethod }, requestBody }) => {
const attrs = JSON.parse(requestBody);
assert.strictEqual(attrs.password, password);
const id = idMethod.split(':')[0];
return { id };
},
);
await visit(urls.globalScope);
const account = await store.findRecord('account', instances.account.id);

await controller.setPassword(account, password);
});
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,94 @@

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { setupIntl } from 'ember-intl/test-support';
import { authenticateSession } from 'ember-simple-auth/test-support';
import { TYPE_AUTH_METHOD_PASSWORD } from 'api/models/auth-method';

module(
'Unit | Controller | scopes/scope/auth-methods/auth-method/accounts/index',
function (hooks) {
setupTest(hooks);
setupMirage(hooks);
setupIntl(hooks, 'en-us');

test('it exists', function (assert) {
let controller = this.owner.lookup(
let controller;
let store;
let getAccountCount;

const instances = {
scopes: {
global: null,
},
authMethod: null,
account: null,
};

const urls = {
accounts: null,
};

hooks.beforeEach(async function () {
authenticateSession({});
controller = this.owner.lookup(
'controller:scopes/scope/auth-methods/auth-method/accounts/index',
);
store = this.owner.lookup('service:store');

instances.scopes.global = this.server.create('scope', {
id: 'global',
type: 'global',
});
instances.authMethod = this.server.create('auth-method', {
scope: instances.scopes.global,
type: TYPE_AUTH_METHOD_PASSWORD,
});
instances.account = this.server.create('account', {
scope: instances.scopes.global,
authMethod: instances.authMethod,
});

getAccountCount = () => this.server.schema.accounts.all().models.length;

urls.accounts = `/scopes/global/auth-methods/${instances.authMethod.id}/accounts`;
});

test('it exists', function (assert) {
assert.ok(controller);
assert.ok(controller.authMethods);
});

test('cancel action rolls-back changes on the specified model', async function (assert) {
await visit(urls.accounts);
const account = await store.findRecord('account', instances.account.id);
account.name = 'test';

assert.strictEqual(account.name, 'test');

await controller.cancel(account);

assert.notEqual(account.name, 'test');
});

test('save action saves changes on the specified model', async function (assert) {
await visit(urls.accounts);
const account = await store.findRecord('account', instances.account.id);
account.name = 'test';

await controller.save(account);

assert.strictEqual(account.name, 'test');
});

test('delete action destroys specified model', async function (assert) {
const account = await store.findRecord('account', instances.account.id);
const accountCount = getAccountCount();

await controller.delete(account);

assert.strictEqual(getAccountCount(), accountCount - 1);
});
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module(
'controller:scopes/scope/auth-methods/auth-method/accounts/new',
);
assert.ok(controller);
assert.ok(controller.accounts);
});
},
);
Loading

0 comments on commit 57568f9

Please sign in to comment.