Skip to content

Commit

Permalink
[KeyVault] - Fix user-agent tests in KeyVault Admin (Azure#21189)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR
@azure/keyvault-admin 

### Issues associated with this PR
Resolves Azure#21188

### Describe the problem that is addressed by this PR
`KeyVaultClientContext` is a generated file that in core v1 included a
non-exported constant `packageVersion`. Because we have a test that depends on
that constant we always have to update the file to re-export it (as the
generated code does not export it).

Even worse, in coreV2 that constant is not even generated anymore, so the test
is outdated.

This PR fixes all of these issues by examining what we _actually_ care about,
which is that our user-agent includes the correct package version.

This way anyone can regenerate the code at any time without getting weird build
errors.
  • Loading branch information
maorleger authored Apr 5, 2022
1 parent 73fc3c1 commit 1abadd1
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 42 deletions.
2 changes: 1 addition & 1 deletion sdk/keyvault/keyvault-admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"constantPaths": [
{
"path": "src/generated/keyVaultClientContext.ts",
"prefix": "packageVersion"
"prefix": "packageDetails"
},
{
"path": "src/constants.ts",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/keyvault/keyvault-admin/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { createTracingClient } from "@azure/core-tracing";
import { SDK_VERSION } from "./constants";
import { createTracingClient } from "@azure/core-tracing";

export const tracingClient = createTracingClient({
namespace: "Microsoft.KeyVault",
Expand Down
42 changes: 30 additions & 12 deletions sdk/keyvault/keyvault-admin/test/internal/userAgent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { KeyVaultAccessControlClient, SDK_VERSION } from "../../src";

import { TokenCredential } from "@azure/core-auth";
import { assert } from "@azure/test-utils";
import { SDK_VERSION } from "../../src/constants";
import { packageVersion } from "../../src/generated/keyVaultClientContext";
import fs from "fs";
import { isNode } from "@azure/core-util";
import path from "path";
import fs from "fs";

describe("Key Vault Admin's user agent (only in Node, because of fs)", function () {
beforeEach(function () {
if (!isNode) {
this.skip();
}
});
describe("Key Vault Admin's user agent", function () {
it("SDK_VERSION and user-agent should match", async function () {
let userAgent: string | undefined;
const client = new KeyVaultAccessControlClient(
"https://myvault.vault.azure.net",
{} as TokenCredential,
{
httpClient: {
sendRequest: async (request) => {
userAgent = request.headers.get("user-agent");
throw new Error("only a test");
},
},
}
);

it("SDK_VERSION and packageVersion should match", async function () {
assert.equal(SDK_VERSION, packageVersion);
try {
await client.getRoleAssignment("/", "");
} catch {
// no-op, we don't care about the response, only the user-agent header
}
assert.exists(userAgent, "Expected a User-Agent header to be sent");
assert.include(userAgent!, `azsdk-js-keyvault-admin/${SDK_VERSION}`);
});

it("the version should also match with the one available in the package.json (only in Node, because of fs)", async function () {
if (!isNode) {
this.skip();
}
let version: string;
try {
const fileContents = JSON.parse(
Expand All @@ -32,6 +50,6 @@ describe("Key Vault Admin's user agent (only in Node, because of fs)", function
);
version = fileContents.version;
}
assert.equal(version, packageVersion);
assert.equal(version, SDK_VERSION);
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions sdk/keyvault/keyvault-certificates/test/internal/userAgent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { assert } from "@azure/test-utils";
import { CertificateClient } from "../../src";
import { ClientSecretCredential } from "@azure/identity";
import { Context } from "mocha";
import { SDK_VERSION } from "../../src/constants";
import { packageVersion } from "../../src/generated/keyVaultClientContext";
import { assert } from "@azure/test-utils";
import { env } from "@azure-tools/test-recorder";
import fs from "fs";
import { isNode } from "@azure/core-http";
import path from "path";
import fs from "fs";

describe("Certificates client's user agent (only in Node, because of fs)", () => {
it("SDK_VERSION and packageVersion should match", async function () {
assert.equal(SDK_VERSION, packageVersion);
it("SDK_VERSION and user-agent should match", async function () {
let userAgent: string | undefined;
const client = new CertificateClient(
"https://myvault.vault.azure.net",
new ClientSecretCredential(env.AZURE_TENANT_ID, env.AZURE_CLIENT_ID, env.AZURE_CLIENT_SECRET),
{
httpClient: {
sendRequest: async (request) => {
userAgent = request.headers.get("user-agent") ?? request.headers.get("x-ms-useragent");
throw new Error("only a test");
},
},
}
);

try {
await client.getCertificate("foo");
} catch {
// no-op, we don't care about the response, only the user-agent header
}
assert.exists(userAgent, "Expected a User-Agent header to be sent");
assert.include(userAgent!, `azsdk-js-keyvault-certificates/${SDK_VERSION}`);
});

it("the version should also match with the one available in the package.json (only in Node, because of fs)", async function (this: Context) {
Expand All @@ -34,6 +56,6 @@ describe("Certificates client's user agent (only in Node, because of fs)", () =>
);
version = fileContents.version;
}
assert.equal(version, packageVersion);
assert.equal(version, SDK_VERSION);
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 31 additions & 9 deletions sdk/keyvault/keyvault-keys/test/internal/userAgent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { assert } from "@azure/test-utils";
import { isNode } from "@azure/core-http";

import { Context } from "mocha";
import { KeyClient } from "../../src";
import { SDK_VERSION } from "../../src/constants";
import { packageVersion } from "../../src/generated/keyVaultClientContext";
import { isNode } from "@azure/core-http";
import path from "path";
import { assert } from "@azure/test-utils";
import fs from "fs";
import path from "path";
import { env } from "@azure-tools/test-recorder";
import { ClientSecretCredential } from "@azure/identity";

describe("Keys client's user agent", () => {
it("SDK_VERSION and user-agent should match", async function () {
let userAgent: string | undefined;
const client = new KeyClient(
"https://myvault.vault.azure.net",
new ClientSecretCredential(env.AZURE_TENANT_ID, env.AZURE_CLIENT_ID, env.AZURE_CLIENT_SECRET),
{
httpClient: {
sendRequest: async (request) => {
userAgent = request.headers.get("user-agent") ?? request.headers.get("x-ms-useragent");
throw new Error("only a test");
},
},
}
);

describe("Keys client's user agent (only in Node, because of fs)", () => {
it("SDK_VERSION and packageVersion should match", async function () {
assert.equal(SDK_VERSION, packageVersion);
try {
await client.getKey("foo");
} catch {
// no-op, we don't care about the response, only the user-agent header
}
assert.exists(userAgent, "Expected a User-Agent header to be sent");
assert.include(userAgent!, `azsdk-js-keyvault-keys/${SDK_VERSION}`);
});

it("the version should also match with the one available in the package.json (only in Node, because of fs)", async function (this: Context) {
if (!isNode) {
this.skip();
return;
}
let version: string;
try {
Expand All @@ -34,6 +56,6 @@ describe("Keys client's user agent (only in Node, because of fs)", () => {
);
version = fileContents.version;
}
assert.equal(version, packageVersion);
assert.equal(version, SDK_VERSION);
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 28 additions & 7 deletions sdk/keyvault/keyvault-secrets/test/internal/userAgent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { assert } from "@azure/test-utils";
import { ClientSecretCredential } from "@azure/identity";
import { Context } from "mocha";
import { SDK_VERSION } from "../../src/constants";
import { packageVersion } from "../../src/generated/keyVaultClientContext";
import { SecretClient } from "../../src";
import { assert } from "@azure/test-utils";
import { env } from "@azure-tools/test-recorder";
import fs from "fs";
import { isNode } from "@azure/core-http";
import path from "path";
import fs from "fs";

describe("Secrets client's user agent (only in Node, because of fs)", () => {
it("SDK_VERSION and packageVersion should match", async function () {
assert.equal(SDK_VERSION, packageVersion);
it("SDK_VERSION and user-agent should match", async function () {
let userAgent: string | undefined;
const client = new SecretClient(
"https://myvault.vault.azure.net",
new ClientSecretCredential(env.AZURE_TENANT_ID, env.AZURE_CLIENT_ID, env.AZURE_CLIENT_SECRET),
{
httpClient: {
sendRequest: async (request) => {
userAgent = request.headers.get("user-agent") ?? request.headers.get("x-ms-useragent");
throw new Error("only a test");
},
},
}
);

try {
await client.getSecret("foo");
} catch {
// no-op, we don't care about the response, only the user-agent header
}
assert.exists(userAgent, "Expected a User-Agent header to be sent");
assert.include(userAgent!, `azsdk-js-keyvault-secrets/${SDK_VERSION}`);
});

it("the version should also match with the one available in the package.json (only in Node, because of fs)", async function (this: Context) {
if (!isNode) {
this.skip();
return;
}
let version: string;
try {
Expand All @@ -34,6 +55,6 @@ describe("Secrets client's user agent (only in Node, because of fs)", () => {
);
version = fileContents.version;
}
assert.equal(version, packageVersion);
assert.equal(version, SDK_VERSION);
});
});

0 comments on commit 1abadd1

Please sign in to comment.