webhooks/beanstalk: Improve test coverage.

We shouldn't just ignore exceptions when encoding the incoming
auth credentials. Even if the incoming credentials are properly
encoded, it is better to know when that is the case or if
something else fails.
This commit is contained in:
Eeshan Garg 2018-09-22 17:39:41 -02:30 committed by Rishi Gupta
parent cbb6692882
commit 185a023745
2 changed files with 7 additions and 11 deletions

View File

@ -100,7 +100,6 @@ not_yet_fully_covered = {
'zerver/data_import/gitter.py',
'zerver/data_import/import_util.py',
# Webhook integrations with incomplete coverage
'zerver/webhooks/beanstalk/view.py',
'zerver/webhooks/bitbucket2/view.py',
'zerver/webhooks/freshdesk/view.py',
'zerver/webhooks/github/view.py',

View File

@ -21,16 +21,13 @@ from zerver.webhooks.github_legacy.view import build_message_from_gitlog
def beanstalk_decoder(view_func: ViewFuncT) -> ViewFuncT:
@wraps(view_func)
def _wrapped_view_func(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
try:
auth_type, encoded_value = request.META['HTTP_AUTHORIZATION'].split() # type: str, str
if auth_type.lower() == "basic":
email, api_key = base64.b64decode(encoded_value).decode('utf-8').split(":")
email = email.replace('%40', '@')
credentials = u"%s:%s" % (email, api_key)
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf8') # type: str
request.META['HTTP_AUTHORIZATION'] = "Basic " + encoded_credentials
except Exception:
pass
auth_type, encoded_value = request.META['HTTP_AUTHORIZATION'].split() # type: str, str
if auth_type.lower() == "basic":
email, api_key = base64.b64decode(encoded_value).decode('utf-8').split(":")
email = email.replace('%40', '@')
credentials = u"%s:%s" % (email, api_key)
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf8') # type: str
request.META['HTTP_AUTHORIZATION'] = "Basic " + encoded_credentials
return view_func(request, *args, **kwargs)