Skip to content

Modular logistics and supply chain operating system (LSOS)

test(console): cover the notification, admin 2FA and email-change controllers

Ronald A. Richardsonec463134dc2b
diff --git a/console/tests/unit/controllers/auth/confirm-email-change-test.js b/console/tests/unit/controllers/auth/confirm-email-change-test.js
new file mode 100644
index 00000000..ccf1d031
--- /dev/null
+++ b/console/tests/unit/controllers/auth/confirm-email-change-test.js
@@ -0,0 +1,105 @@
+import { module, test } from 'qunit';
+import { setupTest } from '@fleetbase/console/tests/helpers';
+import Service from '@ember/service';
+
+module('Unit | Controller | auth/confirm-email-change', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.posted = [];
+ this.postRejectsWith = null;
+ this.isAuthenticated = false;
+ const context = this;
+
+ class FetchStub extends Service {
+ post(path, payload) {
+ context.posted.push({ path, payload });
+ return context.postRejectsWith ? Promise.reject(context.postRejectsWith) : Promise.resolve({ ok: true });
+ }
+ }
+ class NotificationsStub extends Service {
+ successes = [];
+ serverErrors = [];
+ success(message) {
+ this.successes.push(message);
+ }
+ serverError(error) {
+ this.serverErrors.push(error);
+ return 'reported';
+ }
+ }
+ class IntlStub extends Service {
+ t(key) {
+ return key;
+ }
+ }
+ class SessionStub extends Service {
+ get isAuthenticated() {
+ return context.isAuthenticated;
+ }
+ }
+
+ this.owner.register('service:fetch', FetchStub);
+ this.owner.register('service:notifications', NotificationsStub);
+ this.owner.register('service:intl', IntlStub);
+ this.owner.register('service:session', SessionStub);
+
+ this.build = () => {
+ const controller = this.owner.lookup('controller:auth/confirm-email-change');
+ controller.model = { id: 'link_1' };
+ controller.code = '123456';
+
+ this.transitions = [];
+ Object.defineProperty(controller.router, 'transitionTo', {
+ configurable: true,
+ value: (routeName) => {
+ this.transitions.push(routeName);
+ return routeName;
+ },
+ });
+
+ return controller;
+ };
+ this.notifications = () => this.owner.lookup('service:notifications');
+ });
+
+ test('confirming posts the link and code, then sends a signed-in user to the console', async function (assert) {
+ this.isAuthenticated = true;
+ const controller = this.build();
+
+ await controller.confirmEmailChange.perform();
+
+ assert.deepEqual(this.posted, [{ path: 'auth/confirm-email-change', payload: { link: 'link_1', code: '123456' } }]);
+ assert.deepEqual(this.notifications().successes, ['auth.confirm-email-change.success-message']);
+ assert.deepEqual(this.transitions, ['console']);
+ });
+
+ test('a signed-out user is sent to login instead', async function (assert) {
+ const controller = this.build();
+
+ await controller.confirmEmailChange.perform();
+
+ assert.deepEqual(this.transitions, ['auth.login']);
+ });
+
+ test('it prevents the form from submitting itself', async function (assert) {
+ const controller = this.build();
+ let prevented = false;
+
+ await controller.confirmEmailChange.perform({ preventDefault: () => (prevented = true) });
+
+ assert.true(prevented);
+ });
+
+ test('a rejected confirmation is reported and nothing navigates', async function (assert) {
+ const failure = new Error('link already used');
+ this.postRejectsWith = failure;
+ const controller = this.build();
+
+ await controller.confirmEmailChange.perform();
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ assert.deepEqual(this.notifications().successes, [], 'nothing is reported as confirmed');
+ assert.deepEqual(this.transitions, [], 'the user stays on the page');
+ });
+});
diff --git a/console/tests/unit/controllers/console/admin/two-fa-settings-test.js b/console/tests/unit/controllers/console/admin/two-fa-settings-test.js
index 75db0213..2021bc8d 100644
--- a/console/tests/unit/controllers/console/admin/two-fa-settings-test.js
+++ b/console/tests/unit/controllers/console/admin/two-fa-settings-test.js
@@ -1,33 +1,111 @@
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 | console/admin/two-fa-settings', function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
- // Constructor auto-loads the system config; stub fetch so lookup settles deterministically.
+ this.requests = [];
+ this.getResponse = { enabled: true, method: 'email', enforced: false };
+ this.getRejectsWith = null;
+ this.postRejectsWith = null;
+ const context = this;
+
class FetchStub extends Service {
- get() {
- return Promise.resolve({});
+ get(path) {
+ context.requests.push({ method: 'get', path });
+ return context.getRejectsWith ? Promise.reject(context.getRejectsWith) : Promise.resolve(context.getResponse);
+ }
+ post(path, payload) {
+ context.requests.push({ method: 'post', path, payload });
+ return context.postRejectsWith ? Promise.reject(context.postRejectsWith) : Promise.resolve({ ok: true });
+ }
+ }
+ class NotificationsStub extends Service {
+ successes = [];
+ serverErrors = [];
+ success(message) {
+ this.successes.push(message);
+ }
+ serverError(error) {
+ this.serverErrors.push(error);
}
}
+
this.owner.register('service:fetch', FetchStub);
+ this.owner.register('service:notifications', NotificationsStub);
+
+ this.build = () => this.owner.lookup('controller:console/admin/two-fa-settings');
+ this.notifications = () => this.owner.lookup('service:notifications');
+ });
+
+ test('it loads the system 2FA config on construction', async function (assert) {
+ const controller = this.build();
+ await controller.loadSystemTwoFaConfig.last;
+
+ assert.deepEqual(this.requests, [{ method: 'get', path: 'two-fa/config' }]);
+ assert.deepEqual(controller.twoFaSettings, this.getResponse);
+ assert.ok(controller.methods.length > 0, 'the available methods are offered');
+ });
+
+ test('a config that comes back empty leaves the settings untouched', async function (assert) {
+ this.getResponse = null;
+ const controller = this.build();
+ await controller.loadSystemTwoFaConfig.last;
+
+ assert.deepEqual(controller.twoFaSettings, {}, 'nothing is applied');
+ });
+
+ test('a failed config load is reported', async function (assert) {
+ const failure = new Error('config unavailable');
+ this.getRejectsWith = failure;
+ const controller = this.build();
+ await controller.loadSystemTwoFaConfig.last;
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ assert.deepEqual(controller.twoFaSettings, {});
});
- test('the toggle actions immutably patch twoFaSettings', async function (assert) {
- const controller = this.owner.lookup('controller:console/admin/two-fa-settings');
- await settled();
+ test('the toggles and method selection each patch one field of the settings', async function (assert) {
+ const controller = this.build();
+ await controller.loadSystemTwoFaConfig.last;
- controller.onTwoFaToggled(true);
- assert.true(controller.twoFaSettings.enabled);
+ controller.onTwoFaToggled(false);
+ assert.false(controller.twoFaSettings.enabled);
+ assert.strictEqual(controller.twoFaSettings.method, 'email', 'the other fields survive');
- controller.onTwoFaMethodSelected('authenticator');
- assert.strictEqual(controller.twoFaSettings.method, 'authenticator');
- assert.true(controller.twoFaSettings.enabled, 'previous keys are preserved');
+ controller.onTwoFaMethodSelected('sms');
+ assert.strictEqual(controller.twoFaSettings.method, 'sms');
+ assert.false(controller.twoFaSettings.enabled);
controller.onTwoFaEnforceToggled(true);
assert.true(controller.twoFaSettings.enforced);
+ assert.strictEqual(controller.twoFaSettings.method, 'sms');
+ });
+
+ test('saveSettings posts the current settings and reports success', async function (assert) {
+ const controller = this.build();
+ await controller.loadSystemTwoFaConfig.last;
+
+ controller.saveSettings();
+ await controller.saveTwoFactorSettingsForAdmin.last;
+
+ assert.deepEqual(this.requests.at(-1), { method: 'post', path: 'two-fa/config', payload: { twoFaSettings: controller.twoFaSettings } });
+ assert.deepEqual(this.notifications().successes, ['2FA Settings saved for admin successfully.']);
+ assert.false(controller.isLoading, 'the busy flag is cleared either way');
+ });
+
+ test('a failed save is reported and still clears the busy flag', async function (assert) {
+ const failure = new Error('save rejected');
+ this.postRejectsWith = failure;
+ const controller = this.build();
+ await controller.loadSystemTwoFaConfig.last;
+
+ controller.saveSettings();
+ await controller.saveTwoFactorSettingsForAdmin.last;
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ assert.false(controller.isLoading);
});
});
diff --git a/console/tests/unit/controllers/console/settings/notifications-test.js b/console/tests/unit/controllers/console/settings/notifications-test.js
index a271cdb2..cfb9ceac 100644
--- a/console/tests/unit/controllers/console/settings/notifications-test.js
+++ b/console/tests/unit/controllers/console/settings/notifications-test.js
@@ -42,3 +42,161 @@ module('Unit | Controller | console/settings/notifications', function (hooks) {
assert.deepEqual(controller.notificationSettings, { a: 1, b: 2 });
});
});
+
+module('Unit | Controller | console/settings/notifications | persistence', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.requests = [];
+ this.getResponse = { notificationSettings: { 'order:created': { via: [] } } };
+ this.getRejectsWith = null;
+ this.postRejectsWith = null;
+ this.saveRejectsWith = null;
+ const context = this;
+
+ class FetchStub extends Service {
+ get(path) {
+ context.requests.push({ method: 'get', path });
+ return context.getRejectsWith ? Promise.reject(context.getRejectsWith) : Promise.resolve(context.getResponse);
+ }
+ post(path, payload) {
+ context.requests.push({ method: 'post', path, payload });
+ return context.postRejectsWith ? Promise.reject(context.postRejectsWith) : Promise.resolve({ ok: true });
+ }
+ }
+ class NotificationsStub extends Service {
+ successes = [];
+ serverErrors = [];
+ success(message) {
+ this.successes.push(message);
+ }
+ serverError(error) {
+ this.serverErrors.push(error);
+ }
+ }
+ this.owner.register('service:fetch', FetchStub);
+ this.owner.register('service:notifications', NotificationsStub);
+
+ this.saved = 0;
+ this.withCompany = (options) => {
+ const company = {
+ options,
+ set(key, value) {
+ this[key] = value;
+ },
+ save: () => {
+ context.saved++;
+ return context.saveRejectsWith ? Promise.reject(context.saveRejectsWith) : Promise.resolve();
+ },
+ };
+ return company;
+ };
+ this.build = () => this.owner.lookup('controller:console/settings/notifications');
+ this.notifications = () => this.owner.lookup('service:notifications');
+ });
+
+ test('it loads the saved notification settings on construction', async function (assert) {
+ const controller = this.build();
+ await controller.getSettings.last;
+
+ assert.deepEqual(this.requests, [{ method: 'get', path: 'notifications/get-settings' }]);
+ assert.deepEqual(controller.notificationSettings, this.getResponse.notificationSettings);
+ assert.deepEqual(controller.notificationTransportMethods, ['email', 'sms']);
+ });
+
+ test('a failed load is reported', async function (assert) {
+ const failure = new Error('settings unavailable');
+ this.getRejectsWith = failure;
+ const controller = this.build();
+ await controller.getSettings.last;
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ });
+
+ test('toggling the alphanumeric sender id keeps the other company options', async function (assert) {
+ const controller = this.build();
+ await controller.getSettings.last;
+ controller.company = this.withCompany({ existing: 'kept' });
+
+ controller.toggleAlphaNumericSenderId(true);
+
+ assert.deepEqual(controller.company.options, { existing: 'kept', alpha_numeric_sender_id_enabled: true });
+ });
+
+ test('setting the alphanumeric sender id keeps the other company options', async function (assert) {
+ const controller = this.build();
+ await controller.getSettings.last;
+ controller.company = this.withCompany({ existing: 'kept' });
+
+ controller.setAlphaNumericSenderId({ target: { value: 'FLEETBASE' } });
+
+ assert.deepEqual(controller.company.options, { existing: 'kept', alpha_numeric_sender_id: 'FLEETBASE' });
+ });
+
+ test('a company with no options yet starts from an empty set', async function (assert) {
+ const controller = this.build();
+ await controller.getSettings.last;
+ controller.company = this.withCompany(undefined);
+
+ controller.toggleAlphaNumericSenderId(false);
+
+ assert.deepEqual(controller.company.options, { alpha_numeric_sender_id_enabled: false });
+ });
+
+ test('saveSettings posts the settings, saves the company and reports success', async function (assert) {
+ const controller = this.build();
+ await controller.getSettings.last;
+ controller.company = this.withCompany({});
+
+ await controller.saveSettings.perform();
+
+ assert.deepEqual(this.requests.at(-1), { method: 'post', path: 'notifications/save-settings', payload: { notificationSettings: controller.notificationSettings } });
+ assert.strictEqual(this.saved, 1, 'the company options are persisted too');
+ assert.deepEqual(this.notifications().successes, ['Notification settings successfully saved.']);
+ });
+
+ test('saveSettings sends an empty object when nothing has been configured', async function (assert) {
+ const controller = this.build();
+ await controller.getSettings.last;
+ controller.notificationSettings = null;
+
+ await controller.saveSettings.perform();
+
+ assert.deepEqual(this.requests.at(-1).payload, { notificationSettings: {} });
+ });
+
+ test('a failed save is reported and success is not', async function (assert) {
+ const failure = new Error('save rejected');
+ this.postRejectsWith = failure;
+ const controller = this.build();
+ await controller.getSettings.last;
+
+ await controller.saveSettings.perform();
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ assert.deepEqual(this.notifications().successes, []);
+ });
+
+ test('saving company options is skipped when there is no company loaded', async function (assert) {
+ const controller = this.build();
+ await controller.getSettings.last;
+ controller.company = null;
+
+ await controller.saveCompanyOptions.perform();
+
+ assert.strictEqual(this.saved, 0, 'nothing is persisted');
+ assert.deepEqual(this.notifications().serverErrors, []);
+ });
+
+ test('a failed company save is reported on its own', async function (assert) {
+ const failure = new Error('company save rejected');
+ this.saveRejectsWith = failure;
+ const controller = this.build();
+ await controller.getSettings.last;
+ controller.company = this.withCompany({});
+
+ await controller.saveCompanyOptions.perform();
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ });
+});