refactor: 💡 update credential serializer to delete password (#1277)

* refactor: 💡 update credential serializer to delete password

update credential serializer to delete password attribute if empty
string or null allowing the user to update just the username if needed.

✅ Closes: ICU-5731

* refactor: 💡 update serializer comments and test names
This commit is contained in:
Cameron Perera authored and GitHub committed 2022-08-25 17:02:48 -05:00
1 parent 30b9f65251
commit 9a10b68bbe
2 files changed
+69

No files matched your search

@@ -28,6 +28,13 @@ export default class CredentialSerializer extends ApplicationSerializer {
// Remove non-username_password type attributes
delete serialized.attributes.private_key;
delete serialized.attributes.passphrase;
// Remove password from the payload if null
// or empty string. This allows the user to only update
// the username without providing the password again.
// Users cannot unset the password, they would have
// to delete the credential entirely.
if (!serialized?.attributes?.password)
delete serialized.attributes.password;
return serialized;
}
@@ -35,6 +42,11 @@ export default class CredentialSerializer extends ApplicationSerializer {
const serialized = super.serialize(...arguments);
// Remove non-ssh_private_key type attributes
delete serialized.attributes.password;
// Remove private_key and/or passphrase from the payload if null
// or empty string. This allows the user to only update
// the username without providing the private key and passphrase again.
// Users cannot unset the private key or passphrase, they would have
// to delete the credential entirely.
if (!serialized?.attributes?.passphrase)
delete serialized.attributes.passphrase;
if (!serialized?.attributes?.private_key)
@@ -33,6 +33,34 @@ module('Unit | Serializer | credential', function (hooks) {
});
});
test('it serializes username_password type correctly when only the username is updated', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential');
const record = store.createRecord('credential', {
password: '',
username: 'user',
credential_store_id: 'csst_i7p1eu0Nw8',
type: 'username_password',
name: 'Name',
description: 'Description',
version: 1,
});
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
attributes: {
username: 'user',
},
credential_store_id: 'csst_i7p1eu0Nw8',
type: 'username_password',
name: 'Name',
description: 'Description',
version: 1,
});
});
test('it serializes ssh_private_key type correctly on create', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
@@ -64,6 +92,35 @@ module('Unit | Serializer | credential', function (hooks) {
});
});
test('it serializes ssh_private_key type correctly when only the username is updated', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential');
const record = store.createRecord('credential', {
passphrase: '',
private_key: '',
username: 'user',
credential_store_id: 'csst_i7p1eu0Nw8',
type: 'ssh_private_key',
name: 'Name',
description: 'Description',
version: 1,
});
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
attributes: {
username: 'user',
},
credential_store_id: 'csst_i7p1eu0Nw8',
type: 'ssh_private_key',
name: 'Name',
description: 'Description',
version: 1,
});
});
test('it normalizes "username_password" type credential record', async function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');