Merge pull request #1629 from YunoHost/tests-cp

[tests] Rework questions/options tests
This commit is contained in:
Alexandre Aubin 2023-04-04 13:01:35 +02:00 committed by GitHub
commit 17e0a11547
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 1957 additions and 1693 deletions

File diff suppressed because it is too large Load diff

View file

@ -858,7 +858,9 @@ class Question:
# Don't restrict choices if there's none specified # Don't restrict choices if there's none specified
self.choices = question.get("choices", None) self.choices = question.get("choices", None)
self.pattern = question.get("pattern", self.pattern) self.pattern = question.get("pattern", self.pattern)
self.ask = question.get("ask", {"en": self.name}) self.ask = question.get("ask", self.name)
if not isinstance(self.ask, dict):
self.ask = {"en": self.ask}
self.help = question.get("help") self.help = question.get("help")
self.redact = question.get("redact", False) self.redact = question.get("redact", False)
self.filter = question.get("filter", None) self.filter = question.get("filter", None)
@ -964,7 +966,7 @@ class Question:
"app_argument_choice_invalid", "app_argument_choice_invalid",
name=self.name, name=self.name,
value=self.value, value=self.value,
choices=", ".join(self.choices), choices=", ".join(str(choice) for choice in self.choices),
) )
if self.pattern and not re.match(self.pattern["regexp"], str(self.value)): if self.pattern and not re.match(self.pattern["regexp"], str(self.value)):
raise YunohostValidationError( raise YunohostValidationError(
@ -1087,13 +1089,13 @@ class TagsQuestion(Question):
@staticmethod @staticmethod
def humanize(value, option={}): def humanize(value, option={}):
if isinstance(value, list): if isinstance(value, list):
return ",".join(value) return ",".join(str(v) for v in value)
return value return value
@staticmethod @staticmethod
def normalize(value, option={}): def normalize(value, option={}):
if isinstance(value, list): if isinstance(value, list):
return ",".join(value) return ",".join(str(v) for v in value)
if isinstance(value, str): if isinstance(value, str):
value = value.strip() value = value.strip()
return value return value
@ -1104,6 +1106,21 @@ class TagsQuestion(Question):
values = values.split(",") values = values.split(",")
elif values is None: elif values is None:
values = [] values = []
if not isinstance(values, list):
if self.choices:
raise YunohostValidationError(
"app_argument_choice_invalid",
name=self.name,
value=self.value,
choices=", ".join(str(choice) for choice in self.choices),
)
raise YunohostValidationError(
"app_argument_invalid",
name=self.name,
error=f"'{str(self.value)}' is not a list",
)
for value in values: for value in values:
self.value = value self.value = value
super()._prevalidate() super()._prevalidate()
@ -1154,6 +1171,13 @@ class PathQuestion(Question):
def normalize(value, option={}): def normalize(value, option={}):
option = option.__dict__ if isinstance(option, Question) else option option = option.__dict__ if isinstance(option, Question) else option
if not isinstance(value, str):
raise YunohostValidationError(
"app_argument_invalid",
name=option.get("name"),
error="Argument for path should be a string.",
)
if not value.strip(): if not value.strip():
if option.get("optional"): if option.get("optional"):
return "" return ""
@ -1401,7 +1425,7 @@ class NumberQuestion(Question):
return int(value) return int(value)
if value in [None, ""]: if value in [None, ""]:
return value return None
option = option.__dict__ if isinstance(option, Question) else option option = option.__dict__ if isinstance(option, Question) else option
raise YunohostValidationError( raise YunohostValidationError(
@ -1483,8 +1507,12 @@ class FileQuestion(Question):
super()._prevalidate() super()._prevalidate()
# Validation should have already failed if required
if self.value in [None, ""]:
return self.value
if Moulinette.interface.type != "api": if Moulinette.interface.type != "api":
if not self.value or not os.path.exists(str(self.value)): if not os.path.exists(str(self.value)) or not os.path.isfile(str(self.value)):
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_invalid", "app_argument_invalid",
name=self.name, name=self.name,
@ -1495,7 +1523,7 @@ class FileQuestion(Question):
from base64 import b64decode from base64 import b64decode
if not self.value: if not self.value:
return self.value return ""
upload_dir = tempfile.mkdtemp(prefix="ynh_filequestion_") upload_dir = tempfile.mkdtemp(prefix="ynh_filequestion_")
_, file_path = tempfile.mkstemp(dir=upload_dir) _, file_path = tempfile.mkstemp(dir=upload_dir)