Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit 4b0e6f7

Browse files
committed
fix(flask): Use request.remote_addr as fallback for flask too, fix tests
1 parent bdaf68c commit 4b0e6f7

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

raven/contrib/flask.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,15 @@ def get_user_info(self, request):
143143
Requires Flask-Login (https://pypi.python.org/pypi/Flask-Login/)
144144
to be installed and setup.
145145
"""
146+
user_info = {}
147+
146148
try:
147-
user_info = {
148-
'ip_address': request.access_route[0],
149-
}
149+
ip_address = request.access_route[0]
150150
except IndexError:
151-
user_info = {}
151+
ip_address = request.remote_addr
152+
153+
if ip_address:
154+
user_info['ip_address'] = ip_address
152155

153156
if not has_flask_login:
154157
return user_info

tests/contrib/django/tests.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,7 @@ def test_user_info(self):
234234
assert 'user' in event
235235

236236
user_info = event['user']
237-
assert user_info == {
238-
{'ip_address': '127.0.0.1'}
239-
}
237+
assert user_info == {'ip_address': '127.0.0.1'}
240238

241239
assert self.client.login(username='admin', password='password')
242240

@@ -247,6 +245,7 @@ def test_user_info(self):
247245
assert 'user' in event
248246
user_info = event['user']
249247
assert user_info == {
248+
'ip_address': '127.0.0.1',
250249
'username': self.user.username,
251250
'id': self.user.id,
252251
'email': self.user.email,

tests/contrib/flask/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def setup_login(self):
313313

314314
def test_user(self):
315315
self.client.get('/login/')
316-
self.client.get('/an-error/')
316+
self.client.get('/an-error/', environ_overrides={'REMOTE_ADDR': '127.0.0.1'})
317317
event = self.raven.events.pop(0)
318318
assert event['message'] == 'ValueError: hello world'
319319
assert 'request' in event

0 commit comments

Comments
 (0)