webhooks: Replace headers parsing logic with a common source.

When parsing custom HTTP headers in the integrations dev panel, http
headers from fixtures system and the send_webhook_fixture_message
we now use a singular source of logic: standardize_headers which
will take care of converting a dictionary of input headers into a
standard form that Django expects.
This commit is contained in:
Hemanth V. Alluri
2019-06-21 08:11:30 +05:30
committed by Tim Abbott
parent 50d43902fb
commit e2549b3b84
7 changed files with 52 additions and 88 deletions

View File

@@ -20,7 +20,6 @@ from zerver.lib.test_runner import slow
from zerver.models import Recipient, get_user_profile_by_email, get_stream
from django.utils.timezone import now as timezone_now
from zerver.management.commands.send_webhook_fixture_message import Command
from zerver.lib.test_helpers import most_recent_message
from zerver.models import get_realm, UserProfile, Realm, Reaction, Message
from confirmation.models import RealmCreationKey, generate_realm_creation_url
@@ -202,8 +201,8 @@ class TestSendWebhookFixtureMessage(TestCase):
ujson_mock: MagicMock,
client_mock: MagicMock,
os_path_exists_mock: MagicMock) -> None:
ujson_mock.loads.return_value = '{}'
ujson_mock.dumps.return_value = {}
ujson_mock.loads.return_value = {}
ujson_mock.dumps.return_value = "{}"
os_path_exists_mock.return_value = True
client = client_mock()
@@ -213,34 +212,9 @@ class TestSendWebhookFixtureMessage(TestCase):
self.assertTrue(ujson_mock.dumps.called)
self.assertTrue(ujson_mock.loads.called)
self.assertTrue(open_mock.called)
client.post.assert_called_once_with(self.url, {}, content_type="application/json",
client.post.assert_called_once_with(self.url, "{}", content_type="application/json",
HTTP_HOST="zulip.testserver")
@patch('zerver.management.commands.send_webhook_fixture_message.Command._get_fixture_as_json')
@patch('zerver.management.commands.send_webhook_fixture_message.Command._does_fixture_path_exist')
@patch('zerver.management.commands.send_webhook_fixture_message.Command.parse_headers')
def test_check_post_request_with_improper_custom_header(self,
parse_headers_mock: MagicMock,
does_fixture_path_exist_mock: MagicMock,
get_fixture_as_json_mock: MagicMock) -> None:
does_fixture_path_exist_mock.return_value = True
get_fixture_as_json_mock.return_value = "{}"
improper_headers = '{"X-Custom - Headers": "some_val"}'
with self.assertRaises(CommandError):
call_command(self.COMMAND_NAME, fixture=self.fixture_path, url=self.url,
custom_headers=improper_headers)
parse_headers_mock.assert_called_once_with(improper_headers)
def test_parse_headers_method(self) -> None:
command = Command()
self.assertEqual(command.parse_headers(None), None)
self.assertEqual(command.parse_headers('{"Content-Type": "text/plain", "X-Custom-Header": "value"}'),
{"CONTENT_TYPE": "text/plain", "HTTP_X_CUSTOM_HEADER": "value"})
with self.assertRaises(CommandError):
command.parse_headers('{"X-Custom - Headers": "some_val"}')
class TestGenerateRealmCreationLink(ZulipTestCase):
COMMAND_NAME = "generate_realm_creation_link"