diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 1480b1592..c42c5c3cb 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -2500,8 +2500,10 @@ class PasswordArgumentParser(YunoHostArgumentFormatParser): if any(char in question.value for char in self.forbidden_chars): raise YunohostError('pattern_password_app', forbidden_chars=self.forbidden_chars) - from yunohost.utils.password import assert_password_is_strong_enough - assert_password_is_strong_enough('user', question.value) + # If it's an optional argument the value should be empty or strong enough + 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) diff --git a/src/yunohost/tests/test_apps_arguments_parsing.py b/src/yunohost/tests/test_apps_arguments_parsing.py index 598eb9eec..c8203f2c5 100644 --- a/src/yunohost/tests/test_apps_arguments_parsing.py +++ b/src/yunohost/tests/test_apps_arguments_parsing.py @@ -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 +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(): questions = [{"name": "some_string", "optional": True, }] 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(): questions = [{"name": "some_password", "type": "password", "optional": True, }] answers = {} + expected_result = OrderedDict({"some_password": ("", "password")}) - with pytest.raises(YunohostError): - _parse_args_in_yunohost_format(answers, questions) + assert _parse_args_in_yunohost_format(answers, questions) == expected_result 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 +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(): questions = [{"name": "some_password", "type": "password", "optional": True, }] answers = {} @@ -393,6 +418,24 @@ def test_parse_args_in_yunohost_format_password_strong_enough(): _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(): questions = [{"name": "some_path", "type": "path", }] 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 +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(): questions = [{"name": "some_path", "type": "path", "optional": True, }] 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 +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(): questions = [{"name": "some_boolean", "type": "boolean", "optional": True, }] answers = {}