<commit_before>"""
Integration tests for the login handling.

TODO: Clean up the tests in this module.
"""

from django.test import override_settings, TestCase
from django.urls import reverse
from django.contrib.auth import get_user_model

from axes.conf import settings
from axes.models import AccessLog, AccessAttempt
from axes.tests.base import AxesTestCase


@override_settings(AXES_ENABLED=False)
class DjangoLoginTestCase(TestCase):
    def setUp(self):
        self.username = 'john.doe'
        self.password = '<PASSWORD>'

        self.user = get_user_model().objects.create(username=self.username)
        self.user.set_password(self.password)
        self.user.save()

    def test_login(self):
        self.client.login(username=self.username, password=self.password)

    def test_logout(self):
        self.client.login(username=self.username, password=<PASSWORD>)
        self.client.logout()

    def test_force_login(self):
        self.client.force_login(self.user)


class LoginTestCase(AxesTestCase):
    """
    Test for lockouts under different configurations and circumstances to prevent false positives and false negatives.

    Always block attempted logins for the same user from the same IP.
    Always allow attempted logins for a different user from a different IP.
    """

    IP_1 = '10.1.1.1'
    IP_2 = '10.2.2.2'
    USER_1 = 'valid-user-1'
    USER_2 = 'valid-user-2'
    EMAIL_1 = '<EMAIL>'
    EMAIL_2 = '<EMAIL>'

    VALID_USERNAME = USER_1
    VALID_EMAIL = EMAIL_1
    VALID_PASSWORD = '<PASSWORD>'

    VALID_IP_ADDRESS = IP_1

    WRONG_PASSWORD = '<PASSWORD>'
    LOCKED_MESSAGE = 'Account locked: too many login attempts.'
    LOGIN_FORM_KEY = '<input type="submit" value="Log in" />'
    ALLOWED = 302
    BLOCKED = 403

    def _login(self, username, password, ip_addr='127.0.0.1', **kwargs):<commit_msg>Add tests for django.contrib.auth login and logout<commit_after>"""
Integration tests for the login handling.

TODO: Clean up the tests in this module.
"""

from importlib import import_module

from django.http import HttpRequest
from django.test import override_settings, TestCase
from django.urls import reverse
from django.contrib.auth import get_user_model, login, logout

from axes.conf import settings
from axes.models import AccessLog, AccessAttempt
from axes.tests.base import AxesTestCase


class DjangoLoginTestCase(TestCase):
    def setUp(self):
        engine = import_module(settings.SESSION_ENGINE)

        self.request = HttpRequest()
        self.request.session = engine.SessionStore()

        self.username = 'john.doe'
        self.password = '<PASSWORD>'

        self.user = get_user_model().objects.create(username=self.username)
        self.user.set_password(self.password)
        self.user.save()
        self.user.backend = 'django.contrib.auth.backends.ModelBackend'


class DjangoContribAuthLoginTestCase(DjangoLoginTestCase):
    def test_login(self):
        login(self.request, self.user)

    def test_logout(self):
        login(self.request, self.user)
        logout(self.request)


@override_settings(AXES_ENABLED=False)
class DjangoTestClientLoginTestCase(DjangoLoginTestCase):
    def test_client_login(self):
        self.client.login(username=self.username, password=self.password)

    def test_client_logout(self):
        self.client.login(username=self.username, password=self.password)
        self.client.logout()

    def test_client_force_login(self):
        self.client.force_login(self.user)


class LoginTestCase(AxesTestCase):
    """
    Test for lockouts under different configurations and circumstances to prevent false positives and false negatives.

    Always block attempted logins for the same user from the same IP.
    Always allow attempted logins for a different user from a different IP.
    """

    IP_1 = '10.1.1.1'
    IP_2 = '10.2.2.2'
    USER_1 = 'valid-user-1'
    USER_2 = 'valid-user-2'
    EMAIL_1 = '<EMAIL>'
    EMAIL_2 = '<EMAIL>'

    VALID_USERNAME = USER_1
    VALID_EMAIL = EMAIL_1
    VALID_PASSWORD = '<PASSWORD>'

    VALID_IP_ADDRESS = IP_1

    WRONG_PASSWORD = '<PASSWORD>'
    LOCKED_MESSAGE = 'Account locked: too many login attempts.'
    LOGIN_FORM_KEY = '<input type="submit" value="Log in" />'
    ALLOWED = 302
    BLOCKED = 403

    def _login(self, username, password, ip_addr='127.0.0.1', **kwargs):
{'fraction_non_alphanumeric': 0.09917355371900827, 'fraction_numerical': 0.015151515151515152, 'mean_word_length': 3.543274244004171, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6505026', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1317, 'n_words': 338}