authentik.lib.tests.test_utils_email

Test email utils

 1"""Test email utils"""
 2
 3from django.test import TestCase
 4
 5from authentik.lib.utils.email import normalize_addresses
 6
 7
 8class TestNormalizeAddresses(TestCase):
 9    """Test normalize_addresses"""
10
11    def test_none(self):
12        """None returns None"""
13        self.assertIsNone(normalize_addresses(None))
14
15    def test_plain_string(self):
16        """Plain email string"""
17        self.assertEqual(normalize_addresses("user@example.com"), [("", "user@example.com")])
18
19    def test_formatted_string(self):
20        """RFC 5322 formatted string keeps the name"""
21        self.assertEqual(
22            normalize_addresses("John Doe <user@example.com>"),
23            [("John Doe", "user@example.com")],
24        )
25        self.assertEqual(
26            normalize_addresses('"Doe, John" <user@example.com>'),
27            [("Doe, John", "user@example.com")],
28        )
29
30    def test_tuple(self):
31        """(name, email) tuple"""
32        self.assertEqual(
33            normalize_addresses(("John Doe", "user@example.com")),
34            [("John Doe", "user@example.com")],
35        )
36        self.assertEqual(
37            normalize_addresses((None, "user@example.com")),
38            [("", "user@example.com")],
39        )
40
41    def test_mixed_list(self):
42        """List of mixed formats"""
43        self.assertEqual(
44            normalize_addresses(
45                [
46                    "plain@example.com",
47                    "Jane <jane@example.com>",
48                    ("John", "john@example.com"),
49                    ["Listed", "listed@example.com"],
50                ]
51            ),
52            [
53                ("", "plain@example.com"),
54                ("Jane", "jane@example.com"),
55                ("John", "john@example.com"),
56                ("Listed", "listed@example.com"),
57            ],
58        )
59
60    def test_invalid(self):
61        """Invalid inputs raise ValueError"""
62        with self.assertRaises(ValueError):
63            normalize_addresses([])
64        with self.assertRaises(ValueError):
65            normalize_addresses(123)
66        with self.assertRaises(ValueError):
67            normalize_addresses(("only-one",))
68        with self.assertRaises(ValueError):
69            normalize_addresses([123])
class TestNormalizeAddresses(django.test.testcases.TestCase):
 9class TestNormalizeAddresses(TestCase):
10    """Test normalize_addresses"""
11
12    def test_none(self):
13        """None returns None"""
14        self.assertIsNone(normalize_addresses(None))
15
16    def test_plain_string(self):
17        """Plain email string"""
18        self.assertEqual(normalize_addresses("user@example.com"), [("", "user@example.com")])
19
20    def test_formatted_string(self):
21        """RFC 5322 formatted string keeps the name"""
22        self.assertEqual(
23            normalize_addresses("John Doe <user@example.com>"),
24            [("John Doe", "user@example.com")],
25        )
26        self.assertEqual(
27            normalize_addresses('"Doe, John" <user@example.com>'),
28            [("Doe, John", "user@example.com")],
29        )
30
31    def test_tuple(self):
32        """(name, email) tuple"""
33        self.assertEqual(
34            normalize_addresses(("John Doe", "user@example.com")),
35            [("John Doe", "user@example.com")],
36        )
37        self.assertEqual(
38            normalize_addresses((None, "user@example.com")),
39            [("", "user@example.com")],
40        )
41
42    def test_mixed_list(self):
43        """List of mixed formats"""
44        self.assertEqual(
45            normalize_addresses(
46                [
47                    "plain@example.com",
48                    "Jane <jane@example.com>",
49                    ("John", "john@example.com"),
50                    ["Listed", "listed@example.com"],
51                ]
52            ),
53            [
54                ("", "plain@example.com"),
55                ("Jane", "jane@example.com"),
56                ("John", "john@example.com"),
57                ("Listed", "listed@example.com"),
58            ],
59        )
60
61    def test_invalid(self):
62        """Invalid inputs raise ValueError"""
63        with self.assertRaises(ValueError):
64            normalize_addresses([])
65        with self.assertRaises(ValueError):
66            normalize_addresses(123)
67        with self.assertRaises(ValueError):
68            normalize_addresses(("only-one",))
69        with self.assertRaises(ValueError):
70            normalize_addresses([123])

Test normalize_addresses

def test_none(self):
12    def test_none(self):
13        """None returns None"""
14        self.assertIsNone(normalize_addresses(None))

None returns None

def test_plain_string(self):
16    def test_plain_string(self):
17        """Plain email string"""
18        self.assertEqual(normalize_addresses("user@example.com"), [("", "user@example.com")])

Plain email string

def test_formatted_string(self):
20    def test_formatted_string(self):
21        """RFC 5322 formatted string keeps the name"""
22        self.assertEqual(
23            normalize_addresses("John Doe <user@example.com>"),
24            [("John Doe", "user@example.com")],
25        )
26        self.assertEqual(
27            normalize_addresses('"Doe, John" <user@example.com>'),
28            [("Doe, John", "user@example.com")],
29        )

RFC 5322 formatted string keeps the name

def test_tuple(self):
31    def test_tuple(self):
32        """(name, email) tuple"""
33        self.assertEqual(
34            normalize_addresses(("John Doe", "user@example.com")),
35            [("John Doe", "user@example.com")],
36        )
37        self.assertEqual(
38            normalize_addresses((None, "user@example.com")),
39            [("", "user@example.com")],
40        )

(name, email) tuple

def test_mixed_list(self):
42    def test_mixed_list(self):
43        """List of mixed formats"""
44        self.assertEqual(
45            normalize_addresses(
46                [
47                    "plain@example.com",
48                    "Jane <jane@example.com>",
49                    ("John", "john@example.com"),
50                    ["Listed", "listed@example.com"],
51                ]
52            ),
53            [
54                ("", "plain@example.com"),
55                ("Jane", "jane@example.com"),
56                ("John", "john@example.com"),
57                ("Listed", "listed@example.com"),
58            ],
59        )

List of mixed formats

def test_invalid(self):
61    def test_invalid(self):
62        """Invalid inputs raise ValueError"""
63        with self.assertRaises(ValueError):
64            normalize_addresses([])
65        with self.assertRaises(ValueError):
66            normalize_addresses(123)
67        with self.assertRaises(ValueError):
68            normalize_addresses(("only-one",))
69        with self.assertRaises(ValueError):
70            normalize_addresses([123])

Invalid inputs raise ValueError