From 3240203ea448298c2b656e911e7edcb51e0f1c82 Mon Sep 17 00:00:00 2001 From: Dickson Chibuzor Date: Mon, 30 Oct 2023 23:26:45 +0100 Subject: [PATCH 1/2] make sure there is taskdata in the manifest --- basemodels/manifest/manifest.py | 19 ++++++++----------- pyproject.toml | 2 +- setup.py | 2 +- tests/test_manifest_validation.py | 7 +++++++ tests/test_restricted_answer_set.py | 1 + 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/basemodels/manifest/manifest.py b/basemodels/manifest/manifest.py index 926b35d..3845aca 100644 --- a/basemodels/manifest/manifest.py +++ b/basemodels/manifest/manifest.py @@ -20,15 +20,6 @@ from basemodels.constants import JOB_TYPES_FOR_CONTENT_TYPE_VALIDATION -# Validator function for taskdata and taskdata_uri fields -def validator_taskdata_uri(cls, value, values, **kwargs): - taskdata = values.get("taskdata") - taskdata_uri = values.get("taskdata_uri") - if taskdata is not None and len(taskdata) > 0 and taskdata_uri is not None: - raise ValidationError("Specify only one of taskdata {} or taskdata_uri {}".format(taskdata, taskdata_uri)) - return value - - # A validator function for UUID fields def validate_uuid(cls, value): return value or uuid.uuid4() @@ -329,6 +320,14 @@ class Manifest(Model): def validate(cls, values: Dict[str, Any]) -> Dict[str, Any]: start_date = values["start_date"] expiration_date = values["expiration_date"] + # validate at least taskdata or taskdata_uri is present + taskdata = values.get("taskdata") + taskdata_uri = values.get("taskdata_uri") + if taskdata is not None and len(taskdata) > 0 and taskdata_uri is not None: + raise ValueError("Specify only one of taskdata {} or taskdata_uri {}".format(taskdata, taskdata_uri)) + if taskdata is None and taskdata_uri is None: + raise ValueError("No taskdata or taskdata_uri found in manifest") + if not start_date and not expiration_date: # Timestamps are not passed return values @@ -397,8 +396,6 @@ def validate_requester_question_example(cls, value, values, **kwargs): raise ValueError("Lists are not allowed in this challenge type") return value - validate_taskdata_uri = validator("taskdata_uri", allow_reuse=True, always=True)(validator_taskdata_uri) - validate_taskdata = validator("taskdata", allow_reuse=True, always=True)(validator_taskdata_uri) validate_request_type = validator("request_type", allow_reuse=True, always=True)(RequestTypeValidator().validate) validate_job_api_key = validator("job_api_key", always=True, allow_reuse=True)(validate_uuid) validate_job_id = validator("job_id", always=True, allow_reuse=True)(validate_uuid) diff --git a/pyproject.toml b/pyproject.toml index 084de7b..070ff1b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "hmt-basemodels" -version = "0.2.5" +version = "0.2.6" description = "" authors = ["Intuition Machines, Inc "] packages = [ diff --git a/setup.py b/setup.py index a2035aa..9ef0e5c 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setuptools.setup( name="hmt-basemodels", - version="0.2.5", + version="0.2.6", author="HUMAN Protocol", description="Common data models shared by various components of the Human Protocol stack", url="https://github.com/hCaptcha/hmt-basemodels", diff --git a/tests/test_manifest_validation.py b/tests/test_manifest_validation.py index e40c354..e11675b 100755 --- a/tests/test_manifest_validation.py +++ b/tests/test_manifest_validation.py @@ -196,6 +196,13 @@ def test_can_fail_toconstruct2(self): mani.taskdata_uri = "test" self.assertRaises(ValidationError, validate_func(mani)) + def test_manifest_must_contain_taskdata(self): + """Tests that manifest raises error when there is no taskdata""" + manifest = get_data() + manifest["taskdata_uri"] = None + with self.assertRaises(ValidationError): + basemodels.manifest.Manifest(**manifest) + def test_can_make_request_config_job(self): """Test that jobs with valid request_config parameter work""" manifest = a_manifest( diff --git a/tests/test_restricted_answer_set.py b/tests/test_restricted_answer_set.py index 7162b53..97c75d6 100644 --- a/tests/test_restricted_answer_set.py +++ b/tests/test_restricted_answer_set.py @@ -13,6 +13,7 @@ "recording_oracle_addr": "0x6a0E68eA5F706339dd6bd354F53EfcB5B9e53E49", "reputation_oracle_addr": "0x6a0E68eA5F706339dd6bd354F53EfcB5B9e53E49", "reputation_agent_addr": "0x6a0E68eA5F706339dd6bd354F53EfcB5B9e53E49", + "taskdata_uri": "http://google.com/fake", } NESTED_MANIFEST_VALUES = { From d54efebbdda63398c8b397bcd87f642a3a587b22 Mon Sep 17 00:00:00 2001 From: Dickson Chibuzor Date: Tue, 31 Oct 2023 17:09:21 +0100 Subject: [PATCH 2/2] update version --- pyproject.toml | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 070ff1b..99b6b00 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "hmt-basemodels" -version = "0.2.6" +version = "0.2.7" description = "" authors = ["Intuition Machines, Inc "] packages = [ diff --git a/setup.py b/setup.py index 9ef0e5c..c2f87d9 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setuptools.setup( name="hmt-basemodels", - version="0.2.6", + version="0.2.7", author="HUMAN Protocol", description="Common data models shared by various components of the Human Protocol stack", url="https://github.com/hCaptcha/hmt-basemodels",