Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

papertrail: Replace custom validator with check_dict.

Anders Kaseorg596bfb47c49d
diff --git a/zerver/webhooks/papertrail/tests.py b/zerver/webhooks/papertrail/tests.py
index 51806353f3f..1128036b75b 100644
--- a/zerver/webhooks/papertrail/tests.py
+++ b/zerver/webhooks/papertrail/tests.py
@@ -58,7 +58,7 @@ message body 4
self.send_and_test_stream_message('incorrect_post', '', '',
content_type="application/x-www-form-urlencoded")
- self.assertIn("Missing expected keys", e.exception.args[0])
+ self.assertIn("events key is missing from payload", e.exception.args[0])
def get_body(self, fixture_name: str) -> str:
# Papertrail webhook sends a POST request with payload parameter
diff --git a/zerver/webhooks/papertrail/view.py b/zerver/webhooks/papertrail/view.py
index 69f8f8b0340..8d0a44b223e 100644
--- a/zerver/webhooks/papertrail/view.py
+++ b/zerver/webhooks/papertrail/view.py
@@ -1,11 +1,11 @@
-from typing import Any, Dict, Optional
+from typing import Any, Dict
from django.http import HttpRequest, HttpResponse
-from django.utils.translation import ugettext as _
from zerver.decorator import api_key_only_webhook_view
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
+from zerver.lib.validator import check_dict, check_list, check_string
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile
@@ -17,17 +17,19 @@ SEARCH_TEMPLATE = """
```
""".strip()
-def ensure_keys(name: str, data: Any) -> Optional[str]:
- if 'events' in data and 'saved_search' in data:
- saved_search = data['saved_search']
- if 'name' in saved_search and 'html_search_url' in saved_search:
- return None
- return _("Missing expected keys")
-
@api_key_only_webhook_view('Papertrail')
@has_request_variables
-def api_papertrail_webhook(request: HttpRequest, user_profile: UserProfile,
- payload: Dict[str, Any]=REQ(validator=ensure_keys)) -> HttpResponse:
+def api_papertrail_webhook(
+ request: HttpRequest,
+ user_profile: UserProfile,
+ payload: Dict[str, Any] = REQ(validator=check_dict([
+ ("events", check_list(check_dict([]))),
+ ("saved_search", check_dict([
+ ("name", check_string),
+ ("html_search_url", check_string),
+ ])),
+ ])),
+) -> HttpResponse:
matches = MATCHES_TEMPLATE.format(
name=payload["saved_search"]["name"],