Skip to content

How to mock OAuth flow? #1368

Answered by kettanaito
cmacdonnacha asked this question in Q&A
Aug 24, 2022 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

Hey, @cmacdonnacha.

Yeah, sure. Authentication also involves API requests to endpoints, so no extra case here. I haven't worked with the Slack API per se but I imagine it's not much different from other OAuth protocols.

Here's how I'd mock a GitHub OAuth:

rest.get('https://github.com/oauth/v2/authorize', (req, res, ctx) => {
  const callbackUrl = new URL('http://localhost:3000/oauth/callback')
  callbackUrl.searchParams.set('code', 'abc-123')

  // Redirect to the local OAuth callback route with a mock "code".
  // The route handler for this callback remains as-is, no need to mock it.
  return res(ctx.status(301), ctx.set('Location', callbackUrl))
})

rest.post('https://github.com/oauth/v…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@cmacdonnacha
Comment options

@kettanaito
Comment options

Answer selected by cmacdonnacha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants