chore: setup remaining auth redirect logic

This commit is contained in:
Randall Morey committed 2020-07-24 19:12:20 -04:00
1 parent 4b1bb3a508
commit bbbe3e2819
13 files changed
+149 -27

No files matched your search

+1
View File
@@ -18,6 +18,7 @@ Router.map(function () {
this.route('new');
});
});
this.route('cope');
});
this.route('orgs', function () {
+2 -7
View File
@@ -36,16 +36,11 @@ export default class ApplicationRoute extends Route.extend(
/**
* When the session ends, redirect to authenticate and reload the page to
* purge any in-memory state.
*
* TODO This application route is redirecting to a subroute, assuming
* that the user is logged into a specific organization. This won't always
* necessarily be the case. When we support MSP, users may authenticate
* without an org scope. So this will need to be rethought.
*/
async sessionInvalidated() {
// Catch error in this transition, since it will be aborted by the
// org auth route when it redirects to the first auth method.
await this.transitionTo(this.routeIfUnauthenticated);
// scope auth route when it redirects to the first auth method.
await this.transitionTo(this.routeIfUnauthenticated).catch(() => {});
// The Ember way of accessing globals...
const document = getOwner(this).lookup('service:-document').documentElement;
// defaultView === window, but without using globals directly
+14
View File
@@ -0,0 +1,14 @@
import Route from '@ember/routing/route';
export default class IndexRoute extends Route {
// =methods
/**
* Redirects to scopes route for further processing.
*/
redirect() {
this.transitionTo('scopes');
}
}
+29
View File
@@ -0,0 +1,29 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { get } from '@ember/object';
export default class ScopesIndexRoute extends Route {
// =services
@service session;
// =methods
/**
* If authenticated, redirects to the scope route of the authenticted scope.
* If unauthenticated, redirects to the first scope that was loaded (if any).
* @param {[ScopeModel]} model
* @param {?ScopeModel} model.firstObject
*/
redirect({ firstObject }) {
const authenticatedScopeID =
get(this.session, 'data.authenticated.scope.id');
if (authenticatedScopeID) {
this.transitionTo('scopes.scope', authenticatedScopeID);
} else if (firstObject) {
this.transitionTo('scopes.scope', firstObject.id);
}
}
}
+14
View File
@@ -0,0 +1,14 @@
import Route from '@ember/routing/route';
export default class ScopesScopeIndexRoute extends Route {
// =methods
/**
* Redirects to scopes/scope/authenticate for further processing.
*/
redirect() {
this.transitionTo('scopes.scope.authenticate');
}
}
@@ -7,9 +7,17 @@ export default class ScopesScopeProjectsRoute extends Route {
// =services
@service notify;
@service session;
// =methods
/**
* If arriving here unauthenticated, redirect to index for further processing.
*/
beforeModel() {
if (!this.session.isAuthenticated) this.transitionTo('index');
}
/**
* Loads all scopes under the current scope.
* @return {Promise{[ScopeModel]}}
+1
View File
@@ -0,0 +1 @@
{{outlet}}
@@ -0,0 +1 @@
{{outlet}}
+4 -13
View File
@@ -1,6 +1,6 @@
import config from '../config/environment';
import { authHandler, deauthHandler } from './route-handlers/auth';
import { Response } from 'miragejs';
//import { Response } from 'miragejs';
export default function() {
@@ -21,9 +21,10 @@ export default function() {
this.get('/scopes', ({ scopes }, { queryParams: { scope_id } }) => {
// Default parent scope is global
if (!scope_id) scope_id = 'global';
return scopes.where(scope => {
const results = scopes.where(scope => {
return scope.scope ? scope.scope.id === scope_id : false
});
return results;
});
this.post('/scopes', function ({ scopes }, request) {
// Parent scope comes through the payload via `scope_id`, but this needs
@@ -34,17 +35,7 @@ export default function() {
attrs.scope = parentScopeAttrs;
return scopes.create(attrs);
});
// To simulate a possible real-world case, org scopes are not returned,
// but project scopes are.
// TODO this should be expanded to support other scenarios.
this.get('/scopes/:id', ({ scopes }, { params: { id: scopeID } }) => {
const scope = scopes.find(scopeID);
if (scope.type !== 'project') {
return new Response(404, {}, {});
} else {
return scope;
}
});
this.get('/scopes/:id');
this.patch('/scopes/:id');
this.del('/scopes/:id');
@@ -6,15 +6,19 @@ import a11yAudit from 'ember-a11y-testing/test-support/audit';
import {
currentSession,
authenticateSession,
//invalidateSession,
invalidateSession,
} from 'ember-simple-auth/test-support';
module('Acceptance | authentication', function(hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
let indexURL;
let globalScope;
let orgScope;
let orgScopeID;
let scopesURL;
let orgScopeURL;
let scope;
let authMethod;
let authMethodID;
@@ -23,11 +27,19 @@ module('Acceptance | authentication', function(hooks) {
let projectsURL;
hooks.beforeEach(function () {
orgScope = this.server.create('scope', {type: 'org'}, 'withChildren');
invalidateSession();
indexURL = '/';
globalScope = this.server.create('scope', { id: 'global' });
orgScope = this.server.create('scope', {
type: 'org',
scope: { id: globalScope.id, type: globalScope.type }
}, 'withChildren');
scope = { id: orgScope.id, type: orgScope.type };
authMethod = this.server.create('auth-method', { scope });
orgScopeID = orgScope.id;
authMethodID = authMethod.id;
scopesURL = `/scopes`;
orgScopeURL = `/scopes/${orgScopeID}`;
authenticateURL = `/scopes/${orgScopeID}/authenticate`;
authMethodAuthenticateURL = `/scopes/${orgScopeID}/authenticate/${authMethodID}`;
projectsURL = `/scopes/${orgScopeID}/projects`;
@@ -40,6 +52,22 @@ module('Acceptance | authentication', function(hooks) {
assert.equal(currentURL(), authMethodAuthenticateURL);
});
test('visiting any authentication parent route while unauthenticated redirects to first authenticate method', async function(assert) {
assert.expect(3);
await visit(indexURL);
assert.equal(currentURL(), authMethodAuthenticateURL);
await visit(scopesURL);
assert.equal(currentURL(), authMethodAuthenticateURL);
assert.notOk(currentSession().isAuthenticated);
});
test('visiting projects while unauthenticated redirects to first authenticate method', async function(assert) {
assert.expect(2);
await visit(projectsURL);
assert.equal(currentURL(), authMethodAuthenticateURL);
assert.notOk(currentSession().isAuthenticated);
});
test('visiting auth method authenticate route', async function(assert) {
assert.expect(1);
await visit(authMethodAuthenticateURL);
@@ -47,13 +75,20 @@ 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();
test('visiting any authentication parent route while already authenticated redirects to projects', async function(assert) {
assert.expect(6);
authenticateSession({ scope });
await visit(indexURL);
assert.equal(currentURL(), projectsURL);
await visit(scopesURL);
assert.equal(currentURL(), projectsURL);
await visit(orgScopeURL);
assert.equal(currentURL(), projectsURL);
await visit(authenticateURL);
assert.equal(currentURL(), projectsURL);
await visit(authMethodAuthenticateURL);
assert.equal(currentURL(), projectsURL);
assert.ok(currentSession().isAuthenticated);
});
test('failed authentication shows a notification message', async function(assert) {
@@ -78,7 +113,7 @@ module('Acceptance | authentication', function(hooks) {
assert.ok(currentSession().isAuthenticated);
});
test('deauthentication redirects to index', async function(assert) {
test('deauthentication redirects to first authenticate method', async function(assert) {
assert.expect(3);
await visit(authMethodAuthenticateURL);
await fillIn('[name="identification"]', 'test');
@@ -87,7 +122,7 @@ module('Acceptance | authentication', function(hooks) {
assert.ok(currentSession().isAuthenticated);
await click('.rose-button-header-dropdown');
assert.notOk(currentSession().isAuthenticated);
assert.equal(currentURL(), '/');
assert.equal(currentURL(), authMethodAuthenticateURL);
});
});
+11
View File
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | index', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:index');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/index', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/index');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/index', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/index');
assert.ok(route);
});
});