Skip to content

Commit

Permalink
[Text Analytics] Tests read env vars only after they were read by the…
Browse files Browse the repository at this point in the history
… recorder (#12991)

Use the API Key from the corresponding environment var only after it was read by the recorder. 
Also does the following:
- split `createRecordedClient` into `createRecorder` and `createClient` with better arguments.
- get rid of `testEnv`.
- use `createClient` everywhere.

Fixes #12990 and part of #12819.
  • Loading branch information
deyaaeldeen authored Dec 22, 2020
1 parent 7e2f5e2 commit 7ecf3ca
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 47 deletions.
3 changes: 2 additions & 1 deletion sdk/textanalytics/ai-text-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DetectLanguageResultArray>((resolve) => {
const client = new TextAnalyticsClient(testEnv.ENDPOINT, credential, {
const client = createClient("APIKey", {
httpClient: {
sendRequest: async (request: WebResource): Promise<HttpOperationResponse> => ({
status: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions sdk/textanalytics/ai-text-analytics/test/utils/env.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
6 changes: 6 additions & 0 deletions sdk/textanalytics/ai-text-analytics/test/utils/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as dotenv from "dotenv";

dotenv.config();
61 changes: 32 additions & 29 deletions sdk/textanalytics/ai-text-analytics/test/utils/recordedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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: [
Expand All @@ -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);
}

0 comments on commit 7ecf3ca

Please sign in to comment.