11from unittest import TestCase
22from fitbit import Fitbit , FitbitOauth2Client
3+ from fitbit .exceptions import HTTPUnauthorized
34import mock
45from requests_oauthlib import OAuth2Session
5- from oauthlib .oauth2 import TokenExpiredError
66
77
88class Auth2Test (TestCase ):
99 """Add tests for auth part of API
1010 mock the oauth library calls to simulate various responses,
11- make sure we call the right oauth calls, respond correctly based on the responses
11+ make sure we call the right oauth calls, respond correctly based on the
12+ responses
1213 """
1314 client_kwargs = {
1415 'client_id' : 'fake_id' ,
@@ -60,8 +61,8 @@ def test_refresh_token(self):
6061 self .assertEqual ("fake_return_refresh_token" , retval ['refresh_token' ])
6162
6263 def test_auto_refresh_token_exception (self ):
63- # test of auto_refresh with tokenExpired exception
64- # 1. first call to _request causes a TokenExpired
64+ """Test of auto_refresh with Unauthorized exception"""
65+ # 1. first call to _request causes a HTTPUnauthorized
6566 # 2. the token_refresh call is faked
6667 # 3. the second call to _request returns a valid value
6768 kwargs = self .client_kwargs
@@ -70,21 +71,26 @@ def test_auto_refresh_token_exception(self):
7071
7172 fb = Fitbit (** kwargs )
7273 with mock .patch .object (FitbitOauth2Client , '_request' ) as r :
73- r .side_effect = [TokenExpiredError , fake_response (200 , 'correct_response' )]
74+ r .side_effect = [
75+ HTTPUnauthorized (fake_response (401 , b'correct_response' )),
76+ fake_response (200 , 'correct_response' )
77+ ]
7478 with mock .patch .object (OAuth2Session , 'refresh_token' ) as rt :
7579 rt .return_value = {
7680 'access_token' : 'fake_return_access_token' ,
7781 'refresh_token' : 'fake_return_refresh_token'
7882 }
7983 retval = fb .client .make_request (Fitbit .API_ENDPOINT + '/1/user/-/profile.json' )
8084 self .assertEqual ("correct_response" , retval .text )
81- self .assertEqual ("fake_return_access_token" , fb .client .token ['access_token' ])
82- self .assertEqual ("fake_return_refresh_token" , fb .client .token ['refresh_token' ])
85+ self .assertEqual (
86+ "fake_return_access_token" , fb .client .token ['access_token' ])
87+ self .assertEqual (
88+ "fake_return_refresh_token" , fb .client .token ['refresh_token' ])
8389 self .assertEqual (1 , rt .call_count )
8490 self .assertEqual (2 , r .call_count )
8591
86- def test_auto_refresh_token_nonException (self ):
87- # test of auto_refersh when the exception doesn't fire
92+ def test_auto_refresh_token_non_exception (self ):
93+ """Test of auto_refersh when the exception doesn't fire"""
8894 # 1. first call to _request causes a 401 expired token response
8995 # 2. the token_refresh call is faked
9096 # 3. the second call to _request returns a valid value
@@ -94,17 +100,21 @@ def test_auto_refresh_token_nonException(self):
94100
95101 fb = Fitbit (** kwargs )
96102 with mock .patch .object (FitbitOauth2Client , '_request' ) as r :
97- r .side_effect = [fake_response (401 , b'{"errors": [{"message": "Access token invalid or expired: some_token_goes_here", "errorType": "oauth", "fieldName": "access_token"}]}' ),
98- fake_response (200 , 'correct_response' )]
103+ r .side_effect = [
104+ fake_response (401 , b'{"errors": [{"message": "Access token expired: some_token_goes_here", "errorType": "expired_token", "fieldName": "access_token"}]}' ),
105+ fake_response (200 , 'correct_response' )
106+ ]
99107 with mock .patch .object (OAuth2Session , 'refresh_token' ) as rt :
100108 rt .return_value = {
101109 'access_token' : 'fake_return_access_token' ,
102110 'refresh_token' : 'fake_return_refresh_token'
103111 }
104112 retval = fb .client .make_request (Fitbit .API_ENDPOINT + '/1/user/-/profile.json' )
105113 self .assertEqual ("correct_response" , retval .text )
106- self .assertEqual ("fake_return_access_token" , fb .client .token ['access_token' ])
107- self .assertEqual ("fake_return_refresh_token" , fb .client .token ['refresh_token' ])
114+ self .assertEqual (
115+ "fake_return_access_token" , fb .client .token ['access_token' ])
116+ self .assertEqual (
117+ "fake_return_refresh_token" , fb .client .token ['refresh_token' ])
108118 self .assertEqual (1 , rt .call_count )
109119 self .assertEqual (2 , r .call_count )
110120
0 commit comments