From 48b5b2f5d5b2e8a777035f7cad58cc3e02c68aa8 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Tue, 23 Oct 2018 16:24:06 +0000 Subject: [PATCH] hipchat import: Start adding tests. These test are for the handling of HipChat sender info. The data formats are somewhat inconsistent and sometimes require us to generate "mirror" users, so this is potentially fragile code if we don't cover it well. --- zerver/tests/test_hipchat_importer.py | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 zerver/tests/test_hipchat_importer.py diff --git a/zerver/tests/test_hipchat_importer.py b/zerver/tests/test_hipchat_importer.py new file mode 100644 index 0000000000..30431406cb --- /dev/null +++ b/zerver/tests/test_hipchat_importer.py @@ -0,0 +1,55 @@ +from zerver.data_import.hipchat import ( + get_hipchat_sender_id, +) +from zerver.data_import.hipchat_user import ( + UserHandler, +) + +from zerver.lib.test_classes import ( + ZulipTestCase, +) +from typing import Any, Dict + + +class HipChatImporter(ZulipTestCase): + def test_sender_ids(self) -> None: + realm_id = 5 + user_handler = UserHandler() + + # Simulate a "normal" user first. + user_with_id = dict( + id=1, + # other fields don't matter here + ) + user_handler.add_user(user=user_with_id) + + normal_message = dict( + sender=dict( + id=1, + ) + ) # type: Dict[str, Any] + + sender_id = get_hipchat_sender_id( + realm_id=realm_id, + message_dict=normal_message, + user_handler=user_handler, + ) + + self.assertEqual(sender_id, 1) + + bot_message = dict( + sender='fred_bot', + ) + + # Every message from fred_bot should + # return the same sender_id. + fred_bot_sender_id = 2 + + for i in range(3): + sender_id = get_hipchat_sender_id( + realm_id=realm_id, + message_dict=bot_message, + user_handler=user_handler, + ) + + self.assertEqual(sender_id, fred_bot_sender_id)