Skip to content

Commit 6cff481

Browse files
committed
Refactored ServiceConfiguration and TLSConfiguration unit tests
1 parent cd674cb commit 6cff481

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Unit tests for ServiceConfiguration model."""
2+
3+
import pytest
4+
5+
from pydantic import ValidationError
6+
7+
from models.config import ServiceConfiguration, TLSConfiguration
8+
9+
10+
def test_service_configuration_constructor() -> None:
11+
"""
12+
Verify that the ServiceConfiguration constructor sets default
13+
values for all fields.
14+
"""
15+
s = ServiceConfiguration()
16+
assert s is not None
17+
18+
assert s.host == "localhost"
19+
assert s.port == 8080
20+
assert s.auth_enabled is False
21+
assert s.workers == 1
22+
assert s.color_log is True
23+
assert s.access_log is True
24+
assert s.tls_config == TLSConfiguration()
25+
26+
27+
def test_service_configuration_port_value() -> None:
28+
"""Test the ServiceConfiguration port value validation."""
29+
with pytest.raises(ValidationError, match="Input should be greater than 0"):
30+
ServiceConfiguration(port=-1)
31+
32+
with pytest.raises(ValueError, match="Port value should be less than 65536"):
33+
ServiceConfiguration(port=100000)
34+
35+
36+
def test_service_configuration_workers_value() -> None:
37+
"""Test the ServiceConfiguration workers value validation."""
38+
with pytest.raises(ValidationError, match="Input should be greater than 0"):
39+
ServiceConfiguration(workers=-1)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""Unit tests for TLSConfiguration model."""
2+
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
from models.config import ServiceConfiguration, TLSConfiguration
8+
9+
10+
def test_tls_configuration() -> None:
11+
"""Test the TLS configuration."""
12+
cfg = TLSConfiguration(
13+
tls_certificate_path=Path("tests/configuration/server.crt"),
14+
tls_key_path=Path("tests/configuration/server.key"),
15+
tls_key_password=Path("tests/configuration/password"),
16+
)
17+
assert cfg is not None
18+
assert cfg.tls_certificate_path == Path("tests/configuration/server.crt")
19+
assert cfg.tls_key_path == Path("tests/configuration/server.key")
20+
assert cfg.tls_key_password == Path("tests/configuration/password")
21+
22+
23+
def test_tls_configuration_in_service_configuration() -> None:
24+
"""Test the TLS configuration in service configuration."""
25+
cfg = ServiceConfiguration(
26+
tls_config=TLSConfiguration(
27+
tls_certificate_path=Path("tests/configuration/server.crt"),
28+
tls_key_path=Path("tests/configuration/server.key"),
29+
tls_key_password=Path("tests/configuration/password"),
30+
)
31+
)
32+
assert cfg is not None
33+
assert cfg.tls_config is not None
34+
assert cfg.tls_config.tls_certificate_path == Path("tests/configuration/server.crt")
35+
assert cfg.tls_config.tls_key_path == Path("tests/configuration/server.key")
36+
assert cfg.tls_config.tls_key_password == Path("tests/configuration/password")
37+
38+
39+
def test_tls_configuration_wrong_certificate_path() -> None:
40+
"""Test the TLS configuration loading when some path is broken."""
41+
with pytest.raises(ValueError, match="Path does not point to a file"):
42+
TLSConfiguration(
43+
tls_certificate_path=Path("this-is-wrong"),
44+
tls_key_path=Path("tests/configuration/server.key"),
45+
tls_key_password=Path("tests/configuration/password"),
46+
)
47+
48+
49+
def test_tls_configuration_wrong_key_path() -> None:
50+
"""Test the TLS configuration loading when some path is broken."""
51+
with pytest.raises(ValueError, match="Path does not point to a file"):
52+
TLSConfiguration(
53+
tls_certificate_path=Path("tests/configurationserver.crt"),
54+
tls_key_path=Path("this-is-wrong"),
55+
tls_key_password=Path("tests/configuration/password"),
56+
)
57+
58+
59+
def test_tls_configuration_wrong_password_path() -> None:
60+
"""Test the TLS configuration loading when some path is broken."""
61+
with pytest.raises(ValueError, match="Path does not point to a file"):
62+
TLSConfiguration(
63+
tls_certificate_path=Path("tests/configurationserver.crt"),
64+
tls_key_path=Path("tests/configuration/server.key"),
65+
tls_key_password=Path("this-is-wrong"),
66+
)
67+
68+
69+
def test_tls_configuration_certificate_path_to_directory() -> None:
70+
"""Test the TLS configuration loading when some path points to a directory."""
71+
with pytest.raises(ValueError, match="Path does not point to a file"):
72+
TLSConfiguration(
73+
tls_certificate_path=Path("tests/"),
74+
tls_key_path=Path("tests/configuration/server.key"),
75+
tls_key_password=Path("tests/configuration/password"),
76+
)
77+
78+
79+
def test_tls_configuration_key_path_to_directory() -> None:
80+
"""Test the TLS configuration loading when some path points to a directory."""
81+
with pytest.raises(ValueError, match="Path does not point to a file"):
82+
TLSConfiguration(
83+
tls_certificate_path=Path("tests/configurationserver.crt"),
84+
tls_key_path=Path("tests/"),
85+
tls_key_password=Path("tests/configuration/password"),
86+
)
87+
88+
89+
def test_tls_configuration_password_path_to_directory() -> None:
90+
"""Test the TLS configuration loading when some path points to a directory."""
91+
with pytest.raises(ValueError, match="Path does not point to a file"):
92+
TLSConfiguration(
93+
tls_certificate_path=Path("tests/configurationserver.crt"),
94+
tls_key_path=Path("tests/configuration/server.key"),
95+
tls_key_password=Path("tests/"),
96+
)

0 commit comments

Comments
 (0)