-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
report diagnostics on unsupported auth (#5496)
Fixes #5302 it works like this: ![image](https://github.com/user-attachments/assets/18641e23-cca3-49f1-b2e9-96d95eabc852)
- Loading branch information
1 parent
dd36417
commit 74ec773
Showing
10 changed files
with
146 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/http-client-csharp/emitter/test/Unit/auth.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { TestHost } from "@typespec/compiler/testing"; | ||
import { ok, strictEqual } from "assert"; | ||
import { beforeEach, describe, it } from "vitest"; | ||
import { createModel } from "../../src/lib/client-model-builder.js"; | ||
import { | ||
createEmitterContext, | ||
createEmitterTestHost, | ||
createNetSdkContext, | ||
typeSpecCompile, | ||
} from "./utils/test-util.js"; | ||
|
||
describe("Test auth", () => { | ||
let runner: TestHost; | ||
|
||
beforeEach(async () => { | ||
runner = await createEmitterTestHost(); | ||
}); | ||
|
||
it("cookie header is not supported", async () => { | ||
const program = await typeSpecCompile( | ||
` | ||
op test(): NoContentResponse; | ||
`, | ||
runner, | ||
{ | ||
AuthDecorator: `@useAuth(ApiKeyAuth<ApiKeyLocation.cookie, "api-key-name">)`, | ||
}, | ||
); | ||
const context = createEmitterContext(program); | ||
const sdkContext = await createNetSdkContext(context); | ||
const root = createModel(sdkContext); | ||
const diagnostics = context.program.diagnostics; | ||
|
||
const noAuthDiagnostic = diagnostics.find( | ||
(d) => d.code === "@typespec/http-client-csharp/unsupported-auth", | ||
); | ||
ok(noAuthDiagnostic); | ||
strictEqual( | ||
noAuthDiagnostic.message, | ||
"Only header is supported for ApiKey authentication. cookie is not supported.", | ||
); | ||
strictEqual(root.Auth, undefined); // we do not support it therefore it falls back to undefined | ||
}); | ||
|
||
it("query header is not supported", async () => { | ||
const program = await typeSpecCompile( | ||
` | ||
op test(): NoContentResponse; | ||
`, | ||
runner, | ||
{ | ||
AuthDecorator: `@useAuth(ApiKeyAuth<ApiKeyLocation.query, "api-key-name">)`, | ||
}, | ||
); | ||
const context = createEmitterContext(program); | ||
const sdkContext = await createNetSdkContext(context); | ||
const root = createModel(sdkContext); | ||
const diagnostics = context.program.diagnostics; | ||
|
||
const noAuthDiagnostic = diagnostics.find( | ||
(d) => d.code === "@typespec/http-client-csharp/unsupported-auth", | ||
); | ||
ok(noAuthDiagnostic); | ||
strictEqual( | ||
noAuthDiagnostic.message, | ||
"Only header is supported for ApiKey authentication. query is not supported.", | ||
); | ||
strictEqual(root.Auth, undefined); // we do not support it therefore it falls back to undefined | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,7 +168,8 @@ | |
"$id": "18", | ||
"ApiKey": { | ||
"$id": "19", | ||
"Name": "x-ms-api-key" | ||
"Name": "x-ms-api-key", | ||
"In": "header" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,6 +169,7 @@ | |
"ApiKey": { | ||
"$id": "19", | ||
"Name": "Authorization", | ||
"In": "header", | ||
"Prefix": "SharedAccessKey" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3684,7 +3684,8 @@ | |
"$id": "348", | ||
"ApiKey": { | ||
"$id": "349", | ||
"Name": "my-api-key" | ||
"Name": "my-api-key", | ||
"In": "header" | ||
} | ||
} | ||
} |