Skip to content

Commit

Permalink
Merge pull request #39 from Raekkeri/ignore-users
Browse files Browse the repository at this point in the history
Allow ignoring entry from specific users
  • Loading branch information
Raekkeri authored Jun 13, 2022
2 parents adcd7c9 + b49cd05 commit df729dc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions requestlogs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
'REQUEST_ID_HTTP_HEADER': None,
'METHODS': ('GET', 'PUT', 'PATCH', 'POST', 'DELETE'),
'JSON_ENSURE_ASCII': True,
'IGNORE_USER_FIELD': None,
'IGNORE_USERS': [],
}


Expand Down
8 changes: 8 additions & 0 deletions requestlogs/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,20 @@ def finalize(self, response):
self.request = self.django_request_handler(self.django_request)

self.response = self.response_handler(response)

if self.skip_entry():
return

self.store()

def store(self):
storage = SETTINGS['STORAGE_CLASS']()
storage.store(self)

def skip_entry(self):
if SETTINGS['IGNORE_USER_FIELD']:
return self.user.get(SETTINGS['IGNORE_USER_FIELD'], None) in SETTINGS['IGNORE_USERS']

@property
def user(self):
ret = {
Expand Down
45 changes: 39 additions & 6 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ def w_logging(self, request):

class SimpleAuth(BaseAuthentication):
def authenticate(self, request):
if request.META.get('HTTP_AUTHORIZATION') == 'Password 123':
return get_user_model().objects.get(), ('simple',)
auth = request.META.get('HTTP_AUTHORIZATION')
if auth:
return get_user_model().objects.get(password=auth), ('simple',)


class SetUserManually(APIView):
Expand Down Expand Up @@ -412,8 +413,8 @@ class UserSerializer(serializers.Serializer):
})
class TestAuthenticationAndPermissions(APITestCase):
def test_authenticated_user(self):
user = get_user_model().objects.create_user('u1')
self.client.credentials(HTTP_AUTHORIZATION='Password 123')
user = get_user_model().objects.create(username='u1', password='pw1')
self.client.credentials(HTTP_AUTHORIZATION='pw1')

with patch('tests.test_views.UserStorage.do_store') as mocked_store:
response = self.client.get('/user/')
Expand Down Expand Up @@ -462,6 +463,38 @@ def test_set_user_automatically_on_login(self):
'user': {'id': user.id, 'username': 'u1'}}


@override_settings(
ROOT_URLCONF=__name__,
REQUESTLOGS={
'STORAGE_CLASS': 'tests.test_views.UserStorage',
'IGNORE_USER_FIELD': 'username',
'IGNORE_USERS': ['u1'],
},
)
@modify_settings(MIDDLEWARE={
'append': 'requestlogs.middleware.RequestLogsMiddleware',
})
class TestIgnoreUser(APITestCase):
def test_ignore_entry_of_user(self):
user1 = get_user_model().objects.create(username='u1', password='pw1')
user2 = get_user_model().objects.create(username='other', password='pw2')

with patch('tests.test_views.TestStorage.do_store') as mocked_store:
self.client.credentials(HTTP_AUTHORIZATION='pw1')
response = self.client.get('/user/')
assert response.status_code == 200

assert mocked_store.call_args_list == []

with patch('tests.test_views.TestStorage.do_store') as mocked_store:
self.client.credentials(HTTP_AUTHORIZATION='pw2')
response = self.client.get('/user/')
assert response.status_code == 200

call, = mocked_store.call_args_list
assert call[0][0]['user']['id'] == user2.pk


@override_settings(
ROOT_URLCONF=__name__,
REQUESTLOGS={'STORAGE_CLASS': 'tests.test_views.TestStorage'},
Expand All @@ -472,8 +505,8 @@ def test_set_user_automatically_on_login(self):
class TestServerError(RequestLogsTestMixin, APITestCase):
def setUp(self):
super().setUp()
self.user = get_user_model().objects.create_user('u1')
self.client.credentials(HTTP_AUTHORIZATION='Password 123')
self.user = get_user_model().objects.create(username='u1', password='pw1')
self.client.credentials(HTTP_AUTHORIZATION='pw1')
self.expected = {
'action_name': None,
'request': {
Expand Down

0 comments on commit df729dc

Please sign in to comment.