File: test_print_user_for_session.py

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (108 lines) | stat: -rw-r--r-- 3,809 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
from importlib import import_module
from io import StringIO

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.management import CommandError, call_command
from django.test import TestCase

from unittest.mock import patch


class PrintUserForSessionExceptionsTests(TestCase):
    """Test if print_user_for_session command raises exception."""

    def test_should_raise_CommandError_if_session_key_contains_exclamination_mark(self):
        with self.assertRaisesRegex(CommandError, "malformed session key"):
            call_command("print_user_for_session", "l6hxnwblpvrfu8bohelmqjj4soyo2r!?")


class PrintUserForSessionTests(TestCase):
    """Test for print_user_for_session command."""

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

    @patch("sys.stdout", new_callable=StringIO)
    def test_should_print_Session_Key_does_not_exist_or_expired(self, m_stdout):
        call_command("print_user_for_session", "l6hxnwblpvrfu8bohelmqjj4soyo2r12")

        self.assertIn("Session Key does not exist. Expired?", m_stdout.getvalue())

    @patch("sys.stdout", new_callable=StringIO)
    def test_should_print_that_there_is_no_user_associated_with_given_session(
        self, m_stdout
    ):
        session = self.engine.SessionStore()
        session.update(
            {
                "_auth_user_backend": "django.contrib.auth.backends.ModelBackend",
                "_auth_user_hash": "b67352fde8582b12f068c10fd9d29f9fa1af0459",
            }
        )
        session.create()

        call_command("print_user_for_session", session.session_key)

        self.assertIn("No user associated with session", m_stdout.getvalue())

    @patch("sys.stdout", new_callable=StringIO)
    def test_should_print_that_there_is_no_backend_associated_with_given_session(
        self, m_stdout
    ):
        session = self.engine.SessionStore()
        session.update(
            {
                "_auth_user_id": 1234,
                "_auth_user_hash": "b67352fde8582b12f068c10fd9d29f9fa1af0459",
            }
        )
        session.create()

        call_command("print_user_for_session", session.session_key)

        self.assertIn(
            "No authentication backend associated with session", m_stdout.getvalue()
        )

    @patch("sys.stdout", new_callable=StringIO)
    def test_should_print_that_there_is_no_user_associated_with_id(self, m_stdout):
        session = self.engine.SessionStore()
        session.update(
            {
                "_auth_user_id": 1234,
                "_auth_user_backend": "django.contrib.auth.backends.ModelBackend",
                "_auth_user_hash": "b67352fde8582b12f068c10fd9d29f9fa1af0459",
            }
        )
        session.create()

        call_command("print_user_for_session", session.session_key)

        self.assertIn("No user associated with that id.", m_stdout.getvalue())

    @patch("sys.stdout", new_callable=StringIO)
    def test_should_print_user_info_for_session(self, m_stdout):
        user = get_user_model().objects.create(
            first_name="John", last_name="Doe", username="foobar", email="foo@bar.com"
        )
        session = self.engine.SessionStore()
        session.update(
            {
                "_auth_user_id": user.pk,
                "_auth_user_backend": "django.contrib.auth.backends.ModelBackend",
                "_auth_user_hash": "b67352fde8582b12f068c10fd9d29f9fa1af0459",
            }
        )
        session.create()
        expected_out = """User id: {}
full name: John Doe
short name: John
username: foobar
email: foo@bar.com
""".format(user.pk)

        call_command("print_user_for_session", session.session_key)

        self.assertIn(expected_out, m_stdout.getvalue())