From 61e539f4ea9ba8db031d0c861d5eeb795c032edd Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Mon, 25 May 2020 06:10:57 +0200 Subject: [PATCH] [mod] add tests for apps arguments parsing --- .../tests/test_apps_arguments_parsing.py | 199 +++++++++++++++++- 1 file changed, 198 insertions(+), 1 deletion(-) diff --git a/src/yunohost/tests/test_apps_arguments_parsing.py b/src/yunohost/tests/test_apps_arguments_parsing.py index 12ce9f693..491d7c512 100644 --- a/src/yunohost/tests/test_apps_arguments_parsing.py +++ b/src/yunohost/tests/test_apps_arguments_parsing.py @@ -4,7 +4,7 @@ from mock import patch from moulinette import msignals -from yunohost import domain, user +from yunohost import domain, user, app from yunohost.app import _parse_args_in_yunohost_format from yunohost.utils.error import YunohostError @@ -987,3 +987,200 @@ def test_parse_args_in_yunohost_format_user_two_users_default_input(): expected_result = OrderedDict({"some_user": (other_user, "user")}) with patch.object(msignals, "prompt", return_value=other_user): assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + +def test_parse_args_in_yunohost_format_app_empty(): + apps = [ + { + "id": "my_webapp", + "version": "1.0~ynh2", + "description": "Custom Web app with SFTP access", + "name": "Custom Webapp", + } + ] + + questions = [{"name": "some_app", "type": "app",}] + answers = {} + + with patch.object(app, "app_list", return_value={"apps": apps}): + with pytest.raises(YunohostError): + _parse_args_in_yunohost_format(answers, questions) + + +def test_parse_args_in_yunohost_format_app_no_apps(): + apps = [] + questions = [{"name": "some_app", "type": "app",}] + answers = {} + + with patch.object(app, "app_list", return_value={"apps": apps}): + with pytest.raises(YunohostError): + _parse_args_in_yunohost_format(answers, questions) + + +@pytest.mark.skip # XXX should work +def test_parse_args_in_yunohost_format_app_no_apps_optional(): + apps = [] + questions = [{"name": "some_app", "type": "app", "optional": True}] + answers = {} + + with patch.object(app, "app_list", return_value={"apps": apps}): + assert _parse_args_in_yunohost_format(answers, questions) == [] + + +def test_parse_args_in_yunohost_format_app(): + app_name = "my_webapp" + apps = [ + { + "id": app_name, + "version": "1.0~ynh2", + "description": "Custom Web app with SFTP access", + "name": "Custom Webapp", + } + ] + + questions = [{"name": "some_app", "type": "app",}] + answers = {"some_app": app_name} + + expected_result = OrderedDict({"some_app": (app_name, "app")}) + + with patch.object(app, "app_list", return_value={"apps": apps}), patch.object( + app, + "_is_installed", + new_callable=lambda: lambda app_id: app_id in {x["id"] for x in apps}, + ): + assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + +def test_parse_args_in_yunohost_format_app_two_apps(): + app_name = "my_webapp" + other_app = "some_other_app" + apps = [ + { + "id": app_name, + "version": "1.0~ynh2", + "description": "Custom Web app with SFTP access", + "name": "Custom Webapp", + }, + { + "id": other_app, + "version": "1.0", + "description": "blabla", + "name": "stuff", + }, + ] + + questions = [{"name": "some_app", "type": "app",}] + answers = {"some_app": other_app} + expected_result = OrderedDict({"some_app": (other_app, "app")}) + + with patch.object(app, "app_list", return_value={"apps": apps}), patch.object( + app, + "_is_installed", + new_callable=lambda: lambda app_id: app_id in {x["id"] for x in apps}, + ): + assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + answers = {"some_app": app_name} + expected_result = OrderedDict({"some_app": (app_name, "app")}) + + with patch.object(app, "app_list", return_value={"apps": apps}), patch.object( + app, + "_is_installed", + new_callable=lambda: lambda app_id: app_id in {x["id"] for x in apps}, + ): + assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + +def test_parse_args_in_yunohost_format_app_two_apps_wrong_answer(): + app_name = "my_webapp" + other_app = "some_other_app" + apps = [ + { + "id": app_name, + "version": "1.0~ynh2", + "description": "Custom Web app with SFTP access", + "name": "Custom Webapp", + }, + { + "id": other_app, + "version": "1.0", + "description": "blabla", + "name": "stuff", + }, + ] + + questions = [{"name": "some_app", "type": "app",}] + answers = {"some_app": "doesnt_exist"} + + with pytest.raises(YunohostError): + with patch.object(app, "app_list", return_value={"apps": apps}), patch.object( + app, + "_is_installed", + new_callable=lambda: lambda app_id: app_id in {x["id"] for x in apps}, + ): + _parse_args_in_yunohost_format(answers, questions) + + +def test_parse_args_in_yunohost_format_app_two_apps_no_default(): + app_name = "my_app_name.com" + other_app = "some_other_app" + apps = [ + { + "id": app_name, + "version": "1.0~ynh2", + "description": "Custom Web app with SFTP access", + "name": "Custom Webapp", + }, + { + "id": other_app, + "version": "1.0", + "description": "blabla", + "name": "stuff", + }, + ] + + questions = [{"name": "some_app", "type": "app", "ask": "choose a app"}] + answers = {} + + with patch.object(app, "app_list", return_value={"apps": apps}), patch.object( + app, + "_is_installed", + new_callable=lambda: lambda app_id: app_id in {x["id"] for x in apps}, + ): + with pytest.raises(YunohostError): + _parse_args_in_yunohost_format(answers, questions) + + +def test_parse_args_in_yunohost_format_app_two_apps_default_input(): + app_name = "my_app_name.com" + other_app = "some_other_app" + apps = [ + { + "id": app_name, + "version": "1.0~ynh2", + "description": "Custom Web app with SFTP access", + "name": "Custom Webapp", + }, + { + "id": other_app, + "version": "1.0", + "description": "blabla", + "name": "stuff", + }, + ] + + questions = [{"name": "some_app", "type": "app", "ask": "choose a app"}] + answers = {} + + with patch.object(app, "app_list", return_value={"apps": apps}), patch.object( + app, + "_is_installed", + new_callable=lambda: lambda app_id: app_id in {x["id"] for x in apps}, + ): + expected_result = OrderedDict({"some_app": (app_name, "app")}) + with patch.object(msignals, "prompt", return_value=app_name): + assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + expected_result = OrderedDict({"some_app": (other_app, "app")}) + with patch.object(msignals, "prompt", return_value=other_app): + assert _parse_args_in_yunohost_format(answers, questions) == expected_result