From cd8558e1188ae305b595617f249ae4e54ba99fab Mon Sep 17 00:00:00 2001 From: Jess Johnson Date: Wed, 7 Jan 2015 14:47:39 -0500 Subject: [PATCH] Gracefully handle Misfit errors while enabling. --- misfitapp/tests/test_integration.py | 13 ++++++++++++- misfitapp/views.py | 7 ++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/misfitapp/tests/test_integration.py b/misfitapp/tests/test_integration.py index c83abd1..df5a29d 100644 --- a/misfitapp/tests/test_integration.py +++ b/misfitapp/tests/test_integration.py @@ -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 @@ -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', {}) @@ -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 diff --git a/misfitapp/views.py b/misfitapp/views.py index 1570daa..4577df6 100644 --- a/misfitapp/views.py +++ b/misfitapp/views.py @@ -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) @@ -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(