refactor: 💡 implement new pathing for auth methods

This commit is contained in:
Randall Morey committed 2020-09-09 12:24:01 -04:00
1 parent 83dc74460a
commit a368d65d10
17 files changed
+121 -41

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
import ApplicationAdapter from './application';
export default class AuthMethodAdapter extends ApplicationAdapter {
// =attributes
/**
* Auth methods use the new, unprefixed pathing style.
* @override
*/
hasScopePrefix = false;
}
@@ -0,0 +1,13 @@
import ApplicationSerializer from './application';
export default class AuthMethodSerializer extends ApplicationSerializer {
// =attributes
/**
* Serialized the scope ID into the `scope_id` field.
* @type {boolean}
*/
serializeScopeID = true;
}
+1
View File
@@ -0,0 +1 @@
export { default } from 'api/adapters/auth-method';
@@ -0,0 +1 @@
export { default } from 'api/serializers/auth-method';
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Adapter | auth method', function(hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function(assert) {
let adapter = this.owner.lookup('adapter:auth-method');
assert.ok(adapter);
});
});
@@ -0,0 +1,24 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Serializer | auth method', function(hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function(assert) {
let store = this.owner.lookup('service:store');
let serializer = store.serializerFor('auth-method');
assert.ok(serializer);
});
test('it serializes records', function(assert) {
let store = this.owner.lookup('service:store');
let record = store.createRecord('auth-method', {});
let serializedRecord = record.serialize();
assert.ok(serializedRecord);
});
});
@@ -23,8 +23,8 @@ export default class ScopesScopeAuthMethodsRoute extends Route {
* @return {Promise[AuthMethodModel]}
*/
async model() {
const { id: scopeID} = this.modelFor('scopes.scope');
return this.store.findAll('auth-method', { adapterOptions: { scopeID } });
const { id: scope_id } = this.modelFor('scopes.scope');
return this.store.query('auth-method', { scope_id });
}
// =actions
@@ -48,11 +48,12 @@ export default class ScopesScopeAuthMethodsRoute extends Route {
const { isNew } = authMethod;
try {
await authMethod.save();
await this
.transitionTo('scopes.scope.auth-methods.auth-method', authMethod);
this.refresh();
this.notify.success(
this.intl.t(isNew ? 'notify.create-success' : 'notify.save-success')
);
this.transitionTo('scopes.scope.auth-methods.auth-method', authMethod);
} catch (error) {
//TODO: replace with translated strings
this.notify.error(error.message, { closeAfter: null });
@@ -67,9 +68,9 @@ export default class ScopesScopeAuthMethodsRoute extends Route {
async delete(authMethod) {
try {
await authMethod.destroyRecord();
await this.transitionTo('scopes.scope.auth-methods');
this.refresh();
this.notify.success(this.intl.t('notify.delete-success'));
this.transitionTo('scopes.scope.auth-methods');
} catch (error) {
//TODO: replace with translated strings
this.notify.error(error.message, { closeAfter: null });
@@ -9,8 +9,7 @@ export default class ScopesScopeAuthMethodsAuthMethodRoute extends Route {
* @param {string} params.auth_method_id
* @return {authMethodModel}
*/
async model({ auth_method_id}) {
const { id: scopeID } = this.modelFor('scopes.scope');
return this.store.findRecord('auth-method', auth_method_id, { adapterOptions: { scopeID }});
async model({ auth_method_id }) {
return this.store.findRecord('auth-method', auth_method_id);
}
}
@@ -1,6 +1,7 @@
import Route from '@ember/routing/route';
export default class ScopesScopeAuthMethodsNewRoute extends Route {
// =methods
/**
@@ -9,6 +10,10 @@ export default class ScopesScopeAuthMethodsNewRoute extends Route {
*/
model() {
const scopeModel = this.modelFor('scopes.scope');
return this.store.createRecord('auth-method', { type: 'password', scopeModel });
return this.store.createRecord('auth-method', {
scopeModel,
type: 'password'
});
}
}
@@ -15,11 +15,11 @@ export default class ScopesScopeAuthenticateRoute extends Route {
* @return {Promise} `{scope, scopes, authMethods}`
*/
model() {
const adapterOptions = { scopeID: this.modelFor('scopes.scope').id };
const { id: scope_id } = this.modelFor('scopes.scope');
return hash({
scope: this.modelFor('scopes.scope'),
scopes: this.modelFor('scopes'),
authMethods: this.store.findAll('auth-method', { adapterOptions }),
authMethods: this.store.query('auth-method', { scope_id }),
// for integration testing:
// authMethods: A([{
+7 -5
View File
@@ -41,11 +41,13 @@ export default function() {
// Auth & IAM resources
this.get('/scopes/:scope_id/auth-methods');
this.post('/scopes/:scope_id/auth-methods');
this.get('/scopes/:scope_id/auth-methods/:id');
this.patch('/scopes/:scope_id/auth-methods/:id');
this.del('/scopes/:scope_id/auth-methods/:id');
this.get('/auth-methods', ({ authMethods }, { queryParams: { scope_id: scopeId } }) => {
return authMethods.where({ scopeId });
});
this.post('/auth-methods');
this.get('/auth-methods/:id');
this.patch('/auth-methods/:id');
this.del('/auth-methods/:id');
// Authenticate/deauthenticate routes
this.post('/scopes/:scope_id/auth-methods/:id_method', authHandler);
+7
View File
@@ -0,0 +1,7 @@
import { Model, belongsTo } from 'ember-cli-mirage';
export default Model.extend({
scope: belongsTo()
});
+2 -1
View File
@@ -15,7 +15,8 @@ export default function(server) {
const scopePojo = { id: orgScope.id, type: orgScope.type };
// Auth
server.createList('auth-method', 3, { scope: scopePojo });
server.createList('auth-method', 3, { scope: globalScope });
server.createList('auth-method', 3, { scope: orgScope });
// Groups and Users
server.createList('group', 5, { scope: scopePojo }, 'withMembers');
+18 -1
View File
@@ -1,5 +1,22 @@
import ApplicationSerializer from './application';
import { underscore } from '@ember/string';
export default ApplicationSerializer.extend({
modelName: 'auth-method'
modelName: 'auth-method',
keyForRelationshipId(relationshipName) {
return `${this._container.inflector.singularize(
underscore(relationshipName)
)}_id`;
},
_hashForModel(model) {
const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments);
json.scope = {
id: model.scope.id,
type: model.scope.type
};
return json;
}
});
-5
View File
@@ -1,5 +0,0 @@
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
modelName: 'org'
});
-5
View File
@@ -1,5 +0,0 @@
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
modelName: 'project'
});
@@ -21,7 +21,9 @@ module('Acceptance | authentication', function (hooks) {
let scopesURL;
let orgScopeURL;
let scope;
let globalAuthMethod;
let authMethod;
let globalAuthMethodID;
let authMethodID;
let globalAuthenticateURL;
let authenticateURL;
@@ -43,14 +45,16 @@ module('Acceptance | authentication', function (hooks) {
'withChildren'
);
scope = { id: orgScope.id, type: orgScope.type };
authMethod = this.server.create('auth-method', { scope });
globalAuthMethod = this.server.create('auth-method', { scope: globalScope });
authMethod = this.server.create('auth-method', { scope: orgScope });
orgScopeID = orgScope.id;
globalAuthMethodID = globalAuthMethod.id;
authMethodID = authMethod.id;
scopesURL = `/scopes`;
orgScopeURL = `/scopes/${orgScopeID}`;
globalAuthenticateURL = `/scopes/global/authenticate`;
authenticateURL = `/scopes/${orgScopeID}/authenticate`;
authMethodGlobalAuthenticateURL = `/scopes/global/authenticate/${authMethodID}`;
authMethodGlobalAuthenticateURL = `/scopes/global/authenticate/${globalAuthMethodID}`;
authMethodAuthenticateURL = `/scopes/${orgScopeID}/authenticate/${authMethodID}`;
orgsURL = `/scopes/global/orgs`;
projectsURL = `/scopes/${orgScopeID}/projects`;
@@ -74,18 +78,8 @@ module('Acceptance | authentication', function (hooks) {
test('visiting auth method when the scope cannot be loaded is still allowed', async function (assert) {
assert.expect(1);
orgScope.destroy();
this.server.get('/scopes', ({ scopes }, { queryParams: { scope_id } }) => {
// Default parent scope is global
if (!scope_id) scope_id = 'global';
const results = scopes.where(scope => {
return scope.scope ? scope.scope.id === scope_id : false
});
if (!results.length) {
return new Response(404);
} else {
return results;
}
this.server.get('/scopes', () => {
return new Response(404);
});
await visit(authMethodAuthenticateURL);
await a11yAudit();