From a3bb788716fb0b78fc02b4b444538a0aaaa4b094 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Sun, 2 Jan 2022 22:02:56 -0500 Subject: [PATCH] refactor: rewrite OIDC polling for ember pollster --- .../scopes/scope/authenticate/method/oidc.js | 41 +++---------------- ui/admin/config/environment.js | 3 -- .../tests/acceptance/authentication-test.js | 5 ++- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/ui/admin/app/routes/scopes/scope/authenticate/method/oidc.js b/ui/admin/app/routes/scopes/scope/authenticate/method/oidc.js index db24f3bbd..b8fa4519c 100644 --- a/ui/admin/app/routes/scopes/scope/authenticate/method/oidc.js +++ b/ui/admin/app/routes/scopes/scope/authenticate/method/oidc.js @@ -1,42 +1,22 @@ import Route from '@ember/routing/route'; import { getOwner } from '@ember/application'; import { inject as service } from '@ember/service'; -import { task, timeout } from 'ember-concurrency'; import { action } from '@ember/object'; +import runEvery from 'ember-pollster/decorators/route/run-every'; import { notifyError } from 'core/decorators/notify'; import config from '../../../../../config/environment'; const POLL_TIMEOUT_SECONDS = config.oidcPollingTimeoutSeconds; export default class ScopesScopeAuthenticateMethodOidcRoute extends Route { - // =services @service session; @service windowManager; - // =attributes - - /** - * A simple Ember Concurrency-based polling task that refreshes the route - * every POLL_TIMEOUT_SECONDS seconds. This is necessary to display changes - * to session `status` that may occur. - * - * NOTE: tasks are sort of attributes and sort of methods, but they are not - * language-level constructs. Thus we annotate this task as if it - * is an attribute. - * @type {Task} - */ - @task(function * () { - while(!this.session.isAuthenticated) { - yield timeout(POLL_TIMEOUT_SECONDS * 1000); - yield this.refresh(); - } - /* eslint-disable-next-line prettier/prettier */ - }).drop() poller; - // =methods + beforeModel; model() { const scope = this.modelFor('scopes.scope'); const authMethod = this.modelFor('scopes.scope.authenticate.method'); @@ -47,18 +27,9 @@ export default class ScopesScopeAuthenticateMethodOidcRoute extends Route { return oidc.attemptFetchToken({ scope, authMethod }); } - /** - * If authenticated, redirects to index for further processing. - */ - redirect() { - if (this.session.isAuthenticated) this.replaceWith('index'); - } - - /** - * When this route is activated (entered), begin polling for changes. - */ - activate() { - this.poller.perform(); + @runEvery(POLL_TIMEOUT_SECONDS * 1000) + poller() { + this.refresh(); } /** @@ -66,7 +37,6 @@ export default class ScopesScopeAuthenticateMethodOidcRoute extends Route { * any windows opened via the window manager service. */ deactivate() { - this.poller.cancelAll(); this.windowManager.closeAll(); } @@ -81,5 +51,4 @@ export default class ScopesScopeAuthenticateMethodOidcRoute extends Route { // rethrow the error to activate the notifyError decorator throw e; } - } diff --git a/ui/admin/config/environment.js b/ui/admin/config/environment.js index 4792a3c17..dc951f583 100644 --- a/ui/admin/config/environment.js +++ b/ui/admin/config/environment.js @@ -151,9 +151,6 @@ module.exports = function (environment) { // Notification timeout should be 0 for fast tests ENV.notifyTimeout = 0; - // Polling timeouts should be short - ENV.sessionPollingTimeoutSeconds = 0.25; - ENV.oidcPollingTimeoutSeconds = 0; ENV.enableConfirmService = false; // Enable tests for development features diff --git a/ui/admin/tests/acceptance/authentication-test.js b/ui/admin/tests/acceptance/authentication-test.js index 3c1cedd48..b4ed094ea 100644 --- a/ui/admin/tests/acceptance/authentication-test.js +++ b/ui/admin/tests/acceptance/authentication-test.js @@ -12,7 +12,7 @@ import { import { setupApplicationTest } from 'ember-qunit'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import { Response } from 'miragejs'; -//import { run, later } from '@ember/runloop'; +import { runAllJobs } from 'ember-pollster/test-support'; import a11yAudit from 'ember-a11y-testing/test-support/audit'; import { currentSession, @@ -383,6 +383,9 @@ module('Acceptance | authentication', function (hooks) { ); await visit(authMethodOIDCAuthenticateURL); await click('form [type="submit"]'); + // explicitly poll multiple times to trigger mock OIDC auth + await runAllJobs(); + await runAllJobs(); }); // TODO: test OIDC retry and cancel