From 6c3e5ba4a8f11d1210d1f8e63b3d868416fe9854 Mon Sep 17 00:00:00 2001 From: Dharini Jeeva Date: Fri, 15 Jul 2022 13:24:04 -0700 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20add=20static=20credenti?= =?UTF-8?q?al=20store=20support=20(#1191)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: 🤖 add static credential store support to the API layer --- .../api/addon/serializers/credential-store.js | 19 ++++- .../tests/unit/adapters/application-test.js | 6 +- .../unit/models/credential-store-test.js | 29 +++++++- addons/api/tests/unit/models/group-test.js | 6 +- addons/api/tests/unit/models/target-test.js | 6 +- .../unit/serializers/credential-store-test.js | 71 +++++++++++++++++-- 6 files changed, 125 insertions(+), 12 deletions(-) diff --git a/addons/api/addon/serializers/credential-store.js b/addons/api/addon/serializers/credential-store.js index ee9a2e97d..dd65d40fe 100644 --- a/addons/api/addon/serializers/credential-store.js +++ b/addons/api/addon/serializers/credential-store.js @@ -6,7 +6,17 @@ export default class CredentialStoreSerializer extends ApplicationSerializer { * @method serialize * @param {Snapshot} snapshot */ - serialize() { + serialize(snapshot) { + switch (snapshot.record.type) { + case 'vault': + return this.serializeVault(...arguments); + case 'static': + default: + return this.serializeStatic(...arguments); + } + } + + serializeVault() { const serialized = super.serialize(...arguments); if (serialized.attributes) { // Token cannot be unset. If it's falsy, it must be removed. @@ -21,4 +31,11 @@ export default class CredentialStoreSerializer extends ApplicationSerializer { } return serialized; } + + serializeStatic() { + const serialized = super.serialize(...arguments); + // Delete attributes for static cred store + delete serialized.attributes; + return serialized; + } } diff --git a/addons/api/tests/unit/adapters/application-test.js b/addons/api/tests/unit/adapters/application-test.js index 129c3d9c5..7d995ae3e 100644 --- a/addons/api/tests/unit/adapters/application-test.js +++ b/addons/api/tests/unit/adapters/application-test.js @@ -101,7 +101,11 @@ module('Unit | Adapter | application', function (hooks) { assert.expect(1); const store = this.owner.lookup('service:store'); this.server.get('/v1/groups', (_, { queryParams: { scope_id } }) => { - assert.strictEqual(scope_id, 'p_456', 'Scoped resource URL was requested.'); + assert.strictEqual( + scope_id, + 'p_456', + 'Scoped resource URL was requested.' + ); return { items: [] }; }); await store.query('group', { scope_id: 'p_456' }); diff --git a/addons/api/tests/unit/models/credential-store-test.js b/addons/api/tests/unit/models/credential-store-test.js index 6275d3454..504184e35 100644 --- a/addons/api/tests/unit/models/credential-store-test.js +++ b/addons/api/tests/unit/models/credential-store-test.js @@ -4,10 +4,37 @@ import { setupTest } from 'ember-qunit'; module('Unit | Model | credential store', function (hooks) { setupTest(hooks); - // Replace this with your real tests. test('it exists', function (assert) { let store = this.owner.lookup('service:store'); let model = store.createRecord('credential-store', {}); assert.ok(model); }); + + test('it has a `isStatic` computed property and returns the expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelStatic = store.createRecord('credential-store', { + type: 'static', + }); + const modelVault = store.createRecord('credential-store', { + type: 'vault', + }); + assert.strictEqual(typeof modelStatic.isStatic, 'boolean'); + assert.true(modelStatic.isStatic); + assert.false(modelVault.isStatic); + }); + + test('it has a `isVault` computed property and returns the expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelVault = store.createRecord('credential-store', { + type: 'vault', + }); + const modelStatic = store.createRecord('credential-store', { + type: 'static', + }); + assert.strictEqual(typeof modelVault.isVault, 'boolean'); + assert.true(modelVault.isVault); + assert.false(modelStatic.isVault); + }); }); diff --git a/addons/api/tests/unit/models/group-test.js b/addons/api/tests/unit/models/group-test.js index eb22de3f4..9d3e582a1 100644 --- a/addons/api/tests/unit/models/group-test.js +++ b/addons/api/tests/unit/models/group-test.js @@ -50,7 +50,11 @@ module('Unit | Model | group', function (hooks) { 2, 'Group has two entires in member_ids' ); - assert.strictEqual(group.members.length, 2, 'Group has two resolved members'); + assert.strictEqual( + group.members.length, + 2, + 'Group has two resolved members' + ); }); test('it has an `addMembers` method that groups a specific POST API endpoint and serialization', async function (assert) { diff --git a/addons/api/tests/unit/models/target-test.js b/addons/api/tests/unit/models/target-test.js index 2f0cb33ac..fc5b7c489 100644 --- a/addons/api/tests/unit/models/target-test.js +++ b/addons/api/tests/unit/models/target-test.js @@ -55,7 +55,11 @@ module('Unit | Model | target', function (hooks) { 2, 'Target has two entires in host_sources' ); - assert.strictEqual(target.hostSets.length, 2, 'Target has two resolved hostSets'); + assert.strictEqual( + target.hostSets.length, + 2, + 'Target has two resolved hostSets' + ); assert.notOk( target.hostSets[0].hostCatalog, 'Host catalog was not resolved because it is not loaded yet' diff --git a/addons/api/tests/unit/serializers/credential-store-test.js b/addons/api/tests/unit/serializers/credential-store-test.js index fd2415ff2..205f5d388 100644 --- a/addons/api/tests/unit/serializers/credential-store-test.js +++ b/addons/api/tests/unit/serializers/credential-store-test.js @@ -87,7 +87,7 @@ module('Unit | Serializer | credential store', function (hooks) { }); }); - test('it serializes empty strings to null', function (assert) { + test('it serializes empty strings to null for vault-type credential store', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const record = store.createRecord('credential-store', { @@ -98,6 +98,7 @@ module('Unit | Serializer | credential store', function (hooks) { namespace: '', tls_server_name: '', tls_skip_verify: false, + type: 'vault', }); let serializedRecord = record.serialize(); assert.deepEqual(serializedRecord, { @@ -112,11 +113,11 @@ module('Unit | Serializer | credential store', function (hooks) { }, description: null, name: null, - type: null, + type: 'vault', }); }); - test('it does not serialize token if present but falsy', function (assert) { + test('it does not serialize token if present but falsy for vault-type credential store', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const record = store.createRecord('credential-store', { @@ -128,6 +129,7 @@ module('Unit | Serializer | credential store', function (hooks) { namespace: 'foobar', tls_server_name: 'yes', tls_skip_verify: false, + type: 'vault', }); let serializedRecord = record.serialize(); assert.deepEqual( @@ -144,13 +146,13 @@ module('Unit | Serializer | credential store', function (hooks) { }, description: null, name: null, - type: null, + type: 'vault', }, 'token attribute is not expected' ); }); - test('it does not serialize client certificate key if present but falsy when client certificate is present', function (assert) { + test('it does not serialize client certificate key if present but falsy when client certificate is present for vault-type credential store', function (assert) { assert.expect(2); const store = this.owner.lookup('service:store'); const record = store.createRecord('credential-store', { @@ -162,6 +164,7 @@ module('Unit | Serializer | credential store', function (hooks) { namespace: 'foobar', tls_server_name: 'yes', tls_skip_verify: false, + type: 'vault', }); let serializedRecord = record.serialize(); assert.deepEqual( @@ -178,7 +181,7 @@ module('Unit | Serializer | credential store', function (hooks) { }, description: null, name: null, - type: null, + type: 'vault', }, 'client certificate key attribute is not expected' ); @@ -200,9 +203,63 @@ module('Unit | Serializer | credential store', function (hooks) { }, description: null, name: null, - type: null, + type: 'vault', }, 'client certificate key attribute is expected' ); }); + + test('it serializes empty strings to null for static-type credential store', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const record = store.createRecord('credential-store', { + name: '', + description: '', + type: 'static', + }); + let serializedRecord = record.serialize(); + assert.deepEqual(serializedRecord, { + description: null, + name: null, + type: 'static', + }); + }); + + test('it serializes static-type correctly on create', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const record = store.createRecord('credential-store', { + type: 'static', + name: 'Static cred store', + description: 'Description', + }); + assert.deepEqual(record.serialize(), { + type: 'static', + name: 'Static cred store', + description: 'Description', + }); + }); + + test('it serializes static-type correctly on update', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + store.push({ + data: { + id: '2', + type: 'credential-store', + attributes: { + type: 'static', + name: 'Static update', + description: 'Description', + }, + }, + }); + const record = store.peekRecord('credential-store', '2'); + const expectedResult = { + type: 'static', + name: 'Static update', + description: 'Description', + }; + assert.deepEqual(record.serialize(), expectedResult); + }); });