2019-09-11 01:47:10 +02:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from moulinette.actionsmap import (
|
|
|
|
CommentParameter,
|
|
|
|
AskParameter,
|
|
|
|
PatternParameter,
|
|
|
|
RequiredParameter,
|
2019-11-25 17:21:13 +01:00
|
|
|
ActionsMap,
|
2019-09-11 01:47:10 +02:00
|
|
|
)
|
|
|
|
from moulinette.interfaces import BaseActionsMapParser
|
|
|
|
from moulinette.core import MoulinetteError
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def iface():
|
2019-11-25 17:21:13 +01:00
|
|
|
return "iface"
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_comment_parameter_bad_bool_value(iface, caplog):
|
|
|
|
comment = CommentParameter(iface)
|
2019-11-25 17:21:13 +01:00
|
|
|
assert comment.validate(True, "a") == "a"
|
|
|
|
assert any("expecting a non-empty string" in message for message in caplog.messages)
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_comment_parameter_bad_empty_string(iface, caplog):
|
|
|
|
comment = CommentParameter(iface)
|
2019-11-25 17:21:13 +01:00
|
|
|
assert comment.validate("", "a") == "a"
|
|
|
|
assert any("expecting a non-empty string" in message for message in caplog.messages)
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_comment_parameter_bad_type(iface):
|
|
|
|
comment = CommentParameter(iface)
|
|
|
|
with pytest.raises(TypeError):
|
2019-11-25 17:21:13 +01:00
|
|
|
comment.validate({}, "b")
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_ask_parameter_bad_bool_value(iface, caplog):
|
|
|
|
ask = AskParameter(iface)
|
2019-11-25 17:21:13 +01:00
|
|
|
assert ask.validate(True, "a") == "a"
|
|
|
|
assert any("expecting a non-empty string" in message for message in caplog.messages)
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_ask_parameter_bad_empty_string(iface, caplog):
|
|
|
|
ask = AskParameter(iface)
|
2019-11-25 17:21:13 +01:00
|
|
|
assert ask.validate("", "a") == "a"
|
|
|
|
assert any("expecting a non-empty string" in message for message in caplog.messages)
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_ask_parameter_bad_type(iface):
|
|
|
|
ask = AskParameter(iface)
|
|
|
|
with pytest.raises(TypeError):
|
2019-11-25 17:21:13 +01:00
|
|
|
ask.validate({}, "b")
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_pattern_parameter_bad_str_value(iface, caplog):
|
|
|
|
pattern = PatternParameter(iface)
|
2019-11-25 17:21:13 +01:00
|
|
|
assert pattern.validate("", "a") == ["", "pattern_not_match"]
|
|
|
|
assert any("expecting a list" in message for message in caplog.messages)
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
2019-11-25 17:21:13 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"iface", [[], ["pattern_alone"], ["pattern", "message", "extra stuff"]]
|
|
|
|
)
|
2019-09-11 01:47:10 +02:00
|
|
|
def test_pattern_parameter_bad_list_len(iface):
|
|
|
|
pattern = PatternParameter(iface)
|
|
|
|
with pytest.raises(TypeError):
|
2019-11-25 17:21:13 +01:00
|
|
|
pattern.validate(iface, "a")
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_required_paremeter_missing_value(iface):
|
|
|
|
required = RequiredParameter(iface)
|
|
|
|
with pytest.raises(MoulinetteError) as exception:
|
2019-11-25 17:21:13 +01:00
|
|
|
required(True, "a", "")
|
|
|
|
assert "is required" in str(exception)
|
2019-09-11 01:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_actions_map_unknown_authenticator(monkeypatch, tmp_path):
|
2019-11-25 17:21:13 +01:00
|
|
|
monkeypatch.setenv("MOULINETTE_DATA_DIR", str(tmp_path))
|
|
|
|
actionsmap_dir = actionsmap_dir = tmp_path / "actionsmap"
|
2019-09-11 01:47:10 +02:00
|
|
|
actionsmap_dir.mkdir()
|
|
|
|
|
|
|
|
amap = ActionsMap(BaseActionsMapParser)
|
|
|
|
with pytest.raises(ValueError) as exception:
|
2019-11-25 17:21:13 +01:00
|
|
|
amap.get_authenticator_for_profile("unknown")
|
|
|
|
assert "Unknown authenticator" in str(exception)
|