diff --git a/misfitapp/defaults.py b/misfitapp/defaults.py index 4d84d56..18b40bb 100644 --- a/misfitapp/defaults.py +++ b/misfitapp/defaults.py @@ -10,8 +10,12 @@ # removed. MISFIT_LOGOUT_REDIRECT = '/' +# Where to redirect to if there is an error while authenticating a Misfit +# user. +MISFIT_ERROR_REDIRECT = None + # The template to use when an unavoidable error occurs during Misfit -# integration. +# integration. This setting is ignored if MISFIT_ERROR_REDIRECT is set. MISFIT_ERROR_TEMPLATE = 'misfit/error.html' # The default message used by the misfit_integration_warning decorator to diff --git a/misfitapp/tests/test_integration.py b/misfitapp/tests/test_integration.py index df5a29d..e9b7f66 100644 --- a/misfitapp/tests/test_integration.py +++ b/misfitapp/tests/test_integration.py @@ -163,6 +163,13 @@ def _get(self, use_token=True, use_verifier=True, use_limiting=False, **kwargs): return super(TestCompleteView, self)._get(get_kwargs=get_kwargs, **kwargs) + def test_error_redirect(self): + """ Complete view should redirect to MISFIT_ERROR_REDIRECT if set. """ + url = '/' + with patch('celery.app.task.Task.delay') as mock_delay: + with self.settings(MISFIT_ERROR_REDIRECT=url): + response = self._get(use_limiting=True) + self.assertRedirectsNoFollow(response, url) def test_ratelimiting(self): diff --git a/misfitapp/views.py b/misfitapp/views.py index 4577df6..b9ba4bc 100644 --- a/misfitapp/views.py +++ b/misfitapp/views.py @@ -54,7 +54,9 @@ def complete(request): After the user authorizes us, Misfit sends a callback to this URL to complete authentication. - If there was an error, the user is redirected again to the `error` view. + If there was an error, the user is redirected to the url set in + :ref:`MISFIT_ERROR_REDIRECT` if it is set, or to the `error` view + otherwise. If the authorization was successful, the credentials are stored for us to use later, and the user is redirected. If 'next_url' is in the request @@ -74,7 +76,8 @@ def complete(request): misfit = utils.create_misfit(access_token) profile = misfit.profile() except: - return redirect(reverse('misfit-error')) + next_url = utils.get_setting('MISFIT_ERROR_REDIRECT') or reverse('misfit-error') + return redirect(next_url) user_updates = {'access_token': access_token,