Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing APIs that require OAuth tokens [closed]

I am trying to write a suite of automated integration tests to test my C# client library calls to the Yahoo Fantasy Sports API. Several API calls require OAuth tokens, which is where I am having some difficulty. I can use a web browser to generate an access key and secret and then pass those along in my test code, but the tokens expire after an hour, so I need to manually regenerate these and update my test configuration any time I want to run the tests.

Are there best practices for writing API integration tests when OAuth tokens are required?

like image 545
Sean Avatar asked Mar 08 '13 15:03

Sean


1 Answers

normally such api's offer a way to get authentication tokens without the need to use a browser. I am not sure if yahoo sports is one of those though.

Normally you have to create an application to access an OAuth2 system, they then give you a ClientID and ClientSecret, then you hit a token URL and receive the access token which is then valid for an hour.

You might want to consider not having integration tests at all though. If I were you I would simply mock the Api responses and use that in your tests. So, gt a sample of the response for each call and then simply create a fake response which returns that whenever you hit it. you can then still run your tests.

The question you need to answer is this : what exactly am I testing? Are you testing a third party APi or do you want to test your own code.

Also, don't forget each api allows to be hit a certain number of times during a certain time window. One more reason to fake it, I'd say

like image 81
Andrei Dragotoniu Avatar answered Nov 05 '22 22:11

Andrei Dragotoniu