Skip to content

Commit 1abadd1

Browse files
authored
[KeyVault] - Fix user-agent tests in KeyVault Admin (Azure#21189)
### 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.
1 parent 73fc3c1 commit 1abadd1

File tree

10 files changed

+123
-42
lines changed

10 files changed

+123
-42
lines changed

sdk/keyvault/keyvault-admin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"constantPaths": [
7575
{
7676
"path": "src/generated/keyVaultClientContext.ts",
77-
"prefix": "packageVersion"
77+
"prefix": "packageDetails"
7878
},
7979
{
8080
"path": "src/constants.ts",

sdk/keyvault/keyvault-admin/src/generated/keyVaultClientContext.ts

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/keyvault/keyvault-admin/src/tracing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { createTracingClient } from "@azure/core-tracing";
54
import { SDK_VERSION } from "./constants";
5+
import { createTracingClient } from "@azure/core-tracing";
66

77
export const tracingClient = createTracingClient({
88
namespace: "Microsoft.KeyVault",
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4+
import { KeyVaultAccessControlClient, SDK_VERSION } from "../../src";
5+
6+
import { TokenCredential } from "@azure/core-auth";
47
import { assert } from "@azure/test-utils";
5-
import { SDK_VERSION } from "../../src/constants";
6-
import { packageVersion } from "../../src/generated/keyVaultClientContext";
8+
import fs from "fs";
79
import { isNode } from "@azure/core-util";
810
import path from "path";
9-
import fs from "fs";
1011

11-
describe("Key Vault Admin's user agent (only in Node, because of fs)", function () {
12-
beforeEach(function () {
13-
if (!isNode) {
14-
this.skip();
15-
}
16-
});
12+
describe("Key Vault Admin's user agent", function () {
13+
it("SDK_VERSION and user-agent should match", async function () {
14+
let userAgent: string | undefined;
15+
const client = new KeyVaultAccessControlClient(
16+
"https://myvault.vault.azure.net",
17+
{} as TokenCredential,
18+
{
19+
httpClient: {
20+
sendRequest: async (request) => {
21+
userAgent = request.headers.get("user-agent");
22+
throw new Error("only a test");
23+
},
24+
},
25+
}
26+
);
1727

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

2237
it("the version should also match with the one available in the package.json (only in Node, because of fs)", async function () {
38+
if (!isNode) {
39+
this.skip();
40+
}
2341
let version: string;
2442
try {
2543
const fileContents = JSON.parse(
@@ -32,6 +50,6 @@ describe("Key Vault Admin's user agent (only in Node, because of fs)", function
3250
);
3351
version = fileContents.version;
3452
}
35-
assert.equal(version, packageVersion);
53+
assert.equal(version, SDK_VERSION);
3654
});
3755
});

sdk/keyvault/keyvault-certificates/src/generated/keyVaultClientContext.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/keyvault/keyvault-certificates/test/internal/userAgent.spec.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { assert } from "@azure/test-utils";
4+
import { CertificateClient } from "../../src";
5+
import { ClientSecretCredential } from "@azure/identity";
56
import { Context } from "mocha";
67
import { SDK_VERSION } from "../../src/constants";
7-
import { packageVersion } from "../../src/generated/keyVaultClientContext";
8+
import { assert } from "@azure/test-utils";
9+
import { env } from "@azure-tools/test-recorder";
10+
import fs from "fs";
811
import { isNode } from "@azure/core-http";
912
import path from "path";
10-
import fs from "fs";
1113

1214
describe("Certificates client's user agent (only in Node, because of fs)", () => {
13-
it("SDK_VERSION and packageVersion should match", async function () {
14-
assert.equal(SDK_VERSION, packageVersion);
15+
it("SDK_VERSION and user-agent should match", async function () {
16+
let userAgent: string | undefined;
17+
const client = new CertificateClient(
18+
"https://myvault.vault.azure.net",
19+
new ClientSecretCredential(env.AZURE_TENANT_ID, env.AZURE_CLIENT_ID, env.AZURE_CLIENT_SECRET),
20+
{
21+
httpClient: {
22+
sendRequest: async (request) => {
23+
userAgent = request.headers.get("user-agent") ?? request.headers.get("x-ms-useragent");
24+
throw new Error("only a test");
25+
},
26+
},
27+
}
28+
);
29+
30+
try {
31+
await client.getCertificate("foo");
32+
} catch {
33+
// no-op, we don't care about the response, only the user-agent header
34+
}
35+
assert.exists(userAgent, "Expected a User-Agent header to be sent");
36+
assert.include(userAgent!, `azsdk-js-keyvault-certificates/${SDK_VERSION}`);
1537
});
1638

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

sdk/keyvault/keyvault-keys/src/generated/keyVaultClientContext.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { assert } from "@azure/test-utils";
4+
import { isNode } from "@azure/core-http";
5+
56
import { Context } from "mocha";
7+
import { KeyClient } from "../../src";
68
import { SDK_VERSION } from "../../src/constants";
7-
import { packageVersion } from "../../src/generated/keyVaultClientContext";
8-
import { isNode } from "@azure/core-http";
9-
import path from "path";
9+
import { assert } from "@azure/test-utils";
1010
import fs from "fs";
11+
import path from "path";
12+
import { env } from "@azure-tools/test-recorder";
13+
import { ClientSecretCredential } from "@azure/identity";
14+
15+
describe("Keys client's user agent", () => {
16+
it("SDK_VERSION and user-agent should match", async function () {
17+
let userAgent: string | undefined;
18+
const client = new KeyClient(
19+
"https://myvault.vault.azure.net",
20+
new ClientSecretCredential(env.AZURE_TENANT_ID, env.AZURE_CLIENT_ID, env.AZURE_CLIENT_SECRET),
21+
{
22+
httpClient: {
23+
sendRequest: async (request) => {
24+
userAgent = request.headers.get("user-agent") ?? request.headers.get("x-ms-useragent");
25+
throw new Error("only a test");
26+
},
27+
},
28+
}
29+
);
1130

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

1740
it("the version should also match with the one available in the package.json (only in Node, because of fs)", async function (this: Context) {
1841
if (!isNode) {
1942
this.skip();
20-
return;
2143
}
2244
let version: string;
2345
try {
@@ -34,6 +56,6 @@ describe("Keys client's user agent (only in Node, because of fs)", () => {
3456
);
3557
version = fileContents.version;
3658
}
37-
assert.equal(version, packageVersion);
59+
assert.equal(version, SDK_VERSION);
3860
});
3961
});

sdk/keyvault/keyvault-secrets/src/generated/keyVaultClientContext.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/keyvault/keyvault-secrets/test/internal/userAgent.spec.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { assert } from "@azure/test-utils";
4+
import { ClientSecretCredential } from "@azure/identity";
55
import { Context } from "mocha";
66
import { SDK_VERSION } from "../../src/constants";
7-
import { packageVersion } from "../../src/generated/keyVaultClientContext";
7+
import { SecretClient } from "../../src";
8+
import { assert } from "@azure/test-utils";
9+
import { env } from "@azure-tools/test-recorder";
10+
import fs from "fs";
811
import { isNode } from "@azure/core-http";
912
import path from "path";
10-
import fs from "fs";
1113

1214
describe("Secrets client's user agent (only in Node, because of fs)", () => {
13-
it("SDK_VERSION and packageVersion should match", async function () {
14-
assert.equal(SDK_VERSION, packageVersion);
15+
it("SDK_VERSION and user-agent should match", async function () {
16+
let userAgent: string | undefined;
17+
const client = new SecretClient(
18+
"https://myvault.vault.azure.net",
19+
new ClientSecretCredential(env.AZURE_TENANT_ID, env.AZURE_CLIENT_ID, env.AZURE_CLIENT_SECRET),
20+
{
21+
httpClient: {
22+
sendRequest: async (request) => {
23+
userAgent = request.headers.get("user-agent") ?? request.headers.get("x-ms-useragent");
24+
throw new Error("only a test");
25+
},
26+
},
27+
}
28+
);
29+
30+
try {
31+
await client.getSecret("foo");
32+
} catch {
33+
// no-op, we don't care about the response, only the user-agent header
34+
}
35+
assert.exists(userAgent, "Expected a User-Agent header to be sent");
36+
assert.include(userAgent!, `azsdk-js-keyvault-secrets/${SDK_VERSION}`);
1537
});
1638

1739
it("the version should also match with the one available in the package.json (only in Node, because of fs)", async function (this: Context) {
1840
if (!isNode) {
1941
this.skip();
20-
return;
2142
}
2243
let version: string;
2344
try {
@@ -34,6 +55,6 @@ describe("Secrets client's user agent (only in Node, because of fs)", () => {
3455
);
3556
version = fileContents.version;
3657
}
37-
assert.equal(version, packageVersion);
58+
assert.equal(version, SDK_VERSION);
3859
});
3960
});

0 commit comments

Comments
 (0)