|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# See https://github.com/aboutcode-org/django-altcha for support or download. |
| 5 | +# See https://aboutcode.org for more information about AboutCode FOSS projects. |
| 6 | +# |
| 7 | + |
| 8 | +from unittest import mock |
| 9 | + |
| 10 | +from django import forms |
| 11 | +from django.core.exceptions import ImproperlyConfigured |
| 12 | +from django.test import TestCase |
| 13 | +from django.test import override_settings |
| 14 | + |
| 15 | +from django_altcha import AltchaField |
| 16 | + |
| 17 | +from .test_field import make_valid_payload |
| 18 | + |
| 19 | + |
| 20 | +class AltchaFieldLoggingTest(TestCase): |
| 21 | + def setUp(self): |
| 22 | + class TestForm(forms.Form): |
| 23 | + altcha_field = AltchaField() |
| 24 | + |
| 25 | + self.form_class = TestForm |
| 26 | + |
| 27 | + @mock.patch("altcha.verify_solution") |
| 28 | + def test_invalid_token_logs_warning(self, mock_verify_solution): |
| 29 | + mock_verify_solution.return_value = (False, "bad signature") |
| 30 | + form = self.form_class(data={"altcha_field": "anything"}) |
| 31 | + with self.assertLogs("django_altcha", level="WARNING") as captured: |
| 32 | + form.is_valid() |
| 33 | + self.assertEqual(len(captured.records), 1) |
| 34 | + self.assertEqual(captured.records[0].levelname, "WARNING") |
| 35 | + self.assertIn("bad signature", captured.output[0]) |
| 36 | + |
| 37 | + @mock.patch("altcha.verify_solution") |
| 38 | + def test_verification_exception_is_logged_with_traceback( |
| 39 | + self, mock_verify_solution |
| 40 | + ): |
| 41 | + mock_verify_solution.side_effect = RuntimeError("boom") |
| 42 | + form = self.form_class(data={"altcha_field": "anything"}) |
| 43 | + with self.assertLogs("django_altcha", level="ERROR") as captured: |
| 44 | + form.is_valid() |
| 45 | + self.assertEqual(captured.records[0].levelname, "ERROR") |
| 46 | + # Confirms traceback is captured |
| 47 | + self.assertIsNotNone(captured.records[0].exc_info) |
| 48 | + |
| 49 | + @mock.patch("altcha.verify_solution") |
| 50 | + def test_replay_attempt_logs_warning(self, mock_verify_solution): |
| 51 | + mock_verify_solution.return_value = (True, None) |
| 52 | + valid_payload = make_valid_payload(challenge="replay-test-1") |
| 53 | + # First submission succeeds and marks the challenge as used. |
| 54 | + self.form_class(data={"altcha_field": valid_payload}).is_valid() |
| 55 | + # Second submission should log a replay warning. |
| 56 | + form = self.form_class(data={"altcha_field": valid_payload}) |
| 57 | + with self.assertLogs("django_altcha", level="WARNING") as captured: |
| 58 | + form.is_valid() |
| 59 | + self.assertTrue( |
| 60 | + any("replay" in r.getMessage().lower() for r in captured.records) |
| 61 | + ) |
| 62 | + |
| 63 | + @override_settings(ALTCHA_HMAC_KEY=None) |
| 64 | + def test_missing_hmac_key_logs_error(self): |
| 65 | + from django_altcha import get_hmac_key |
| 66 | + |
| 67 | + with self.assertLogs("django_altcha", level="ERROR") as captured: |
| 68 | + with self.assertRaises(ImproperlyConfigured): |
| 69 | + get_hmac_key() |
| 70 | + self.assertEqual(captured.records[0].levelname, "ERROR") |
0 commit comments