| 1 |
from django.contrib.auth import get_user_model |
| 2 |
from django.core import validators |
| 3 |
from django.test import TestCase |
| 4 |
from django.test.client import Client |
| 5 |
from django.test.utils import override_settings |
| 6 |
from django.urls import reverse |
| 7 |
|
| 8 |
from allauth.account import app_settings |
| 9 |
from allauth.account.signals import user_logged_out |
| 10 |
from tests.mocking import Mock |
| 11 |
|
| 12 |
|
| 13 |
test_username_validators = [ |
| 14 |
validators.RegexValidator(regex=r"^[a-c]+$", message="not abc") |
| 15 |
] |
| 16 |
|
| 17 |
|
| 18 |
@override_settings( |
| 19 |
ACCOUNT_DEFAULT_HTTP_PROTOCOL="https", |
| 20 |
ACCOUNT_EMAIL_VERIFICATION=app_settings.EmailVerificationMethod.MANDATORY, |
| 21 |
ACCOUNT_LOGIN_METHODS={app_settings.LoginMethod.USERNAME}, |
| 22 |
ACCOUNT_SIGNUP_FORM_CLASS=None, |
| 23 |
ACCOUNT_EMAIL_SUBJECT_PREFIX=None, |
| 24 |
LOGIN_REDIRECT_URL="/accounts/profile/", |
| 25 |
ACCOUNT_SIGNUP_REDIRECT_URL="/accounts/welcome/", |
| 26 |
ACCOUNT_ADAPTER="allauth.account.adapter.DefaultAccountAdapter", |
| 27 |
ACCOUNT_USERNAME_REQUIRED=True, |
| 28 |
) |
| 29 |
class LogoutTests(TestCase): |
| 30 |
@override_settings(ACCOUNT_LOGOUT_ON_GET=True) |
| 31 |
def test_logout_view_on_get(self): |
| 32 |
c, resp = self._logout_view("get") |
| 33 |
self.assertTemplateUsed(resp, "account/messages/logged_out.txt") |
| 34 |
|
| 35 |
@override_settings(ACCOUNT_LOGOUT_ON_GET=False) |
| 36 |
def test_logout_view_on_post(self): |
| 37 |
c, resp = self._logout_view("get") |
| 38 |
self.assertTemplateUsed( |
| 39 |
resp, f"account/logout.{app_settings.TEMPLATE_EXTENSION}" |
| 40 |
) |
| 41 |
|
| 42 |
receiver_mock = Mock() |
| 43 |
user_logged_out.connect(receiver_mock) |
| 44 |
|
| 45 |
resp = c.post(reverse("account_logout")) |
| 46 |
|
| 47 |
self.assertTemplateUsed(resp, "account/messages/logged_out.txt") |
| 48 |
receiver_mock.assert_called_once_with( |
| 49 |
sender=get_user_model(), |
| 50 |
request=resp.wsgi_request, |
| 51 |
user=get_user_model().objects.get(username="john"), |
| 52 |
signal=user_logged_out, |
| 53 |
) |
| 54 |
|
| 55 |
user_logged_out.disconnect(receiver_mock) |
| 56 |
|
| 57 |
def _logout_view(self, method): |
| 58 |
c = Client() |
| 59 |
user = get_user_model().objects.create(username="john", is_active=True) |
| 60 |
user.set_password("doe") |
| 61 |
user.save() |
| 62 |
c = Client() |
| 63 |
c.login(username="john", password="doe") |
| 64 |
return c, getattr(c, method)(reverse("account_logout")) |