Skip to content

Commit

Permalink
set up mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Sep 21, 2023
1 parent d5c9e20 commit 1a8cead
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
Binary file added tests/fixtures/google/google-avatar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions tests/mocks/google.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import fs from 'node:fs'
import { faker } from '@faker-js/faker'
import { HttpResponse, passthrough, http, type HttpHandler } from 'msw'

const { json } = HttpResponse

export const mockGoogleProfile = {
sub: faker.string.uuid(),
picture: `https://lh3.googleusercontent.com/a-/${faker.string.uuid()}`,
email: faker.internet.email(),
email_verified: true,
}

const passthroughGoogle =
!process.env.GOOGLE_CLIENT_ID.startsWith('MOCK_') && !process.env.TESTING
export const handlers: Array<HttpHandler> = [
http.get(
'https://accounts.google.com/.well-known/openid-configuration',
() => {
if (passthroughGoogle) return passthrough()

return json({
issuer: 'https://accounts.google.com',
authorization_endpoint: 'https://accounts.google.com/o/oauth2/v2/auth',
device_authorization_endpoint:
'https://oauth2.googleapis.com/device/code',
token_endpoint: 'https://oauth2.googleapis.com/token',
userinfo_endpoint: 'https://openidconnect.googleapis.com/v1/userinfo',
revocation_endpoint: 'https://oauth2.googleapis.com/revoke',
jwks_uri: 'https://www.googleapis.com/oauth2/v3/certs',
response_types_supported: [
'code',
'token',
'id_token',
'code token',
'code id_token',
'token id_token',
'code token id_token',
'none',
],
subject_types_supported: ['public'],
id_token_signing_alg_values_supported: ['RS256'],
scopes_supported: ['openid', 'email', 'profile'],
token_endpoint_auth_methods_supported: [
'client_secret_post',
'client_secret_basic',
],
claims_supported: [
'aud',
'email',
'email_verified',
'exp',
'family_name',
'given_name',
'iat',
'iss',
'locale',
'name',
'picture',
'sub',
],
code_challenge_methods_supported: ['plain', 'S256'],
grant_types_supported: [
'authorization_code',
'refresh_token',
'urn:ietf:params:oauth:grant-type:device_code',
'urn:ietf:params:oauth:grant-type:jwt-bearer',
],
})
},
),
http.post('https://oauth2.googleapis.com/token', async () => {
if (passthroughGoogle) return passthrough()

return json({
access_token: '__MOCK_ACCESS_TOKEN__',
expires_in: 3599,
scope: 'openid https://www.googleapis.com/auth/userinfo.email',
token_type: 'Bearer',
id_token: faker.string.uuid(),
})
}),
http.get('https://openidconnect.googleapis.com/v1/userinfo', async () => {
if (passthroughGoogle) return passthrough()
return json(mockGoogleProfile)
}),
http.get('https://lh3.googleusercontent.com/a-/:id', async () => {
if (passthroughGoogle) return passthrough()

const buffer = await fs.promises.readFile(
'./tests/fixtures/google/google-avatar.jpg',
)
return new Response(buffer, {
headers: { 'content-type': 'image/jpg' },
})
}),
]
2 changes: 2 additions & 0 deletions tests/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import closeWithGrace from 'close-with-grace'
import { passthrough, http } from 'msw'
import { setupServer } from 'msw/node'
import { handlers as githubHandlers } from './github.ts'
import { handlers as googleHandlers } from './google.ts'
import { handlers as resendHandlers } from './resend.ts'

const miscHandlers = [
Expand All @@ -13,6 +14,7 @@ const miscHandlers = [
export const server = setupServer(
...miscHandlers,
...resendHandlers,
...googleHandlers,
...githubHandlers,
)

Expand Down

0 comments on commit 1a8cead

Please sign in to comment.