From f4940b7d037e147c80f73ceec2b95b6436af0174 Mon Sep 17 00:00:00 2001 From: Samuel Fendell Date: Mon, 13 Nov 2023 16:22:53 -0800 Subject: [PATCH 1/8] [SDK-400] Validate url, secret, and topic list on webhook creation. --- labelbox/schema/webhook.py | 8 ++++++- tests/integration/test_webhook.py | 36 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/labelbox/schema/webhook.py b/labelbox/schema/webhook.py index ae0ab1a02..1e988ab1b 100644 --- a/labelbox/schema/webhook.py +++ b/labelbox/schema/webhook.py @@ -77,9 +77,15 @@ def create(client, topics, url, secret, project) -> "Webhook": ValueError: If the topic is not one of Topic or status is not one of Status Information on configuring your server can be found here (this is where the url points to and the secret is set). - https://docs.labelbox.com/en/configure-editor/webhooks-setup#setup-steps + https://docs.labelbox.com/reference/webhook """ + if not secret: + raise TypeError(f"Secret must be a non-empty string.") + if not topics: + raise TypeError(f"Topics must be a non-empty list.") + if not url: + raise TypeError(f"URL must be a non-empty string.") Webhook.validate_topics(topics) project_str = "" if project is None \ diff --git a/tests/integration/test_webhook.py b/tests/integration/test_webhook.py index cfb58e08c..5ee73e6fe 100644 --- a/tests/integration/test_webhook.py +++ b/tests/integration/test_webhook.py @@ -40,3 +40,39 @@ def test_webhook_create_update(project, rand_gen): "Topics must be List[Webhook.Topic]. Found `invalid..`" webhook.delete() + + +def test_webhook_create_with_no_secret(project, rand_gen): + client = project.client + secret = "" + url = "https:/" + rand_gen(str) + topics = [] + + with pytest.raises(TypeError) as exc_info: + webhook = Webhook.create(client, topics, url, secret, project) + assert str(exc_info.value) == \ + "Secret must be a non-empty string." + + +def test_webhook_create_with_no_topics(project, rand_gen): + client = project.client + secret = rand_gen(str) + url = "https:/" + rand_gen(str) + topics = [] + + with pytest.raises(TypeError) as exc_info: + webhook = Webhook.create(client, topics, url, secret, project) + assert str(exc_info.value) == \ + "Topics must be a non-empty list." + + +def test_webhook_create_with_no_url(project, rand_gen): + client = project.client + secret = rand_gen(str) + url = "" + topics = [Webhook.LABEL_CREATED, Webhook.LABEL_DELETED] + + with pytest.raises(TypeError) as exc_info: + webhook = Webhook.create(client, topics, url, secret, project) + assert str(exc_info.value) == \ + "URL must be a non-empty string." From 1afa212fb72b8e219442b86a683f261391e378c9 Mon Sep 17 00:00:00 2001 From: Samuel Fendell Date: Wed, 15 Nov 2023 15:28:35 -0800 Subject: [PATCH 2/8] Addressing PR comment. --- labelbox/schema/webhook.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/labelbox/schema/webhook.py b/labelbox/schema/webhook.py index 1e988ab1b..e46a3ee23 100644 --- a/labelbox/schema/webhook.py +++ b/labelbox/schema/webhook.py @@ -81,11 +81,11 @@ def create(client, topics, url, secret, project) -> "Webhook": """ if not secret: - raise TypeError(f"Secret must be a non-empty string.") + raise ValueError(f"Secret must be a non-empty string.") if not topics: - raise TypeError(f"Topics must be a non-empty list.") + raise ValueError(f"Topics must be a non-empty list.") if not url: - raise TypeError(f"URL must be a non-empty string.") + raise ValueError(f"URL must be a non-empty string.") Webhook.validate_topics(topics) project_str = "" if project is None \ From a3c3bd317e40577e1ac583ccca33921f6dbd3777 Mon Sep 17 00:00:00 2001 From: Samuel Fendell Date: Thu, 16 Nov 2023 09:11:46 -0800 Subject: [PATCH 3/8] Minor refactoring, addressing PR comments. --- labelbox/schema/webhook.py | 6 +++--- tests/integration/test_webhook.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/labelbox/schema/webhook.py b/labelbox/schema/webhook.py index e46a3ee23..cdcbd811a 100644 --- a/labelbox/schema/webhook.py +++ b/labelbox/schema/webhook.py @@ -81,11 +81,11 @@ def create(client, topics, url, secret, project) -> "Webhook": """ if not secret: - raise ValueError(f"Secret must be a non-empty string.") + raise ValueError("Secret must be a non-empty string.") if not topics: - raise ValueError(f"Topics must be a non-empty list.") + raise ValueError("Topics must be a non-empty list.") if not url: - raise ValueError(f"URL must be a non-empty string.") + raise ValueError("URL must be a non-empty string.") Webhook.validate_topics(topics) project_str = "" if project is None \ diff --git a/tests/integration/test_webhook.py b/tests/integration/test_webhook.py index 5ee73e6fe..a4b6360b4 100644 --- a/tests/integration/test_webhook.py +++ b/tests/integration/test_webhook.py @@ -49,7 +49,7 @@ def test_webhook_create_with_no_secret(project, rand_gen): topics = [] with pytest.raises(TypeError) as exc_info: - webhook = Webhook.create(client, topics, url, secret, project) + Webhook.create(client, topics, url, secret, project) assert str(exc_info.value) == \ "Secret must be a non-empty string." @@ -61,7 +61,7 @@ def test_webhook_create_with_no_topics(project, rand_gen): topics = [] with pytest.raises(TypeError) as exc_info: - webhook = Webhook.create(client, topics, url, secret, project) + Webhook.create(client, topics, url, secret, project) assert str(exc_info.value) == \ "Topics must be a non-empty list." @@ -73,6 +73,6 @@ def test_webhook_create_with_no_url(project, rand_gen): topics = [Webhook.LABEL_CREATED, Webhook.LABEL_DELETED] with pytest.raises(TypeError) as exc_info: - webhook = Webhook.create(client, topics, url, secret, project) + Webhook.create(client, topics, url, secret, project) assert str(exc_info.value) == \ "URL must be a non-empty string." From 050e82d7e2bafbab38ea5b1baaec1ee512847eb5 Mon Sep 17 00:00:00 2001 From: Samuel Fendell Date: Thu, 16 Nov 2023 16:07:15 -0800 Subject: [PATCH 4/8] Fix integration tests. --- tests/integration/test_webhook.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integration/test_webhook.py b/tests/integration/test_webhook.py index a4b6360b4..25c8c667a 100644 --- a/tests/integration/test_webhook.py +++ b/tests/integration/test_webhook.py @@ -48,7 +48,7 @@ def test_webhook_create_with_no_secret(project, rand_gen): url = "https:/" + rand_gen(str) topics = [] - with pytest.raises(TypeError) as exc_info: + with pytest.raises(ValueError) as exc_info: Webhook.create(client, topics, url, secret, project) assert str(exc_info.value) == \ "Secret must be a non-empty string." @@ -60,7 +60,7 @@ def test_webhook_create_with_no_topics(project, rand_gen): url = "https:/" + rand_gen(str) topics = [] - with pytest.raises(TypeError) as exc_info: + with pytest.raises(ValueError) as exc_info: Webhook.create(client, topics, url, secret, project) assert str(exc_info.value) == \ "Topics must be a non-empty list." @@ -72,7 +72,7 @@ def test_webhook_create_with_no_url(project, rand_gen): url = "" topics = [Webhook.LABEL_CREATED, Webhook.LABEL_DELETED] - with pytest.raises(TypeError) as exc_info: + with pytest.raises(ValueError) as exc_info: Webhook.create(client, topics, url, secret, project) assert str(exc_info.value) == \ "URL must be a non-empty string." From 679e47ea9e41b332d4dc0a60d5c209bd1a56a6d7 Mon Sep 17 00:00:00 2001 From: Samuel Fendell Date: Fri, 17 Nov 2023 10:24:48 -0800 Subject: [PATCH 5/8] Integration tests moved to unit. --- tests/unit/test_webhook.py | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/unit/test_webhook.py diff --git a/tests/unit/test_webhook.py b/tests/unit/test_webhook.py new file mode 100644 index 000000000..7f005f812 --- /dev/null +++ b/tests/unit/test_webhook.py @@ -0,0 +1,42 @@ +from unittest.mock import MagicMock +import pytest + +from labelbox import Webhook + +def test_webhook_create_with_no_secret(rand_gen): + client = MagicMock() + project = MagicMock() + secret = "" + url = "https:/" + rand_gen(str) + topics = [] + + with pytest.raises(ValueError) as exc_info: + Webhook.create(client, topics, url, secret, project) + assert str(exc_info.value) == \ + "Secret must be a non-empty string." + + +def test_webhook_create_with_no_topics(rand_gen): + client = MagicMock() + project = MagicMock() + secret = rand_gen(str) + url = "https:/" + rand_gen(str) + topics = ["1"] + + with pytest.raises(ValueError) as exc_info: + Webhook.create(client, topics, url, secret, project) + assert str(exc_info.value) == \ + "Topics must be a non-empty list." + + +def test_webhook_create_with_no_url(rand_gen): + client = MagicMock() + project = MagicMock() + secret = rand_gen(str) + url = "" + topics = [Webhook.Topic.LABEL_CREATED, Webhook.Topic.LABEL_DELETED] + + with pytest.raises(ValueError) as exc_info: + Webhook.create(client, topics, url, secret, project) + assert str(exc_info.value) == \ + "URL must be a non-empty string." From b74f91c34f05badd1cbe853cb948110c5fe5ea74 Mon Sep 17 00:00:00 2001 From: Samuel Fendell Date: Fri, 17 Nov 2023 10:26:54 -0800 Subject: [PATCH 6/8] Formatting. --- tests/unit/test_webhook.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/test_webhook.py b/tests/unit/test_webhook.py index 7f005f812..377cb068f 100644 --- a/tests/unit/test_webhook.py +++ b/tests/unit/test_webhook.py @@ -3,6 +3,7 @@ from labelbox import Webhook + def test_webhook_create_with_no_secret(rand_gen): client = MagicMock() project = MagicMock() From 2263de9f74ec5219b7f3ad4819e82e0cb3a92c6d Mon Sep 17 00:00:00 2001 From: Samuel Fendell Date: Fri, 17 Nov 2023 11:02:01 -0800 Subject: [PATCH 7/8] Disambiguate test file names. --- tests/unit/{test_webhook.py => test_unit_webhook.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/unit/{test_webhook.py => test_unit_webhook.py} (100%) diff --git a/tests/unit/test_webhook.py b/tests/unit/test_unit_webhook.py similarity index 100% rename from tests/unit/test_webhook.py rename to tests/unit/test_unit_webhook.py From 1745a07c38976d8a3aeec61afdc44b0949416d76 Mon Sep 17 00:00:00 2001 From: Samuel Fendell Date: Fri, 17 Nov 2023 11:44:47 -0800 Subject: [PATCH 8/8] Fix typo. --- tests/unit/test_unit_webhook.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_unit_webhook.py b/tests/unit/test_unit_webhook.py index 377cb068f..405955ce6 100644 --- a/tests/unit/test_unit_webhook.py +++ b/tests/unit/test_unit_webhook.py @@ -22,7 +22,7 @@ def test_webhook_create_with_no_topics(rand_gen): project = MagicMock() secret = rand_gen(str) url = "https:/" + rand_gen(str) - topics = ["1"] + topics = [] with pytest.raises(ValueError) as exc_info: Webhook.create(client, topics, url, secret, project)