Skip to content

Commit

Permalink
Gracefully handle Misfit errors while enabling.
Browse files Browse the repository at this point in the history
  • Loading branch information
grokcode committed Jan 7, 2015
1 parent 87ebde3 commit cd8558e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 12 additions & 1 deletion misfitapp/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.http import HttpRequest
from misfit import Misfit
from misfit.auth import MisfitAuth
from misfit.exceptions import MisfitRateLimitError
from mock import patch

from misfitapp import utils
Expand Down Expand Up @@ -148,9 +149,11 @@ def setUp(self):
super(TestCompleteView, self).setUp()
self.misfit_user.delete()

def _get(self, use_token=True, use_verifier=True, **kwargs):
def _get(self, use_token=True, use_verifier=True, use_limiting=False, **kwargs):
MisfitAuth.fetch_token = mock.MagicMock(return_value=self.access_token)
Misfit.profile = mock.MagicMock(return_value=self.profile)
if use_limiting:
Misfit.profile.side_effect = MisfitRateLimitError(429, 'Ooopsy.')
if use_token:
self._set_session_vars(state=self.state)
get_kwargs = kwargs.pop('get_kwargs', {})
Expand All @@ -160,6 +163,14 @@ def _get(self, use_token=True, use_verifier=True, **kwargs):
return super(TestCompleteView, self)._get(get_kwargs=get_kwargs,
**kwargs)



def test_ratelimiting(self):
with patch('celery.app.task.Task.delay') as mock_delay:
response = self._get(use_limiting=True)
self.assertRedirectsNoFollow(response, reverse('misfit-error'))


def test_get(self):
"""
Complete view should fetch & store the user's access token and add
Expand Down
7 changes: 4 additions & 3 deletions misfitapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ def complete(request):
redirect_uri=redirect_uri)
access_token = auth.fetch_token(request.GET['code'],
request.GET['state'])
misfit = utils.create_misfit(access_token)
profile = misfit.profile()
except:
return redirect(reverse('misfit-error'))

misfit = utils.create_misfit(access_token)
profile = misfit.profile()

user_updates = {'access_token': access_token,
'misfit_user_id': profile.userId}
misfit_user = MisfitUser.objects.filter(user=request.user)
Expand All @@ -88,7 +89,7 @@ def complete(request):
# Add the Misfit user info to the session
request.session['misfit_profile'] = profile.data

# Import their data TODO: Fix failing tests, when below line is uncommented
# Import their data
import_historical.delay(misfit_user)

next_url = request.session.pop('misfit_next', None) or utils.get_setting(
Expand Down

0 comments on commit cd8558e

Please sign in to comment.