papertrail: Replace custom validator with check_dict.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2020-06-20 20:15:08 -07:00
committed by Tim Abbott
parent 114f859d3a
commit 596bfb47c4
2 changed files with 14 additions and 12 deletions

View File

@@ -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

View File

@@ -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"],