Skip to content

Modular logistics and supply chain operating system (LSOS)

test(console): cover the verification and organization settings controllers

Ronald A. Richardsonb56b72752436
diff --git a/console/tests/unit/controllers/auth/verification-test.js b/console/tests/unit/controllers/auth/verification-test.js
index 7fe365ca..175a88bb 100644
--- a/console/tests/unit/controllers/auth/verification-test.js
+++ b/console/tests/unit/controllers/auth/verification-test.js
@@ -99,3 +99,225 @@ module('Unit | Controller | auth/verification', function (hooks) {
assert.strictEqual(transitioned, 'auth.login');
});
});
+
+/**
+ * The controller's constructor schedules a 75-second "still waiting" timer. willDestroy
+ * cancels it at teardown, but nothing here may await settled() after the lookup or the run
+ * loop would sit on that timer until the test times out.
+ */
+module('Unit | Controller | auth/verification | resend and verify', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.posted = [];
+ this.postResponse = { status: 'ok', token: 'auth-token' };
+ this.postRejectsWith = null;
+ const context = this;
+
+ class FetchStub extends Service {
+ post(path, payload) {
+ context.posted.push({ path, payload });
+ return context.postRejectsWith ? Promise.reject(context.postRejectsWith) : Promise.resolve(context.postResponse);
+ }
+ }
+ class NotificationsStub extends Service {
+ errors = [];
+ successes = [];
+ infos = [];
+ serverErrors = [];
+ error(message) {
+ this.errors.push(message);
+ }
+ success(message) {
+ this.successes.push(message);
+ }
+ info(message) {
+ this.infos.push(message);
+ }
+ serverError(error) {
+ this.serverErrors.push(error);
+ }
+ }
+ class SessionStub extends Service {
+ authenticated = [];
+ manuallyAuthenticate(token) {
+ this.authenticated.push(token);
+ }
+ }
+ class IntlStub extends Service {
+ t() {
+ return 'Fleetbase';
+ }
+ }
+ class ModalsManagerStub extends Service {
+ shown = [];
+ show(name, options) {
+ this.shown.push({ name, options });
+ }
+ }
+
+ this.owner.register('service:fetch', FetchStub);
+ this.owner.register('service:notifications', NotificationsStub);
+ this.owner.register('service:session', SessionStub);
+ this.owner.register('service:intl', IntlStub);
+ this.owner.register('service:modals-manager', ModalsManagerStub);
+
+ this.build = () => {
+ const controller = this.owner.lookup('controller:auth/verification');
+ 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');
+ this.modal = (options = {}) => {
+ const calls = [];
+ return {
+ calls,
+ startLoading: () => calls.push('startLoading'),
+ stopLoading: () => calls.push('stopLoading'),
+ done: () => calls.push('done'),
+ getOption: (key) => options[key],
+ };
+ };
+ });
+
+ test('verifyCode authenticates and lands in the console when a token comes back', async function (assert) {
+ const controller = this.build();
+ controller.token = 'tok';
+ controller.code = '123456';
+ controller.email = '[email protected]';
+
+ await controller.verifyCode.perform();
+
+ assert.deepEqual(this.posted, [{ path: 'auth/verify-email', payload: { token: 'tok', code: '123456', email: '[email protected]', authenticate: true } }]);
+ assert.deepEqual(this.owner.lookup('service:session').authenticated, ['auth-token']);
+ assert.deepEqual(this.transitions, ['console']);
+ assert.deepEqual(this.notifications().successes, ['Email successfully verified!']);
+ assert.deepEqual(this.notifications().infos, ['Welcome to Fleetbase']);
+ });
+
+ test('verifyCode sends the user to login when no token comes back', async function (assert) {
+ this.postResponse = { status: 'ok' };
+ const controller = this.build();
+
+ await controller.verifyCode.perform();
+
+ assert.deepEqual(this.transitions, ['auth.login']);
+ assert.deepEqual(this.owner.lookup('service:session').authenticated, [], 'nothing to authenticate with');
+ });
+
+ test('verifyCode stays put on a non-ok status', async function (assert) {
+ this.postResponse = { status: 'invalid' };
+ const controller = this.build();
+
+ assert.strictEqual(await controller.verifyCode.perform(), undefined);
+ assert.deepEqual(this.transitions, []);
+ assert.deepEqual(this.notifications().successes, [], 'nothing is reported as verified');
+ });
+
+ test('verifyCode reports a failed request as a server error', async function (assert) {
+ const failure = new Error('verification service down');
+ this.postRejectsWith = failure;
+ const controller = this.build();
+
+ await controller.verifyCode.perform();
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ });
+
+ test('resendBySms opens the phone modal and sends the code with the session', async function (assert) {
+ const controller = this.build();
+ controller.hello = 'session-token';
+
+ controller.resendBySms();
+
+ const { name, options } = this.owner.lookup('service:modals-manager').shown[0];
+ assert.strictEqual(name, 'modals/verify-by-sms');
+ assert.strictEqual(options.title, 'Verify Account by Phone');
+ assert.strictEqual(options.phone, controller.currentUser.phone);
+
+ const modal = this.modal({ phone: '+15550001111' });
+ await options.confirm(modal);
+
+ assert.deepEqual(this.posted.at(-1), { path: 'onboard/send-verification-sms', payload: { phone: '+15550001111', session: 'session-token' } });
+ assert.deepEqual(modal.calls, ['startLoading', 'done']);
+ assert.deepEqual(this.notifications().successes, ['Verification code SMS sent!']);
+ });
+
+ test('confirming the sms modal without a number reports it', async function (assert) {
+ const controller = this.build();
+ controller.resendBySms();
+
+ await this.owner.lookup('service:modals-manager').shown[0].options.confirm(this.modal({ phone: '' }));
+
+ assert.deepEqual(this.notifications().errors, ['No phone number provided.']);
+ });
+
+ test('a failed sms send leaves the modal open', async function (assert) {
+ const failure = new Error('sms gateway down');
+ this.postRejectsWith = failure;
+ const controller = this.build();
+ controller.resendBySms();
+
+ const modal = this.modal({ phone: '+15550001111' });
+ await this.owner.lookup('service:modals-manager').shown[0].options.confirm(modal);
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ assert.deepEqual(modal.calls, ['startLoading', 'stopLoading'], 'the user can retry');
+ });
+
+ test('resendEmail opens the email modal and sends the code with the session', async function (assert) {
+ const controller = this.build();
+ controller.hello = 'session-token';
+
+ controller.resendEmail();
+
+ const { name, options } = this.owner.lookup('service:modals-manager').shown[0];
+ assert.strictEqual(name, 'modals/resend-verification-email');
+ assert.strictEqual(options.email, controller.currentUser.email);
+
+ const modal = this.modal({ email: '[email protected]' });
+ await options.confirm(modal);
+
+ assert.deepEqual(this.posted.at(-1), { path: 'onboard/send-verification-email', payload: { email: '[email protected]', session: 'session-token' } });
+ assert.deepEqual(modal.calls, ['startLoading', 'done']);
+ assert.deepEqual(this.notifications().successes, ['Verification code email sent!']);
+ });
+
+ test('confirming the email modal without an address reports it', async function (assert) {
+ const controller = this.build();
+ controller.resendEmail();
+
+ await this.owner.lookup('service:modals-manager').shown[0].options.confirm(this.modal({ email: '' }));
+
+ assert.deepEqual(this.notifications().errors, ['No email number provided.']);
+ });
+
+ test('a failed email send leaves the modal open', async function (assert) {
+ const failure = new Error('smtp unavailable');
+ this.postRejectsWith = failure;
+ const controller = this.build();
+ controller.resendEmail();
+
+ const modal = this.modal({ email: '[email protected]' });
+ await this.owner.lookup('service:modals-manager').shown[0].options.confirm(modal);
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ assert.deepEqual(modal.calls, ['startLoading', 'stopLoading']);
+ });
+
+ test('willDestroy cancels the still-waiting timer', function (assert) {
+ const controller = this.build();
+ assert.ok(controller.stillWaitingTimer, 'a timer was scheduled on construction');
+
+ controller.willDestroy();
+
+ assert.false(controller.stillWaiting, 'the timer never fired, so the flag is untouched');
+ });
+});
diff --git a/console/tests/unit/controllers/console/admin/organizations/details/settings-test.js b/console/tests/unit/controllers/console/admin/organizations/details/settings-test.js
new file mode 100644
index 00000000..ac718363
--- /dev/null
+++ b/console/tests/unit/controllers/console/admin/organizations/details/settings-test.js
@@ -0,0 +1,185 @@
+import { module, test } from 'qunit';
+import { setupTest } from '@fleetbase/console/tests/helpers';
+import Service from '@ember/service';
+
+/**
+ * Every action on this controller opens a modal and does its work in the confirm callback,
+ * so each test opens the modal, inspects what it was configured with, then drives confirm.
+ */
+module('Unit | Controller | console/admin/organizations/details/settings', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.patched = [];
+ this.patchRejectsWith = null;
+ this.saveRejectsWith = null;
+ const context = this;
+
+ class FetchStub extends Service {
+ patch(path, payload) {
+ context.patched.push({ path, payload });
+ return context.patchRejectsWith ? Promise.reject(context.patchRejectsWith) : Promise.resolve({ ok: true });
+ }
+ }
+ class NotificationsStub extends Service {
+ successes = [];
+ serverErrors = [];
+ success(message) {
+ this.successes.push(message);
+ }
+ serverError(error) {
+ this.serverErrors.push(error);
+ }
+ }
+ class ModalsManagerStub extends Service {
+ shown = [];
+ confirmed = [];
+ show(name, options) {
+ this.shown.push({ name, options });
+ }
+ confirm(options) {
+ this.confirmed.push(options);
+ }
+ }
+
+ this.owner.register('service:fetch', FetchStub);
+ this.owner.register('service:notifications', NotificationsStub);
+ this.owner.register('service:modals-manager', ModalsManagerStub);
+
+ this.saved = 0;
+ this.build = () => {
+ const controller = this.owner.lookup('controller:console/admin/organizations/details/settings');
+ controller.model = {
+ uuid: 'co_1',
+ name: 'Fleetbase',
+ save: () => {
+ this.saved++;
+ return this.saveRejectsWith ? Promise.reject(this.saveRejectsWith) : Promise.resolve();
+ },
+ };
+
+ this.refreshes = 0;
+ Object.defineProperty(controller.router, 'refresh', {
+ configurable: true,
+ value: () => {
+ this.refreshes++;
+ return 'refreshed';
+ },
+ });
+
+ return controller;
+ };
+ this.modals = () => this.owner.lookup('service:modals-manager');
+ this.notifications = () => this.owner.lookup('service:notifications');
+ this.modal = () => {
+ const calls = [];
+ return { calls, startLoading: () => calls.push('startLoading') };
+ };
+ });
+
+ test('editOrganization opens the edit modal against the loaded organization', function (assert) {
+ const controller = this.build();
+
+ controller.editOrganization();
+
+ const { name, options } = this.modals().shown[0];
+ assert.strictEqual(name, 'modals/edit-organization');
+ assert.strictEqual(options.title, 'Edit Organization');
+ assert.strictEqual(options.acceptButtonText, 'Save Changes');
+ assert.strictEqual(options.organization, controller.model, 'the modal edits the loaded record');
+ });
+
+ test('confirming the edit saves the organization and reloads', async function (assert) {
+ const controller = this.build();
+ controller.editOrganization();
+ const modal = this.modal();
+
+ await this.modals().shown[0].options.confirm(modal);
+
+ assert.strictEqual(this.saved, 1);
+ assert.deepEqual(modal.calls, ['startLoading']);
+ assert.deepEqual(this.notifications().successes, ['Organization updated.']);
+ assert.strictEqual(this.refreshes, 1);
+ });
+
+ test('a failed edit is reported and nothing reloads', async function (assert) {
+ const failure = new Error('save rejected');
+ this.saveRejectsWith = failure;
+ const controller = this.build();
+ controller.editOrganization();
+
+ await this.modals().shown[0].options.confirm(this.modal());
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ assert.strictEqual(this.refreshes, 0);
+ });
+
+ test('setStatus asks for confirmation naming the new status', function (assert) {
+ this.build().setStatus('suspended');
+
+ const options = this.modals().confirmed[0];
+ assert.strictEqual(options.title, 'Update Organization Status');
+ assert.strictEqual(options.body, 'Set this organization status to suspended?');
+ assert.strictEqual(options.acceptButtonText, 'Update Status');
+ });
+
+ test('confirming a status change patches the organization and reloads', async function (assert) {
+ this.build().setStatus('active');
+
+ await this.modals().confirmed[0].confirm(this.modal());
+
+ assert.deepEqual(this.patched, [{ path: 'companies/co_1/status', payload: { status: 'active' } }]);
+ assert.deepEqual(this.notifications().successes, ['Organization status updated.']);
+ assert.strictEqual(this.refreshes, 1);
+ });
+
+ test('a failed status change is reported and nothing reloads', async function (assert) {
+ const failure = new Error('status patch rejected');
+ this.patchRejectsWith = failure;
+ this.build().setStatus('active');
+
+ await this.modals().confirmed[0].confirm(this.modal());
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ assert.strictEqual(this.refreshes, 0);
+ });
+
+ test('setOnboarding words its prompt for completing onboarding', function (assert) {
+ this.build().setOnboarding(true);
+
+ const options = this.modals().confirmed[0];
+ assert.strictEqual(options.title, 'Mark Onboarding Complete');
+ assert.strictEqual(options.body, 'Mark this organization onboarding as complete?');
+ assert.strictEqual(options.acceptButtonText, 'Mark Complete');
+ });
+
+ test('setOnboarding words its prompt for reopening onboarding', function (assert) {
+ this.build().setOnboarding(false);
+
+ const options = this.modals().confirmed[0];
+ assert.strictEqual(options.title, 'Mark Onboarding Incomplete');
+ assert.strictEqual(options.body, 'Mark this organization onboarding as incomplete?');
+ assert.strictEqual(options.acceptButtonText, 'Mark Incomplete');
+ });
+
+ test('confirming an onboarding change patches the organization and reloads', async function (assert) {
+ this.build().setOnboarding(true);
+
+ await this.modals().confirmed[0].confirm(this.modal());
+
+ assert.deepEqual(this.patched, [{ path: 'companies/co_1/onboarding', payload: { completed: true } }]);
+ assert.deepEqual(this.notifications().successes, ['Organization onboarding updated.']);
+ assert.strictEqual(this.refreshes, 1);
+ });
+
+ test('a failed onboarding change is reported and nothing reloads', async function (assert) {
+ const failure = new Error('onboarding patch rejected');
+ this.patchRejectsWith = failure;
+ this.build().setOnboarding(false);
+
+ await this.modals().confirmed[0].confirm(this.modal());
+
+ assert.deepEqual(this.notifications().serverErrors, [failure]);
+ assert.strictEqual(this.refreshes, 0);
+ });
+});