diff --git a/sdk/textanalytics/ai-text-analytics/package.json b/sdk/textanalytics/ai-text-analytics/package.json index 5b7ed25b8941..cbd082a19f3b 100644 --- a/sdk/textanalytics/ai-text-analytics/package.json +++ b/sdk/textanalytics/ai-text-analytics/package.json @@ -47,7 +47,8 @@ "skipFolder": true }, "browser": { - "./dist-esm/src/utils/url.js": "./dist-esm/src/utils/url.browser.js" + "./dist-esm/src/utils/url.js": "./dist-esm/src/utils/url.browser.js", + "./dist-esm/test/utils/env.js": "./dist-esm/test/utils/env.browser.js" }, "scripts": { "audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit", diff --git a/sdk/textanalytics/ai-text-analytics/test/public/apiKey.spec.ts b/sdk/textanalytics/ai-text-analytics/test/public/apiKey.spec.ts index f93acb17378e..19d3842845a9 100644 --- a/sdk/textanalytics/ai-text-analytics/test/public/apiKey.spec.ts +++ b/sdk/textanalytics/ai-text-analytics/test/public/apiKey.spec.ts @@ -7,8 +7,8 @@ chaiUse(chaiPromises); import { isLiveMode, isRecordMode, Recorder } from "@azure/test-utils-recorder"; -import { createRecordedClient, testEnv } from "../utils/recordedClient"; -import { TextAnalyticsClient, AzureKeyCredential } from "../../src"; +import { createClient, createRecorder } from "../utils/recordedClient"; +import { TextAnalyticsClient } from "../../src"; import { assertAllSuccess } from "../utils/resultHelper"; const testDataEn = [ @@ -22,14 +22,13 @@ describe("[API Key] TextAnalyticsClient", function() { let recorder: Recorder; let client: TextAnalyticsClient; - const apiKey = new AzureKeyCredential(testEnv.TEXT_ANALYTICS_API_KEY); - // eslint-disable-next-line no-invalid-this this.timeout(100000); beforeEach(function() { // eslint-disable-next-line no-invalid-this - ({ client, recorder } = createRecordedClient(this, apiKey)); + recorder = createRecorder(this); + client = createClient("APIKey"); }); afterEach(async function() { diff --git a/sdk/textanalytics/ai-text-analytics/test/public/pipelineOptions.spec.ts b/sdk/textanalytics/ai-text-analytics/test/public/pipelineOptions.spec.ts index e1ab4542f17c..45a762094ddb 100644 --- a/sdk/textanalytics/ai-text-analytics/test/public/pipelineOptions.spec.ts +++ b/sdk/textanalytics/ai-text-analytics/test/public/pipelineOptions.spec.ts @@ -3,22 +3,16 @@ import { assert } from "chai"; -import { - TextAnalyticsClient, - DetectLanguageResultArray, - DetectLanguageSuccessResult, - AzureKeyCredential -} from "../../src"; -import { testEnv } from "../utils/recordedClient"; +import { DetectLanguageResultArray, DetectLanguageSuccessResult } from "../../src"; +import { createClient } from "../utils/recordedClient"; import { WebResource, HttpOperationResponse, HttpHeaders } from "@azure/core-http"; -describe("TextAnalyticsClient Custom PipelineOptions", function() { - const credential = new AzureKeyCredential(testEnv.TEXT_ANALYTICS_API_KEY); +describe("TextAnalyticsClient Custom PipelineOptions", function() { it("use custom HTTPClient", async () => { const pipelineTester = new Promise((resolve) => { - const client = new TextAnalyticsClient(testEnv.ENDPOINT, credential, { + const client = createClient("APIKey", { httpClient: { sendRequest: async (request: WebResource): Promise => ({ status: 200, diff --git a/sdk/textanalytics/ai-text-analytics/test/public/textAnalyticsClient.spec.ts b/sdk/textanalytics/ai-text-analytics/test/public/textAnalyticsClient.spec.ts index add09c459c5a..749f72319dca 100644 --- a/sdk/textanalytics/ai-text-analytics/test/public/textAnalyticsClient.spec.ts +++ b/sdk/textanalytics/ai-text-analytics/test/public/textAnalyticsClient.spec.ts @@ -7,7 +7,7 @@ import { assert } from "chai"; import { isLiveMode, isRecordMode, Recorder } from "@azure/test-utils-recorder"; -import { createRecordedClient } from "../utils/recordedClient"; +import { createClient, createRecorder } from "../utils/recordedClient"; import { TextAnalyticsClient, TextDocumentInput, @@ -45,7 +45,8 @@ describe("[AAD] TextAnalyticsClient", function() { beforeEach(function() { // eslint-disable-next-line no-invalid-this - ({ client, recorder } = createRecordedClient(this)); + recorder = createRecorder(this); + client = createClient("AAD"); let nextId = 0; getId = () => { nextId += 1; diff --git a/sdk/textanalytics/ai-text-analytics/test/utils/env.browser.ts b/sdk/textanalytics/ai-text-analytics/test/utils/env.browser.ts new file mode 100644 index 000000000000..fd2aca680c7b --- /dev/null +++ b/sdk/textanalytics/ai-text-analytics/test/utils/env.browser.ts @@ -0,0 +1,2 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. diff --git a/sdk/textanalytics/ai-text-analytics/test/utils/env.ts b/sdk/textanalytics/ai-text-analytics/test/utils/env.ts new file mode 100644 index 000000000000..0e06855b73ae --- /dev/null +++ b/sdk/textanalytics/ai-text-analytics/test/utils/env.ts @@ -0,0 +1,6 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import * as dotenv from "dotenv"; + +dotenv.config(); diff --git a/sdk/textanalytics/ai-text-analytics/test/utils/recordedClient.ts b/sdk/textanalytics/ai-text-analytics/test/utils/recordedClient.ts index 0d7d171f328f..8f3a423ae2e8 100644 --- a/sdk/textanalytics/ai-text-analytics/test/utils/recordedClient.ts +++ b/sdk/textanalytics/ai-text-analytics/test/utils/recordedClient.ts @@ -6,12 +6,8 @@ import { Context } from "mocha"; import { env, Recorder, record, RecorderEnvironmentSetup } from "@azure/test-utils-recorder"; import { TokenCredential, ClientSecretCredential } from "@azure/identity"; -import { AzureKeyCredential, TextAnalyticsClient } from "../../src/"; - -export interface RecordedClient { - client: TextAnalyticsClient; - recorder: Recorder; -} +import { AzureKeyCredential, TextAnalyticsClient, TextAnalyticsClientOptions } from "../../src/"; +import "./env"; const replaceableVariables: { [k: string]: string } = { AZURE_CLIENT_ID: "azure_client_id", @@ -23,12 +19,6 @@ const replaceableVariables: { [k: string]: string } = { ENDPOINT: "https://endpoint/" }; -export const testEnv = new Proxy(replaceableVariables, { - get: (target, key: string) => { - return env[key] || target[key]; - } -}); - export const environmentSetup: RecorderEnvironmentSetup = { replaceableVariables, customizationsOnRecordings: [ @@ -46,25 +36,38 @@ export const environmentSetup: RecorderEnvironmentSetup = { queryParametersToSkip: [] }; -export function createRecordedClient( - context: Context, - apiKey?: AzureKeyCredential -): RecordedClient { - const recorder = record(context, environmentSetup); +export type AuthMethod = "APIKey" | "AAD"; +export function createClient( + authMethod: AuthMethod, + options?: TextAnalyticsClientOptions +): TextAnalyticsClient { let credential: AzureKeyCredential | TokenCredential; - if (apiKey !== undefined) { - credential = apiKey; - } else { - credential = new ClientSecretCredential( - testEnv.AZURE_TENANT_ID, - testEnv.AZURE_CLIENT_ID, - testEnv.AZURE_CLIENT_SECRET - ); + switch (authMethod) { + case "APIKey": { + credential = new AzureKeyCredential(env.TEXT_ANALYTICS_API_KEY); + break; + } + case "AAD": { + credential = new ClientSecretCredential( + env.AZURE_TENANT_ID, + env.AZURE_CLIENT_ID, + env.AZURE_CLIENT_SECRET + ); + break; + } + default: { + throw Error(`Unsupported authentication method: ${authMethod}`); + } } + return new TextAnalyticsClient(env.ENDPOINT, credential, options); +} - return { - client: new TextAnalyticsClient(testEnv.ENDPOINT, credential), - recorder - }; +/** + * creates the recorder and reads the environment variables from the `.env` file. + * Should be called first in the test suite to make sure environment variables are + * read before they are being used. + */ +export function createRecorder(context: Context): Recorder { + return record(context, environmentSetup); }