smtp-test-server/tests/net/test_ports.py
Juergen Edelbluth 08943cd8d5
Initial Commit
This commit consolidates the SMTP mock code from some of my testing projects into a re-usable form.

This package will be used later for realizing the upcoming pytest plugin.
2023-11-29 20:33:27 +01:00

59 lines
1.9 KiB
Python

"""
Test the ports module
"""
import pytest
from hamcrest import contains_string, equal_to, greater_than_or_equal_to
from hamcrest.core import assert_that
from smtp_test_server.exceptions import NotALocalHostnameOrIPAddressToBindToError
from smtp_test_server.net import find_free_port
@pytest.mark.parametrize("host", ["localhost", "127.0.0.1", "0.0.0.0"])
def test_get_random_port(host):
"""
Find a random free port on localhost or "all" addresses
"""
port = find_free_port(host)
assert_that(port, greater_than_or_equal_to(1024))
@pytest.mark.parametrize(
["host", "expected_exception_class", "expected_error_code", "expected_error_message"],
[
(
"google.com",
NotALocalHostnameOrIPAddressToBindToError,
-1,
"'google.com' is not a local host name / ip address to bind to",
),
(
"does-not.exist",
NotALocalHostnameOrIPAddressToBindToError,
-1,
"'does-not.exist' is not a local host name / ip address to bind to",
),
(
"8.8.8.8",
NotALocalHostnameOrIPAddressToBindToError,
-1,
"'8.8.8.8' is not a local host name / ip address to bind to",
),
(
"1.2.3.4",
NotALocalHostnameOrIPAddressToBindToError,
-1,
"'1.2.3.4' is not a local host name / ip address to bind to",
),
],
)
def test_failing_to_get_a_random_port(host, expected_exception_class, expected_error_code, expected_error_message):
"""
Test failure of getting a port
"""
with pytest.raises(expected_exception_class) as err:
find_free_port(host)
error_code, error_message = err.value.args
assert_that(error_code, equal_to(expected_error_code))
assert_that(error_message.lower(), contains_string(expected_error_message))