diff --git a/labelbox/schema/webhook.py b/labelbox/schema/webhook.py index ae0ab1a02..cdcbd811a 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 ValueError("Secret must be a non-empty string.") + if not topics: + raise ValueError("Topics must be a non-empty list.") + if not url: + 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 cfb58e08c..25c8c667a 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(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(project, rand_gen): + client = project.client + secret = rand_gen(str) + 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) == \ + "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(ValueError) as exc_info: + Webhook.create(client, topics, url, secret, project) + assert str(exc_info.value) == \ + "URL must be a non-empty string." diff --git a/tests/unit/test_unit_webhook.py b/tests/unit/test_unit_webhook.py new file mode 100644 index 000000000..405955ce6 --- /dev/null +++ b/tests/unit/test_unit_webhook.py @@ -0,0 +1,43 @@ +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 = [] + + 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."