forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.test.ts
146 lines (139 loc) · 3.54 KB
/
security.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { deepStrictEqual } from "assert";
import { describe, it } from "vitest";
import { openApiFor } from "./test-host.js";
describe("openapi3: security", () => {
it("set a basic auth", async () => {
const res = await openApiFor(
`
@service({title: "My service"})
@useAuth(BasicAuth)
namespace MyService {}
`
);
deepStrictEqual(res.components.securitySchemes, {
BasicAuth: {
type: "http",
scheme: "basic",
},
});
deepStrictEqual(res.security, [{ BasicAuth: [] }]);
});
it("set a bearer auth", async () => {
const res = await openApiFor(
`
@service({title: "My service"})
@useAuth(BearerAuth)
namespace MyService {}
`
);
deepStrictEqual(res.components.securitySchemes, {
BearerAuth: {
type: "http",
scheme: "bearer",
},
});
deepStrictEqual(res.security, [{ BearerAuth: [] }]);
});
it("set a ApiKeyAuth ", async () => {
const res = await openApiFor(
`
@service({title: "My service"})
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "x-my-header">)
namespace MyService {}
`
);
deepStrictEqual(res.components.securitySchemes, {
ApiKeyAuth: {
type: "apiKey",
in: "header",
name: "x-my-header",
},
});
deepStrictEqual(res.security, [{ ApiKeyAuth: [] }]);
});
it("set a oauth2 auth", async () => {
const res = await openApiFor(
`
@service({title: "My service"})
@useAuth(OAuth2Auth<[MyFlow]>)
namespace MyService {
model MyFlow {
type: OAuth2FlowType.implicit;
authorizationUrl: "https://api.example.com/oauth2/authorize";
refreshUrl: "https://api.example.com/oauth2/refresh";
scopes: ["read", "write"];
}
}
`
);
deepStrictEqual(res.components.securitySchemes, {
OAuth2Auth: {
type: "oauth2",
flows: {
implicit: {
authorizationUrl: "https://api.example.com/oauth2/authorize",
refreshUrl: "https://api.example.com/oauth2/refresh",
scopes: {
read: "",
write: "",
},
},
},
},
});
deepStrictEqual(res.security, [{ OAuth2Auth: ["read", "write"] }]);
});
it("can specify custom auth name with description", async () => {
const res = await openApiFor(
`
@service({title: "My service"})
@useAuth(MyAuth)
@test namespace Foo {
@doc("My custom basic auth")
model MyAuth is BasicAuth;
}
`
);
deepStrictEqual(res.components.securitySchemes, {
MyAuth: {
type: "http",
scheme: "basic",
description: "My custom basic auth",
},
});
deepStrictEqual(res.security, [{ MyAuth: [] }]);
});
it("can use multiple auth", async () => {
const res = await openApiFor(
`
@service({title: "My service"})
@useAuth(BearerAuth | [ApiKeyAuth<ApiKeyLocation.header, "x-my-header">, BasicAuth])
namespace MyService {}
`
);
deepStrictEqual(res.components.securitySchemes, {
ApiKeyAuth: {
in: "header",
name: "x-my-header",
type: "apiKey",
},
BasicAuth: {
scheme: "basic",
type: "http",
},
BearerAuth: {
scheme: "bearer",
type: "http",
},
});
deepStrictEqual(res.security, [
{
BearerAuth: [],
},
{
ApiKeyAuth: [],
BasicAuth: [],
},
]);
});
});