diff --git a/ui/core/app/routes/scopes/scope/authenticate.js b/ui/core/app/routes/scopes/scope/authenticate.js index 3ae8edbcf..42d238ffd 100644 --- a/ui/core/app/routes/scopes/scope/authenticate.js +++ b/ui/core/app/routes/scopes/scope/authenticate.js @@ -1,8 +1,19 @@ import Route from '@ember/routing/route'; +import { inject as service } from '@ember/service'; import { hash } from 'rsvp'; export default class ScopesScopeAuthenticateRoute extends Route { + // =services + + @service session; + + // =attributes + + routeIfAlreadyAuthenticated = 'scopes.scope.projects'; + + // =methods + /** * Returns all auth methods for the current scope, along with the current * scope and all scopes (for org navigation). @@ -23,4 +34,10 @@ export default class ScopesScopeAuthenticateRoute extends Route { }); } + redirect() { + if (this.session.isAuthenticated) { + this.transitionTo(this.routeIfAlreadyAuthenticated); + } + } + } diff --git a/ui/core/app/routes/scopes/scope/authenticate/index.js b/ui/core/app/routes/scopes/scope/authenticate/index.js new file mode 100644 index 000000000..5ee523b46 --- /dev/null +++ b/ui/core/app/routes/scopes/scope/authenticate/index.js @@ -0,0 +1,20 @@ +import Route from '@ember/routing/route'; + +export default class ScopesScopeAuthenticateIndexRoute extends Route { + + // =methods + + /** + * This index route exists solely to redirect the user on to the + * first auth method. + * @param {object} model + * @param {[AuthMethodModel]} model.authMethods + */ + redirect({ authMethods }) { + const firstAuthMethod = authMethods.firstObject; + if (firstAuthMethod) { + this.transitionTo('scopes.scope.authenticate.method', firstAuthMethod); + } + } + +} diff --git a/ui/core/app/routes/scopes/scope/authenticate/method.js b/ui/core/app/routes/scopes/scope/authenticate/method.js index 29014e819..b52406722 100644 --- a/ui/core/app/routes/scopes/scope/authenticate/method.js +++ b/ui/core/app/routes/scopes/scope/authenticate/method.js @@ -10,6 +10,10 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route { @service notify; @service intl; + // =attributes + + routeAfterAuthentication = 'scopes.scope.projects'; + // =methods /** @@ -52,7 +56,7 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route { requestCookies, { scope, authMethod } ); - this.transitionTo('scopes.scope.projects'); + this.transitionTo(this.routeAfterAuthentication); } catch (e) { const errorMessage = this.intl.t('errors.titles.authentication-failed'); this.notify.error(errorMessage, { closeAfter: null }); diff --git a/ui/core/app/templates/scopes/scope/authenticate/index.hbs b/ui/core/app/templates/scopes/scope/authenticate/index.hbs new file mode 100644 index 000000000..e2147cab0 --- /dev/null +++ b/ui/core/app/templates/scopes/scope/authenticate/index.hbs @@ -0,0 +1 @@ +{{outlet}} \ No newline at end of file diff --git a/ui/core/tests/acceptance/authentication-test.js b/ui/core/tests/acceptance/authentication-test.js index bd5c2a1a5..e71c1a3b0 100644 --- a/ui/core/tests/acceptance/authentication-test.js +++ b/ui/core/tests/acceptance/authentication-test.js @@ -5,7 +5,7 @@ import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import a11yAudit from 'ember-a11y-testing/test-support/audit'; import { currentSession, - //authenticateSession, + authenticateSession, //invalidateSession, } from 'ember-simple-auth/test-support'; @@ -18,6 +18,7 @@ module('Acceptance | authentication', function(hooks) { let scope; let authMethod; let authMethodID; + let authenticateURL; let authMethodAuthenticateURL; let projectsURL; @@ -27,10 +28,18 @@ module('Acceptance | authentication', function(hooks) { authMethod = this.server.create('auth-method', { scope }); orgScopeID = orgScope.id; authMethodID = authMethod.id; + authenticateURL = `/scopes/${orgScopeID}/authenticate`; authMethodAuthenticateURL = `/scopes/${orgScopeID}/authenticate/${authMethodID}`; projectsURL = `/scopes/${orgScopeID}/projects`; }); + test('visiting auth methods authenticate route redirects to first auth method', async function(assert) { + assert.expect(1); + await visit(authenticateURL); + await a11yAudit(); + assert.equal(currentURL(), authMethodAuthenticateURL); + }); + test('visiting auth method authenticate route', async function(assert) { assert.expect(1); await visit(authMethodAuthenticateURL); @@ -38,6 +47,15 @@ module('Acceptance | authentication', function(hooks) { assert.equal(currentURL(), authMethodAuthenticateURL); }); + test('visiting any authenticate route while already authenticated redirects to projects', async function(assert) { + assert.expect(2); + authenticateSession(); + await visit(authenticateURL); + assert.equal(currentURL(), projectsURL); + await visit(authMethodAuthenticateURL); + assert.equal(currentURL(), projectsURL); + }); + test('failed authentication shows a notification message', async function(assert) { assert.expect(3); await visit(authMethodAuthenticateURL); diff --git a/ui/core/tests/unit/routes/scopes/scope/authenticate/index-test.js b/ui/core/tests/unit/routes/scopes/scope/authenticate/index-test.js new file mode 100644 index 000000000..88d5a1121 --- /dev/null +++ b/ui/core/tests/unit/routes/scopes/scope/authenticate/index-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Route | scopes/scope/authenticate/index', function(hooks) { + setupTest(hooks); + + test('it exists', function(assert) { + let route = this.owner.lookup('route:scopes/scope/authenticate/index'); + assert.ok(route); + }); +});