diff --git a/common/config/rush/pnpm-lock.yaml b/common/config/rush/pnpm-lock.yaml index ba803d4ebe9d..831472ea32b8 100644 --- a/common/config/rush/pnpm-lock.yaml +++ b/common/config/rush/pnpm-lock.yaml @@ -160,6 +160,7 @@ specifiers: '@rush-temp/synapse-monitoring': file:./projects/synapse-monitoring.tgz '@rush-temp/synapse-spark': file:./projects/synapse-spark.tgz '@rush-temp/template': file:./projects/template.tgz + '@rush-temp/test-credential': file:./projects/test-credential.tgz '@rush-temp/test-recorder': file:./projects/test-recorder.tgz '@rush-temp/test-recorder-new': file:./projects/test-recorder-new.tgz '@rush-temp/test-utils': file:./projects/test-utils.tgz @@ -329,6 +330,7 @@ dependencies: '@rush-temp/synapse-monitoring': file:projects/synapse-monitoring.tgz '@rush-temp/synapse-spark': file:projects/synapse-spark.tgz '@rush-temp/template': file:projects/template.tgz + '@rush-temp/test-credential': file:projects/test-credential.tgz '@rush-temp/test-recorder': file:projects/test-recorder.tgz '@rush-temp/test-recorder-new': file:projects/test-recorder-new.tgz '@rush-temp/test-utils': file:projects/test-utils.tgz @@ -470,6 +472,21 @@ packages: - supports-color dev: false + /@azure/arm-storage/17.0.0-beta.1: + resolution: {integrity: sha512-wLDQSo1XZZtPuh9T8OO4CmicnyJoEADuKnNQ9JCAe1KnAqckBiidJ+KmGAxQhg2NFQvtCUj8Xnhe7/MZWthjWA==} + engines: {node: '>=12.0.0'} + dependencies: + '@azure/abort-controller': 1.0.4 + '@azure/core-auth': 1.3.2 + '@azure/core-client': 1.3.2 + '@azure/core-lro': 2.2.1 + '@azure/core-paging': 1.2.0 + '@azure/core-rest-pipeline': 1.3.2 + tslib: 2.3.1 + transitivePeerDependencies: + - supports-color + dev: false + /@azure/arm-storage/30.0.0-beta.2: resolution: {integrity: sha512-KCmM5z3HlWZI6SXxwqoCi8V9Toau99EDSv/3t4zUTGrSv1v4UmuwKemsuvukvaEeOu6Y1gR9jO71snCJolD/yg==} engines: {node: '>=12.0.0'} @@ -1005,6 +1022,15 @@ packages: - supports-color dev: false + /@azure/msal-common/5.1.0: + resolution: {integrity: sha512-4zHZ5Ec7jAgTIWZO3ap1ozgIPGAirF1wL8UhsmPF9QDoZz0cMHdaNmtov5i2+6Xq37YMzhN5s50EFHBuXd7sDQ==} + engines: {node: '>=0.8.0'} + dependencies: + debug: 4.3.2 + transitivePeerDependencies: + - supports-color + dev: false + /@azure/msal-common/5.2.0: resolution: {integrity: sha512-oVc4soy5MEZOp9NvCDqBk57mtiUTJXQQ8Z8S/4UiRQP8RG8snuCFQUs9xxdIfvl2FWIvgiBz+SMByyjTaRX42Q==} engines: {node: '>=0.8.0'} @@ -1055,7 +1081,7 @@ packages: resolution: {integrity: sha512-ZtVCVzr7V4xEeqICa7E9g6BY3noZv96XG11ENuqEiz/PA1OzPD1/x0QF6BPHVldST8wwoevXxPw+t/h3AFII7w==} engines: {node: 10 || 12 || 14 || 16} dependencies: - '@azure/msal-common': 5.2.0 + '@azure/msal-common': 5.1.0 axios: 0.21.4 jsonwebtoken: 8.5.1 uuid: 8.3.2 @@ -1568,6 +1594,7 @@ packages: /@opentelemetry/node/0.22.0_@opentelemetry+api@1.0.3: resolution: {integrity: sha512-+HhGbDruQ7cwejVOIYyxRa28uosnG8W95NiQZ6qE8PXXPsDSyGeftAPbtYpGit0H2f5hrVcMlwmWHeAo9xkSLA==} engines: {node: '>=8.0.0'} + deprecated: Package renamed to @opentelemetry/sdk-trace-node peerDependencies: '@opentelemetry/api': ^1.0.0 dependencies: @@ -8287,6 +8314,7 @@ packages: name: '@rush-temp/arm-batch' version: 0.0.0 dependencies: + '@azure/arm-storage': 17.0.0-beta.1 '@microsoft/api-extractor': 7.18.19 '@rollup/plugin-commonjs': 11.0.2_rollup@1.32.1 '@rollup/plugin-json': 4.1.0_rollup@1.32.1 @@ -10130,9 +10158,11 @@ packages: '@types/chai': 4.2.22 '@types/mocha': 7.0.2 '@types/node': 12.20.37 + '@types/sinon': 9.0.11 chai: 4.3.4 cross-env: 7.0.3 eslint: 7.32.0 + form-data: 4.0.0 inherits: 2.0.4 karma: 6.3.8 karma-chrome-launcher: 3.1.0 @@ -12277,10 +12307,12 @@ packages: '@types/chai': 4.2.22 '@types/mocha': 7.0.2 '@types/node': 12.20.37 + '@types/sinon': 9.0.11 chai: 4.3.4 cross-env: 7.0.3 dotenv: 8.6.0 eslint: 7.32.0 + form-data: 4.0.0 karma: 6.3.8 karma-chrome-launcher: 3.1.0 karma-coverage: 2.0.3 @@ -12302,6 +12334,7 @@ packages: prettier: 2.2.1 rimraf: 3.0.2 rollup: 1.32.1 + sinon: 9.2.4 source-map-support: 0.5.20 tslib: 2.3.1 typedoc: 0.15.2 @@ -13439,6 +13472,24 @@ packages: - utf-8-validate dev: false + file:projects/test-credential.tgz: + resolution: {integrity: sha512-8m+/96tIbosDc/j30zMuSCakBuFiD9IM/TQ/+Z2uOeTUROiN2OXiJEfsjwPeKa3ujlHlzcpqJT/YH6y1yYY06g==, tarball: file:projects/test-credential.tgz} + name: '@rush-temp/test-credential' + version: 0.0.0 + dependencies: + '@types/chai': 4.2.22 + '@types/mocha': 7.0.2 + '@types/node': 12.20.37 + eslint: 7.32.0 + prettier: 1.19.1 + rimraf: 3.0.2 + rollup: 1.32.1 + sinon: 9.2.4 + typescript: 4.2.4 + transitivePeerDependencies: + - supports-color + dev: false + file:projects/test-recorder-new.tgz: resolution: {integrity: sha512-3vm8AuXUmIG1xAGO7frvsnbqa10IyXlFcvMyNlJATlgfqCPCUcKNB7jaq6KjijaC4K86vVRiBJ4HF1DiJ7C2eA==, tarball: file:projects/test-recorder-new.tgz} name: '@rush-temp/test-recorder-new' @@ -13624,7 +13675,7 @@ packages: dev: false file:projects/testing-recorder-new.tgz: - resolution: {integrity: sha512-lwre9kPplykEX6uth2c80ZfTV7v2yxGia/v1zRmCoRDXxOen7psOaU4nNJfU/xp8cjpUK/GEulNpXMFCnNoJww==, tarball: file:projects/testing-recorder-new.tgz} + resolution: {integrity: sha512-OZ560z1sPAZVa6eoSCBtMH/RRLwRtJ9PuJhFRcQv8ldBSPsFFhLgbX2S/04kqc0ShVtDJT7IQ9so01cZvyKMRg==, tarball: file:projects/testing-recorder-new.tgz} name: '@rush-temp/testing-recorder-new' version: 0.0.0 dependencies: diff --git a/rush.json b/rush.json index 0bdf615ba0d5..c3f5f3ce7f82 100644 --- a/rush.json +++ b/rush.json @@ -716,6 +716,11 @@ "projectFolder": "sdk/test-utils/recorder-new", "versionPolicyName": "utility" }, + { + "packageName": "@azure-tools/test-credential", + "projectFolder": "sdk/test-utils/test-credential", + "versionPolicyName": "utility" + }, { "packageName": "@azure/test-utils-perf", "projectFolder": "sdk/test-utils/perf", diff --git a/sdk/test-utils/recorder-new/CHANGELOG.md b/sdk/test-utils/recorder-new/CHANGELOG.md index ed8a3ea3ac6c..ec5e2af3b1ae 100644 --- a/sdk/test-utils/recorder-new/CHANGELOG.md +++ b/sdk/test-utils/recorder-new/CHANGELOG.md @@ -2,6 +2,10 @@ ## 1.0.0 (Unreleased) +## 2021-12-17 + +- Moving `NoOpCredential` to the new `@azure-tools/test-credential` package. + ## 2021-12-15 - Change the API for using variables. Variables can now be accessed using the syntax: diff --git a/sdk/test-utils/recorder-new/src/index.ts b/sdk/test-utils/recorder-new/src/index.ts index bef70fd9c0ea..ed5546e7725b 100644 --- a/sdk/test-utils/recorder-new/src/index.ts +++ b/sdk/test-utils/recorder-new/src/index.ts @@ -5,5 +5,4 @@ export { TestProxyHttpClient } from "./core-v2-recorder"; export { TestProxyHttpClientCoreV1 } from "./core-v1-recorder"; export { relativeRecordingsPath } from "./utils/relativePathCalculator"; export { SanitizerOptions, RecorderStartOptions } from "./utils/utils"; -export { NoOpCredential } from "./recorderAADCredential"; export { env } from "./utils/env"; diff --git a/sdk/test-utils/test-credential/.eslintrc.json b/sdk/test-utils/test-credential/.eslintrc.json new file mode 100644 index 000000000000..47d9fc35be4a --- /dev/null +++ b/sdk/test-utils/test-credential/.eslintrc.json @@ -0,0 +1,4 @@ +{ + "plugins": ["@azure/azure-sdk"], + "extends": ["plugin:@azure/azure-sdk/azure-sdk-base"] +} diff --git a/sdk/test-utils/test-credential/CHANGELOG.md b/sdk/test-utils/test-credential/CHANGELOG.md new file mode 100644 index 000000000000..6f011000cefe --- /dev/null +++ b/sdk/test-utils/test-credential/CHANGELOG.md @@ -0,0 +1,8 @@ +# Release History + +## 1.0.0 (#Unreleased) + +### Features Added + +Provides the credential to be used in the tests. `NoOpCredential` in playback and `ClientSecretCredential` in record/live modes. +[#19423](https://github.com/Azure/azure-sdk-for-js/pull/19423) diff --git a/sdk/test-utils/test-credential/LICENSE b/sdk/test-utils/test-credential/LICENSE new file mode 100644 index 000000000000..2d3163745319 --- /dev/null +++ b/sdk/test-utils/test-credential/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021 Microsoft + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/sdk/test-utils/test-credential/README.md b/sdk/test-utils/test-credential/README.md new file mode 100644 index 000000000000..39884281868e --- /dev/null +++ b/sdk/test-utils/test-credential/README.md @@ -0,0 +1,45 @@ +# Azure Tools - Test Credential + +This library provides a `TokenCredential` implementation for testing Azure SDK packages. + +## Getting started + +### Installation + +Add this library as a dev dependency in your project. + +> rush add -p @azure-tools/test-credential --dev + +## Key concepts + +### Usage +```ts +import { createTestCredential } from "@azure-tools/test-credential"; + +const credential = createTestCredential(); +``` + +This library provides the credential to be used in the tests + +In playback mode + +- returns a `NoOpCredential` (does not make a request to AAD and produces a fake access_token) + +In record/live modes + +- returns a `ClientSecretCredential` from `@azure/identity` (expects AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET in your environment or in the .env file) +- AAD traffic won't be recorded if this credential is used. + +## Next steps + +Try out this package in your application and provide feedback! + +## Troubleshooting + +Log an issue at https://github.com/Azure/azure-sdk-for-js/issues + +## Contributing + +If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code. + +![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Ftest-utils%2Ftest-credential%2FREADME.png) diff --git a/sdk/test-utils/test-credential/package.json b/sdk/test-utils/test-credential/package.json new file mode 100644 index 000000000000..88be49edcd02 --- /dev/null +++ b/sdk/test-utils/test-credential/package.json @@ -0,0 +1,70 @@ +{ + "name": "@azure-tools/test-credential", + "version": "1.0.0", + "sdk-type": "utility", + "description": "Test utilities library that provides the test credential", + "main": "dist/index.js", + "module": "dist-esm/src/index.js", + "types": "./types/latest/src/index.d.ts", + "engines": { + "node": ">=12.0.0" + }, + "scripts": { + "audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit", + "build": "npm run clean && tsc -p . && rollup -c 2>&1", + "build:samples": "echo Skipped.", + "build:test": "echo not needed", + "check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", + "clean": "rimraf dist dist-* types *.tgz *.log", + "format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", + "integration-test:browser": "echo skipped", + "integration-test:node": "echo skipped", + "integration-test": "npm run integration-test:node && npm run integration-test:browser", + "lint:fix": "eslint --no-eslintrc -c ../../.eslintrc.internal.json package.json src test --ext .ts --fix --fix-type [problem,suggestion]", + "lint": "eslint --no-eslintrc -c ../../.eslintrc.internal.json package.json src test --ext .ts", + "pack": "npm pack 2>&1", + "unit-test:browser": "echo skipped", + "unit-test:node": "echo skipped", + "unit-test": "npm run unit-test:node && npm run unit-test:browser", + "test:browser": "npm run clean && npm run build npm run unit-test:browser", + "test:node": "npm run clean && npm run build && npm run unit-test:node", + "test": "npm run clean && npm run build && npm run unit-test" + }, + "files": [ + "dist/", + "dist-esm/src/", + "types/src/index.d.ts", + "README.md", + "LICENSE" + ], + "repository": "github:Azure/azure-sdk-for-js", + "keywords": [ + "Azure", + "cloud", + "test", + "framework" + ], + "author": "Microsoft Corporation", + "license": "MIT", + "bugs": { + "url": "https://github.com/Azure/azure-sdk-for-js/issues" + }, + "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/test-utils/test-credential/README.md", + "sideEffects": false, + "private": true, + "dependencies": { + "@azure/core-auth": "^1.3.2", + "@azure-tools/test-recorder": "^1.0.0", + "@azure/identity": "^2.0.1" + }, + "devDependencies": { + "@azure/dev-tool": "^1.0.0", + "@azure/eslint-plugin-azure-sdk": "^3.0.0", + "@types/node": "^12.0.0", + "eslint": "^7.15.0", + "prettier": "^1.16.4", + "rimraf": "^3.0.0", + "rollup": "^1.16.3", + "typescript": "~4.2.0" + } +} diff --git a/sdk/test-utils/test-credential/rollup.config.js b/sdk/test-utils/test-credential/rollup.config.js new file mode 100644 index 000000000000..5d7deee44c14 --- /dev/null +++ b/sdk/test-utils/test-credential/rollup.config.js @@ -0,0 +1,3 @@ +import { makeConfig } from "@azure/dev-tool/shared-config/rollup"; + +export default makeConfig(require("./package.json")); diff --git a/sdk/test-utils/test-credential/src/index.ts b/sdk/test-utils/test-credential/src/index.ts new file mode 100644 index 000000000000..d5196baeb27f --- /dev/null +++ b/sdk/test-utils/test-credential/src/index.ts @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { ClientSecretCredential, TokenCredentialOptions } from "@azure/identity"; +import { env, isPlaybackMode } from "@azure-tools/test-recorder"; +import { NoOpCredential } from "./noOpCredential"; + +/** + * ## Credential to be used in the tests. + * + * ### In playback mode + * - returns the NoOpCredential (helps bypass the AAD traffic) + * + * ### In record/live modes + * - returns the ClientSecretCredential (expects AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET in your environment or in the .env file) + * - AAD traffic won't be recorded if this credential is used. + */ +export function createTestCredential(options?: TokenCredentialOptions) { + return isPlaybackMode() + ? new NoOpCredential() + : new ClientSecretCredential( + env["AZURE_TENANT_ID"], + env["AZURE_CLIENT_ID"], + env["AZURE_CLIENT_SECRET"], + options + ); +} + +export { NoOpCredential }; diff --git a/sdk/test-utils/recorder-new/src/recorderAADCredential.ts b/sdk/test-utils/test-credential/src/noOpCredential.ts similarity index 100% rename from sdk/test-utils/recorder-new/src/recorderAADCredential.ts rename to sdk/test-utils/test-credential/src/noOpCredential.ts diff --git a/sdk/test-utils/test-credential/test/index.spec.ts b/sdk/test-utils/test-credential/test/index.spec.ts new file mode 100644 index 000000000000..fd2aca680c7b --- /dev/null +++ b/sdk/test-utils/test-credential/test/index.spec.ts @@ -0,0 +1,2 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. diff --git a/sdk/test-utils/test-credential/tsconfig.json b/sdk/test-utils/test-credential/tsconfig.json new file mode 100644 index 000000000000..82e643af7e8c --- /dev/null +++ b/sdk/test-utils/test-credential/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../tsconfig.package", + "compilerOptions": { + "outDir": "./dist-esm", + "declarationDir": "./types/latest" + }, + "include": ["src/**/*.ts", "test/**/*.ts"] +} diff --git a/sdk/test-utils/test-utils/README.md b/sdk/test-utils/test-utils/README.md index 83a4d557f2c2..7f771c1220e8 100644 --- a/sdk/test-utils/test-utils/README.md +++ b/sdk/test-utils/test-utils/README.md @@ -1,6 +1,6 @@ # Azure test-utils client library for JavaScript -The Azure SDk for JavaSCript is composed of a multitude of repositories that attempt to deliver a common, homogenous SDK to make use of all the services that Azure can provide. +The Azure SDK for JavaScript is composed of a multitude of repositories that attempt to deliver a common, homogenous SDK to make use of all the services that Azure can provide. This non-shipping library `@azure/test-utils` attempts to add additional testing support to libraries. It is supposed to be added only as a devDependency and should be used only for the tests of an SDK library. diff --git a/sdk/test-utils/testing-recorder-new/package.json b/sdk/test-utils/testing-recorder-new/package.json index 7ee9b72ccb9d..235069bbb30a 100644 --- a/sdk/test-utils/testing-recorder-new/package.json +++ b/sdk/test-utils/testing-recorder-new/package.json @@ -62,6 +62,7 @@ "@azure/identity": "^2.0.1", "@azure-tools/test-recorder": "^1.0.0", "@azure-tools/test-recorder-new": "^1.0.0", + "@azure-tools/test-credential": "^1.0.0", "@azure/core-auth": "^1.3.2", "@azure/storage-queue": "^12.6.0", "@azure/dev-tool": "^1.0.0", diff --git a/sdk/test-utils/testing-recorder-new/test/noOpCredentialTest.spec.ts b/sdk/test-utils/testing-recorder-new/test/noOpCredentialTest.spec.ts index a853fcd371f8..6930ce4e226b 100644 --- a/sdk/test-utils/testing-recorder-new/test/noOpCredentialTest.spec.ts +++ b/sdk/test-utils/testing-recorder-new/test/noOpCredentialTest.spec.ts @@ -1,14 +1,10 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { - RecorderStartOptions, - NoOpCredential, - TestProxyHttpClient -} from "@azure-tools/test-recorder-new"; -import { env, isPlaybackMode } from "@azure-tools/test-recorder"; +import { RecorderStartOptions, TestProxyHttpClient } from "@azure-tools/test-recorder-new"; +import { env } from "@azure-tools/test-recorder"; +import { createTestCredential } from "@azure-tools/test-credential"; import { TokenCredential } from "@azure/core-auth"; -import { ClientSecretCredential } from "@azure/identity"; import { TableServiceClient } from "@azure/data-tables"; const recorderStartOptions: RecorderStartOptions = { @@ -35,13 +31,7 @@ describe(`NoOp credential with Tables`, () => { beforeEach(async function() { recorder = new TestProxyHttpClient(this.currentTest); await recorder.start(recorderStartOptions); - credential = isPlaybackMode() - ? new NoOpCredential() - : new ClientSecretCredential( - env["AZURE_TENANT_ID"], - env["AZURE_CLIENT_ID"], - env["AZURE_CLIENT_SECRET"] - ); + credential = createTestCredential(); }); afterEach(async function() {