Skip to content

Commit

Permalink
[#105509372] Fix tests and upgrade release version
Browse files Browse the repository at this point in the history
  • Loading branch information
percyperez committed Mar 30, 2016
1 parent ef5c8c3 commit 691b0be
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.2.2 (2016-03-30)
==================
* Refresh token bugfixes

0.2.1 (2016-03-28)
==================
* Update requirements to use requests-oauthlib>=0.6.1
Expand Down
4 changes: 2 additions & 2 deletions fitbit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
__copyright__ = 'Copyright 2012-2015 ORCAS'
__license__ = 'Apache 2.0'

__version__ = '0.2.1'
__release__ = '0.2.1'
__version__ = '0.2.2'
__release__ = '0.2.2'

# Module namespace.

Expand Down
2 changes: 0 additions & 2 deletions fitbit/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import base64
import datetime
import json
import requests
Expand All @@ -11,7 +10,6 @@
from urllib import urlencode

from requests_oauthlib import OAuth2, OAuth2Session
from oauthlib.oauth2 import TokenExpiredError
from fitbit.exceptions import (BadResponse, DeleteError, HTTPBadRequest,
HTTPUnauthorized, HTTPForbidden,
HTTPServerError, HTTPConflict, HTTPNotFound,
Expand Down
36 changes: 23 additions & 13 deletions fitbit_tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from unittest import TestCase
from fitbit import Fitbit, FitbitOauth2Client
from fitbit.exceptions import HTTPUnauthorized
import mock
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import TokenExpiredError


class Auth2Test(TestCase):
"""Add tests for auth part of API
mock the oauth library calls to simulate various responses,
make sure we call the right oauth calls, respond correctly based on the responses
make sure we call the right oauth calls, respond correctly based on the
responses
"""
client_kwargs = {
'client_id': 'fake_id',
Expand Down Expand Up @@ -60,8 +61,8 @@ def test_refresh_token(self):
self.assertEqual("fake_return_refresh_token", retval['refresh_token'])

def test_auto_refresh_token_exception(self):
# test of auto_refresh with tokenExpired exception
# 1. first call to _request causes a TokenExpired
"""Test of auto_refresh with Unauthorized exception"""
# 1. first call to _request causes a HTTPUnauthorized
# 2. the token_refresh call is faked
# 3. the second call to _request returns a valid value
kwargs = self.client_kwargs
Expand All @@ -70,21 +71,26 @@ def test_auto_refresh_token_exception(self):

fb = Fitbit(**kwargs)
with mock.patch.object(FitbitOauth2Client, '_request') as r:
r.side_effect = [TokenExpiredError, fake_response(200, 'correct_response')]
r.side_effect = [
HTTPUnauthorized(fake_response(401, b'correct_response')),
fake_response(200, 'correct_response')
]
with mock.patch.object(OAuth2Session, 'refresh_token') as rt:
rt.return_value = {
'access_token': 'fake_return_access_token',
'refresh_token': 'fake_return_refresh_token'
}
retval = fb.client.make_request(Fitbit.API_ENDPOINT + '/1/user/-/profile.json')
self.assertEqual("correct_response", retval.text)
self.assertEqual("fake_return_access_token", fb.client.token['access_token'])
self.assertEqual("fake_return_refresh_token", fb.client.token['refresh_token'])
self.assertEqual(
"fake_return_access_token", fb.client.token['access_token'])
self.assertEqual(
"fake_return_refresh_token", fb.client.token['refresh_token'])
self.assertEqual(1, rt.call_count)
self.assertEqual(2, r.call_count)

def test_auto_refresh_token_nonException(self):
# test of auto_refersh when the exception doesn't fire
def test_auto_refresh_token_non_exception(self):
"""Test of auto_refersh when the exception doesn't fire"""
# 1. first call to _request causes a 401 expired token response
# 2. the token_refresh call is faked
# 3. the second call to _request returns a valid value
Expand All @@ -94,17 +100,21 @@ def test_auto_refresh_token_nonException(self):

fb = Fitbit(**kwargs)
with mock.patch.object(FitbitOauth2Client, '_request') as r:
r.side_effect = [fake_response(401, b'{"errors": [{"message": "Access token invalid or expired: some_token_goes_here", "errorType": "oauth", "fieldName": "access_token"}]}'),
fake_response(200, 'correct_response')]
r.side_effect = [
fake_response(401, b'{"errors": [{"message": "Access token expired: some_token_goes_here", "errorType": "expired_token", "fieldName": "access_token"}]}'),
fake_response(200, 'correct_response')
]
with mock.patch.object(OAuth2Session, 'refresh_token') as rt:
rt.return_value = {
'access_token': 'fake_return_access_token',
'refresh_token': 'fake_return_refresh_token'
}
retval = fb.client.make_request(Fitbit.API_ENDPOINT + '/1/user/-/profile.json')
self.assertEqual("correct_response", retval.text)
self.assertEqual("fake_return_access_token", fb.client.token['access_token'])
self.assertEqual("fake_return_refresh_token", fb.client.token['refresh_token'])
self.assertEqual(
"fake_return_access_token", fb.client.token['access_token'])
self.assertEqual(
"fake_return_refresh_token", fb.client.token['refresh_token'])
self.assertEqual(1, rt.call_count)
self.assertEqual(2, r.call_count)

Expand Down

0 comments on commit 691b0be

Please sign in to comment.