Skip to content

Commit

Permalink
it handles anonymous users; closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinogagliardi committed Dec 21, 2020
1 parent f227815 commit 5b8a68b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions monitus/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def __call__(self, request):
if response.status_code == HTTPStatus.FORBIDDEN and not settings.DEBUG:
path = request.get_full_path()
ip = request.META.get("REMOTE_ADDR", "<none>")
if not request.user.is_authenticated:
mail_admins(
"Got 403!",
f"Got error 403 on {path} from {ip}. User: Anonymous",
fail_silently=True,
)
return response
user = request.user
mail_admins(
"Got 403!",
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = django-monitus
version = 0.0.3
version = 0.0.4
description = Tiny error reporting middleware(s) for Django.
long_description = file: README.rst
long_description_content_type = text/x-rst
Expand Down
4 changes: 4 additions & 0 deletions tests/test_ middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def test_it_sends_email_to_admins(self):
self.assertIn(self.user.username, body)
self.assertIn(self.user.email, body)

def test_it_handles_anon_user(self):
self.client.logout()
self.client.get("/secret-area/")


class FailedLoginMiddlewareTest(SimpleTestCase):
def test_it_sends_email_to_admins(self):
Expand Down

0 comments on commit 5b8a68b

Please sign in to comment.