File: test_login.py

package info (click to toggle)
python-django 3%3A6.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,992 kB
  • sloc: python: 371,353; javascript: 19,376; xml: 211; makefile: 187; sh: 28
file content (72 lines) | stat: -rw-r--r-- 2,813 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from django.contrib import auth
from django.contrib.auth.models import AnonymousUser, User
from django.http import HttpRequest
from django.test import TestCase
from django.utils.deprecation import RemovedInDjango61Warning


class TestLogin(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.user = User.objects.create_user(username="testuser", password="password")

    def setUp(self):
        self.request = HttpRequest()
        self.request.session = self.client.session

    def test_user_login(self):
        auth.login(self.request, self.user)
        self.assertEqual(self.request.session[auth.SESSION_KEY], str(self.user.pk))

    def test_inactive_user(self):
        self.user.is_active = False
        self.user.save(update_fields=["is_active"])

        auth.login(self.request, self.user)
        self.assertEqual(self.request.session[auth.SESSION_KEY], str(self.user.pk))

    # RemovedInDjango61Warning: When the deprecation ends, replace with:
    # def test_without_user(self):
    def test_without_user_no_request_user(self):
        # RemovedInDjango61Warning: When the deprecation ends, replace with:
        # with self.assertRaisesMessage(
        #     AttributeError,
        #     "'NoneType' object has no attribute 'get_session_auth_hash'",
        # ):
        #     auth.login(self.request, None)
        with (
            self.assertRaisesMessage(
                AttributeError,
                "'HttpRequest' object has no attribute 'user'",
            ),
            self.assertWarnsMessage(
                RemovedInDjango61Warning,
                "Fallback to request.user when user is None will be removed.",
            ),
        ):
            auth.login(self.request, None)

    # RemovedInDjango61Warning: When the deprecation ends, remove completely.
    def test_without_user_anonymous_request(self):
        self.request.user = AnonymousUser()
        with (
            self.assertRaisesMessage(
                AttributeError,
                "'AnonymousUser' object has no attribute '_meta'",
            ),
            self.assertWarnsMessage(
                RemovedInDjango61Warning,
                "Fallback to request.user when user is None will be removed.",
            ),
        ):
            auth.login(self.request, None)

    # RemovedInDjango61Warning: When the deprecation ends, remove completely.
    def test_without_user_authenticated_request(self):
        self.request.user = self.user
        self.assertNotIn(auth.SESSION_KEY, self.request.session)

        msg = "Fallback to request.user when user is None will be removed."
        with self.assertWarnsMessage(RemovedInDjango61Warning, msg):
            auth.login(self.request, None)
        self.assertEqual(self.request.session[auth.SESSION_KEY], str(self.user.pk))