|
| 1 | +import * as fastify from 'fastify'; |
| 2 | +import * as fastifyOauth2 from '.'; |
| 3 | +import { expectType, expectError, expectAssignable } from 'tsd'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Preparing some data for testing. |
| 7 | + */ |
| 8 | +const auth = fastifyOauth2.GOOGLE_CONFIGURATION; |
| 9 | +const scope = ['r_emailaddress', 'r_basicprofile']; |
| 10 | +const credentials = { |
| 11 | + client: { |
| 12 | + id: 'test_id', |
| 13 | + secret: 'test_secret', |
| 14 | + }, |
| 15 | + auth: auth, |
| 16 | +}; |
| 17 | +const OAuth2Options = { |
| 18 | + name: 'testOAuthName', |
| 19 | + scope: scope, |
| 20 | + credentials: credentials, |
| 21 | + callbackUri: 'http://localhost/testOauth/callback', |
| 22 | + callbackUriParams: {}, |
| 23 | + generateStateFunction: () => {}, |
| 24 | + checkStateFunction: () => {}, |
| 25 | + startRedirectPath: '/login/testOauth', |
| 26 | +}; |
| 27 | + |
| 28 | +const server = fastify(); |
| 29 | + |
| 30 | +server.register(fastifyOauth2, OAuth2Options); |
| 31 | + |
| 32 | +declare module 'fastify' { |
| 33 | + // Developers need to define this in their code like they have to do with all decorators. |
| 34 | + interface FastifyInstance { |
| 35 | + testOAuthName: fastifyOauth2.OAuth2Namespace; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Actual testing. |
| 41 | + */ |
| 42 | +expectType<fastifyOauth2.ProviderConfiguration>(auth); |
| 43 | +expectType<string[]>(scope); |
| 44 | +expectType<fastifyOauth2.Credentials>(credentials); |
| 45 | + |
| 46 | +expectType<void>(fastifyOauth2(server, OAuth2Options, () => {})); |
| 47 | +expectError(fastifyOauth2()); // error because missing required arguments |
| 48 | +expectError(fastifyOauth2(server, {}, () => {})); // error because missing required options |
| 49 | + |
| 50 | +expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.FACEBOOK_CONFIGURATION); |
| 51 | +expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.GITHUB_CONFIGURATION); |
| 52 | +expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.GOOGLE_CONFIGURATION); |
| 53 | +expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.LINKEDIN_CONFIGURATION); |
| 54 | +expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.MICROSOFT_CONFIGURATION); |
| 55 | +expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.SPOTIFY_CONFIGURATION); |
| 56 | +expectAssignable<fastifyOauth2.ProviderConfiguration>(fastifyOauth2.VKONTAKTE_CONFIGURATION); |
| 57 | + |
| 58 | +server.get('/testOauth/callback', async request => { |
| 59 | + expectType<fastifyOauth2.OAuth2Namespace>(server.testOAuthName); |
| 60 | + |
| 61 | + expectType<fastifyOauth2.OAuth2Token>(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); |
| 62 | + expectType<Promise<fastifyOauth2.OAuth2Token>>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); |
| 63 | + expectType<void>( |
| 64 | + server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (t: fastifyOauth2.OAuth2Token): void => {}), |
| 65 | + ); |
| 66 | + |
| 67 | + expectError<void>(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); // error because Promise should not return void |
| 68 | + expectError<fastifyOauth2.OAuth2Token>( |
| 69 | + server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (t: fastifyOauth2.OAuth2Token): void => {}), |
| 70 | + ); // error because non-Promise function call should return void and have a callback argument |
| 71 | + expectError<void>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); // error because function call does not pass a callback as second argument. |
| 72 | + |
| 73 | + const token = await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request); |
| 74 | + if (token.refresh_token) { |
| 75 | + expectType<fastifyOauth2.OAuth2Token>( |
| 76 | + await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.refresh_token, {}), |
| 77 | + ); |
| 78 | + expectType<Promise<fastifyOauth2.OAuth2Token>>( |
| 79 | + server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.refresh_token, {}), |
| 80 | + ); |
| 81 | + expectType<void>( |
| 82 | + server.testOAuthName.getNewAccessTokenUsingRefreshToken( |
| 83 | + token.refresh_token, |
| 84 | + {}, |
| 85 | + (t: fastifyOauth2.OAuth2Token): void => {}, |
| 86 | + ), |
| 87 | + ); |
| 88 | + |
| 89 | + expectError<void>(await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.refresh_token, {})); // error because Promise should not return void |
| 90 | + expectError<fastifyOauth2.OAuth2Token>( |
| 91 | + server.testOAuthName.getNewAccessTokenUsingRefreshToken( |
| 92 | + token.refresh_token, |
| 93 | + {}, |
| 94 | + (t: fastifyOauth2.OAuth2Token): void => {}, |
| 95 | + ), |
| 96 | + ); // error because non-Promise function call should return void and have a callback argument |
| 97 | + expectError<void>(server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.refresh_token, {})); // error because function call does not pass a callback as second argument. |
| 98 | + } |
| 99 | + |
| 100 | + return { |
| 101 | + access_token: token.access_token, |
| 102 | + }; |
| 103 | +}); |
0 commit comments