refactor: 💡 add serialize method for ldap account type (#1774)
✅ Closes: ICU-10057
This commit is contained in:
1 parent
b6f93c45bc
commit
228f8d33b4
2 files changed
+90
-2
No files matched your search
@@ -4,6 +4,11 @@
|
||||
*/
|
||||
|
||||
import ApplicationSerializer from './application';
|
||||
import {
|
||||
TYPE_AUTH_METHOD_PASSWORD,
|
||||
TYPE_AUTH_METHOD_OIDC,
|
||||
TYPE_AUTH_METHOD_LDAP,
|
||||
} from 'api/models/auth-method';
|
||||
|
||||
export default class AccountSerializer extends ApplicationSerializer {
|
||||
// =properties
|
||||
@@ -26,13 +31,31 @@ export default class AccountSerializer extends ApplicationSerializer {
|
||||
*/
|
||||
serialize(snapshot) {
|
||||
switch (snapshot.record.type) {
|
||||
case 'password':
|
||||
case TYPE_AUTH_METHOD_PASSWORD:
|
||||
return this.serializePassword(...arguments);
|
||||
case 'oidc':
|
||||
case TYPE_AUTH_METHOD_OIDC:
|
||||
return this.serializeOIDC(...arguments);
|
||||
case TYPE_AUTH_METHOD_LDAP:
|
||||
return this.serializeLDAP(...arguments);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* LDAP serialization limits the `attributes` posted on create and omits
|
||||
* `attributes` entirely on update.
|
||||
*/
|
||||
serializeLDAP(snapshot) {
|
||||
const { isNew } = snapshot?.record || {};
|
||||
let serialized = super.serialize(...arguments);
|
||||
delete serialized.attributes;
|
||||
if (isNew) {
|
||||
serialized.attributes = {};
|
||||
const { login_name } = snapshot.record;
|
||||
if (login_name) serialized.attributes.login_name = login_name;
|
||||
}
|
||||
return serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* OIDC serialization limits the `attributes` posted on create and omits
|
||||
* `attributes` entirely on update.
|
||||
|
||||
@@ -8,6 +8,7 @@ import { setupTest } from 'ember-qunit';
|
||||
import {
|
||||
TYPE_AUTH_METHOD_PASSWORD,
|
||||
TYPE_AUTH_METHOD_OIDC,
|
||||
TYPE_AUTH_METHOD_LDAP,
|
||||
} from 'api/models/auth-method';
|
||||
|
||||
module('Unit | Serializer | account', function (hooks) {
|
||||
@@ -186,4 +187,68 @@ module('Unit | Serializer | account', function (hooks) {
|
||||
version: 1,
|
||||
});
|
||||
});
|
||||
|
||||
test('it serializes LDAP-type with only login_name in `attributes` on create', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('account');
|
||||
const record = store.createRecord('account', {
|
||||
type: TYPE_AUTH_METHOD_LDAP,
|
||||
name: 'Name',
|
||||
auth_method_id: '1',
|
||||
description: 'Description',
|
||||
login_name: 'Login Name',
|
||||
full_name: 'Full Nname',
|
||||
email: 'Email',
|
||||
dn: 'dn',
|
||||
member_of_groups: ['Group'],
|
||||
version: 1,
|
||||
});
|
||||
const snapshot = record._createSnapshot();
|
||||
const serializedRecord = serializer.serialize(snapshot);
|
||||
assert.deepEqual(serializedRecord, {
|
||||
type: TYPE_AUTH_METHOD_LDAP,
|
||||
name: 'Name',
|
||||
auth_method_id: '1',
|
||||
description: 'Description',
|
||||
version: 1,
|
||||
attributes: {
|
||||
login_name: 'Login Name',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('it serializes LDAP-type without `attributes`', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('account');
|
||||
store.push({
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'account',
|
||||
attributes: {
|
||||
type: TYPE_AUTH_METHOD_LDAP,
|
||||
name: 'Name',
|
||||
auth_method_id: '1',
|
||||
description: 'Description',
|
||||
login_name: 'Login Name',
|
||||
full_name: 'Full Nname',
|
||||
email: 'Email',
|
||||
dn: 'dn',
|
||||
member_of_groups: ['Group'],
|
||||
version: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
const record = store.peekRecord('account', '1');
|
||||
const snapshot = record._createSnapshot();
|
||||
const serializedRecord = serializer.serialize(snapshot);
|
||||
assert.deepEqual(serializedRecord, {
|
||||
type: TYPE_AUTH_METHOD_LDAP,
|
||||
name: 'Name',
|
||||
auth_method_id: '1',
|
||||
description: 'Description',
|
||||
version: 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user