File size: 3,731 Bytes
4a4c0cf
3a1c55b
4a4c0cf
 
 
3a1c55b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
109
110
import pytest
import socket
import dns.resolver
from unittest.mock import MagicMock

from blossomtune_gradio.util import is_port_open, validate_email, strtobool


def test_is_port_open_success(mocker):
    """
    Tests the case where the port is open and the connection succeeds.
    """
    mock_socket = mocker.patch("blossomtune_gradio.util.socket.socket")
    mock_socket.return_value.__enter__.return_value.connect.return_value = None

    result = is_port_open("testhost", 1234)

    assert result is True
    # Verify that the socket was created and connect was called with the correct args
    mock_socket.return_value.__enter__.return_value.settimeout.assert_called_once_with(
        1.0
    )
    mock_socket.return_value.__enter__.return_value.connect.assert_called_once_with(
        ("testhost", 1234)
    )


@pytest.mark.parametrize("exception", [ConnectionRefusedError, socket.timeout, OSError])
def test_is_port_open_failures(mocker, exception):
    """
    Tests various failure scenarios where the port is not open.
    - ConnectionRefusedError: The host actively refuses the connection.
    - socket.timeout: The connection attempt times out.
    - OSError: A generic network error occurs.
    """
    mock_socket = mocker.patch("blossomtune_gradio.util.socket.socket")
    mock_socket.return_value.__enter__.return_value.connect.side_effect = exception

    result = is_port_open("testhost", 1234)
    assert result is False


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