Skip to content

Modular logistics and supply chain operating system (LSOS)

test(console): unit tests for auth + console action controllers

Ronald A. Richardson516efcd66d6d
diff --git a/console/tests/unit/controllers/auth/login-test.js b/console/tests/unit/controllers/auth/login-test.js
index dc362586..194279f6 100644
--- a/console/tests/unit/controllers/auth/login-test.js
+++ b/console/tests/unit/controllers/auth/login-test.js
@@ -1,12 +1,52 @@
import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
+import Service from '@ember/service';
module('Unit | Controller | auth/login', function (hooks) {
setupTest(hooks);
- // TODO: Replace this with your real tests.
- test('it exists', function (assert) {
- let controller = this.owner.lookup('controller:auth/login');
- assert.ok(controller);
+ test("reset('success') clears the form and loading flags", function (assert) {
+ const controller = this.owner.lookup('controller:auth/login');
+ controller.isLoading = true;
+ controller.isSlowConnection = true;
+ controller.identity = '[email protected]';
+ controller.password = 'secret';
+ controller.isValidating = true;
+
+ controller.reset('success');
+
+ assert.false(controller.isLoading);
+ assert.false(controller.isSlowConnection);
+ assert.strictEqual(controller.identity, null);
+ assert.strictEqual(controller.password, null);
+ assert.false(controller.isValidating);
+ });
+
+ test("reset('error') clears only the password and keeps the identity", function (assert) {
+ const controller = this.owner.lookup('controller:auth/login');
+ controller.identity = '[email protected]';
+ controller.password = 'secret';
+
+ controller.reset('error');
+
+ assert.strictEqual(controller.password, null);
+ assert.strictEqual(controller.identity, '[email protected]', 'identity is preserved on error');
+ });
+
+ test('login warns and stops when no identity is provided', async function (assert) {
+ class NotificationsStub extends Service {
+ warning() {
+ this.warned = true;
+ }
+ }
+ this.owner.register('service:notifications', NotificationsStub);
+
+ const controller = this.owner.lookup('controller:auth/login');
+ controller.identity = null;
+
+ await controller.login({ preventDefault() {} });
+
+ assert.true(this.owner.lookup('service:notifications').warned, 'warns about the missing identity');
+ assert.false(controller.isLoading, 'never enters the loading state');
});
});
diff --git a/console/tests/unit/controllers/auth/two-fa-test.js b/console/tests/unit/controllers/auth/two-fa-test.js
index 0bd93ec3..83ce5e08 100644
--- a/console/tests/unit/controllers/auth/two-fa-test.js
+++ b/console/tests/unit/controllers/auth/two-fa-test.js
@@ -1,12 +1,45 @@
import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
+import Service from '@ember/service';
module('Unit | Controller | auth/two-fa', function (hooks) {
setupTest(hooks);
- // TODO: Replace this with your real tests.
- test('it exists', function (assert) {
- let controller = this.owner.lookup('controller:auth/two-fa');
- assert.ok(controller);
+ test('handleCodeExpired flags expiry and stops the countdown', function (assert) {
+ const controller = this.owner.lookup('controller:auth/two-fa');
+ controller.isCodeExpired = false;
+ controller.countdownReady = true;
+
+ controller.handleCodeExpired();
+
+ assert.true(controller.isCodeExpired);
+ assert.false(controller.countdownReady);
+ });
+
+ test('getExpirationDateFromClientToken decodes the expiry from a base64 client token', function (assert) {
+ const controller = this.owner.lookup('controller:auth/two-fa');
+ const token = btoa('2024-01-01T00:00:00|session-payload');
+
+ const result = controller.getExpirationDateFromClientToken(token);
+
+ assert.true(result instanceof Date, 'returns a Date for a pipe-delimited token');
+ assert.strictEqual(controller.getExpirationDateFromClientToken(btoa('no-delimiter')), null, 'null when the token has no expiry segment');
+ });
+
+ test('verifyCode short-circuits with an error when the client token is missing', async function (assert) {
+ class NotificationsStub extends Service {
+ error() {
+ this.errored = true;
+ }
+ success() {}
+ }
+ this.owner.register('service:notifications', NotificationsStub);
+
+ const controller = this.owner.lookup('controller:auth/two-fa');
+ controller.clientToken = null;
+
+ await controller.verifyCode();
+
+ assert.true(this.owner.lookup('service:notifications').errored, 'reports an invalid-session error');
});
});
diff --git a/console/tests/unit/controllers/console-test.js b/console/tests/unit/controllers/console-test.js
index d3df3dfc..421aefb9 100644
--- a/console/tests/unit/controllers/console-test.js
+++ b/console/tests/unit/controllers/console-test.js
@@ -4,9 +4,28 @@ import { setupTest } from '@fleetbase/console/tests/helpers';
module('Unit | Controller | console', function (hooks) {
setupTest(hooks);
- // TODO: Replace this with your real tests.
- test('it exists', function (assert) {
- let controller = this.owner.lookup('controller:console');
- assert.ok(controller);
+ test('onAction dispatches to a matching method with its params', function (assert) {
+ const controller = this.owner.lookup('controller:console');
+ const calls = [];
+ controller.ping = (...args) => calls.push(args);
+
+ controller.onAction('ping', 1, 'two');
+
+ assert.deepEqual(calls, [[1, 'two']], 'the named method receives the params');
+ });
+
+ test('onAction ignores an unknown action name', function (assert) {
+ const controller = this.owner.lookup('controller:console');
+
+ controller.onAction('doesNotExist');
+
+ assert.ok(true, 'no error is thrown for a missing method');
+ });
+
+ test('currentRouteClass dasherizes the current route name', function (assert) {
+ const controller = this.owner.lookup('controller:console');
+ Object.defineProperty(controller.router, 'currentRouteName', { value: 'console.admin.index', configurable: true });
+
+ assert.strictEqual(controller.currentRouteClass, 'console-admin-index');
});
});
diff --git a/console/tests/unit/controllers/console/notifications-test.js b/console/tests/unit/controllers/console/notifications-test.js
index 311923ca..5fc381c2 100644
--- a/console/tests/unit/controllers/console/notifications-test.js
+++ b/console/tests/unit/controllers/console/notifications-test.js
@@ -4,9 +4,33 @@ import { setupTest } from '@fleetbase/console/tests/helpers';
module('Unit | Controller | console/notifications', function (hooks) {
setupTest(hooks);
- // TODO: Replace this with your real tests.
- test('it exists', function (assert) {
- let controller = this.owner.lookup('controller:console/notifications');
- assert.ok(controller);
+ hooks.beforeEach(function () {
+ // The controller's constructor subscribes to the universe event bus; the real
+ // singleton can't be swapped via owner.register, so neutralize .on on the instance.
+ this.owner.lookup('service:universe').on = () => {};
+ });
+
+ test('selectNotification toggles a notification in the selection', function (assert) {
+ const controller = this.owner.lookup('controller:console/notifications');
+ const a = { id: '1' };
+
+ controller.selectNotification(a);
+ assert.deepEqual(controller.selected, [a], 'adds when not selected');
+
+ controller.selectNotification(a);
+ assert.deepEqual(controller.selected, [], 'removes when already selected');
+ });
+
+ test('selectAll selects every model row then clears on a second call', function (assert) {
+ const controller = this.owner.lookup('controller:console/notifications');
+ const a = { id: '1' };
+ const b = { id: '2' };
+ controller.model = { length: 2, toArray: () => [a, b] };
+
+ controller.selectAll();
+ assert.deepEqual(controller.selected, [a, b], 'selects all rows');
+
+ controller.selectAll();
+ assert.deepEqual(controller.selected, [], 'clears when all are already selected');
});
});
diff --git a/console/tests/unit/controllers/invite/for-user-test.js b/console/tests/unit/controllers/invite/for-user-test.js
index 168d6a06..a6b2ca83 100644
--- a/console/tests/unit/controllers/invite/for-user-test.js
+++ b/console/tests/unit/controllers/invite/for-user-test.js
@@ -1,12 +1,43 @@
import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
+import { settled } from '@ember/test-helpers';
+import Service from '@ember/service';
module('Unit | Controller | invite/for-user', function (hooks) {
setupTest(hooks);
- // TODO: Replace this with your real tests.
- test('it exists', function (assert) {
- let controller = this.owner.lookup('controller:invite/for-user');
- assert.ok(controller);
+ test('acceptInvite authenticates the session and transitions to the console', async function (assert) {
+ class FetchStub extends Service {
+ post() {
+ return Promise.resolve({ token: 'invite-token', needs_password: false });
+ }
+ }
+ class SessionStub extends Service {
+ manuallyAuthenticate(token) {
+ this.token = token;
+ }
+ }
+ class NotificationsStub extends Service {
+ success() {}
+ serverError() {}
+ }
+ this.owner.register('service:fetch', FetchStub);
+ this.owner.register('service:session', SessionStub);
+ this.owner.register('service:notifications', NotificationsStub);
+
+ const controller = this.owner.lookup('controller:invite/for-user');
+ let transitioned;
+ controller.router.transitionTo = (route) => {
+ transitioned = route;
+ return Promise.resolve();
+ };
+ controller.code = 'invite-code';
+
+ controller.acceptInvite();
+ await settled();
+
+ assert.strictEqual(this.owner.lookup('service:session').token, 'invite-token', 'authenticated with the returned token');
+ assert.strictEqual(transitioned, 'console');
+ assert.notOk(controller.isLoading, 'loading flag is reset');
});
});