def test__send_http_should_return_None_when_receive_invalid_data(
            self, mock_get):
        tcb = TelegramController()
        dummy_response = MagicMock()
        dummy_response.json.side_effect = ValueError()
        mock_get.return_value = dummy_response

        updates = tcb._send_http("test_url")
        self.assertEqual(updates, None)
 def test__get_updates_call_getUpdates_api_correctly(self):
     tcb = TelegramController()
     tcb.TOKEN = "banana"
     expected_response = "banana_result"
     tcb._send_http = MagicMock(return_value=expected_response)
     updates = tcb._get_updates()
     self.assertEqual(updates, expected_response)
     tcb._send_http.assert_called_once_with(
         "https://api.telegram.org/banana/getUpdates?offset=1&timeout=10")
 def test__send_http_should_call_requests_get_when_is_post_False(
         self, mock_get):
     tcb = TelegramController()
     expected_response = {"dummy"}
     dummy_response = MagicMock()
     dummy_response.json.return_value = expected_response
     mock_get.return_value = dummy_response
     updates = tcb._send_http("test_url")
     self.assertEqual(updates, expected_response)
     self.assertEqual(mock_get.call_args[0][0].find("test_url"), 0)
    def test__send_http_should_return_None_when_connection_fail(
            self, mock_get):
        tcb = TelegramController()
        dummy_response = MagicMock()
        dummy_response.json.return_value = "dummy_result"
        dummy_response.raise_for_status.side_effect = requests.exceptions.RequestException(
            "RequestException dummy exception")
        mock_get.return_value = dummy_response

        updates = tcb._send_http("test_url")
        self.assertEqual(updates, None)
 def test__send_http_should_call_requests_post_with_file_and_return_result(
         self, mock_post, mock_file):
     tcb = TelegramController()
     expected_response = {"dummy"}
     dummy_response = MagicMock()
     dummy_response.json.return_value = expected_response
     mock_post.return_value = dummy_response
     updates = tcb._send_http("test_url", True, "mango")
     mock_file.assert_called_with("mango", "rb")
     self.assertEqual(updates, expected_response)
     self.assertEqual(mock_post.call_args[0][0].find("test_url"), 0)
     self.assertEqual(mock_post.call_args[1]["files"], {"photo": ANY})
    def test__send_text_message_shoul_call_sendMessage_api_correctly(self):
        tcb = TelegramController()
        tcb.post_worker = MagicMock()
        tcb.TOKEN = "banana"
        tcb.CHAT_ID = "to_banana"
        tcb._send_http = MagicMock()
        tcb._send_text_message("hello~ banana")
        tcb.post_worker.post_task.assert_called_once_with(ANY)
        task = tcb.post_worker.post_task.call_args[0][0]
        tcb.post_worker.post_task.call_args[0][0]["runnable"](task)

        tcb._send_http.assert_called_once_with(
            "https://api.telegram.org/banana/sendMessage?chat_id=to_banana&text=hello%7E%20banana"
        )