Skip to content

Commit b1f6f88

Browse files
committed
Merge pull request #88 from orcasgit/tests-fixes
Fix tests and upgrade release version
2 parents ef5c8c3 + 691b0be commit b1f6f88

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.2.2 (2016-03-30)
2+
==================
3+
* Refresh token bugfixes
4+
15
0.2.1 (2016-03-28)
26
==================
37
* Update requirements to use requests-oauthlib>=0.6.1

fitbit/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
__copyright__ = 'Copyright 2012-2015 ORCAS'
1818
__license__ = 'Apache 2.0'
1919

20-
__version__ = '0.2.1'
21-
__release__ = '0.2.1'
20+
__version__ = '0.2.2'
21+
__release__ = '0.2.2'
2222

2323
# Module namespace.
2424

fitbit/api.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
import base64
32
import datetime
43
import json
54
import requests
@@ -11,7 +10,6 @@
1110
from urllib import urlencode
1211

1312
from requests_oauthlib import OAuth2, OAuth2Session
14-
from oauthlib.oauth2 import TokenExpiredError
1513
from fitbit.exceptions import (BadResponse, DeleteError, HTTPBadRequest,
1614
HTTPUnauthorized, HTTPForbidden,
1715
HTTPServerError, HTTPConflict, HTTPNotFound,

fitbit_tests/test_auth.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from unittest import TestCase
22
from fitbit import Fitbit, FitbitOauth2Client
3+
from fitbit.exceptions import HTTPUnauthorized
34
import mock
45
from requests_oauthlib import OAuth2Session
5-
from oauthlib.oauth2 import TokenExpiredError
66

77

88
class 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

Comments
 (0)