Merge pull request #1079 from YunoHost/fix-optional-args

Fix optional args
This commit is contained in:
Alexandre Aubin 2020-11-25 18:38:23 +01:00 committed by GitHub
commit fe6333d2d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 4 deletions

View file

@ -2500,8 +2500,10 @@ class PasswordArgumentParser(YunoHostArgumentFormatParser):
if any(char in question.value for char in self.forbidden_chars): if any(char in question.value for char in self.forbidden_chars):
raise YunohostError('pattern_password_app', forbidden_chars=self.forbidden_chars) raise YunohostError('pattern_password_app', forbidden_chars=self.forbidden_chars)
from yunohost.utils.password import assert_password_is_strong_enough # If it's an optional argument the value should be empty or strong enough
assert_password_is_strong_enough('user', question.value) if not question.optional or question.value:
from yunohost.utils.password import assert_password_is_strong_enough
assert_password_is_strong_enough('user', question.value)
return super(PasswordArgumentParser, self)._post_parse_value(question) return super(PasswordArgumentParser, self)._post_parse_value(question)

View file

@ -95,6 +95,15 @@ def test_parse_args_in_yunohost_format_string_optional_with_input():
assert _parse_args_in_yunohost_format(answers, questions) == expected_result assert _parse_args_in_yunohost_format(answers, questions) == expected_result
def test_parse_args_in_yunohost_format_string_optional_with_empty_input():
questions = [{"name": "some_string", "ask": "some question", "optional": True, }]
answers = {}
expected_result = OrderedDict({"some_string": ("", "string")})
with patch.object(msignals, "prompt", return_value=""):
assert _parse_args_in_yunohost_format(answers, questions) == expected_result
def test_parse_args_in_yunohost_format_string_optional_with_input_without_ask(): def test_parse_args_in_yunohost_format_string_optional_with_input_without_ask():
questions = [{"name": "some_string", "optional": True, }] questions = [{"name": "some_string", "optional": True, }]
answers = {} answers = {}
@ -247,9 +256,9 @@ def test_parse_args_in_yunohost_format_password_input_no_ask():
def test_parse_args_in_yunohost_format_password_no_input_optional(): def test_parse_args_in_yunohost_format_password_no_input_optional():
questions = [{"name": "some_password", "type": "password", "optional": True, }] questions = [{"name": "some_password", "type": "password", "optional": True, }]
answers = {} answers = {}
expected_result = OrderedDict({"some_password": ("", "password")})
with pytest.raises(YunohostError): assert _parse_args_in_yunohost_format(answers, questions) == expected_result
_parse_args_in_yunohost_format(answers, questions)
def test_parse_args_in_yunohost_format_password_optional_with_input(): def test_parse_args_in_yunohost_format_password_optional_with_input():
@ -268,6 +277,22 @@ def test_parse_args_in_yunohost_format_password_optional_with_input():
assert _parse_args_in_yunohost_format(answers, questions) == expected_result assert _parse_args_in_yunohost_format(answers, questions) == expected_result
def test_parse_args_in_yunohost_format_password_optional_with_empty_input():
questions = [
{
"name": "some_password",
"ask": "some question",
"type": "password",
"optional": True,
}
]
answers = {}
expected_result = OrderedDict({"some_password": ("", "password")})
with patch.object(msignals, "prompt", return_value=""):
assert _parse_args_in_yunohost_format(answers, questions) == expected_result
def test_parse_args_in_yunohost_format_password_optional_with_input_without_ask(): def test_parse_args_in_yunohost_format_password_optional_with_input_without_ask():
questions = [{"name": "some_password", "type": "password", "optional": True, }] questions = [{"name": "some_password", "type": "password", "optional": True, }]
answers = {} answers = {}
@ -393,6 +418,24 @@ def test_parse_args_in_yunohost_format_password_strong_enough():
_parse_args_in_yunohost_format({"some_password": "password"}, questions) _parse_args_in_yunohost_format({"some_password": "password"}, questions)
def test_parse_args_in_yunohost_format_password_optional_strong_enough():
questions = [
{
"name": "some_password",
"ask": "some question",
"type": "password",
"optional": True,
}
]
with pytest.raises(YunohostError):
# too short
_parse_args_in_yunohost_format({"some_password": "a"}, questions)
with pytest.raises(YunohostError):
_parse_args_in_yunohost_format({"some_password": "password"}, questions)
def test_parse_args_in_yunohost_format_path(): def test_parse_args_in_yunohost_format_path():
questions = [{"name": "some_path", "type": "path", }] questions = [{"name": "some_path", "type": "path", }]
answers = {"some_path": "some_value"} answers = {"some_path": "some_value"}
@ -444,6 +487,17 @@ def test_parse_args_in_yunohost_format_path_optional_with_input():
assert _parse_args_in_yunohost_format(answers, questions) == expected_result assert _parse_args_in_yunohost_format(answers, questions) == expected_result
def test_parse_args_in_yunohost_format_path_optional_with_empty_input():
questions = [
{"name": "some_path", "ask": "some question", "type": "path", "optional": True, }
]
answers = {}
expected_result = OrderedDict({"some_path": ("", "path")})
with patch.object(msignals, "prompt", return_value=""):
assert _parse_args_in_yunohost_format(answers, questions) == expected_result
def test_parse_args_in_yunohost_format_path_optional_with_input_without_ask(): def test_parse_args_in_yunohost_format_path_optional_with_input_without_ask():
questions = [{"name": "some_path", "type": "path", "optional": True, }] questions = [{"name": "some_path", "type": "path", "optional": True, }]
answers = {} answers = {}
@ -663,6 +717,22 @@ def test_parse_args_in_yunohost_format_boolean_optional_with_input():
assert _parse_args_in_yunohost_format(answers, questions) == expected_result assert _parse_args_in_yunohost_format(answers, questions) == expected_result
def test_parse_args_in_yunohost_format_boolean_optional_with_empty_input():
questions = [
{
"name": "some_boolean",
"ask": "some question",
"type": "boolean",
"optional": True,
}
]
answers = {}
expected_result = OrderedDict({"some_boolean": (0, "boolean")}) # default to false
with patch.object(msignals, "prompt", return_value=""):
assert _parse_args_in_yunohost_format(answers, questions) == expected_result
def test_parse_args_in_yunohost_format_boolean_optional_with_input_without_ask(): def test_parse_args_in_yunohost_format_boolean_optional_with_input_without_ask():
questions = [{"name": "some_boolean", "type": "boolean", "optional": True, }] questions = [{"name": "some_boolean", "type": "boolean", "optional": True, }]
answers = {} answers = {}