Skip to content

Commit 1cba7e1

Browse files
committed
move import, fix return
1 parent 0119ca1 commit 1cba7e1

File tree

4 files changed

+37
-51
lines changed

4 files changed

+37
-51
lines changed

src/sentry/api/serializers/models/organization.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
from sentry.users.models.user import User
9090
from sentry.users.services.user.model import RpcUser
9191
from sentry.users.services.user.service import user_service
92+
from sentry.utils.display_name_filter import is_spam_display_name
9293

9394
if TYPE_CHECKING:
9495
from sentry.api.serializers.models.project import OrganizationProjectResponse
@@ -176,11 +177,10 @@ def validate_name(self, value: str) -> str:
176177
"Organization name cannot contain URL schemes (e.g. http:// or https://)."
177178
)
178179

179-
from sentry.utils.display_name_filter import check_spam_display_name
180-
181-
spam_error = check_spam_display_name(value)
182-
if spam_error:
183-
raise serializers.ValidationError(spam_error)
180+
if is_spam_display_name(value):
181+
raise serializers.ValidationError(
182+
"This name contains disallowed content. Please choose a different name."
183+
)
184184

185185
return value
186186

src/sentry/sentry_apps/api/parsers/sentry_app.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from sentry.sentry_apps.api.parsers.schema import validate_ui_element_schema
1212
from sentry.sentry_apps.models.sentry_app import REQUIRED_EVENT_PERMISSIONS, UUID_CHARS_IN_SLUG
1313
from sentry.sentry_apps.utils.webhooks import VALID_EVENT_RESOURCES
14+
from sentry.utils.display_name_filter import is_spam_display_name
1415

1516

1617
@extend_schema_field(build_typed_list(OpenApiTypes.STR))
@@ -165,11 +166,10 @@ def validate_name(self, value):
165166
if len(value) > max_length:
166167
raise ValidationError("Cannot exceed %d characters" % max_length)
167168

168-
from sentry.utils.display_name_filter import check_spam_display_name
169-
170-
spam_error = check_spam_display_name(value)
171-
if spam_error:
172-
raise ValidationError(spam_error)
169+
if is_spam_display_name(value):
170+
raise ValidationError(
171+
"This name contains disallowed content. Please choose a different name."
172+
)
173173

174174
return value
175175

src/sentry/utils/display_name_filter.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,8 @@ def _has_cta(lowered: str) -> bool:
8787
]
8888

8989

90-
def check_spam_display_name(name: str) -> str | None:
91-
"""Return an error string if the name matches 2+ spam categories, else None."""
90+
def is_spam_display_name(name: str) -> bool:
91+
"""Return True if the name matches 2+ spam signal categories."""
9292
lowered = name.lower()
9393
matched = sum(1 for _, check in _CATEGORIES if check(lowered))
94-
if matched >= 2:
95-
return "This name contains disallowed content. Please choose a different name."
96-
return None
94+
return matched >= 2
Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,69 @@
1-
from sentry.utils.display_name_filter import check_spam_display_name
1+
from sentry.utils.display_name_filter import is_spam_display_name
22

33

4-
class TestCheckSpamDisplayName:
4+
class TestIsSpamDisplayName:
55
def test_clean_name_passes(self) -> None:
6-
assert check_spam_display_name("My Company Inc.") is None
6+
assert not is_spam_display_name("My Company Inc.")
77

88
def test_single_currency_signal_passes(self) -> None:
9-
assert check_spam_display_name("BTC Analytics") is None
9+
assert not is_spam_display_name("BTC Analytics")
1010

1111
def test_single_cta_verb_passes(self) -> None:
12-
assert check_spam_display_name("Click Studios") is None
12+
assert not is_spam_display_name("Click Studios")
1313

1414
def test_single_cta_urgency_passes(self) -> None:
15-
assert check_spam_display_name("Do It Now Labs") is None
15+
assert not is_spam_display_name("Do It Now Labs")
1616

1717
def test_cta_verb_plus_urgency_alone_passes(self) -> None:
18-
assert check_spam_display_name("Click Here Studios") is None
18+
assert not is_spam_display_name("Click Here Studios")
1919

2020
def test_single_shorturl_signal_passes(self) -> None:
21-
assert check_spam_display_name("bit.ly/promo team") is None
21+
assert not is_spam_display_name("bit.ly/promo team")
2222

2323
def test_currency_plus_cta_rejected(self) -> None:
24-
assert check_spam_display_name("Free BTC - Click Here") is not None
24+
assert is_spam_display_name("Free BTC - Click Here")
2525

2626
def test_currency_plus_shorturl_rejected(self) -> None:
27-
assert check_spam_display_name("Earn $100 via 2g.tel/promo") is not None
27+
assert is_spam_display_name("Earn $100 via 2g.tel/promo")
2828

2929
def test_shorturl_without_slash_not_matched(self) -> None:
30-
assert check_spam_display_name("support.com Solutions") is None
30+
assert not is_spam_display_name("support.com Solutions")
3131

3232
def test_cta_plus_shorturl_rejected(self) -> None:
33-
assert check_spam_display_name("Click Here: bit.ly/free") is not None
33+
assert is_spam_display_name("Click Here: bit.ly/free")
3434

3535
def test_bare_shorturl_domain_without_path_passes(self) -> None:
36-
assert check_spam_display_name("Free BTC bit.ly") is None
36+
assert not is_spam_display_name("Free BTC bit.ly")
3737

3838
def test_all_three_categories_rejected(self) -> None:
39-
result = check_spam_display_name("Win $50 ETH bit.ly/offer Claim Now")
40-
assert result is not None
39+
assert is_spam_display_name("Win $50 ETH bit.ly/offer Claim Now")
4140

4241
def test_case_insensitive(self) -> None:
43-
result = check_spam_display_name("FREE BTC - CLICK HERE")
44-
assert result is not None
42+
assert is_spam_display_name("FREE BTC - CLICK HERE")
4543

4644
def test_currency_emoji_detected(self) -> None:
47-
result = check_spam_display_name(
48-
"\U0001f4b2Compensation Btc: 2g.tel/x Click Your Pay Link."
49-
)
50-
assert result is not None
45+
assert is_spam_display_name("\U0001f4b2Compensation Btc: 2g.tel/x Click Your Pay Link.")
5146

5247
def test_single_currency_emoji_passes(self) -> None:
53-
assert check_spam_display_name("My \U0001f4b0 Company") is None
48+
assert not is_spam_display_name("My \U0001f4b0 Company")
5449

5550
def test_cta_novel_combo_rejected(self) -> None:
56-
result = check_spam_display_name("Withdraw Now - Free BTC")
57-
assert result is not None
51+
assert is_spam_display_name("Withdraw Now - Free BTC")
5852

5953
def test_substring_sol_in_solutions_not_matched(self) -> None:
60-
assert check_spam_display_name("Impactful Solutions") is None
54+
assert not is_spam_display_name("Impactful Solutions")
6155

6256
def test_substring_eth_in_method_not_matched(self) -> None:
63-
assert check_spam_display_name("Method Analytics") is None
57+
assert not is_spam_display_name("Method Analytics")
6458

6559
def test_substring_act_in_contact_not_matched(self) -> None:
66-
assert check_spam_display_name("Contact Knowledge Solutions") is None
60+
assert not is_spam_display_name("Contact Knowledge Solutions")
6761

6862
def test_substring_now_in_knowledge_not_matched(self) -> None:
69-
assert check_spam_display_name("Knowledge Now Platform") is None
63+
assert not is_spam_display_name("Knowledge Now Platform")
7064

7165
def test_substring_here_in_where_not_matched(self) -> None:
72-
assert check_spam_display_name("Where We Shine") is None
66+
assert not is_spam_display_name("Where We Shine")
7367

7468
def test_empty_string_passes(self) -> None:
75-
assert check_spam_display_name("") is None
76-
77-
def test_error_message_format(self) -> None:
78-
result = check_spam_display_name("Free BTC - Click Here")
79-
assert result is not None
80-
assert "disallowed content" in result
81-
assert "Please choose a different name" in result
69+
assert not is_spam_display_name("")

0 commit comments

Comments
 (0)