From 9a10b68bbe06267b4c04d9cc60d3d75731738041 Mon Sep 17 00:00:00 2001 From: Cameron Perera Date: Thu, 25 Aug 2022 17:02:48 -0500 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20update=20credential?= =?UTF-8?q?=20serializer=20to=20delete=20password=20(#1277)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- addons/api/addon/serializers/credential.js | 12 ++++ .../tests/unit/serializers/credential-test.js | 57 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/addons/api/addon/serializers/credential.js b/addons/api/addon/serializers/credential.js index c3e84f577..8a9fcf64a 100644 --- a/addons/api/addon/serializers/credential.js +++ b/addons/api/addon/serializers/credential.js @@ -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) diff --git a/addons/api/tests/unit/serializers/credential-test.js b/addons/api/tests/unit/serializers/credential-test.js index e856add16..87ec047a4 100644 --- a/addons/api/tests/unit/serializers/credential-test.js +++ b/addons/api/tests/unit/serializers/credential-test.js @@ -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');