From e733c78ff6cff73e598cf42de4d9612538a6142a Mon Sep 17 00:00:00 2001 From: Jordan Reimer Date: Mon, 8 Nov 2021 14:29:00 -0700 Subject: [PATCH] Secrets header version badge (#13015) * updates secret list header to display badge for all versions * adds changelog entry * updates secret list header to only show badge for kv and generic engine types * adds secret-engine mirage factory * adds test helper for pushing serialized mirage data into store and returning ember data models * adds secret engine type version badge display test * updates mirage application serializer to return singular type key --- changelog/13015.txt | 3 ++ ui/app/components/secret-list-header.js | 4 ++ .../components/secret-list-header.hbs | 6 +-- ui/mirage/factories/secret-engine.js | 31 ++++++++++++++ ui/mirage/serializers/application.js | 6 ++- ui/package.json | 1 + ui/tests/helpers/mirage-to-models.js | 12 ++++++ .../components/secret-list-header-test.js | 40 +++++++++++++++++++ ui/yarn.lock | 5 +++ 9 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 changelog/13015.txt create mode 100644 ui/mirage/factories/secret-engine.js create mode 100644 ui/tests/helpers/mirage-to-models.js create mode 100644 ui/tests/integration/components/secret-list-header-test.js diff --git a/changelog/13015.txt b/changelog/13015.txt new file mode 100644 index 000000000..48fd26307 --- /dev/null +++ b/changelog/13015.txt @@ -0,0 +1,3 @@ +```release-note:improvement +ui: Display badge for all versions in secrets engine header +``` \ No newline at end of file diff --git a/ui/app/components/secret-list-header.js b/ui/app/components/secret-list-header.js index 9b2495400..4bb884d46 100644 --- a/ui/app/components/secret-list-header.js +++ b/ui/app/components/secret-list-header.js @@ -8,4 +8,8 @@ export default class SecretListHeader extends Component { backendCrumb = null; model = null; options = null; + + get isKV() { + return ['kv', 'generic'].includes(this.args.model.engineType); + } } diff --git a/ui/app/templates/components/secret-list-header.hbs b/ui/app/templates/components/secret-list-header.hbs index ed8206c12..25aca00c0 100644 --- a/ui/app/templates/components/secret-list-header.hbs +++ b/ui/app/templates/components/secret-list-header.hbs @@ -16,9 +16,9 @@

{{@model.id}} - {{#if (eq @model.options.version 2)}} - - Version 2 + {{#if this.isKV}} + + Version {{or @model.options.version "1"}} {{/if}}

diff --git a/ui/mirage/factories/secret-engine.js b/ui/mirage/factories/secret-engine.js new file mode 100644 index 000000000..5d6e66a8f --- /dev/null +++ b/ui/mirage/factories/secret-engine.js @@ -0,0 +1,31 @@ +import { Factory } from 'ember-cli-mirage'; +import faker from 'faker'; +import { supportedSecretBackends } from 'vault/helpers/supported-secret-backends'; + +export default Factory.extend({ + path: () => faker.system.directoryPath(), + description: () => faker.git.commitMessage(), + local: () => faker.datatype.boolean(), + sealWrap: () => faker.datatype.boolean(), + // set in afterCreate + accessor: null, + type: null, + options: null, + + afterCreate(secretEngine) { + if (!secretEngine.type) { + const type = faker.random.arrayElement(supportedSecretBackends()); + secretEngine.type = type; + + if (!secretEngine.accessor) { + secretEngine.accessor = `type_${faker.git.shortSha()}`; + } + } + + if (!secretEngine.options && ['generic', 'kv'].includes(secretEngine.type)) { + secretEngine.options = { + version: faker.random.arrayElement('1', '2'), + }; + } + }, +}); diff --git a/ui/mirage/serializers/application.js b/ui/mirage/serializers/application.js index 6224ab8c9..42feb0e47 100644 --- a/ui/mirage/serializers/application.js +++ b/ui/mirage/serializers/application.js @@ -1,3 +1,7 @@ import { JSONAPISerializer } from 'ember-cli-mirage'; -export default JSONAPISerializer.extend({}); +export default JSONAPISerializer.extend({ + typeKeyForModel(model) { + return model.modelName; + }, +}); diff --git a/ui/package.json b/ui/package.json index 77540a268..3bf9eeedb 100644 --- a/ui/package.json +++ b/ui/package.json @@ -208,6 +208,7 @@ ] }, "dependencies": { + "faker": "^5.5.3", "handlebars": "^4.3.0", "highlight.js": "^10.4.1", "jquery": "^3.5.0", diff --git a/ui/tests/helpers/mirage-to-models.js b/ui/tests/helpers/mirage-to-models.js new file mode 100644 index 000000000..1584a25d3 --- /dev/null +++ b/ui/tests/helpers/mirage-to-models.js @@ -0,0 +1,12 @@ +import { getContext } from '@ember/test-helpers'; + +export default data => { + const context = getContext(); + const store = context.owner.lookup('service:store'); + const modelName = Array.isArray(data) ? data[0].modelName : data.modelName; + const json = context.server.serializerOrRegistry.serialize(data); + store.push(json); + return Array.isArray(data) + ? data.map(({ id }) => store.peekRecord(modelName, id)) + : store.peekRecord(modelName, data.id); +}; diff --git a/ui/tests/integration/components/secret-list-header-test.js b/ui/tests/integration/components/secret-list-header-test.js new file mode 100644 index 000000000..b07385705 --- /dev/null +++ b/ui/tests/integration/components/secret-list-header-test.js @@ -0,0 +1,40 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'ember-qunit'; +import { render } from '@ember/test-helpers'; +import hbs from 'htmlbars-inline-precompile'; +import { supportedSecretBackends } from 'vault/helpers/supported-secret-backends'; +import { setupMirage } from 'ember-cli-mirage/test-support'; +import mirageToModels from 'vault/tests/helpers/mirage-to-models'; + +module('Integration | Component | secret-list-header', function(hooks) { + setupRenderingTest(hooks); + setupMirage(hooks); + + test('it should render version badge for kv and generic engine types', async function(assert) { + const backends = supportedSecretBackends(); + assert.expect(backends.length); + + this.server.post('/sys/capabilities-self', () => {}); + + for (let type of backends) { + const data = this.server.create('secret-engine', 2, { type }); + this.model = mirageToModels(data); + await render(hbs` + + `); + const selector = '[data-test-kv-version-badge]'; + if (['kv', 'generic'].includes(type)) { + assert + .dom(selector) + .hasText( + `Version ${this.model.options.version}`, + `Badge renders with correct version for ${type} engine type` + ); + } else { + assert.dom(selector).doesNotExist(`Version badge does not render for ${type} engine type`); + } + } + }); +}); diff --git a/ui/yarn.lock b/ui/yarn.lock index 4f3d34f25..d986109be 100644 --- a/ui/yarn.lock +++ b/ui/yarn.lock @@ -9620,6 +9620,11 @@ fake-xml-http-request@^2.1.1: resolved "https://registry.yarnpkg.com/fake-xml-http-request/-/fake-xml-http-request-2.1.2.tgz#f1786720cae50bbb46273035a0173414f3e85e74" integrity sha512-HaFMBi7r+oEC9iJNpc3bvcW7Z7iLmM26hPDmlb0mFwyANSsOQAtJxbdWsXITKOzZUyMYK0zYCv3h5yDj9TsiXg== +faker@^5.5.3: + version "5.5.3" + resolved "https://registry.yarnpkg.com/faker/-/faker-5.5.3.tgz#c57974ee484431b25205c2c8dc09fda861e51e0e" + integrity sha512-wLTv2a28wjUyWkbnX7u/ABZBkUkIF2fCd73V6P2oFqEGEktDfzWx4UxrSqtPRw0xPRAcjeAOIiJWqZm3pP4u3g== + fast-deep-equal@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz#7b05218ddf9667bf7f370bf7fdb2cb15fdd0aa49"