Icu 6657 auth method model fragments refactor (#1507)
* refactor: 💡 Remove fragment usage for arrays in auth method * chore: 🤖 Remove ember-data-model-fragments ✅ Closes: ICU-6657
This commit is contained in:
1 parent
f8429b15e9
commit
c3d9fd8fc5
32 files changed
+161
-341
No files matched your search
@@ -71,4 +71,34 @@ export default class GeneratedAuthMethodModel extends BaseModel {
|
||||
disable_discovered_config_validation;
|
||||
|
||||
@attr('boolean', { isNestedAttribute: true }) dry_run;
|
||||
|
||||
@attr('string-array', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
claims_scopes;
|
||||
|
||||
@attr('string-array', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
signing_algorithms;
|
||||
|
||||
@attr('string-array', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
allowed_audiences;
|
||||
|
||||
@attr('string-array', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
idp_ca_certs;
|
||||
|
||||
@attr('account-claim-map-array', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
account_claim_maps;
|
||||
}
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
// app/initializers/fragment-serializer.js
|
||||
import FragmentAuthMethodAttributesAccountClaimMapSerializer from '../serializers/fragment-auth-method-attributes-account-claim-map';
|
||||
|
||||
export function initialize(application) {
|
||||
application.register(
|
||||
'serializer:fragment-auth-method-attributes-account-claim-map',
|
||||
FragmentAuthMethodAttributesAccountClaimMapSerializer
|
||||
);
|
||||
}
|
||||
|
||||
// Fragment serializers must be manually registered
|
||||
export default {
|
||||
name: 'fragment-auth-method-attributes-account-claim-map',
|
||||
initialize: initialize,
|
||||
};
|
||||
@@ -1,12 +0,0 @@
|
||||
// app/initializers/fragment-serializer.js
|
||||
import FragmentStringSerializer from '../serializers/fragment-string';
|
||||
|
||||
export function initialize(application) {
|
||||
application.register('serializer:fragment-string', FragmentStringSerializer);
|
||||
}
|
||||
|
||||
// Fragment serializers must be manually registered
|
||||
export default {
|
||||
name: 'fragment-string',
|
||||
initialize: initialize,
|
||||
};
|
||||
@@ -1,5 +1,4 @@
|
||||
import GeneratedAuthMethodModel from '../generated/models/auth-method';
|
||||
import { fragmentArray } from 'ember-data-model-fragments/attributes';
|
||||
import { equal } from '@ember/object/computed';
|
||||
|
||||
/**
|
||||
@@ -29,36 +28,6 @@ export const options = {
|
||||
export default class AuthMethodModel extends GeneratedAuthMethodModel {
|
||||
// =attributes
|
||||
|
||||
@fragmentArray('fragment-auth-method-attributes-account-claim-map', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
account_claim_maps;
|
||||
|
||||
@fragmentArray('fragment-string', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
claims_scopes;
|
||||
|
||||
@fragmentArray('fragment-string', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
signing_algorithms;
|
||||
|
||||
@fragmentArray('fragment-string', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
allowed_audiences;
|
||||
|
||||
@fragmentArray('fragment-string', {
|
||||
emptyArrayIfMissing: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
idp_ca_certs;
|
||||
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import Fragment from 'ember-data-model-fragments/fragment';
|
||||
import { attr } from '@ember-data/model';
|
||||
|
||||
export default class FragmentAuthMethodAttributesAccountClaimMapModel extends Fragment {
|
||||
// =attributes (OIDC)
|
||||
|
||||
@attr('string') from;
|
||||
@attr('string') to;
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import Fragment from 'ember-data-model-fragments/fragment';
|
||||
import { attr } from '@ember-data/model';
|
||||
|
||||
/**
|
||||
* Represents an item in an array of strings.
|
||||
*/
|
||||
export default class FragmentStringModel extends Fragment {
|
||||
// =attributes
|
||||
|
||||
@attr('string', {
|
||||
description: 'The value of the string.',
|
||||
})
|
||||
value;
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import JSONSerializer from '@ember-data/serializer/json';
|
||||
|
||||
export default class FragmentHostSourceSerializer extends JSONSerializer {
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* Serializes an account claim to string.
|
||||
* @param {Snapshot} snapshot
|
||||
* @return {string}
|
||||
*/
|
||||
serialize(snapshot) {
|
||||
const from = snapshot.attr('from');
|
||||
const to = snapshot.attr('to');
|
||||
return `${from}=${to}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes account claim strings into JSON API objects.
|
||||
* @param {Model} typeClass
|
||||
* @param {string} value
|
||||
* @return {object}
|
||||
*/
|
||||
normalize(typeClass, value) {
|
||||
const normalizedHash = {
|
||||
from: value.split('=')[0],
|
||||
to: value.split('=')[1],
|
||||
};
|
||||
return super.normalize(typeClass, normalizedHash);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import JSONSerializer from '@ember-data/serializer/json';
|
||||
|
||||
export default class FragmentStringSerializer extends JSONSerializer {
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* Serializes a to string.
|
||||
* @param {Snapshot} snapshot
|
||||
* @return {string}
|
||||
*/
|
||||
serialize(snapshot) {
|
||||
return snapshot.attr('value');
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes from strings.
|
||||
* @param {Model} typeClass
|
||||
* @param {string} value
|
||||
* @return {object}
|
||||
*/
|
||||
normalize(typeClass, value) {
|
||||
return super.normalize(typeClass, { value });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import Transform from '@ember-data/serializer/transform';
|
||||
|
||||
export default class AccountClaimMapArrayTransform extends Transform {
|
||||
deserialize(serialized) {
|
||||
const accountClaims = serialized || [];
|
||||
return accountClaims.map((accountClaimString) => ({
|
||||
from: accountClaimString.split('=')[0],
|
||||
to: accountClaimString.split('=')[1],
|
||||
}));
|
||||
}
|
||||
|
||||
serialize(deserialized) {
|
||||
const accountClaims = deserialized || [];
|
||||
return accountClaims.map(({ from, to }) => `${from}=${to}`);
|
||||
}
|
||||
}
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
export {
|
||||
default,
|
||||
initialize,
|
||||
} from 'api/initializers/fragment-auth-method-attributes-account-claim-map-serializer';
|
||||
@@ -1 +0,0 @@
|
||||
export { default, initialize } from 'api/initializers/fragment-string';
|
||||
@@ -1 +0,0 @@
|
||||
export { default } from 'api/models/fragment-auth-method-attributes-account-claim-map';
|
||||
@@ -1 +0,0 @@
|
||||
export { default } from 'api/models/fragment-string';
|
||||
@@ -1 +0,0 @@
|
||||
export { default } from 'api/serializers/fragment-string';
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'api/transforms/account-claim-map-array';
|
||||
@@ -81,7 +81,17 @@ export default function () {
|
||||
return authMethods.create(attrs);
|
||||
});
|
||||
this.get('/auth-methods/:id');
|
||||
this.patch('/auth-methods/:id');
|
||||
this.patch(
|
||||
'/auth-methods/:id',
|
||||
function ({ authMethods }, { params: { id } }) {
|
||||
const attrs = this.normalizedRequestAttrs();
|
||||
if (attrs.type === 'oidc') {
|
||||
attrs.attributes.state = 'active-public';
|
||||
}
|
||||
|
||||
return authMethods.find(id).update(attrs);
|
||||
}
|
||||
);
|
||||
this.del('/auth-methods/:id', ({ authMethods }, { params: { id } }) => {
|
||||
const authMethod = authMethods.find(id);
|
||||
const scope = authMethod.scope;
|
||||
|
||||
@@ -31,8 +31,8 @@
|
||||
"ember-cli-babel": "^7.26.10",
|
||||
"ember-cli-htmlbars": "^6.1.0",
|
||||
"ember-cli-mirage": "^2.2.0",
|
||||
"ember-copy": "^2.0.1",
|
||||
"ember-data": "^3.20.0",
|
||||
"ember-data-model-fragments": "5.0.0-beta.2",
|
||||
"ember-fetch": "^8.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -50,7 +50,6 @@
|
||||
"ember-cli-inject-live-reload": "^2.1.0",
|
||||
"ember-cli-sri": "^2.1.1",
|
||||
"ember-cli-terser": "^4.0.2",
|
||||
"ember-copy": "^2.0.1",
|
||||
"ember-disable-prototype-extensions": "^1.1.3",
|
||||
"ember-export-application-global": "^2.0.1",
|
||||
"ember-load-initializers": "^2.1.2",
|
||||
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
import Application from '@ember/application';
|
||||
|
||||
import { initialize } from 'dummy/initializers/fragment-auth-method-attributes-account-claim-map-serializer';
|
||||
import { module, test } from 'qunit';
|
||||
import Resolver from 'ember-resolver';
|
||||
import { run } from '@ember/runloop';
|
||||
|
||||
module(
|
||||
'Unit | Initializer | fragment-auth-method-attributes-account-claim-map-serializer',
|
||||
function (hooks) {
|
||||
hooks.beforeEach(function () {
|
||||
this.TestApplication = class TestApplication extends Application {};
|
||||
this.TestApplication.initializer({
|
||||
name: 'initializer under test',
|
||||
initialize,
|
||||
});
|
||||
|
||||
this.application = this.TestApplication.create({
|
||||
autoboot: false,
|
||||
Resolver,
|
||||
});
|
||||
});
|
||||
|
||||
hooks.afterEach(function () {
|
||||
run(this.application, 'destroy');
|
||||
});
|
||||
|
||||
// TODO: Replace this with your real tests.
|
||||
test('it works', async function (assert) {
|
||||
await this.application.boot();
|
||||
|
||||
assert.ok(true);
|
||||
});
|
||||
}
|
||||
);
|
||||
@@ -1,32 +0,0 @@
|
||||
import Application from '@ember/application';
|
||||
|
||||
import { initialize } from 'dummy/initializers/fragment-string';
|
||||
import { module, test } from 'qunit';
|
||||
import Resolver from 'ember-resolver';
|
||||
import { run } from '@ember/runloop';
|
||||
|
||||
module('Unit | Initializer | fragment-string', function (hooks) {
|
||||
hooks.beforeEach(function () {
|
||||
this.TestApplication = class TestApplication extends Application {};
|
||||
this.TestApplication.initializer({
|
||||
name: 'initializer under test',
|
||||
initialize,
|
||||
});
|
||||
|
||||
this.application = this.TestApplication.create({
|
||||
autoboot: false,
|
||||
Resolver,
|
||||
});
|
||||
});
|
||||
|
||||
hooks.afterEach(function () {
|
||||
run(this.application, 'destroy');
|
||||
});
|
||||
|
||||
// TODO: Replace this with your real tests.
|
||||
test('it works', async function (assert) {
|
||||
await this.application.boot();
|
||||
|
||||
assert.ok(true);
|
||||
});
|
||||
});
|
||||
-18
@@ -1,18 +0,0 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module(
|
||||
'Unit | Model | fragment auth method attributes account claim map',
|
||||
function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test('it exists', function (assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let model = store.createRecord(
|
||||
'fragment-auth-method-attributes-account-claim-map',
|
||||
{}
|
||||
);
|
||||
assert.ok(model);
|
||||
});
|
||||
}
|
||||
);
|
||||
@@ -1,12 +0,0 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module('Unit | Model | fragment string', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test('it exists', function (assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let model = store.createRecord('fragment-string', {});
|
||||
assert.ok(model);
|
||||
});
|
||||
});
|
||||
@@ -157,16 +157,10 @@ module('Unit | Serializer | auth method', function (hooks) {
|
||||
this.server.get('/v1/auth-methods/oidc123', () => ({
|
||||
attributes: {
|
||||
account_claim_maps: ['from=to', 'foo=bar'],
|
||||
claims_scopes: [{ value: 'profile' }, { value: 'email' }],
|
||||
signing_algorithms: [{ value: 'RS256' }, { value: 'RS384' }],
|
||||
allowed_audiences: [
|
||||
{ value: 'www.alice.com' },
|
||||
{ value: 'www.alice.com/admin' },
|
||||
],
|
||||
idp_ca_certs: [
|
||||
{ value: 'certificate-1234' },
|
||||
{ value: 'certificate-5678' },
|
||||
],
|
||||
claims_scopes: ['profile', 'email'],
|
||||
signing_algorithms: ['RS256', 'RS384'],
|
||||
allowed_audiences: ['www.alice.com', 'www.alice.com/admin'],
|
||||
idp_ca_certs: ['certificate-1234', 'certificate-5678'],
|
||||
api_url_prefix: apiUrlPrefix,
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
@@ -181,12 +175,31 @@ module('Unit | Serializer | auth method', function (hooks) {
|
||||
}));
|
||||
|
||||
const record = await store.findRecord('auth-method', 'oidc123');
|
||||
const { account_claim_maps } = record;
|
||||
|
||||
assert.strictEqual(account_claim_maps.firstObject.from, 'from');
|
||||
assert.strictEqual(account_claim_maps.firstObject.to, 'to');
|
||||
assert.strictEqual(account_claim_maps.lastObject.from, 'foo');
|
||||
assert.strictEqual(account_claim_maps.lastObject.to, 'bar');
|
||||
const {
|
||||
account_claim_maps,
|
||||
claims_scopes,
|
||||
allowed_audiences,
|
||||
signing_algorithms,
|
||||
idp_ca_certs,
|
||||
} = record;
|
||||
console.log(record);
|
||||
assert.deepEqual(account_claim_maps, [
|
||||
{ from: 'from', to: 'to' },
|
||||
{ from: 'foo', to: 'bar' },
|
||||
]);
|
||||
assert.deepEqual(claims_scopes, [{ value: 'profile' }, { value: 'email' }]);
|
||||
assert.deepEqual(allowed_audiences, [
|
||||
{ value: 'www.alice.com' },
|
||||
{ value: 'www.alice.com/admin' },
|
||||
]);
|
||||
assert.deepEqual(signing_algorithms, [
|
||||
{ value: 'RS256' },
|
||||
{ value: 'RS384' },
|
||||
]);
|
||||
assert.deepEqual(idp_ca_certs, [
|
||||
{ value: 'certificate-1234' },
|
||||
{ value: 'certificate-5678' },
|
||||
]);
|
||||
assert.strictEqual(record.api_url_prefix, apiUrlPrefix);
|
||||
assert.strictEqual(record.client_id, clientId);
|
||||
assert.strictEqual(record.client_secret, clientSecret);
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module('Unit | Serializer | fragment string', 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('fragment-string');
|
||||
|
||||
assert.ok(serializer);
|
||||
});
|
||||
|
||||
test('it serializes records', function (assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let record = store.createRecord('fragment-string', {
|
||||
value: 'string value',
|
||||
});
|
||||
|
||||
let serializedRecord = record.serialize();
|
||||
|
||||
assert.strictEqual(serializedRecord, 'string value');
|
||||
});
|
||||
|
||||
test('it normalizes records', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('fragment-string');
|
||||
const modelClass = store.createRecord('fragment-string').constructor;
|
||||
const payload = 'string value';
|
||||
const normalized = serializer.normalizeSingleResponse(
|
||||
store,
|
||||
modelClass,
|
||||
payload
|
||||
);
|
||||
assert.deepEqual(normalized, {
|
||||
included: [],
|
||||
data: {
|
||||
id: null,
|
||||
type: 'fragment-string',
|
||||
attributes: {
|
||||
value: 'string value',
|
||||
},
|
||||
relationships: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module('Unit | Transform | account claim map array', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test('it deserializes an array of account claims', function (assert) {
|
||||
assert.expect(1);
|
||||
const transform = this.owner.lookup('transform:account-claim-map-array');
|
||||
const deserialized = transform.deserialize(['oid=sub', 'full_name=name']);
|
||||
assert.deepEqual(deserialized, [
|
||||
{ from: 'oid', to: 'sub' },
|
||||
{ from: 'full_name', to: 'name' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('it serializes an array of account claims', function (assert) {
|
||||
assert.expect(1);
|
||||
const transform = this.owner.lookup('transform:account-claim-map-array');
|
||||
const serialized = transform.serialize([
|
||||
{ from: 'oid', to: 'sub' },
|
||||
{ from: 'full_name', to: 'name' },
|
||||
]);
|
||||
assert.deepEqual(serialized, ['oid=sub', 'full_name=name']);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user