From 821b9b56a7f88a1e89a450ef4dd42347f6ad34e5 Mon Sep 17 00:00:00 2001 From: lisbet-alvarez <107949262+lisbet-alvarez@users.noreply.github.com> Date: Wed, 20 Jul 2022 12:34:18 -0700 Subject: [PATCH] Icu 5230 credential serializer (#1200) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 🎸 Create Credential Model in API ✅ Closes: ICU-5229 * feat: 🎸 update credential model Add attributes to credential model ✅ Closes: ICU-5229 * feat: 🎸 no verify ✅ Closes: ICU-5230 * feat: 🎸 Add credential serializer and tests ✅ Closes: ICU-5230 * feat: 🎸 Add credential serializer Credential serializer along with unit tests were added. ✅ Closes: ICU-5230 * feat: 🎸 Credential serializer & unit tests ✅ Closes: ICU-5230 * fix: 🐛 Resolved commit conflicts ✅ Closes: ICU-5230 * fix: 🐛 deleted password_hmac from inside the serializer ✅ Closes: ICU-5230 Co-authored-by: Cameron Perera --- .../api/addon/generated/models/credential.js | 7 ++ addons/api/addon/serializers/credential.js | 17 +++++ addons/api/app/serializers/credential.js | 1 + addons/api/mirage/config.js | 4 ++ .../tests/unit/serializers/credential-test.js | 64 +++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 addons/api/addon/serializers/credential.js create mode 100644 addons/api/app/serializers/credential.js create mode 100644 addons/api/tests/unit/serializers/credential-test.js diff --git a/addons/api/addon/generated/models/credential.js b/addons/api/addon/generated/models/credential.js index 302e4a6db..4620e5106 100644 --- a/addons/api/addon/generated/models/credential.js +++ b/addons/api/addon/generated/models/credential.js @@ -54,6 +54,13 @@ export default class GeneratedCredentialModel extends BaseModel { }) password_hmac; + @attr('string', { + for: 'username_password', + isNestedAttribute: true, + description: 'The password for credential.', + }) + password; + @attr('string', { for: 'username_password', isNestedAttribute: true, diff --git a/addons/api/addon/serializers/credential.js b/addons/api/addon/serializers/credential.js new file mode 100644 index 000000000..222316f9c --- /dev/null +++ b/addons/api/addon/serializers/credential.js @@ -0,0 +1,17 @@ +import ApplicationSerializer from './application'; +import { copy } from 'ember-copy'; + +export default class CredentialSerializer extends ApplicationSerializer { + serialize() { + let serialized = super.serialize(...arguments); + delete serialized['attributes']['password_hmac']; + return serialized; + } + + normalize(typeClass, hash, ...rest) { + const normalizedHash = copy(hash, true); + const normalized = super.normalize(typeClass, normalizedHash, ...rest); + normalized['data']['attributes']['password'] = ''; + return normalized; + } +} diff --git a/addons/api/app/serializers/credential.js b/addons/api/app/serializers/credential.js new file mode 100644 index 000000000..2b333ae7e --- /dev/null +++ b/addons/api/app/serializers/credential.js @@ -0,0 +1 @@ +export { default } from 'api/serializers/credential'; diff --git a/addons/api/mirage/config.js b/addons/api/mirage/config.js index e93674b22..0e1dc1d27 100644 --- a/addons/api/mirage/config.js +++ b/addons/api/mirage/config.js @@ -590,6 +590,10 @@ export default function () { this.del('/credential-libraries/:id'); this.patch('/credential-libraries/:id'); + // credentials + + this.get('/credentials/:id'); + // managed-groups this.get( '/managed-groups', diff --git a/addons/api/tests/unit/serializers/credential-test.js b/addons/api/tests/unit/serializers/credential-test.js new file mode 100644 index 000000000..92c29913b --- /dev/null +++ b/addons/api/tests/unit/serializers/credential-test.js @@ -0,0 +1,64 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Serializer | credential', function (hooks) { + setupTest(hooks); + + test('it serializes correctly on create', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('credential'); + const record = store.createRecord('credential', { + password: 'pass', + username: 'user', + credential_store_id: 'cs_i7p1eu0Nw8', + type: 'username_password', + name: 'Name', + description: 'Description', + }); + const snapshot = record._createSnapshot(); + const serializedRecord = serializer.serialize(snapshot); + + assert.deepEqual(serializedRecord, { + attributes: { + password: 'pass', + username: 'user', + }, + credential_store_id: 'cs_i7p1eu0Nw8', + type: 'username_password', + name: 'Name', + description: 'Description', + }); + }); + + test('it normalizes credential record', async function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('credential'); + const credentialModelClass = store.createRecord('credential').constructor; + const payload = { + id: 'cred_123', + version: 1, + type: 'username_password', + attributes: { + username: 'username', + password: '12345678', + }, + }; + const normalized = serializer.normalize(credentialModelClass, payload); + + assert.deepEqual(normalized, { + data: { + attributes: { + password: '', + type: 'username_password', + username: 'username', + version: 1, + }, + id: 'cred_123', + relationships: {}, + type: 'credential', + }, + }); + }); +});