Icu 5476 refactor target model serializer and mirage (#1235)

chore: 🤖 refactor target model to support credentials
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2022-08-08 09:21:07 -07:00
1 parent 2cda4f639e
commit 1f0baceb5e
12 files changed
+210 -66

No files matched your search

+22 -18
View File
@@ -48,17 +48,21 @@ export default class TargetModel extends GeneratedTargetModel {
}
/**
* An array of resolved credential library instances. Model instances
* An array of resolved credential library and credential instances. Model instances
* must already be loaded into the store (this method will not load unloaded
* instances). Unresolvable instances are excluded from the array.
* @type {[CredentialLibraryModel]}
* @type {[CredentialLibraryModel, CredentialModel]}
*/
@computed('application_credential_source_ids.[]', 'store')
get credentialLibraries() {
get credentialSources() {
return this.application_credential_source_ids
.map((source) =>
this.store.peekRecord('credential-library', source.value)
)
.map((source) => {
if (source.value.startsWith('cred')) {
return this.store.peekRecord('credential', source.value);
} else {
return this.store.peekRecord('credential-library', source.value);
}
})
.filter(Boolean);
}
@@ -150,17 +154,17 @@ export default class TargetModel extends GeneratedTargetModel {
}
/**
* Adds credential libraries via the `add-credential-sources` method.
* Adds credential sources via the `add-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} credentialLibraryIDs
* @param {[string]} credentialSourceIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
addCredentialSources(credentialLibraryIDs, options = { adapterOptions: {} }) {
addCredentialSources(credentialSourceIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'add-credential-sources',
credentialLibraryIDs,
credentialSourceIDs,
};
// There is no "deep merge" in ES.
return this.save({
@@ -173,20 +177,20 @@ export default class TargetModel extends GeneratedTargetModel {
}
/**
* Delete credential libraries via the `remove-credential-sources` method.
* Delete credential libraries and credentials via the `remove-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} credentialLibraryIDs
* @param {[string]} credentialSourceIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
removeCredentialSources(
credentialLibraryIDs,
credentialSourceIDs,
options = { adapterOptions: {} }
) {
const defaultAdapterOptions = {
method: 'remove-credential-sources',
credentialLibraryIDs,
credentialSourceIDs,
};
// There is no "deep merge" in ES.
return this.save({
@@ -199,13 +203,13 @@ export default class TargetModel extends GeneratedTargetModel {
}
/**
* Delete a single credential library set via the `remove-credential-sources` method.
* @param {number} credentialLibraryID
* Delete a single credential library/credential set via the `remove-credential-sources` method.
* @param {number} credentialSourceID
* @param {object} options
* @return {Promise}
*/
removeCredentialSource(credentialLibraryID, options) {
return this.removeCredentialSources([credentialLibraryID], options);
removeCredentialSource(credentialSourceID, options) {
return this.removeCredentialSources([credentialSourceID], options);
}
/**
+5 -5
View File
@@ -7,10 +7,10 @@ export default class TargetSerializer extends ApplicationSerializer {
* If `adapterOptions.hostSets` is set to an array of host set models,
* the resulting target serialization should include **only host sets**
* and the version.
* If `adapterOptions.credentialLibraries` is set to an array of
* credential library models, the resulting target serialization should
* include **only credential libraries** and the version.
* Normally, neither host sets or credential libraries are not serialized.
* If `adapterOptions.credentialSources` is set to an array of
* credential library and credential models, the resulting target serialization should
* include **only credential libraries and crendentials** and the version.
* Normally, neither host sets or credential libraries or credentials are not serialized.
* @override
* @param {Snapshot} snapshot
* @return {object}
@@ -21,7 +21,7 @@ export default class TargetSerializer extends ApplicationSerializer {
if (hostSourceIDs) {
serialized = this.serializeWithHostSources(snapshot, hostSourceIDs);
}
const credentialSourceIDs = snapshot?.adapterOptions?.credentialLibraryIDs;
const credentialSourceIDs = snapshot?.adapterOptions?.credentialSourceIDs;
if (credentialSourceIDs)
serialized = this.serializeWithCredentialSources(
snapshot,
+14 -2
View File
@@ -464,19 +464,31 @@ export default function () {
// If adding credential sources, push them into the array
if (method === 'add-credential-sources') {
updatedAttrs.credentialLibraryIds = target.credentialLibraryIds;
updatedAttrs.credentialIds = target.credentialIds;
attrs.applicationCredentialSourceIds.forEach((id) => {
if (!updatedAttrs.credentialLibraryIds.includes(id)) {
updatedAttrs.credentialLibraryIds.push(id);
if (
!updatedAttrs.credentialLibraryIds.includes(id) ||
!updatedAttrs.credentialIds.includes(id)
) {
if (id.startsWith('cred')) {
updatedAttrs.credentialIds.push(id);
} else {
updatedAttrs.credentialLibraryIds.push(id);
}
}
});
}
// If deleting credential sources, filter them out of the array
if (method === 'remove-credential-sources') {
updatedAttrs.credentialLibraryIds = target.credentialLibraryIds;
updatedAttrs.credentialIds = target.credentialIds;
updatedAttrs.credentialLibraryIds =
updatedAttrs.credentialLibraryIds.filter((id) => {
return !attrs.applicationCredentialSourceIds.includes(id);
});
updatedAttrs.credentialIds = updatedAttrs.credentialIds.filter((id) => {
return !attrs.applicationCredentialSourceIds.includes(id);
});
}
return target.update(updatedAttrs);
}
+7 -2
View File
@@ -49,7 +49,9 @@ export default factory.extend({
*/
withAssociations: trait({
afterCreate(target, server) {
let randomlySelectedHostSets, randomlySelectedCredentialLibraries;
let randomlySelectedHostSets,
randomlySelectedCredentialLibraries,
randomlySelectedCredentials;
randomlySelectedHostSets = server.schema.hostSets
// BLERG: fun fact, for no reason at all, the element passed
// into a where function is not a full model instance, as you might
@@ -64,10 +66,13 @@ export default factory.extend({
(credentialLibrary) => credentialLibrary.scopeId === target.scope.id
)
.models.filter(() => randomBoolean());
randomlySelectedCredentials = server.schema.credentials
.where((credential) => credential.scopeId === target.scope.id)
.models.filter(() => randomBoolean());
target.update({
hostSets: randomlySelectedHostSets,
credentialLibraries: randomlySelectedCredentialLibraries,
credentials: randomlySelectedCredentials,
});
},
}),
+1
View File
@@ -5,4 +5,5 @@ export default Model.extend({
scope: belongsTo(),
hostSets: hasMany(),
credentialLibraries: hasMany(),
credentials: hasMany(),
});
+5 -2
View File
@@ -23,8 +23,11 @@ export default ApplicationSerializer.extend({
return { id: host_set_id, host_catalog_id };
});
}
if (model.credentialLibraryIds?.length) {
json.application_credential_source_ids = model.credentialLibraryIds;
if (model.credentialLibraryIds?.length || model.credentialIds?.length) {
json.application_credential_source_ids = [
...model.credentialLibraryIds,
...model.credentialIds,
];
}
return json;
},
+6 -6
View File
@@ -231,7 +231,7 @@ module('Unit | Model | target', function (hooks) {
);
});
test('it has a `credentialLibraries` array of resolved model instances (if those instances are already in the store)', function (assert) {
test('it has a `credentialSources` array of resolved model instances (if those instances are already in the store)', function (assert) {
assert.expect(4);
const store = this.owner.lookup('service:store');
store.push({
@@ -250,9 +250,9 @@ module('Unit | Model | target', function (hooks) {
'Target has two entires in application_credential_source_ids'
);
assert.strictEqual(
target.credentialLibraries.length,
target.credentialSources.length,
0,
'Target has no resolved credentialLibraries because they are not loaded yet'
'Target has no resolved credentialSources because they are not loaded yet'
);
store.push({
data: {
@@ -268,7 +268,7 @@ module('Unit | Model | target', function (hooks) {
attributes: {},
},
});
// Since `credentialLibraries` is computed on `application_credential_source_ids`,
// Since `credentialSources` is computed on `application_credential_source_ids`,
// not the store itself, it's necessary to do this assignment to kick-off the
// computed update.
/* eslint-disable no-self-assign */
@@ -281,9 +281,9 @@ module('Unit | Model | target', function (hooks) {
'Target has two entires in application_credential_source_ids'
);
assert.strictEqual(
target.credentialLibraries.length,
target.credentialSources.length,
2,
'Target has two resolved credentialLibraries'
'Target has two resolved credentialSources'
);
});
@@ -66,7 +66,7 @@ module('Unit | Serializer | target', function (hooks) {
});
});
test('it serializes only credential sources and version when an `adapterOptions.credentialLibraryIDs` array is passed', function (assert) {
test('it serializes only credential sources and version when an `adapterOptions.credentialSourceIDs` array is passed', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
@@ -78,7 +78,7 @@ module('Unit | Serializer | target', function (hooks) {
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
credentialLibraryIDs: ['4', '5'],
credentialSourceIDs: ['4', '5'],
};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {