Files
threshold-ui/addons/api/addon/serializers/auth-method.js
T
Devon Anderson 10b91ed21e refactor: 💡 refactor test and serializer (#1786)
* refactor: 💡 refactor test and serializer

Refactor `auth-method` mirage serializer to delete secret attributes.
Refactor `auth-method` mirage serializer unit test for LDAP

✅ Closes: ICU-10025

* refactor: 💡 merge main

merge main

* refactor: 💡 rebase

rebase

* refactor: 💡 refactor serializer and test

Refactor serializer normalize method to delete secret attributes field.

* refactor: 💡 refactor test

Refactor failing test due to deleting secrets in mirage

* refactor: 💡 refactor test

changed assertion

* refactor: 💡 refactor test

change assertion expect

* refactor: 💡 refactor test

Refactor `auth-method-test` to include normalize oidc and ldap secrets

* refactor: 💡 rebase

rebase
2023-07-25 14:52:12 -04:00

106 lines
2.9 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import ApplicationSerializer from './application';
import { TYPE_AUTH_METHOD_PASSWORD } from 'api/models/auth-method';
export default class AuthMethodSerializer extends ApplicationSerializer {
// =methods
/**
* Delegates to a type-specific serialization, or default.
* @override
* @param {Snapshot} snapshot
* @return {object}
*/
serialize(snapshot) {
switch (snapshot.record.type) {
case TYPE_AUTH_METHOD_PASSWORD:
return this.serializePassword(...arguments);
default:
return this.serializeDefault(...arguments);
}
}
/**
* Password serialization omits `attributes`.
* @return {object}
*/
serializePassword() {
let serialized = super.serialize(...arguments);
delete serialized.attributes;
return serialized;
}
/**
* If `adapterOptions.state` is set, the serialization should
* include **only state** and version. Normally, this is not serialized.
* @param {Snapshot} snapshot
* @return {object}
*/
serializeDefault(snapshot) {
let serialized = super.serialize(...arguments);
const state = snapshot?.adapterOptions?.state;
if (state) {
serialized = this.serializeWithState(snapshot, state);
} else {
delete serialized.attributes.state;
}
return serialized;
}
/**
* Returns a payload containing only state and version.
* @param {Snapshot} snapshot
* @param {string} state
* @return {object}
*/
serializeWithState(snapshot, state) {
return {
version: snapshot.attr('version'),
attributes: { state },
};
}
/**
* Sorts the array in order of "is_primary" such that the primary method
* appears first. The "natural" order of auth methods places primary first.
*
* @override
* @return {object}
*/
normalizeArrayResponse() {
const normalized = super.normalizeArrayResponse(...arguments);
normalized.data = normalized.data.sort((a) =>
a.attributes.is_primary ? -1 : 1
);
return normalized;
}
/**
* Sets `is_primary` to `false` if it is missing in the response. The API
* omits "zero" values per:
* https://developers.google.com/protocol-buffers/docs/proto3#default
*
* TODO: generalize this so that all zero values are reified.
* @override
* @param {Model} typeClass
* @param {Object} hash
* @return {Object}
*/
normalize(typeClass, hash, ...rest) {
let normalizedHash = structuredClone(hash);
const normalized = super.normalize(typeClass, normalizedHash, ...rest);
if (!normalized.data.attributes.is_primary) {
normalized.data.attributes.is_primary = false;
}
// Remove secret fields as we don't track them after being created/updated
normalized.data.attributes.client_certificate_key = '';
normalized.data.attributes.bind_password = '';
normalized.data.attributes.client_secret = '';
return normalized;
}
}