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');