Hello!
Does anyone have a clue of how I can test the logging that is saved when a user_login_failed signal is sent?
- Is the user_login_failed signal sent when the form validation fails?
- Or is the signal sent when the authenticate() function fails?
This is the test case:
class TestLogUserLoggedInFailedSignal(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.signin_page_url = reverse("auth_app:signin")
self.password = "TheWorldIsFlat123"
self.user = baker.make(Member, username="test_user", password=self.password, is_active=False)
self.account_settings = baker.make(AccountSettings, member=self.user)
self.sign_in_history = baker.make(SignInHistory, user=self.user)
self.user.set_password(self.password)
self.user.save()
@patch("auth_app.signals.user_login_failed")
def test_user_login_failed_signal(self, mock_info_logger):
with self.settings(DJANGO_ENV="Production"):
request = self.factory.post(self.signin_page_url, {'username': self.user.username, 'password': 'WrongPassword'})
user = authenticate(username=self.user.username, password="ThisIsAWrongPassword")
request.user = AnonymousUser()
form = AllowInactiveUsersToSignInForm(data={"username": self.user.username, "password": "WrongPassword"})
self.assertFalse(form.is_valid())
self.assertIsNone(user)
mock_info_logger.assert_called()
self.assertEqual(mock_info_logger.call_count, 1)
And this is what I want to test:
signin_failed_logger = logging.getLogger("failed_signin")
prod_or_dev = os.environ.get('DJANGO_ENV', 'development')
@receiver(user_login_failed)
def log_user_login_failed(credentials, request, **kwargs):
if prod_or_dev == "production":
signin_failed_logger.info(f'User login failed: {credentials.get("username")} from IP: {request.META.get("REMOTE_ADDR")}')