diff --git a/sdk/keyvault/keyvault-secrets/rollup.base.config.js b/sdk/keyvault/keyvault-secrets/rollup.base.config.js index 74c0213f36ca..d49ac28e1727 100644 --- a/sdk/keyvault/keyvault-secrets/rollup.base.config.js +++ b/sdk/keyvault/keyvault-secrets/rollup.base.config.js @@ -64,7 +64,7 @@ export function nodeConfig(test = false) { // different output file baseConfig.output.file = "dist-test/index.node.js"; - baseConfig.external.push("assert", "fs", "path"); + baseConfig.external.push("assert", "fs", "path", "chai"); baseConfig.context = "null"; @@ -117,7 +117,8 @@ export function browserConfig(test = false) { json(), cjs({ namedExports: { - assert: ["ok", "equal", "strictEqual", "deepEqual"], + chai: ["assert"], + assert: ["ok", "equal", "strictEqual", "deepEqual", "exists"], "@opentelemetry/api": ["CanonicalCode", "SpanKind", "TraceFlags"] } }) diff --git a/sdk/keyvault/keyvault-secrets/src/transformations.ts b/sdk/keyvault/keyvault-secrets/src/transformations.ts index 970a09872705..73adeef9e75a 100644 --- a/sdk/keyvault/keyvault-secrets/src/transformations.ts +++ b/sdk/keyvault/keyvault-secrets/src/transformations.ts @@ -23,9 +23,13 @@ export function getSecretFromSecretBundle( value: secretBundle.value, name: parsedId.name, properties: { - expiresOn: (attributes as any).expires, - createdOn: (attributes as any).created, - updatedOn: (attributes as any).updated, + expiresOn: attributes?.expires, + createdOn: attributes?.created, + updatedOn: attributes?.updated, + enabled: attributes?.enabled, + notBefore: attributes?.notBefore, + recoverableDays: attributes?.recoverableDays, + recoveryLevel: attributes?.recoveryLevel, id: secretBundle.id, contentType: secretBundle.contentType, diff --git a/sdk/keyvault/keyvault-secrets/test/internal/transformations.spec.ts b/sdk/keyvault/keyvault-secrets/test/internal/transformations.spec.ts new file mode 100644 index 000000000000..de38374c4f1b --- /dev/null +++ b/sdk/keyvault/keyvault-secrets/test/internal/transformations.spec.ts @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { assert } from "chai"; +import { DeletedSecret, KeyVaultSecret } from "../../src"; +import { DeletedSecretBundle, SecretBundle } from "../../src/generated"; +import { getSecretFromSecretBundle } from "../../src/transformations"; + +describe("Transformations", () => { + it("correctly assigns all properties for a secret", () => { + const date = new Date(); + const bundle: SecretBundle = { + attributes: { + created: date, + enabled: true, + expires: date, + notBefore: date, + recoverableDays: 7, + recoveryLevel: "Purgable", + updated: date + }, + contentType: "content_type", + id: "https://azure_keyvault.vault.azure.net/keys/abc123/1", + kid: "test_kid", + managed: true, + tags: { + tag1: "value1", + tag2: "value2" + }, + value: "my secret value" + }; + + const expectedResult: KeyVaultSecret = { + value: bundle.value, + name: "abc123", + properties: { + expiresOn: date, + createdOn: date, + updatedOn: date, + enabled: true, + notBefore: date, + recoverableDays: 7, + recoveryLevel: "Purgable", + id: "https://azure_keyvault.vault.azure.net/keys/abc123/1", + contentType: "content_type", + tags: { + tag1: "value1", + tag2: "value2" + }, + managed: true, + vaultUrl: "https://azure_keyvault.vault.azure.net", + version: "1", + name: "abc123" + } + }; + + const secret: KeyVaultSecret = getSecretFromSecretBundle(bundle); + assert.deepEqual(secret, expectedResult); + }); + + it("correctly assigns all properties for a deleted secret", () => { + const date = new Date(); + const bundle: DeletedSecretBundle = { + id: "https://azure_keyvault.vault.azure.net/keys/abc123/1", + recoveryId: "recovery_id", + scheduledPurgeDate: date, + deletedDate: date + }; + + const secret: DeletedSecret = getSecretFromSecretBundle(bundle); + assert.equal(secret.properties.recoveryId, "recovery_id"); + assert.equal(secret.properties.deletedOn, date); + assert.equal(secret.properties.scheduledPurgeDate, date); + }); +});