From 837323fe56539d3c9bca2c4d1176576a7554398e Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Mon, 14 Oct 2024 12:29:22 +0800 Subject: [PATCH 1/2] specs, fix mockapi in auth scenarios --- .changeset/polite-jeans-mix.md | 6 ++++++ packages/cadl-ranch-api/src/scenarios.ts | 5 ++++- .../cadl-ranch-specs/http/authentication/api-key/mockapi.ts | 6 +++--- .../http/authentication/http/custom/mockapi.ts | 6 +++--- .../cadl-ranch-specs/http/authentication/oauth2/mockapi.ts | 4 ++-- 5 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 .changeset/polite-jeans-mix.md diff --git a/.changeset/polite-jeans-mix.md b/.changeset/polite-jeans-mix.md new file mode 100644 index 000000000..64639c455 --- /dev/null +++ b/.changeset/polite-jeans-mix.md @@ -0,0 +1,6 @@ +--- +"@azure-tools/cadl-ranch-api": minor +"@azure-tools/cadl-ranch-specs": patch +--- + +Fix mockapi in authentication scenarios. diff --git a/packages/cadl-ranch-api/src/scenarios.ts b/packages/cadl-ranch-api/src/scenarios.ts index 872038b55..9b021cf33 100644 --- a/packages/cadl-ranch-api/src/scenarios.ts +++ b/packages/cadl-ranch-api/src/scenarios.ts @@ -27,7 +27,10 @@ export function passOnSuccess( * @param code Status code all endpoint should return * @param apis Endpoint or List of endpoints for this scenario */ -export function passOnCode(code: number, apis: MockApi | readonly MockApi[]): PassOnCodeScenario { +export function passOnCode( + code: number, + apis: MockApi | readonly MockApi[] | MockApiDefinition | readonly MockApiDefinition[], +): PassOnCodeScenario { return { passCondition: "status-code", code, diff --git a/packages/cadl-ranch-specs/http/authentication/api-key/mockapi.ts b/packages/cadl-ranch-specs/http/authentication/api-key/mockapi.ts index 3d1a7fa24..78a254f69 100644 --- a/packages/cadl-ranch-specs/http/authentication/api-key/mockapi.ts +++ b/packages/cadl-ranch-specs/http/authentication/api-key/mockapi.ts @@ -1,4 +1,4 @@ -import { json, MockRequest, passOnSuccess } from "@azure-tools/cadl-ranch-api"; +import { json, MockRequest, passOnCode, passOnSuccess } from "@azure-tools/cadl-ranch-api"; import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api"; export const Scenarios: Record = {}; @@ -21,12 +21,12 @@ Scenarios.Authentication_ApiKey_valid = passOnSuccess({ kind: "MockApiDefinition", }); -Scenarios.Authentication_ApiKey_invalid = passOnSuccess({ +Scenarios.Authentication_ApiKey_invalid = passOnCode(403, { uri: `/authentication/api-key/invalid`, method: `get`, request: { headers: { - "x-ms-api-key": "valid-key", + "x-ms-api-key": "invalid-key", }, status: 403, }, diff --git a/packages/cadl-ranch-specs/http/authentication/http/custom/mockapi.ts b/packages/cadl-ranch-specs/http/authentication/http/custom/mockapi.ts index 3e94f45e5..a90b7122b 100644 --- a/packages/cadl-ranch-specs/http/authentication/http/custom/mockapi.ts +++ b/packages/cadl-ranch-specs/http/authentication/http/custom/mockapi.ts @@ -1,4 +1,4 @@ -import { json, MockRequest, passOnSuccess } from "@azure-tools/cadl-ranch-api"; +import { json, MockRequest, passOnSuccess, passOnCode } from "@azure-tools/cadl-ranch-api"; import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api"; export const Scenarios: Record = {}; @@ -21,12 +21,12 @@ Scenarios.Authentication_Http_Custom_valid = passOnSuccess({ kind: "MockApiDefinition", }); -Scenarios.Authentication_Http_Custom_invalid = passOnSuccess({ +Scenarios.Authentication_Http_Custom_invalid = passOnCode(403, { uri: `/authentication/http/custom/invalid`, method: "get", request: { headers: { - authorization: "SharedAccessKey valid-key", + authorization: "SharedAccessKey invalid-key", }, status: 403, }, diff --git a/packages/cadl-ranch-specs/http/authentication/oauth2/mockapi.ts b/packages/cadl-ranch-specs/http/authentication/oauth2/mockapi.ts index 18a4a5e56..f9a86d2d6 100644 --- a/packages/cadl-ranch-specs/http/authentication/oauth2/mockapi.ts +++ b/packages/cadl-ranch-specs/http/authentication/oauth2/mockapi.ts @@ -1,4 +1,4 @@ -import { json, MockRequest, passOnSuccess } from "@azure-tools/cadl-ranch-api"; +import { json, MockRequest, passOnSuccess, passOnCode } from "@azure-tools/cadl-ranch-api"; import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api"; export const Scenarios: Record = {}; @@ -21,7 +21,7 @@ Scenarios.Authentication_OAuth2_valid = passOnSuccess({ kind: "MockApiDefinition", }); -Scenarios.Authentication_OAuth2_invalid = passOnSuccess({ +Scenarios.Authentication_OAuth2_invalid = passOnCode(403, { uri: `/authentication/oauth2/invalid`, method: "get", request: { From 2f9d5dcec61889940720afb05b05f0be85707fbc Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 16 Oct 2024 10:22:48 +0800 Subject: [PATCH 2/2] fix github action lint error in repo --- packages/cadl-ranch-api/src/scenarios.ts | 6 +----- .../azure/client-generator-core/flatten-property/mockapi.ts | 1 - .../cadl-ranch-specs/http/azure/example/basic/mockapi.ts | 1 - 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/cadl-ranch-api/src/scenarios.ts b/packages/cadl-ranch-api/src/scenarios.ts index 9b021cf33..254061d29 100644 --- a/packages/cadl-ranch-api/src/scenarios.ts +++ b/packages/cadl-ranch-api/src/scenarios.ts @@ -1,7 +1,6 @@ import { KeyedMockApi, KeyedMockApiDefinition, - KeyedMockRequestHandler, MockApi, MockApiDefinition, PassByKeyScenario, @@ -27,10 +26,7 @@ export function passOnSuccess( * @param code Status code all endpoint should return * @param apis Endpoint or List of endpoints for this scenario */ -export function passOnCode( - code: number, - apis: MockApi | readonly MockApi[] | MockApiDefinition | readonly MockApiDefinition[], -): PassOnCodeScenario { +export function passOnCode(code: number, apis: MockApi | readonly MockApi[] | MockApiDefinition): PassOnCodeScenario { return { passCondition: "status-code", code, diff --git a/packages/cadl-ranch-specs/http/azure/client-generator-core/flatten-property/mockapi.ts b/packages/cadl-ranch-specs/http/azure/client-generator-core/flatten-property/mockapi.ts index 6ee94dc50..5991341e7 100644 --- a/packages/cadl-ranch-specs/http/azure/client-generator-core/flatten-property/mockapi.ts +++ b/packages/cadl-ranch-specs/http/azure/client-generator-core/flatten-property/mockapi.ts @@ -1,6 +1,5 @@ import { passOnSuccess, json, MockApiDefinition, MockRequest } from "@azure-tools/cadl-ranch-api"; import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api"; -import { Mock } from "node:test"; export const Scenarios: Record = {}; function createMockApiDefinitions(route: string, request: any, response: any): MockApiDefinition { diff --git a/packages/cadl-ranch-specs/http/azure/example/basic/mockapi.ts b/packages/cadl-ranch-specs/http/azure/example/basic/mockapi.ts index 9d0c965ca..bcfc69538 100644 --- a/packages/cadl-ranch-specs/http/azure/example/basic/mockapi.ts +++ b/packages/cadl-ranch-specs/http/azure/example/basic/mockapi.ts @@ -1,6 +1,5 @@ import { passOnSuccess, json, MockRequest } from "@azure-tools/cadl-ranch-api"; import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api"; -import { Mock } from "node:test"; export const Scenarios: Record = {};