From 3d3914b901a155c88a3e5dfae1833bc661972054 Mon Sep 17 00:00:00 2001 From: Timo van Veenendaal Date: Mon, 10 Jan 2022 14:15:27 -0800 Subject: [PATCH] [Unified Recorder] Add sample unified recorder tests to template (#18817) Resolves #18227. --- sdk/template/template/karma.conf.js | 3 + sdk/template/template/package.json | 16 +- ...determined_setting_has_expected_value.json | 96 ++++++------ ...redetermined_setting_has_expected_value.js | 148 ------------------ ...determined_setting_has_expected_value.json | 46 ++++++ .../template/src/configurationClient.ts | 11 +- .../test/public/configurationClient.spec.ts | 56 +++---- 7 files changed, 131 insertions(+), 245 deletions(-) delete mode 100644 sdk/template/template/recordings/node/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.js create mode 100644 sdk/template/template/recordings/node/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.json diff --git a/sdk/template/template/karma.conf.js b/sdk/template/template/karma.conf.js index 906ce5c77075..30b25eb3ebe8 100644 --- a/sdk/template/template/karma.conf.js +++ b/sdk/template/template/karma.conf.js @@ -3,6 +3,8 @@ // https://github.com/karma-runner/karma-chrome-launcher process.env.CHROME_BIN = require("puppeteer").executablePath(); +process.env.RECORDINGS_RELATIVE_PATH = + require("@azure-tools/test-recorder-new").relativeRecordingsPath(); require("dotenv").config(); const { jsonRecordingFilterFunction, @@ -61,6 +63,7 @@ module.exports = function (config) { "AZURE_CLIENT_ID", "AZURE_CLIENT_SECRET", "AZURE_TENANT_ID", + "RECORDINGS_RELATIVE_PATH", ], // test results reporter to use diff --git a/sdk/template/template/package.json b/sdk/template/template/package.json index 588fa1294e6e..cce951297967 100644 --- a/sdk/template/template/package.json +++ b/sdk/template/template/package.json @@ -38,17 +38,17 @@ "extract-api": "tsc -p . && api-extractor run --local", "format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"", "generate:client": "autorest --typescript ./swagger/README.md", - "integration-test:browser": "karma start --single-run", - "integration-test:node": "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 5000000 --full-trace \"dist-esm/test/{,!(browser)/**/}/*.spec.js\"", + "integration-test:browser": "dev-tool run test:browser", + "integration-test:node": "dev-tool run test:node-ts-input -- --timeout 1200000 'test/**/*.spec.ts'", "integration-test": "npm run integration-test:node && npm run integration-test:browser", "lint:fix": "eslint package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]", "lint": "eslint package.json api-extractor.json src test --ext .ts", "pack": "npm pack 2>&1", - "test:browser": "npm run clean && npm run build:test && npm run unit-test:browser && npm run integration-test:browser", - "test:node": "npm run clean && tsc -p . && npm run unit-test:node && npm run integration-test:node", + "test:browser": "npm run clean && npm run build:test && npm run integration-test:browser", + "test:node": "npm run clean && tsc -p . && npm run integration-test:node", "test": "npm run clean && tsc -p . && npm run unit-test:node && rollup -c 2>&1 && npm run unit-test:browser && npm run integration-test", - "unit-test:browser": "karma start --single-run", - "unit-test:node": "mocha -r esm -r ts-node/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace --exclude \"test/**/browser/*.spec.ts\" \"test/**/*.spec.ts\"", + "unit-test:browser": "npm run integration-test:browser", + "unit-test:node": "npm run integration-test:node", "unit-test": "npm run unit-test:node && npm run unit-test:browser" }, "files": [ @@ -121,7 +121,9 @@ "rollup": "^1.16.3", "source-map-support": "^0.5.9", "typescript": "~4.2.0", - "util": "^0.12.1" + "util": "^0.12.1", + "@azure-tools/test-recorder-new": "~1.0.0", + "@azure-tools/test-credential": "~1.0.0" }, "//sampleConfiguration": { "skipFolder": false, diff --git a/sdk/template/template/recordings/browsers/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.json b/sdk/template/template/recordings/browsers/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.json index b1ad21521a3e..f3cf4b7feef0 100644 --- a/sdk/template/template/recordings/browsers/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.json +++ b/sdk/template/template/recordings/browsers/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.json @@ -1,56 +1,54 @@ { - "recordings": [ + "Entries": [ { - "method": "POST", - "url": "https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/oauth2/v2.0/token", - "query": {}, - "requestBody": "response_type=token&grant_type=client_credentials&client_id=azure_client_id&client_secret=azure_client_secret&scope=https%3A%2F%2Fmyappconfig.azconfig.io%2F.default", - "status": 200, - "response": "{\"token_type\":\"Bearer\",\"expires_in\":3599,\"ext_expires_in\":3599,\"access_token\":\"access_token\"}", - "responseHeaders": { - "cache-control": "no-store, no-cache", - "content-length": "1491", - "content-type": "application/json; charset=utf-8", - "date": "Mon, 26 Oct 2020 21:18:44 GMT", - "expires": "-1", - "p3p": "CP=\"DSP CUR OTPi IND OTRi ONL FIN\"", - "pragma": "no-cache", - "referrer-policy": "strict-origin-when-cross-origin", - "strict-transport-security": "max-age=31536000; includeSubDomains", - "x-content-type-options": "nosniff", - "x-ms-ests-server": "2.1.11169.11 - SCUS ProdSlices", - "x-ms-request-id": "a05c2f53-347b-43be-8e75-8adac3e40101" - } - }, - { - "method": "GET", - "url": "https://myappconfig.azconfig.io/kv/test-key", - "query": { - "api-version": "1.0" + "RequestUri": "https://myappconfig.azconfig.io/kv/test-key?api-version=1.0", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/vnd.microsoft.appconfig.kv\u002Bjson, application/json, application/problem\u002Bjson", + "Accept-Encoding": "gzip, deflate, br", + "Accept-Language": "en-US", + "Authorization": "Sanitized", + "Connection": "keep-alive", + "Referer": "http://localhost:9876/", + "sec-ch-ua": "", + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": "", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "same-site", + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/93.0.4577.0 Safari/537.36", + "x-ms-client-request-id": "0a240cc2-fffd-45da-9a4c-fe8cebdff715", + "x-ms-useragent": "azsdk-js-template-template/1.0.11-beta.1 core-http/2.2.3 OS/Linuxx86_64" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "Access-Control-Allow-Credentials": "true", + "Access-Control-Allow-Origin": "*", + "Access-Control-Expose-Headers": "DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Authorization, x-ms-client-request-id, x-ms-useragent, x-ms-content-sha256, x-ms-date, host, Accept, Accept-Datetime, Date, If-Match, If-None-Match, Sync-Token, x-ms-return-client-request-id, ETag, Last-Modified, Link, Memento-Datetime, retry-after-ms, x-ms-request-id, x-ms-client-session-id, x-ms-effective-locale, WWW-Authenticate, traceparent, tracestate", + "Connection": "keep-alive", + "Content-Type": "application/vnd.microsoft.appconfig.kv\u002Bjson; charset=utf-8", + "Date": "Fri, 10 Dec 2021 11:09:32 GMT", + "ETag": "\u0022jHnSsNSkboEWJFD7nnfzHgnZjRA\u0022", + "Last-Modified": "Wed, 01 Dec 2021 02:23:41 GMT", + "Server": "openresty/1.17.8.2", + "Strict-Transport-Security": "max-age=15724800; includeSubDomains", + "Sync-Token": "zAJw6V16=NDoxOSM1MjM0MTM4;sn=5234138", + "Transfer-Encoding": "chunked", + "x-ms-correlation-request-id": "cb5d6844-d7dd-48e7-866f-dcb2331ed70f", + "x-ms-request-id": "cb5d6844-d7dd-48e7-866f-dcb2331ed70f" }, - "requestBody": null, - "status": 200, - "response": "{\"etag\":\"cbARBFLodwGLL3qktvCwmLa1vet\",\"key\":\"test-key\",\"label\":null,\"content_type\":\"\",\"value\":\"test-value\",\"tags\":{},\"locked\":false,\"last_modified\":\"2020-10-16T00:08:12+00:00\"}", - "responseHeaders": { - "access-control-allow-credentials": "true", - "access-control-allow-origin": "*", - "access-control-expose-headers": "DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Authorization, x-ms-client-request-id, x-ms-useragent, x-ms-content-sha256, x-ms-date, host, Accept, Accept-Datetime, Date, If-Match, If-None-Match, Sync-Token, x-ms-return-client-request-id, ETag, Last-Modified, Link, Memento-Datetime, retry-after-ms, x-ms-request-id, WWW-Authenticate", - "content-type": "application/vnd.microsoft.appconfig.kv+json; charset=utf-8", - "date": "Mon, 26 Oct 2020 21:18:45 GMT", - "etag": "\"cbARBFLodwGLL3qktvCwmLa1vet\"", - "last-modified": "Fri, 16 Oct 2020 00:08:12 GMT", - "server": "openresty/1.17.8.2", - "status": "200", - "strict-transport-security": "max-age=15724800; includeSubDomains", - "sync-token": "zAJw6V16=MDozIzUxNTAyNjY=;sn=5150266", - "x-ms-correlation-request-id": "764e6e56-1dac-4c11-94b7-8a45c90d3bb9", - "x-ms-request-id": "764e6e56-1dac-4c11-94b7-8a45c90d3bb9" + "ResponseBody": { + "etag": "jHnSsNSkboEWJFD7nnfzHgnZjRA", + "key": "test-key", + "label": null, + "content_type": null, + "value": "test-value", + "tags": {}, + "locked": false, + "last_modified": "2021-12-01T02:23:41\u002B00:00" } } ], - "uniqueTestInfo": { - "uniqueName": {}, - "newDate": {} - }, - "hash": "dbff5419a75940fa5401dd76cca9de72" + "Variables": {} } diff --git a/sdk/template/template/recordings/node/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.js b/sdk/template/template/recordings/node/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.js deleted file mode 100644 index 75b93d96caa5..000000000000 --- a/sdk/template/template/recordings/node/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.js +++ /dev/null @@ -1,148 +0,0 @@ -let nock = require('nock'); - -module.exports.hash = "284f885078bc2c7cebc8f8ae9be8dc2c"; - -module.exports.testInfo = {"uniqueName":{},"newDate":{}} - -nock('https://login.microsoftonline.com:443', {"encodedQueryParams":true}) - .get('/common/discovery/instance') - .query(true) - .reply(200, {"tenant_discovery_endpoint":"https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/v2.0/.well-known/openid-configuration","api-version":"1.1","metadata":[{"preferred_network":"login.microsoftonline.com","preferred_cache":"login.windows.net","aliases":["login.microsoftonline.com","login.windows.net","login.microsoft.com","sts.windows.net"]},{"preferred_network":"login.partner.microsoftonline.cn","preferred_cache":"login.partner.microsoftonline.cn","aliases":["login.partner.microsoftonline.cn","login.chinacloudapi.cn"]},{"preferred_network":"login.microsoftonline.de","preferred_cache":"login.microsoftonline.de","aliases":["login.microsoftonline.de"]},{"preferred_network":"login.microsoftonline.us","preferred_cache":"login.microsoftonline.us","aliases":["login.microsoftonline.us","login.usgovcloudapi.net"]},{"preferred_network":"login-us.microsoftonline.com","preferred_cache":"login-us.microsoftonline.com","aliases":["login-us.microsoftonline.com"]}]}, [ - 'Cache-Control', - 'max-age=86400, private', - 'Content-Type', - 'application/json; charset=utf-8', - 'Strict-Transport-Security', - 'max-age=31536000; includeSubDomains', - 'X-Content-Type-Options', - 'nosniff', - 'Access-Control-Allow-Origin', - '*', - 'Access-Control-Allow-Methods', - 'GET, OPTIONS', - 'P3P', - 'CP="DSP CUR OTPi IND OTRi ONL FIN"', - 'x-ms-request-id', - '07df832e-67e9-4b00-a217-88c0f83e7f00', - 'x-ms-ests-server', - '2.1.11787.14 - EUS ProdSlices', - 'Set-Cookie', - 'fpc=AklbZDuZ-z9MvbjTqYC1cm8; expires=Mon, 12-Jul-2021 01:38:57 GMT; path=/; secure; HttpOnly; SameSite=None', - 'Set-Cookie', - 'esctx=AQABAAAAAAD--DLA3VO7QrddgJg7WevrYhrtx1chLthdEhWpdZwpobQFLC2yhGY-8rL3wgE7RjM205Ajz4xnCH8x0mUNVi1RcdQhSO79VS7W98F2Mbf6_OqjZmj7TZeFQVHtI7KnhnWFbCVEn0i_FdC6Hgat2UX1trwts4Ys0BRS98pJhrPfcw5swe5GfqxASqvnVdcDYNUgAA; domain=.login.microsoftonline.com; path=/; secure; HttpOnly; SameSite=None', - 'Set-Cookie', - 'x-ms-gateway-slice=estsfd; path=/; secure; samesite=none; httponly', - 'Set-Cookie', - 'stsservicecookie=estsfd; path=/; secure; samesite=none; httponly', - 'Date', - 'Sat, 12 Jun 2021 01:38:56 GMT', - 'Content-Length', - '980' -]); - -nock('https://login.microsoftonline.com:443', {"encodedQueryParams":true}) - .get('/12345678-1234-1234-1234-123456789012/v2.0/.well-known/openid-configuration') - .reply(200, {"token_endpoint":"https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/oauth2/v2.0/token","token_endpoint_auth_methods_supported":["client_secret_post","private_key_jwt","client_secret_basic"],"jwks_uri":"https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/discovery/v2.0/keys","response_modes_supported":["query","fragment","form_post"],"subject_types_supported":["pairwise"],"id_token_signing_alg_values_supported":["RS256"],"response_types_supported":["code","id_token","code id_token","id_token token"],"scopes_supported":["openid","profile","email","offline_access"],"issuer":"https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/v2.0","request_uri_parameter_supported":false,"userinfo_endpoint":"https://graph.microsoft.com/oidc/userinfo","authorization_endpoint":"https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/oauth2/v2.0/authorize","device_authorization_endpoint":"https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/oauth2/v2.0/devicecode","http_logout_supported":true,"frontchannel_logout_supported":true,"end_session_endpoint":"https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/oauth2/v2.0/logout","claims_supported":["sub","iss","cloud_instance_name","cloud_instance_host_name","cloud_graph_host_name","msgraph_host","aud","exp","iat","auth_time","acr","nonce","preferred_username","name","tid","ver","at_hash","c_hash","email"],"kerberos_endpoint":"https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/kerberos","tenant_region_scope":"WW","cloud_instance_name":"microsoftonline.com","cloud_graph_host_name":"graph.windows.net","msgraph_host":"graph.microsoft.com","rbac_url":"https://pas.windows.net"}, [ - 'Cache-Control', - 'max-age=86400, private', - 'Content-Type', - 'application/json; charset=utf-8', - 'Strict-Transport-Security', - 'max-age=31536000; includeSubDomains', - 'X-Content-Type-Options', - 'nosniff', - 'Access-Control-Allow-Origin', - '*', - 'Access-Control-Allow-Methods', - 'GET, OPTIONS', - 'P3P', - 'CP="DSP CUR OTPi IND OTRi ONL FIN"', - 'x-ms-request-id', - 'b9fedfca-792d-4092-a143-9f31d7e52c00', - 'x-ms-ests-server', - '2.1.11829.4 - NCUS ProdSlices', - 'Set-Cookie', - 'fpc=AklbZDuZ-z9MvbjTqYC1cm8; expires=Mon, 12-Jul-2021 01:38:57 GMT; path=/; secure; HttpOnly; SameSite=None', - 'Set-Cookie', - 'esctx=AQABAAAAAAD--DLA3VO7QrddgJg7WevrurErfe2krcodjLBX3bYrJNkeQj7BF8DuCR2-j08XG0ytP_sNsxDKfhz1rhOwUoWCU2wpg9eQyutCZwjrX3KxUzp1ey7l4J7r7tZ3GMvTOB3QV8qgObPsDEUfmuZUucHJCK2qUlt_MHho9PUVhxotbi4w4SyKcAsOfe2O6UnNRDogAA; domain=.login.microsoftonline.com; path=/; secure; HttpOnly; SameSite=None', - 'Set-Cookie', - 'x-ms-gateway-slice=estsfd; path=/; secure; samesite=none; httponly', - 'Set-Cookie', - 'stsservicecookie=estsfd; path=/; secure; samesite=none; httponly', - 'Date', - 'Sat, 12 Jun 2021 01:38:56 GMT', - 'Content-Length', - '1753' -]); - -nock('https://login.microsoftonline.com:443', {"encodedQueryParams":true}) - .filteringRequestBody(function (body) { - return body.replace(/client-request-id=[^&]*/g, "client-request-id=client-request-id"); - }) - .post('/12345678-1234-1234-1234-123456789012/oauth2/v2.0/token', "client_id=azure_client_id&scope=https%3A%2F%2Fmyappconfig.azconfig.io%2F.default%20openid%20profile%20offline_access&grant_type=client_credentials&client-request-id=client-request-id&client_secret=azure_client_secret") - .reply(200, {"token_type":"Bearer","expires_in":86399,"ext_expires_in":86399,"access_token":"access_token"}, [ - 'Cache-Control', - 'no-store, no-cache', - 'Pragma', - 'no-cache', - 'Content-Type', - 'application/json; charset=utf-8', - 'Expires', - '-1', - 'Strict-Transport-Security', - 'max-age=31536000; includeSubDomains', - 'X-Content-Type-Options', - 'nosniff', - 'P3P', - 'CP="DSP CUR OTPi IND OTRi ONL FIN"', - 'x-ms-request-id', - 'ba53f6ce-8702-41dd-8091-90869e8f2c00', - 'x-ms-ests-server', - '2.1.11829.4 - EUS ProdSlices', - 'x-ms-clitelem', - '1,0,0,,', - 'Set-Cookie', - 'fpc=AklbZDuZ-z9MvbjTqYC1cm-WH-CrAQAAADAHVtgOAAAA; expires=Mon, 12-Jul-2021 01:38:57 GMT; path=/; secure; HttpOnly; SameSite=None', - 'Set-Cookie', - 'x-ms-gateway-slice=estsfd; path=/; secure; samesite=none; httponly', - 'Set-Cookie', - 'stsservicecookie=estsfd; path=/; secure; samesite=none; httponly', - 'Date', - 'Sat, 12 Jun 2021 01:38:56 GMT', - 'Content-Length', - '1335' -]); - -nock('https://myappconfig.azconfig.io:443', {"encodedQueryParams":true}) - .get('/kv/test-key') - .query(true) - .reply(200, {"etag":"FQ2XSpKRpSv2FAbNvw2sdY9Hq2G","key":"test-key","label":null,"content_type":"text/plain","value":"test-value","tags":{},"locked":false,"last_modified":"2021-02-16T18:26:35+00:00"}, [ - 'Server', - 'openresty/1.17.8.2', - 'Date', - 'Sat, 12 Jun 2021 01:38:58 GMT', - 'Content-Type', - 'application/vnd.microsoft.appconfig.kv+json; charset=utf-8', - 'Transfer-Encoding', - 'chunked', - 'Connection', - 'keep-alive', - 'Last-Modified', - 'Tue, 16 Feb 2021 18:26:35 GMT', - 'ETag', - '"FQ2XSpKRpSv2FAbNvw2sdY9Hq2G"', - 'Sync-Token', - 'zAJw6V16=MDoxNyM5MjU3MTIx;sn=9257121', - 'x-ms-request-id', - 'd24c5c59-432d-46ba-ad96-598703b18e87', - 'x-ms-correlation-request-id', - 'd24c5c59-432d-46ba-ad96-598703b18e87', - 'Access-Control-Allow-Origin', - '*', - 'Access-Control-Allow-Credentials', - 'true', - 'Access-Control-Expose-Headers', - 'DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Authorization, x-ms-client-request-id, x-ms-useragent, x-ms-content-sha256, x-ms-date, host, Accept, Accept-Datetime, Date, If-Match, If-None-Match, Sync-Token, x-ms-return-client-request-id, ETag, Last-Modified, Link, Memento-Datetime, retry-after-ms, x-ms-request-id, x-ms-client-session-id, x-ms-effective-locale, WWW-Authenticate', - 'Strict-Transport-Security', - 'max-age=15724800; includeSubDomains' -]); diff --git a/sdk/template/template/recordings/node/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.json b/sdk/template/template/recordings/node/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.json new file mode 100644 index 000000000000..deeaf1112752 --- /dev/null +++ b/sdk/template/template/recordings/node/aad_configurationclient_functional_tests/recording_predetermined_setting_has_expected_value.json @@ -0,0 +1,46 @@ +{ + "Entries": [ + { + "RequestUri": "https://myappconfig.azconfig.io/kv/test-key?api-version=1.0", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/vnd.microsoft.appconfig.kv\u002Bjson, application/json, application/problem\u002Bjson", + "Accept-Encoding": "gzip,deflate", + "Authorization": "Sanitized", + "Connection": "keep-alive", + "Cookie": "", + "User-Agent": "azsdk-js-template-template/1.0.11-beta.1 core-http/2.2.3 Node/v16.13.0 OS/(x64-Linux-5.10.60.1-microsoft-standard-WSL2)", + "x-ms-client-request-id": "f6f68ce8-5b02-461d-bdf1-2af09b79624a" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "Access-Control-Allow-Credentials": "true", + "Access-Control-Allow-Origin": "*", + "Access-Control-Expose-Headers": "DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Authorization, x-ms-client-request-id, x-ms-useragent, x-ms-content-sha256, x-ms-date, host, Accept, Accept-Datetime, Date, If-Match, If-None-Match, Sync-Token, x-ms-return-client-request-id, ETag, Last-Modified, Link, Memento-Datetime, retry-after-ms, x-ms-request-id, x-ms-client-session-id, x-ms-effective-locale, WWW-Authenticate, traceparent, tracestate", + "Connection": "keep-alive", + "Content-Type": "application/vnd.microsoft.appconfig.kv\u002Bjson; charset=utf-8", + "Date": "Thu, 09 Dec 2021 01:06:27 GMT", + "ETag": "\u0022jRXE6grlhA98vbDBE98P9JDaMPm\u0022", + "Last-Modified": "Thu, 09 Dec 2021 01:05:58 GMT", + "Server": "openresty/1.17.8.2", + "Strict-Transport-Security": "max-age=15724800; includeSubDomains", + "Sync-Token": "zAJw6V16=NToxOSM4NDcxODQ4;sn=8471848", + "Transfer-Encoding": "chunked", + "x-ms-correlation-request-id": "5625011c-719c-4adc-ab86-a0b59fda97dd", + "x-ms-request-id": "5625011c-719c-4adc-ab86-a0b59fda97dd" + }, + "ResponseBody": { + "etag": "jRXE6grlhA98vbDBE98P9JDaMPm", + "key": "test-key", + "label": null, + "content_type": "", + "value": "test-value", + "tags": {}, + "locked": false, + "last_modified": "2021-12-09T01:05:58\u002B00:00" + } + } + ], + "Variables": {} +} diff --git a/sdk/template/template/src/configurationClient.ts b/sdk/template/template/src/configurationClient.ts index 4f72149e5684..0a8f3ca1ca9d 100644 --- a/sdk/template/template/src/configurationClient.ts +++ b/sdk/template/template/src/configurationClient.ts @@ -1,20 +1,19 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +import { ConfigurationSetting, GeneratedClient } from "./generated"; import { + InternalPipelineOptions, + OperationOptions, PipelineOptions, TokenCredential, - OperationOptions, bearerTokenAuthenticationPolicy, createPipelineFromOptions, - InternalPipelineOptions, } from "@azure/core-http"; -import { SpanStatusCode } from "@azure/core-tracing"; - import { SDK_VERSION } from "./constants"; -import { logger } from "./logger"; -import { ConfigurationSetting, GeneratedClient } from "./generated"; +import { SpanStatusCode } from "@azure/core-tracing"; import { createSpan } from "./tracing"; +import { logger } from "./logger"; import { quoteETag } from "./util"; // re-export generated types that are used as public interfaces. diff --git a/sdk/template/template/test/public/configurationClient.spec.ts b/sdk/template/template/test/public/configurationClient.spec.ts index 6900d3b0a5fb..7f57f2f161b9 100644 --- a/sdk/template/template/test/public/configurationClient.spec.ts +++ b/sdk/template/template/test/public/configurationClient.spec.ts @@ -6,8 +6,9 @@ import { Context } from "mocha"; import { ConfigurationClient } from "../../src"; -import { ClientSecretCredential } from "@azure/identity"; -import { env, record, Recorder } from "@azure-tools/test-recorder"; +import { env } from "@azure-tools/test-recorder"; +import { Recorder } from "@azure-tools/test-recorder-new"; +import { createTestCredential } from "@azure-tools/test-credential"; // When the recorder observes the values of these environment variables in any // recorded HTTP request or response, it will replace them with the values they @@ -21,23 +22,24 @@ const replaceableVariables: Record = { AZURE_CLIENT_SECRET: "azure_client_secret", }; -function createConfigurationClient(): ConfigurationClient { +function createConfigurationClient(recorder: Recorder): ConfigurationClient { // Retrieve the endpoint from the environment variable // we saved to the .env file earlier const endpoint = env.APPCONFIG_ENDPOINT; - // We use ClientSecretCredential instead of DefaultAzureCredential in order - // to ensure that the requests made to the AAD server are always the same. If - // we used DefaultAzureCredential, they might be different on some machines - // than on others, depending on which credentials are available (such as - // Managed Identity or developer credentials). - const credential = new ClientSecretCredential( - env.AZURE_TENANT_ID, - env.AZURE_CLIENT_ID, - env.AZURE_CLIENT_SECRET + // We use the createTestCredential helper from the test-credential tools package. + // This function returns the special NoOpCredential in playback mode, which + // is a special TokenCredential implementation that does not make any requests + // to AAD. + + // recorder.configureClientOptionsCoreV1 mixes in the necessary options that + // need to be passed to the corev1 ConfigurationClient for the recorder to + // capture requests. + return new ConfigurationClient( + endpoint, + createTestCredential(), + recorder.configureClientOptionsCoreV1({}) ); - - return new ConfigurationClient(endpoint, credential); } // You want to give the test suite a descriptive name. Here, I add [AAD] to @@ -52,33 +54,17 @@ describe("[AAD] ConfigurationClient functional tests", function () { // NOTE: use of "function" and not ES6 arrow-style functions with the // beforeEach hook is IMPORTANT due to the use of `this` in the function // body. - beforeEach(function (this: Context) { + beforeEach(async function (this: Context) { // The recorder has some convenience methods, and we need to store a // reference to it so that we can `stop()` the recorder later in the // `afterEach` hook. - recorder = record(this, { - // == Recorder Environment Setup == Add the replaceable variables from - // above - replaceableVariables, - - // We don't use this in the template, but if we had any query parameters - // we wished to discard, we could add them here - queryParametersToSkip: [], - - // Finally, we need to remove the AAD `access_token` from any requests. - // This is very important, as it cannot be removed using environment - // variable or query parameter replacement. The - // `customizationsOnRecordings` field allows us to make arbitrary - // replacements within recordings. - customizationsOnRecordings: [ - (recording: any): any => - recording.replace(/"access_token":"[^"]*"/g, `"access_token":"access_token"`), - ], - }); + recorder = new Recorder(this.currentTest); + + await recorder.start({ envSetupForPlayback: replaceableVariables }); // We'll be able to refer to the instantiated `client` in tests, since we // initialize it before each test - client = createConfigurationClient(); + client = createConfigurationClient(recorder); }); // After each test, we need to stop the recording.