Skip to content

Commit

Permalink
testing refresh_authentication method is able to update api_params
Browse files Browse the repository at this point in the history
  • Loading branch information
filipeximenes committed Feb 28, 2016
1 parent c0fceb9 commit b101eaf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/source/adapter_class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ Given an exception, should returne if the authentication is expired. If so, tapi

.. method:: refresh_authentication(self, api_params, *args, **kwargs):

Should run refresh authentication logic.
Should run refresh authentication logic. Make sure you update `api_params` dictionary with the new token.
2 changes: 1 addition & 1 deletion tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def is_authentication_expired(self, exception, *args, **kwargs):
return exception.status_code == 401

def refresh_authentication(self, api_params, *args, **kwargs):
pass
api_params['token'] = 'new_token'


TokenRefreshClient = generate_wrapper_from_adapter(TokenRefreshClientAdapter)
24 changes: 14 additions & 10 deletions tests/test_tapioca.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def test_simple_pages_max_item_zero_iterator(self):
class TestTokenRefreshing(unittest.TestCase):

def setUp(self):
self.wrapper = TokenRefreshClient()
self.wrapper = TokenRefreshClient(token='token')

@responses.activate
def test_not_token_refresh_ready_client_call_raises_not_implemented(self):
Expand All @@ -474,21 +474,25 @@ def test_token_expired_and_no_refresh_flag(self):
with self.assertRaises(ClientError) as context:
response = self.wrapper.test().post()

def request_callback(self, request):
if self.first_call:
self.first_call = False
return (401, {'content_type':'application/json'}, json.dumps('{"error": "Token expired"}'))
else:
self.first_call = None
return (201, {'content_type':'application/json'}, json.dumps('{"error": "Token expired"}'))

@responses.activate
def test_token_expired_with_active_refresh_flag(self):
self.first_call = True

def request_callback(request):
if self.first_call:
self.first_call = False
return (401, {'content_type':'application/json'}, json.dumps('{"error": "Token expired"}'))
else:
self.first_call = None
return (201, {'content_type':'application/json'}, '')

responses.add_callback(
responses.POST, self.wrapper.test().data,
callback=self.request_callback,
callback=request_callback,
content_type='application/json',
)

response = self.wrapper.test().post(refresh_auth=True)

# refresh_authentication method should be able to update api_params
self.assertEqual(response._api_params['token'], 'new_token')

0 comments on commit b101eaf

Please sign in to comment.