-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathindex.test-d.ts
More file actions
302 lines (274 loc) · 10.7 KB
/
Copy pathindex.test-d.ts
File metadata and controls
302 lines (274 loc) · 10.7 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import fastify, { FastifyInstance } from 'fastify'
import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd'
import fastifyOauth2, {
FastifyOAuth2Options,
Credentials,
OAuth2Namespace,
OAuth2Token,
ProviderConfiguration,
UserInfoExtraOptions
} from '..'
import type { ModuleOptions } from 'simple-oauth2'
/**
* Preparing some data for testing.
*/
const auth = fastifyOauth2.GOOGLE_CONFIGURATION
const scope = ['r_emailaddress', 'r_basicprofile']
const tags = ['oauth2', 'oauth']
const credentials: Credentials = {
client: {
id: 'test_id',
secret: 'test_secret',
},
auth,
}
const simpleOauth2Options: ModuleOptions = {
client: {
id: 'test_id',
secret: 'test_secret',
},
auth,
}
const OAuth2NoneOptional: FastifyOAuth2Options = {
name: 'testOAuthName',
credentials,
callbackUri: 'http://localhost/testOauth/callback'
}
const OAuth2Options: FastifyOAuth2Options = {
name: 'testOAuthName',
scope,
credentials,
callbackUri: 'http://localhost/testOauth/callback',
callbackUriParams: {},
generateStateFunction: function () {
expectType<FastifyInstance>(this)
return 'test'
},
checkStateFunction: function () {
expectType<FastifyInstance>(this)
return true
},
startRedirectPath: '/login/testOauth',
cookie: {
secure: true,
sameSite: 'none'
},
redirectStateCookieName: 'redirect-state-cookie',
verifierCookieName: 'verifier-cookie',
}
expectAssignable<FastifyOAuth2Options>({
name: 'testOAuthName',
scope,
credentials,
callbackUri: 'http://localhost/testOauth/callback',
callbackUriParams: {},
startRedirectPath: '/login/testOauth',
pkce: 'S256'
})
expectAssignable<FastifyOAuth2Options>({
name: 'testOAuthName',
scope,
credentials,
callbackUri: req => `${req.protocol}://${req.hostname}/callback`,
callbackUriParams: {},
startRedirectPath: '/login/testOauth',
pkce: 'S256'
})
expectAssignable<FastifyOAuth2Options>({
name: 'testOAuthName',
scope,
credentials,
callbackUri: 'http://localhost/testOauth/callback',
callbackUriParams: {},
startRedirectPath: '/login/testOauth',
discovery: { issuer: 'https://idp.mycompany.com' }
})
expectNotAssignable<FastifyOAuth2Options>({
name: 'testOAuthName',
scope,
credentials,
callbackUri: 'http://localhost/testOauth/callback',
callbackUriParams: {},
startRedirectPath: '/login/testOauth',
discovery: { issuer: 1 }
})
expectAssignable<FastifyOAuth2Options>({
name: 'testOAuthName',
scope,
credentials,
callbackUri: 'http://localhost/testOauth/callback',
callbackUriParams: {},
startRedirectPath: '/login/testOauth',
pkce: 'plain'
})
expectNotAssignable<FastifyOAuth2Options>({
name: 'testOAuthName',
scope,
credentials,
callbackUri: 'http://localhost/testOauth/callback',
callbackUriParams: {},
generateStateFunction: () => {
},
checkStateFunction: () => {
},
startRedirectPath: '/login/testOauth',
pkce: 'SOMETHING'
})
const server = fastify()
server.register(fastifyOauth2, OAuth2NoneOptional)
server.register(fastifyOauth2, OAuth2Options)
server.register(fastifyOauth2, {
name: 'testOAuthName',
scope,
credentials,
callbackUri: 'http://localhost/testOauth/callback',
checkStateFunction: () => true,
})
expectError(server.register(fastifyOauth2, {
name: 'testOAuthName',
scope,
credentials,
callbackUri: 'http://localhost/testOauth/callback',
checkStateFunction: () => true,
startRedirectPath: 2,
}))
declare module 'fastify' {
// Developers need to define this in their code like they have to do with all decorators.
interface FastifyInstance {
testOAuthName: OAuth2Namespace;
}
}
/**
* Actual testing.
*/
expectType<ProviderConfiguration>(auth)
expectType<string[]>(scope)
expectType<string[]>(tags)
expectType<Credentials>(credentials)
// Ensure duplicayed simple-oauth2 are compatible with simple-oauth2
expectAssignable<ModuleOptions<string>>({ auth: { tokenHost: '' }, ...credentials })
expectAssignable<ModuleOptions['auth']>(auth)
// Ensure published types of simple-oauth2 are accepted
expectAssignable<Credentials>(simpleOauth2Options)
expectAssignable<ProviderConfiguration>(simpleOauth2Options.auth)
expectError(fastifyOauth2()) // error because missing required arguments
expectError(fastifyOauth2(server, {}, () => {
})) // error because missing required options
expectAssignable<ProviderConfiguration>(fastifyOauth2.DISCORD_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.FACEBOOK_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.GITHUB_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.GITLAB_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.GOOGLE_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.LINKEDIN_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.MICROSOFT_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.SPOTIFY_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.VKONTAKTE_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.TWITCH_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.VATSIM_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.VATSIM_DEV_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.EPIC_GAMES_CONFIGURATION)
expectAssignable<ProviderConfiguration>(fastifyOauth2.YANDEX_CONFIGURATION)
server.get('/testOauth/callback', async (request, reply) => {
expectType<OAuth2Namespace>(server.testOAuthName)
expectType<OAuth2Namespace | undefined>(server.oauth2TestOAuthName)
expectType<OAuth2Token>(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request))
expectType<Promise<OAuth2Token>>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request))
expectType<OAuth2Token>(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply))
expectType<Promise<OAuth2Token>>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply))
expectType<void>(
server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (_err: any, _t: OAuth2Token): void => {
})
)
expectType<void>(
server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply, (_err: any, _t: OAuth2Token): void => {
})
)
// error because Promise should not return void
expectError<void>(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request))
// error because non-Promise function call should return void and have a callback argument
expectError<OAuth2Token>(
server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (_err: any, _t: OAuth2Token): void => {
})
)
// error because function call does not pass a callback as second argument.
expectError<void>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request))
const token = await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)
if (token.token.refresh_token) {
expectType<OAuth2Token>(
await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})
)
expectType<Promise<OAuth2Token>>(
server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})
)
expectType<void>(
server.testOAuthName.getNewAccessTokenUsingRefreshToken(
token.token,
{},
(_err: any, _t: OAuth2Token): void => {
}
)
)
// Expect error because Promise should not return void
expectError<void>(server.testOAuthName.revokeToken(token.token, 'access_token', undefined))
// Correct way
expectType<Promise<void>>(server.testOAuthName.revokeToken(token.token, 'access_token', undefined))
// Expect error because invalid Type test isn't an access_token or refresh_token
expectError<Promise<void>>(server.testOAuthName.revokeToken(token.token, 'test', undefined))
// Correct way
expectType<void>(
server.testOAuthName.revokeToken(token.token, 'refresh_token', undefined, (_err: any): void => {
})
)
// Expect error because invalid Type test isn't an access_token or refresh_token
expectError<void>(
server.testOAuthName.revokeToken(token.token, 'test', undefined, (_err: any): void => {
})
)
// Expect error because invalid Type test isn't an access_token or refresh_token
expectError<void>(
server.testOAuthName.revokeToken(token.token, 'access_token', undefined, undefined)
)
// Expect error because Promise should not return void
expectError<void>(server.testOAuthName.revokeAllToken(token.token, undefined))
// Correct way
expectType<Promise<void>>(server.testOAuthName.revokeAllToken(token.token, undefined))
// Correct way too
expectType<void>(server.testOAuthName.revokeAllToken(token.token, undefined, (_err: any): void => {
}))
// Invalid content
expectError<void>(server.testOAuthName.revokeAllToken(token.token, undefined, undefined))
// error because Promise should not return void
expectError<void>(await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {}))
// error because non-Promise function call should return void and have a callback argument
expectError<OAuth2Token>(
server.testOAuthName.getNewAccessTokenUsingRefreshToken(
token.token,
{},
(_err: any, _t: OAuth2Token): void => {
}
)
)
// error because function call does not pass a callback as second argument.
expectError<void>(server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {}))
}
expectType<Promise<string>>(server.testOAuthName.generateAuthorizationUri(request, reply))
expectType<void>(server.testOAuthName.generateAuthorizationUri(request, reply, (_err) => {}))
// BEGIN userinfo tests
expectType<Promise<Object>>(server.testOAuthName.userinfo(token.token))
expectType<Promise<Object>>(server.testOAuthName.userinfo(token.token.access_token))
expectType<Object>(await server.testOAuthName.userinfo(token.token.access_token))
expectType<void>(server.testOAuthName.userinfo(token.token.access_token, () => {}))
expectType<void>(server.testOAuthName.userinfo(token.token.access_token, undefined, () => {}))
expectAssignable<UserInfoExtraOptions>({ method: 'GET', params: {}, via: 'header' })
expectAssignable<UserInfoExtraOptions>({ method: 'POST', params: { a: 1 }, via: 'header' })
expectAssignable<UserInfoExtraOptions>({ via: 'body' })
expectNotAssignable<UserInfoExtraOptions>({ via: 'donkey' })
expectNotAssignable<UserInfoExtraOptions>({ something: 1 })
// END userinfo tests
expectType<string>(await server.testOAuthName.generateAuthorizationUri(request, reply))
// error because missing reply argument
expectError<string>(server.testOAuthName.generateAuthorizationUri(request))
return {
access_token: token.token.access_token,
}
})