Skip to content

Commit

Permalink
Add tests and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hvelarde committed Jun 26, 2017
1 parent 7b8eaf5 commit 5cbf8e1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions CHANGES.rst
Expand Up @@ -4,12 +4,13 @@ Changelog
1.3b3 (unreleased)
------------------

- Fixed issue that lead to error when logging is run by anonymous user
- Fix `AttributeError` when logging activity from anonymous users (fixes `#57 <https://github.com/collective/collective.fingerpointing/issues/57>`_).
[keul]

- Added support for ``HTTP_X_FORWARDED_FOR`` when getting request informations
- Add support for ``HTTP_X_FORWARDED_FOR`` request header to log real client IP addresses.
[keul]


1.3b2 (2017-05-25)
------------------

Expand Down
5 changes: 5 additions & 0 deletions src/collective/fingerpointing/tests/test_utils.py
Expand Up @@ -16,6 +16,11 @@ def test_get_request_information(self):
self.assertEqual(
get_request_information(), ('test_user_1_', 'None'))

def test_get_request_information_anonymous(self):
from plone.app.testing import logout
logout()
self.assertEqual(get_request_information(), ('-', 'None'))

def test_get_request_information_cloudflare(self):
self.request.environ['HTTP_CF_CONNECTING_IP'] = '192.168.1.1'
self.assertEqual(
Expand Down
10 changes: 4 additions & 6 deletions src/collective/fingerpointing/utils.py
Expand Up @@ -6,12 +6,10 @@
def get_request_information():
"""Return logged in user name and remote IP address."""
request = getRequest()
user = api.user.get_current()
try:
user_id = user.getMemberId()
user_id = api.user.get_current().getMemberId()
except AttributeError:
# Anonymous user don't have getMemberId
user_id = user.getUserName()
user_id = '-' # anonymous user

# honor Cloudflare real client IP address request header if present
# see: https://support.cloudflare.com/hc/en-us/articles/200170986
Expand All @@ -21,6 +19,6 @@ def get_request_information():
elif 'HTTP_X_FORWARDED_FOR' in request.environ:
ip = request.environ['HTTP_X_FORWARDED_FOR']
else:
ip = request.getClientAddr() or 'None'
ip = request.getClientAddr() or 'None' # return 'None' on tests

return user_id, ip # returns ('test_user_1_', 'None') on tests
return user_id, ip

0 comments on commit 5cbf8e1

Please sign in to comment.