From a8f8ee5e6cacc2a2e7248ffc081ffda2ac8e7b7f Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 24 May 2020 04:22:34 +0200 Subject: [PATCH] [mod] add tests for user arguments parsing --- .../tests/test_apps_arguments_parsing.py | 175 +++++++++++++++++- 1 file changed, 174 insertions(+), 1 deletion(-) diff --git a/src/yunohost/tests/test_apps_arguments_parsing.py b/src/yunohost/tests/test_apps_arguments_parsing.py index 61df50d87..12ce9f693 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 +from yunohost import domain, user from yunohost.app import _parse_args_in_yunohost_format from yunohost.utils.error import YunohostError @@ -814,3 +814,176 @@ def test_parse_args_in_yunohost_format_domain_two_domains_default_input(): expected_result = OrderedDict({"some_domain": (other_domain, "domain")}) with patch.object(msignals, "prompt", return_value=other_domain): assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + +def test_parse_args_in_yunohost_format_user_empty(): + users = { + "some_user": { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "p@ynh.local", + "fullname": "the first name the last name", + } + } + + questions = [{"name": "some_user", "type": "user",}] + answers = {} + + with patch.object(user, "user_list", return_value={"users": users}): + with pytest.raises(YunohostError): + _parse_args_in_yunohost_format(answers, questions) + + +def test_parse_args_in_yunohost_format_user(): + username = "some_user" + users = { + username: { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "p@ynh.local", + "fullname": "the first name the last name", + } + } + + questions = [{"name": "some_user", "type": "user",}] + answers = {"some_user": username} + + expected_result = OrderedDict({"some_user": (username, "user")}) + + with patch.object(user, "user_list", return_value={"users": users}): + assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + +def test_parse_args_in_yunohost_format_user_two_users(): + username = "some_user" + other_user = "some_other_user" + users = { + username: { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "p@ynh.local", + "fullname": "the first name the last name", + }, + other_user: { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "z@ynh.local", + "fullname": "john doe", + }, + } + + questions = [{"name": "some_user", "type": "user",}] + answers = {"some_user": other_user} + expected_result = OrderedDict({"some_user": (other_user, "user")}) + + with patch.object(user, "user_list", return_value={"users": users}): + assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + answers = {"some_user": username} + expected_result = OrderedDict({"some_user": (username, "user")}) + + with patch.object(user, "user_list", return_value={"users": users}): + assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + +def test_parse_args_in_yunohost_format_user_two_users_wrong_answer(): + username = "my_username.com" + other_user = "some_other_user" + users = { + username: { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "p@ynh.local", + "fullname": "the first name the last name", + }, + other_user: { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "z@ynh.local", + "fullname": "john doe", + }, + } + + questions = [{"name": "some_user", "type": "user",}] + answers = {"some_user": "doesnt_exist.pouet"} + + with patch.object(user, "user_list", return_value={"users": users}): + with pytest.raises(YunohostError): + _parse_args_in_yunohost_format(answers, questions) + + +def test_parse_args_in_yunohost_format_user_two_users_no_default(): + username = "my_username.com" + other_user = "some_other_user.tld" + users = { + username: { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "p@ynh.local", + "fullname": "the first name the last name", + }, + other_user: { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "z@ynh.local", + "fullname": "john doe", + }, + } + + questions = [{"name": "some_user", "type": "user", "ask": "choose a user"}] + answers = {} + + with patch.object(user, "user_list", return_value={"users": users}): + with pytest.raises(YunohostError): + _parse_args_in_yunohost_format(answers, questions) + + +def test_parse_args_in_yunohost_format_user_two_users_default_input(): + username = "my_username.com" + other_user = "some_other_user.tld" + users = { + username: { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "p@ynh.local", + "fullname": "the first name the last name", + }, + other_user: { + "ssh_allowed": False, + "username": "some_user", + "shell": "/bin/false", + "mailbox-quota": "0", + "mail": "z@ynh.local", + "fullname": "john doe", + }, + } + + questions = [{"name": "some_user", "type": "user", "ask": "choose a user"}] + answers = {} + + with patch.object(user, "user_list", return_value={"users": users}): + expected_result = OrderedDict({"some_user": (username, "user")}) + with patch.object(msignals, "prompt", return_value=username): + assert _parse_args_in_yunohost_format(answers, questions) == expected_result + + 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