Skip to content

Commit

Permalink
Handle situations where request has no User object, closes #1746 (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
alukach authored and amplifi committed Aug 31, 2017
1 parent cad9e01 commit 7216a13
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cadasta/accounts/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
class UserLanguageMiddleware(object):

def process_response(self, request, response):
if not hasattr(request, 'user'):
return response

if not request.user.is_authenticated:
return response

Expand Down
13 changes: 13 additions & 0 deletions cadasta/accounts/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,16 @@ def test_process_response_activate_user_language(self, mock_translation):
assert 1 == mock_translation.activate.call_count
assert 'fr' == session_lang[self.LANGUAGE_SESSION_KEY]
assert res is self.mock_response

@patch.object(middleware, 'translation')
def test_process_response_wsgi_request(self, mock_translation):
"""
If Auth middleware isn't reached (eg if we return a redirect to
an endpoint with an appended slash if a slash is ommited from a
URL and settings.APPEND_SLASH=True), request will be a
WSGIRequest that does not contain a user property.
"""
del self.mock_request.user
res = self.ulm.process_response(self.mock_request, self.mock_response)
assert 0 == mock_translation.activate.call_count
assert res is self.mock_response

0 comments on commit 7216a13

Please sign in to comment.