File size: 2,416 Bytes
4a4c0cf
 
 
 
 
 
 
 
 
 
 
e60e679
4a4c0cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e60e679
4a4c0cf
 
 
 
 
 
 
 
 
 
e60e679
4a4c0cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
import dns.resolver
from unittest.mock import MagicMock

from blossomtune_gradio.util import validate_email, strtobool


def test_validate_email_valid(monkeypatch):
    """Tests a syntactically valid email with an existing MX record."""
    # Mock the dns.resolver.query to return a successful result.
    mock_query = MagicMock()
    monkeypatch.setattr(dns.resolver, "resolve", mock_query)

    email = "test@google.com"
    assert validate_email(email) is True
    # Verify that the query function was called with the correct arguments.
    mock_query.assert_called_once_with("google.com", "MX")


def test_validate_email_invalid_format():
    """Tests an email with an invalid regex format."""
    assert validate_email("invalid-email") is False
    assert validate_email("user@.com") is False
    assert validate_email("@domain.com") is False


def test_validate_email_no_mx_record(monkeypatch):
    """Tests a domain that exists but has no MX record."""
    # Mock the dns.resolver.query to raise a NoAnswer exception.
    mock_query = MagicMock(side_effect=dns.resolver.NoAnswer)
    monkeypatch.setattr(dns.resolver, "resolve", mock_query)

    email = "user@example.com"
    assert validate_email(email) is False
    mock_query.assert_called_once_with("example.com", "MX")


def test_validate_email_non_existent_domain(monkeypatch):
    """Tests a domain that does not exist."""
    # Mock the dns.resolver.query to raise an NXDOMAIN exception.
    mock_query = MagicMock(side_effect=dns.resolver.NXDOMAIN)
    monkeypatch.setattr(dns.resolver, "resolve", mock_query)

    email = "user@not-a-real-domain-123.com"
    assert validate_email(email) is False
    mock_query.assert_called_once_with("not-a-real-domain-123.com", "MX")


@pytest.mark.parametrize(
    "value, expected",
    [
        ("y", True),
        ("yes", True),
        ("on", True),
        ("1", True),
        ("true", True),
        ("t", True),
        ("Y", True),
        ("YES", True),
        ("On", True),
        ("0", False),
        ("n", False),
        ("off", False),
        ("false", False),
        ("f", False),
        ("no", False),
        ("anything else", False),
        ("", False),
        (None, False),  # Test with None to ensure it doesn't crash
    ],
)
def test_strtobool(value, expected):
    """Tests the strtobool function with various inputs."""
    assert strtobool(value) == expected