Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate Integration Test against an Oauth2 enabled API in .net

I have an API that uses another API (example google calendar API) which is authenticated with OAuth 2.

httpRequest => MyApi under test => uses external Oauth2 enabled API

If the "Oauth2 enabled API" were using HTTP basic authentication, I could just hardcode the username and password somewhere to test the application —using the username and password of a test user created in the external APP that exposes the API that I am using.

As with Oauth2 we require the user to consent (the user is usually redirected to a web page) to ask them for consent to the app to access their data through the API.

I just want to create simple Integration Test: For example, my API creates an event in the google calendar, then deletes it for cleanup, but without human intervention.

Is this possible and how?

like image 896
Cedric Dumont Avatar asked Jan 25 '26 04:01

Cedric Dumont


1 Answers

I've been wondering about the best way to do this myself. So far I've found a few of options:

  1. Use the password grant type, to authenticate as a user. This is apparently no longer recommended as per best practices, but that's for end-users. Not for testing.
  2. Use the client_credentials grant type, to authenticate as the app itself. The problem with this is that if your test depends on being able to retrieve user data, the app won't have any associated to itself, unless you manipulate it beforehand.
  3. Request a refresh_token, to re-authenticate as a previously authenticated user. This is done by requesting the offline_access scope. A user will have to do the first authentication, get a refresh token and provision the test script with it. The script then must be able to keep updating itself with a fresh refresh token each time it runs. And if the refresh token should expire before the next run, human intervention will be required again.
  4. Use the device_code grant type to poll for end-user consent elsewhere. This is like what YouTube uses to pair your SmartTV, whereby you start the login on your SmartTV and consent to it with a pairing code on your mobile device. Here, human intervention is required as well for the consent, at least the first time, and then again should the consent expire.
like image 92
Mig82 Avatar answered Jan 27 '26 18:01

Mig82



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!