Skip to content

Commit 371dcc7

Browse files
[SDK-400] Validate url, secret, and topic list on webhook creation. (#1281)
1 parent 032b6f3 commit 371dcc7

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

labelbox/schema/webhook.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,15 @@ def create(client, topics, url, secret, project) -> "Webhook":
7777
ValueError: If the topic is not one of Topic or status is not one of Status
7878
7979
Information on configuring your server can be found here (this is where the url points to and the secret is set).
80-
https://docs.labelbox.com/en/configure-editor/webhooks-setup#setup-steps
80+
https://docs.labelbox.com/reference/webhook
8181
8282
"""
83+
if not secret:
84+
raise ValueError("Secret must be a non-empty string.")
85+
if not topics:
86+
raise ValueError("Topics must be a non-empty list.")
87+
if not url:
88+
raise ValueError("URL must be a non-empty string.")
8389
Webhook.validate_topics(topics)
8490

8591
project_str = "" if project is None \

tests/integration/test_webhook.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,39 @@ def test_webhook_create_update(project, rand_gen):
4040
"Topics must be List[Webhook.Topic]. Found `invalid..`"
4141

4242
webhook.delete()
43+
44+
45+
def test_webhook_create_with_no_secret(project, rand_gen):
46+
client = project.client
47+
secret = ""
48+
url = "https:/" + rand_gen(str)
49+
topics = []
50+
51+
with pytest.raises(ValueError) as exc_info:
52+
Webhook.create(client, topics, url, secret, project)
53+
assert str(exc_info.value) == \
54+
"Secret must be a non-empty string."
55+
56+
57+
def test_webhook_create_with_no_topics(project, rand_gen):
58+
client = project.client
59+
secret = rand_gen(str)
60+
url = "https:/" + rand_gen(str)
61+
topics = []
62+
63+
with pytest.raises(ValueError) as exc_info:
64+
Webhook.create(client, topics, url, secret, project)
65+
assert str(exc_info.value) == \
66+
"Topics must be a non-empty list."
67+
68+
69+
def test_webhook_create_with_no_url(project, rand_gen):
70+
client = project.client
71+
secret = rand_gen(str)
72+
url = ""
73+
topics = [Webhook.LABEL_CREATED, Webhook.LABEL_DELETED]
74+
75+
with pytest.raises(ValueError) as exc_info:
76+
Webhook.create(client, topics, url, secret, project)
77+
assert str(exc_info.value) == \
78+
"URL must be a non-empty string."

tests/unit/test_unit_webhook.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from unittest.mock import MagicMock
2+
import pytest
3+
4+
from labelbox import Webhook
5+
6+
7+
def test_webhook_create_with_no_secret(rand_gen):
8+
client = MagicMock()
9+
project = MagicMock()
10+
secret = ""
11+
url = "https:/" + rand_gen(str)
12+
topics = []
13+
14+
with pytest.raises(ValueError) as exc_info:
15+
Webhook.create(client, topics, url, secret, project)
16+
assert str(exc_info.value) == \
17+
"Secret must be a non-empty string."
18+
19+
20+
def test_webhook_create_with_no_topics(rand_gen):
21+
client = MagicMock()
22+
project = MagicMock()
23+
secret = rand_gen(str)
24+
url = "https:/" + rand_gen(str)
25+
topics = []
26+
27+
with pytest.raises(ValueError) as exc_info:
28+
Webhook.create(client, topics, url, secret, project)
29+
assert str(exc_info.value) == \
30+
"Topics must be a non-empty list."
31+
32+
33+
def test_webhook_create_with_no_url(rand_gen):
34+
client = MagicMock()
35+
project = MagicMock()
36+
secret = rand_gen(str)
37+
url = ""
38+
topics = [Webhook.Topic.LABEL_CREATED, Webhook.Topic.LABEL_DELETED]
39+
40+
with pytest.raises(ValueError) as exc_info:
41+
Webhook.create(client, topics, url, secret, project)
42+
assert str(exc_info.value) == \
43+
"URL must be a non-empty string."

0 commit comments

Comments
 (0)