mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-06-28 14:51:43 +00:00
add more validation rules
This commit is contained in:
@ -62,9 +62,11 @@ def test_schema(configuration: Configuration) -> None:
|
||||
assert schema.pop("email")
|
||||
assert schema.pop("github")
|
||||
assert schema.pop("html")
|
||||
assert schema.pop("report")
|
||||
assert schema.pop("rsync")
|
||||
assert schema.pop("s3")
|
||||
assert schema.pop("telegram")
|
||||
assert schema.pop("upload")
|
||||
|
||||
assert schema == CONFIGURATION_SCHEMA
|
||||
|
||||
|
@ -16,4 +16,4 @@ def validator(configuration: Configuration) -> Validator:
|
||||
Returns:
|
||||
Validator: validator test instance
|
||||
"""
|
||||
return Validator(instance=configuration, schema=CONFIGURATION_SCHEMA)
|
||||
return Validator(configuration=configuration, schema=CONFIGURATION_SCHEMA)
|
||||
|
@ -1,6 +1,6 @@
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, call as MockCall
|
||||
|
||||
from ahriman.core.configuration.validator import Validator
|
||||
|
||||
@ -18,7 +18,7 @@ def test_normalize_coerce_absolute_path(validator: Validator) -> None:
|
||||
must convert string value to path by using configuration converters
|
||||
"""
|
||||
convert_mock = MagicMock()
|
||||
validator.instance.converters["path"] = convert_mock
|
||||
validator.configuration.converters["path"] = convert_mock
|
||||
|
||||
validator._normalize_coerce_absolute_path("value")
|
||||
convert_mock.assert_called_once_with("value")
|
||||
@ -46,12 +46,56 @@ def test_normalize_coerce_list(validator: Validator) -> None:
|
||||
must convert string value to list by using configuration converters
|
||||
"""
|
||||
convert_mock = MagicMock()
|
||||
validator.instance.converters["list"] = convert_mock
|
||||
validator.configuration.converters["list"] = convert_mock
|
||||
|
||||
validator._normalize_coerce_list("value")
|
||||
convert_mock.assert_called_once_with("value")
|
||||
|
||||
|
||||
def test_validate_is_ip_address(validator: Validator, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must validate addresses correctly
|
||||
"""
|
||||
error_mock = mocker.patch("ahriman.core.configuration.validator.Validator._error")
|
||||
|
||||
validator._validate_is_ip_address(["localhost"], "field", "localhost")
|
||||
validator._validate_is_ip_address([], "field", "localhost")
|
||||
|
||||
validator._validate_is_ip_address([], "field", "127.0.0.1")
|
||||
validator._validate_is_ip_address([], "field", "::")
|
||||
validator._validate_is_ip_address([], "field", "0.0.0.0")
|
||||
|
||||
validator._validate_is_ip_address([], "field", "random string")
|
||||
|
||||
error_mock.assert_has_calls([
|
||||
MockCall("field", "Value localhost must be valid IP address"),
|
||||
MockCall("field", "Value random string must be valid IP address"),
|
||||
])
|
||||
|
||||
|
||||
def test_validate_is_url(validator: Validator, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must validate url correctly
|
||||
"""
|
||||
error_mock = mocker.patch("ahriman.core.configuration.validator.Validator._error")
|
||||
|
||||
validator._validate_is_url([], "field", "http://example.com")
|
||||
validator._validate_is_url([], "field", "https://example.com")
|
||||
validator._validate_is_url([], "field", "file:///tmp")
|
||||
|
||||
validator._validate_is_url(["http", "https"], "field", "file:///tmp")
|
||||
|
||||
validator._validate_is_url([], "field", "http:///path")
|
||||
|
||||
validator._validate_is_url([], "field", "random string")
|
||||
|
||||
error_mock.assert_has_calls([
|
||||
MockCall("field", "Url file:///tmp scheme must be one of ['http', 'https']"),
|
||||
MockCall("field", "Location must be set for url http:///path of scheme http"),
|
||||
MockCall("field", "Url scheme is not set for random string"),
|
||||
])
|
||||
|
||||
|
||||
def test_validate_path_exists(validator: Validator, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must validate that paths exists
|
||||
@ -67,4 +111,6 @@ def test_validate_path_exists(validator: Validator, mocker: MockerFixture) -> No
|
||||
mocker.patch("pathlib.Path.exists", return_value=True)
|
||||
validator._validate_path_exists(True, "field", Path("3"))
|
||||
|
||||
error_mock.assert_called_once_with("field", "Path 2 must exist")
|
||||
error_mock.assert_has_calls([
|
||||
MockCall("field", "Path 2 must exist"),
|
||||
])
|
||||
|
Reference in New Issue
Block a user