Skip to content

Commit 5959a5e

Browse files
committed
chore: update index.tst.ts
1 parent af438cf commit 5959a5e

1 file changed

Lines changed: 90 additions & 95 deletions

File tree

types/index.tst.ts

Lines changed: 90 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -61,69 +61,68 @@ const OAuth2Options: FastifyOAuth2Options = {
6161
verifierCookieName: 'verifier-cookie',
6262
}
6363

64-
expect({
64+
expect<FastifyOAuth2Options>().type.toBeAssignableFrom({
6565
name: 'testOAuthName',
6666
scope,
6767
credentials,
6868
callbackUri: 'http://localhost/testOauth/callback',
6969
callbackUriParams: {},
7070
startRedirectPath: '/login/testOauth',
71-
pkce: 'S256'
72-
} as const).type.toBeAssignableTo<FastifyOAuth2Options>()
71+
pkce: 'S256' as const
72+
})
7373

74-
expect({
74+
expect<FastifyOAuth2Options>().type.toBeAssignableFrom({
7575
name: 'testOAuthName',
7676
scope,
7777
credentials,
7878
callbackUri: (req: FastifyRequest) => `${req.protocol}://${req.hostname}/callback`,
7979
callbackUriParams: {},
8080
startRedirectPath: '/login/testOauth',
81-
pkce: 'S256'
82-
} as const).type.toBeAssignableTo<FastifyOAuth2Options>()
81+
pkce: 'S256' as const
82+
})
8383

84-
expect({
84+
expect<FastifyOAuth2Options>().type.toBeAssignableFrom({
8585
name: 'testOAuthName',
8686
scope,
8787
credentials,
8888
callbackUri: 'http://localhost/testOauth/callback',
8989
callbackUriParams: {},
9090
startRedirectPath: '/login/testOauth',
9191
discovery: { issuer: 'https://idp.mycompany.com' }
92-
} as const).type.toBeAssignableTo<FastifyOAuth2Options>()
92+
})
9393

94-
expect({
94+
expect<FastifyOAuth2Options>().type.not.toBeAssignableFrom({
9595
name: 'testOAuthName',
9696
scope,
9797
credentials,
9898
callbackUri: 'http://localhost/testOauth/callback',
9999
callbackUriParams: {},
100100
startRedirectPath: '/login/testOauth',
101101
discovery: { issuer: 1 }
102-
}).type.not.toBeAssignableTo<FastifyOAuth2Options>()
102+
})
103103

104-
expect({
104+
expect<FastifyOAuth2Options>().type.toBeAssignableFrom({
105105
name: 'testOAuthName',
106106
scope,
107107
credentials,
108108
callbackUri: 'http://localhost/testOauth/callback',
109109
callbackUriParams: {},
110110
startRedirectPath: '/login/testOauth',
111-
pkce: 'plain'
112-
} as const).type.toBeAssignableTo<FastifyOAuth2Options>()
111+
pkce: 'plain' as const
112+
})
113113

114-
expect({
115-
name: 'testOAuthName',
116-
scope,
117-
credentials,
118-
callbackUri: 'http://localhost/testOauth/callback',
119-
callbackUriParams: {},
120-
generateStateFunction: () => {
121-
},
122-
checkStateFunction: () => {
123-
},
124-
startRedirectPath: '/login/testOauth',
125-
pkce: 'SOMETHING'
126-
}).type.not.toBeAssignableTo<FastifyOAuth2Options>()
114+
expect<FastifyOAuth2Options>()
115+
.type.not.toBeAssignableFrom({
116+
name: 'testOAuthName',
117+
scope,
118+
credentials,
119+
callbackUri: 'http://localhost/testOauth/callback',
120+
callbackUriParams: {},
121+
generateStateFunction: () => {},
122+
checkStateFunction: () => {},
123+
startRedirectPath: '/login/testOauth',
124+
pkce: 'SOMETHING' as const
125+
})
127126

128127
const server = fastify()
129128

@@ -138,14 +137,13 @@ server.register(fastifyOauth2, {
138137
checkStateFunction: () => true,
139138
})
140139

141-
// @ts-expect-error!
142-
server.register(fastifyOauth2, {
140+
expect(server.register).type.not.toBeCallableWith(fastifyOauth2, {
143141
name: 'testOAuthName',
144142
scope,
145143
credentials,
146144
callbackUri: 'http://localhost/testOauth/callback',
147145
checkStateFunction: () => true,
148-
startRedirectPath: 2,
146+
startRedirectPath: 2
149147
})
150148

151149
declare module 'fastify' {
@@ -170,35 +168,37 @@ expect(auth).type.toBeAssignableTo<ModuleOptions['auth']>()
170168
expect(simpleOauth2Options).type.toBeAssignableTo<Credentials>()
171169
expect(simpleOauth2Options.auth).type.toBeAssignableTo<ProviderConfiguration>()
172170

173-
// @ts-expect-error!
174-
fastifyOauth2()
175-
176-
// @ts-expect-error!
177-
fastifyOauth2(server, {}, () => {})
178-
179-
expect(fastifyOauth2.DISCORD_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
180-
expect(fastifyOauth2.FACEBOOK_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
181-
expect(fastifyOauth2.GITHUB_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
182-
expect(fastifyOauth2.GITLAB_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
183-
expect(fastifyOauth2.GOOGLE_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
184-
expect(fastifyOauth2.LINKEDIN_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
185-
expect(fastifyOauth2.MICROSOFT_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
186-
expect(fastifyOauth2.SPOTIFY_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
187-
expect(fastifyOauth2.VKONTAKTE_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
188-
expect(fastifyOauth2.TWITCH_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
189-
expect(fastifyOauth2.VATSIM_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
190-
expect(fastifyOauth2.VATSIM_DEV_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
191-
expect(fastifyOauth2.EPIC_GAMES_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
192-
expect(fastifyOauth2.YANDEX_CONFIGURATION).type.toBeAssignableTo<ProviderConfiguration>()
171+
expect(fastifyOauth2).type.not.toBeCallableWith()
172+
expect(fastifyOauth2).type.not.toBeCallableWith(server, {}, () => {})
173+
174+
expect(fastifyOauth2.DISCORD_CONFIGURATION).type.toBe<ProviderConfiguration>()
175+
expect(fastifyOauth2.FACEBOOK_CONFIGURATION).type.toBe<ProviderConfiguration>()
176+
expect(fastifyOauth2.GITHUB_CONFIGURATION).type.toBe<ProviderConfiguration>()
177+
expect(fastifyOauth2.GITLAB_CONFIGURATION).type.toBe<ProviderConfiguration>()
178+
expect(fastifyOauth2.GOOGLE_CONFIGURATION).type.toBe<ProviderConfiguration>()
179+
expect(fastifyOauth2.LINKEDIN_CONFIGURATION).type.toBe<ProviderConfiguration>()
180+
expect(
181+
fastifyOauth2.MICROSOFT_CONFIGURATION
182+
).type.toBe<ProviderConfiguration>()
183+
expect(fastifyOauth2.SPOTIFY_CONFIGURATION).type.toBe<ProviderConfiguration>()
184+
expect(
185+
fastifyOauth2.VKONTAKTE_CONFIGURATION
186+
).type.toBe<ProviderConfiguration>()
187+
expect(fastifyOauth2.TWITCH_CONFIGURATION).type.toBe<ProviderConfiguration>()
188+
expect(fastifyOauth2.VATSIM_CONFIGURATION).type.toBe<ProviderConfiguration>()
189+
expect(
190+
fastifyOauth2.VATSIM_DEV_CONFIGURATION
191+
).type.toBe<ProviderConfiguration>()
192+
expect(
193+
fastifyOauth2.EPIC_GAMES_CONFIGURATION
194+
).type.toBe<ProviderConfiguration>()
195+
expect(fastifyOauth2.YANDEX_CONFIGURATION).type.toBe<ProviderConfiguration>()
193196

194197
server.get('/testOauth/callback', async (request, reply) => {
195-
expect(server.testOAuthName).type.toBe<OAuth2Namespace>()
196198
expect(server.oauth2TestOAuthName).type.toBe<OAuth2Namespace | undefined>()
197199

198-
expect(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)).type.toBe<OAuth2Token>()
199200
expect(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)).type.toBe<Promise<OAuth2Token>>()
200201

201-
expect(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply)).type.toBe<OAuth2Token>()
202202
expect(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply)).type.toBe<Promise<OAuth2Token>>()
203203

204204
expect(
@@ -211,25 +211,8 @@ server.get('/testOauth/callback', async (request, reply) => {
211211
})
212212
).type.toBe<void>()
213213

214-
expect(
215-
await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)
216-
).type.not.toBe<void>()
217-
218-
expect(
219-
server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(
220-
request,
221-
(_err: any, _t: OAuth2Token): void => {}
222-
)
223-
).type.not.toBe<OAuth2Token>()
224-
225-
expect(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)).type.not.toBe<void>()
226-
227214
const token = await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)
228215
if (token.token.refresh_token) {
229-
expect(
230-
await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})
231-
).type.toBe<OAuth2Token>()
232-
233216
expect(
234217
server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})
235218
).type.toBe<Promise<OAuth2Token>>()
@@ -243,23 +226,25 @@ server.get('/testOauth/callback', async (request, reply) => {
243226
)
244227
).type.toBe<void>()
245228

246-
expect(server.testOAuthName.revokeToken(token.token, 'access_token', undefined)).type.not.toBe<void>()
247-
248229
expect(server.testOAuthName.revokeToken(token.token, 'access_token', undefined)).type.toBe<Promise<void>>()
249230

250-
// @ts-expect-error!
251-
server.testOAuthName.revokeToken(token.token, 'test', undefined)
252-
231+
expect(server.testOAuthName.revokeToken).type.not.toBeCallableWith(
232+
token.token,
233+
'test',
234+
undefined
235+
)
253236
expect(
254237
server.testOAuthName.revokeToken(token.token, 'refresh_token', undefined, (_err: any): void => {
255238
})
256239
).type.toBe<void>()
257240

258-
// @ts-expect-error!
259-
server.testOAuthName.revokeToken(token.token, 'test', undefined, (_err: any): void => {})
260-
261-
// @ts-expect-error!
262-
server.testOAuthName.revokeToken(token.token, 'access_token', undefined, undefined)
241+
expect(server.testOAuthName.revokeToken).type.not.toBeCallableWith(
242+
token.token,
243+
'test',
244+
undefined,
245+
(_err: any): void => {}
246+
)
247+
expect(server.testOAuthName.revokeToken).type.not.toBeCallableWith(token.token, 'test', undefined)
263248

264249
expect(server.testOAuthName.revokeAllToken(token.token, undefined)).type.not.toBe<void>()
265250

@@ -268,19 +253,24 @@ server.get('/testOauth/callback', async (request, reply) => {
268253
expect(server.testOAuthName.revokeAllToken(token.token, undefined, (_err: any): void => {
269254
})).type.toBe<void>()
270255

271-
// @ts-expect-error!
272-
server.testOAuthName.revokeAllToken(token.token, undefined, undefined)
256+
expect(server.testOAuthName.revokeToken).type.not.toBeCallableWith(
257+
token.token,
258+
'access_token',
259+
undefined,
260+
undefined
261+
)
273262

274-
expect(await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})).type.not.toBe<void>()
263+
expect(
264+
server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})
265+
).type.toBe<Promise<OAuth2Token>>()
275266

276267
expect(
277268
server.testOAuthName.getNewAccessTokenUsingRefreshToken(
278269
token.token,
279270
{},
280-
(_err: any, _t: OAuth2Token): void => {
281-
}
271+
(_err: any, _t: OAuth2Token): void => {}
282272
)
283-
).type.not.toBe<OAuth2Token>()
273+
).type.toBe<void>()
284274

285275
expect(server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})).type.not.toBe<void>()
286276
}
@@ -291,22 +281,27 @@ server.get('/testOauth/callback', async (request, reply) => {
291281
// BEGIN userinfo tests
292282
expect(server.testOAuthName.userinfo(token.token)).type.toBe<Promise<Object>>()
293283
expect(server.testOAuthName.userinfo(token.token.access_token)).type.toBe<Promise<Object>>()
294-
expect(await server.testOAuthName.userinfo(token.token.access_token)).type.toBe<Object>()
295284
expect(server.testOAuthName.userinfo(token.token.access_token, () => {})).type.toBe<void>()
296285
expect(server.testOAuthName.userinfo(token.token.access_token, undefined, () => {})).type.toBe<void>()
297286

298-
expect({ method: 'GET', params: {}, via: 'header' } as const).type.toBeAssignableTo<UserInfoExtraOptions>()
299-
expect({ method: 'POST', params: { a: 1 }, via: 'header' } as const).type.toBeAssignableTo<UserInfoExtraOptions>()
300-
expect({ via: 'body' } as const).type.toBeAssignableTo<UserInfoExtraOptions>()
301-
302-
expect({ via: 'donkey' }).type.not.toBeAssignableTo<UserInfoExtraOptions>()
303-
expect({ something: 1 }).type.not.toBeAssignableTo<UserInfoExtraOptions>()
287+
expect<UserInfoExtraOptions>().type.toBeAssignableFrom({ method: 'GET' as const, params: {}, via: 'header' as const })
288+
expect<UserInfoExtraOptions>().type.toBeAssignableFrom({ method: 'POST' as const, params: { a: 1 }, via: 'header' as const })
289+
expect<UserInfoExtraOptions>().type.toBeAssignableFrom({ via: 'body' as const })
290+
expect<UserInfoExtraOptions>().type.not.toBeAssignableFrom({
291+
via: 'donkey' as const
292+
})
293+
expect<UserInfoExtraOptions>().type.not.toBeAssignableFrom({
294+
something: 1
295+
})
304296
// END userinfo tests
305297

306-
expect(await server.testOAuthName.generateAuthorizationUri(request, reply)).type.toBe<string>()
298+
expect(
299+
server.testOAuthName.generateAuthorizationUri(request, reply)
300+
).type.toBe<Promise<string>>()
307301

308-
// @ts-expect-error!
309-
server.testOAuthName.generateAuthorizationUri(request)
302+
expect(
303+
server.testOAuthName.generateAuthorizationUri
304+
).type.not.toBeCallableWith(request)
310305

311306
return {
312307
access_token: token.token.access_token,

0 commit comments

Comments
 (0)