Search code examples
pythonjsonmockinggoogle-oauthwiremock

How to mock a complete workflow of google oauth2 authentication using wiremock?


I have a situation where I need to mock entire google drive workflow for internal testing. We don't want to use actual service account for testing. I use wiremock and mocked (or I think) following api requests.

  1. Auth Endpoint:
request": {
    "method": "GET",
    "urlPattern": ".*/o/oauth2/v2/auth.*",
  "headers": {
      "Host": {
        "equalTo": "accounts.google.com"
      }
    }
    },
  "response": {
    "status": 302,
    "headers": {
       "Location": "callbackurl"
    }
  1. Redirect Endpoint:
request": {
    "method": "GET",
    "urlPattern": ".*/suite/oauth/callback.*" 
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "callback is succesful"
}
}

  1. Token Endpoint:
{
  "request": {
    "method": "POST",
    "url": "/token",
    "headers" : {
    "Host" : {
    "equalTo" : "oauth2.googleapis.com"
    }
    }
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "{ \"access_token\": \"your_access_token\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"refresh_token\": \"your_refresh_token\" }"
  }
}

However this works when I hit the endpoint from postman or python script. But this does not work when I try to use my actual application (I have enabled the proxy with the wiremock) it opens a new tab in chrome and it throws error. The url is exactly same on what I test using postman and this application. The problem is the request in the application gets opened in a new window

You can’t sign in because this app sent an invalid request. You can try again later, or contact the developer about this issue. Learn more about this error
If you are a developer of this app, see error details.
Error 400: redirect_uri_mismatch

Do you have any idea?


Solution

  • I got it resolved. Basically, the 2nd mock is incorrect. Instead of mocking the callback with 200 response, I had to initiate a request to callback url with the 'state' value received from auth end point and also need to send the dummy auth token.