diff --git a/src/paperless/signals.py b/src/paperless/signals.py index 9dc3383e3..f6dccd7a6 100644 --- a/src/paperless/signals.py +++ b/src/paperless/signals.py @@ -12,21 +12,21 @@ def handle_failed_login(sender, credentials, request, **kwargs): client_ip, _ = ipware.get_client_ip( meta=request.META, ) - username = credentials.get("username") or "anonymous" + username = credentials.get("username") + log_output = ( + "No authentication provided" + if username is None + else f"Login failed for user `{username}`" + ) if client_ip is None: - logger.info( - f"Login failed for user `{username}`. Unable to determine IP address.", - ) + log_output += ". Unable to determine IP address." else: if client_ip.is_global: # We got the client's IP address - logger.info( - f"Login failed for user `{username}` from IP `{client_ip}.`", - ) + log_output += f" from IP `{client_ip}.`" else: # The client's IP address is private - logger.info( - f"Login failed for user `{username}`" - f" from private IP `{client_ip}.`", - ) + log_output += f" from private IP `{client_ip}.`" + + logger.info(log_output) diff --git a/src/paperless/tests/test_signals.py b/src/paperless/tests/test_signals.py index 0b1ca1b22..e9e9eb43e 100644 --- a/src/paperless/tests/test_signals.py +++ b/src/paperless/tests/test_signals.py @@ -12,6 +12,26 @@ class TestFailedLoginLogging(TestCase): "username": "john lennon", } + def test_unauthenticated(self): + """ + GIVEN: + - Request with no authentication provided + WHEN: + - Request provided to signal handler + THEN: + - Unable to determine logged for unauthenticated user + """ + request = HttpRequest() + request.META = {} + with self.assertLogs("paperless.auth") as logs: + handle_failed_login(None, {}, request) + self.assertEqual( + logs.output, + [ + "INFO:paperless.auth:No authentication provided. Unable to determine IP address.", + ], + ) + def test_none(self): """ GIVEN: