authentik.core.tests.test_hash_password_command

Tests for hash_password management command.

 1"""Tests for hash_password management command."""
 2
 3from io import StringIO
 4from unittest.mock import patch
 5
 6from django.contrib.auth.hashers import check_password
 7from django.core.management import call_command
 8from django.core.management.base import CommandError
 9from django.test import TestCase
10
11
12class TestHashPasswordCommand(TestCase):
13    """Test hash_password management command."""
14
15    def test_hash_password_stdin(self):
16        """Test hashing a password read from standard input."""
17        out = StringIO()
18        with patch(
19            "authentik.core.management.commands.hash_password.sys.stdin",
20            StringIO("test123\n"),
21        ):
22            call_command("hash_password", stdout=out)
23        hashed = out.getvalue().strip()
24
25        self.assertTrue(hashed.startswith("pbkdf2_sha256$"))
26        self.assertTrue(check_password("test123", hashed))
27
28    def test_hash_password_prompt(self):
29        """Test hashing a password entered through the hidden prompt."""
30        out = StringIO()
31        with (
32            patch(
33                "authentik.core.management.commands.hash_password.getpass",
34                side_effect=["test123", "test123"],
35            ),
36            patch(
37                "authentik.core.management.commands.hash_password.sys.stdin.isatty",
38                return_value=True,
39            ),
40        ):
41            call_command("hash_password", stdout=out)
42        hashed = out.getvalue().strip()
43
44        self.assertTrue(hashed.startswith("pbkdf2_sha256$"))
45        self.assertTrue(check_password("test123", hashed))
46
47    def test_hash_password_empty_stdin_fails(self):
48        """Test that an empty password read from standard input raises an error."""
49        with (
50            patch(
51                "authentik.core.management.commands.hash_password.sys.stdin",
52                StringIO("\n"),
53            ),
54            self.assertRaises(CommandError) as ctx,
55        ):
56            call_command("hash_password")
57
58        self.assertIn("Password cannot be empty", str(ctx.exception))
59
60    def test_hash_password_empty_prompt_fails(self):
61        """Test that an empty prompted password raises an error."""
62        with (
63            patch(
64                "authentik.core.management.commands.hash_password.getpass",
65                side_effect=["", ""],
66            ),
67            patch(
68                "authentik.core.management.commands.hash_password.sys.stdin.isatty",
69                return_value=True,
70            ),
71            self.assertRaises(CommandError) as ctx,
72        ):
73            call_command("hash_password")
74
75        self.assertIn("Password cannot be empty", str(ctx.exception))
76
77    def test_hash_password_mismatched_prompt_fails(self):
78        """Test that mismatched prompted passwords raise an error."""
79        with (
80            patch(
81                "authentik.core.management.commands.hash_password.getpass",
82                side_effect=["test123", "different"],
83            ),
84            patch(
85                "authentik.core.management.commands.hash_password.sys.stdin.isatty",
86                return_value=True,
87            ),
88            self.assertRaises(CommandError) as ctx,
89        ):
90            call_command("hash_password")
91
92        self.assertIn("Passwords do not match", str(ctx.exception))
class TestHashPasswordCommand(django.test.testcases.TestCase):
13class TestHashPasswordCommand(TestCase):
14    """Test hash_password management command."""
15
16    def test_hash_password_stdin(self):
17        """Test hashing a password read from standard input."""
18        out = StringIO()
19        with patch(
20            "authentik.core.management.commands.hash_password.sys.stdin",
21            StringIO("test123\n"),
22        ):
23            call_command("hash_password", stdout=out)
24        hashed = out.getvalue().strip()
25
26        self.assertTrue(hashed.startswith("pbkdf2_sha256$"))
27        self.assertTrue(check_password("test123", hashed))
28
29    def test_hash_password_prompt(self):
30        """Test hashing a password entered through the hidden prompt."""
31        out = StringIO()
32        with (
33            patch(
34                "authentik.core.management.commands.hash_password.getpass",
35                side_effect=["test123", "test123"],
36            ),
37            patch(
38                "authentik.core.management.commands.hash_password.sys.stdin.isatty",
39                return_value=True,
40            ),
41        ):
42            call_command("hash_password", stdout=out)
43        hashed = out.getvalue().strip()
44
45        self.assertTrue(hashed.startswith("pbkdf2_sha256$"))
46        self.assertTrue(check_password("test123", hashed))
47
48    def test_hash_password_empty_stdin_fails(self):
49        """Test that an empty password read from standard input raises an error."""
50        with (
51            patch(
52                "authentik.core.management.commands.hash_password.sys.stdin",
53                StringIO("\n"),
54            ),
55            self.assertRaises(CommandError) as ctx,
56        ):
57            call_command("hash_password")
58
59        self.assertIn("Password cannot be empty", str(ctx.exception))
60
61    def test_hash_password_empty_prompt_fails(self):
62        """Test that an empty prompted password raises an error."""
63        with (
64            patch(
65                "authentik.core.management.commands.hash_password.getpass",
66                side_effect=["", ""],
67            ),
68            patch(
69                "authentik.core.management.commands.hash_password.sys.stdin.isatty",
70                return_value=True,
71            ),
72            self.assertRaises(CommandError) as ctx,
73        ):
74            call_command("hash_password")
75
76        self.assertIn("Password cannot be empty", str(ctx.exception))
77
78    def test_hash_password_mismatched_prompt_fails(self):
79        """Test that mismatched prompted passwords raise an error."""
80        with (
81            patch(
82                "authentik.core.management.commands.hash_password.getpass",
83                side_effect=["test123", "different"],
84            ),
85            patch(
86                "authentik.core.management.commands.hash_password.sys.stdin.isatty",
87                return_value=True,
88            ),
89            self.assertRaises(CommandError) as ctx,
90        ):
91            call_command("hash_password")
92
93        self.assertIn("Passwords do not match", str(ctx.exception))

Test hash_password management command.

def test_hash_password_stdin(self):
16    def test_hash_password_stdin(self):
17        """Test hashing a password read from standard input."""
18        out = StringIO()
19        with patch(
20            "authentik.core.management.commands.hash_password.sys.stdin",
21            StringIO("test123\n"),
22        ):
23            call_command("hash_password", stdout=out)
24        hashed = out.getvalue().strip()
25
26        self.assertTrue(hashed.startswith("pbkdf2_sha256$"))
27        self.assertTrue(check_password("test123", hashed))

Test hashing a password read from standard input.

def test_hash_password_prompt(self):
29    def test_hash_password_prompt(self):
30        """Test hashing a password entered through the hidden prompt."""
31        out = StringIO()
32        with (
33            patch(
34                "authentik.core.management.commands.hash_password.getpass",
35                side_effect=["test123", "test123"],
36            ),
37            patch(
38                "authentik.core.management.commands.hash_password.sys.stdin.isatty",
39                return_value=True,
40            ),
41        ):
42            call_command("hash_password", stdout=out)
43        hashed = out.getvalue().strip()
44
45        self.assertTrue(hashed.startswith("pbkdf2_sha256$"))
46        self.assertTrue(check_password("test123", hashed))

Test hashing a password entered through the hidden prompt.

def test_hash_password_empty_stdin_fails(self):
48    def test_hash_password_empty_stdin_fails(self):
49        """Test that an empty password read from standard input raises an error."""
50        with (
51            patch(
52                "authentik.core.management.commands.hash_password.sys.stdin",
53                StringIO("\n"),
54            ),
55            self.assertRaises(CommandError) as ctx,
56        ):
57            call_command("hash_password")
58
59        self.assertIn("Password cannot be empty", str(ctx.exception))

Test that an empty password read from standard input raises an error.

def test_hash_password_empty_prompt_fails(self):
61    def test_hash_password_empty_prompt_fails(self):
62        """Test that an empty prompted password raises an error."""
63        with (
64            patch(
65                "authentik.core.management.commands.hash_password.getpass",
66                side_effect=["", ""],
67            ),
68            patch(
69                "authentik.core.management.commands.hash_password.sys.stdin.isatty",
70                return_value=True,
71            ),
72            self.assertRaises(CommandError) as ctx,
73        ):
74            call_command("hash_password")
75
76        self.assertIn("Password cannot be empty", str(ctx.exception))

Test that an empty prompted password raises an error.

def test_hash_password_mismatched_prompt_fails(self):
78    def test_hash_password_mismatched_prompt_fails(self):
79        """Test that mismatched prompted passwords raise an error."""
80        with (
81            patch(
82                "authentik.core.management.commands.hash_password.getpass",
83                side_effect=["test123", "different"],
84            ),
85            patch(
86                "authentik.core.management.commands.hash_password.sys.stdin.isatty",
87                return_value=True,
88            ),
89            self.assertRaises(CommandError) as ctx,
90        ):
91            call_command("hash_password")
92
93        self.assertIn("Passwords do not match", str(ctx.exception))

Test that mismatched prompted passwords raise an error.