forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.test.ts
85 lines (73 loc) · 2.69 KB
/
auth.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Operation } from "@typespec/compiler";
import { BasicTestRunner } from "@typespec/compiler/testing";
import { ok, strictEqual } from "assert";
import { beforeEach, describe, expect, it } from "vitest";
import { getAuthenticationForOperation } from "../src/auth.js";
import { createHttpTestRunner } from "./test-host.js";
let runner: BasicTestRunner;
beforeEach(async () => {
runner = await createHttpTestRunner();
});
describe("per operation authentication", () => {
/** Test function that will expect api key auth only and return the name of the one selected */
async function getTestOperationApiKeyAuthName(code: string) {
const { test } = (await runner.compile(code)) as { test: Operation };
ok(test, "Should have operation called test marked with @test");
const auth = getAuthenticationForOperation(runner.program, test);
const scheme = auth?.options[0].schemes[0];
strictEqual(scheme?.type, "apiKey");
return scheme.name;
}
it("use explicit value on operation", async () => {
const auth = await getTestOperationApiKeyAuthName(`
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "x-for-namespace">)
namespace MyNamespace {
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "x-for-interface">)
interface MyInterface {
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "x-for-op">)
@test op test(): void;
}
}
`);
expect(auth).toEqual("x-for-op");
});
it("go up to interface", async () => {
const auth = await getTestOperationApiKeyAuthName(`
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "x-for-namespace">)
namespace MyNamespace {
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "x-for-interface">)
interface MyInterface {
@test op test(): void;
}
}
`);
expect(auth).toEqual("x-for-interface");
});
it("go up to first namespace", async () => {
const auth = await getTestOperationApiKeyAuthName(`
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "x-for-namespace">)
namespace MyNamespace {
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "x-for-sub-namespace">)
namespace MySubNamespace {
interface MyInterface {
@test op test(): void;
}
}
}
`);
expect(auth).toEqual("x-for-sub-namespace");
});
it("go up to top namespace", async () => {
const auth = await getTestOperationApiKeyAuthName(`
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "x-for-namespace">)
namespace MyNamespace {
namespace MySubNamespace {
interface MyInterface {
@test op test(): void;
}
}
}
`);
expect(auth).toEqual("x-for-namespace");
});
});