|
| 1 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 2 | +# Licensed under the Apache License, Version 2.0 (the “License”); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an “AS IS” BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 14 | +import os |
| 15 | +from unittest.mock import MagicMock, patch |
| 16 | + |
| 17 | +import pytest |
| 18 | +from requests import RequestException |
| 19 | + |
| 20 | +from camel.toolkits.whatsapp_toolkit import WhatsAppToolkit |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def whatsapp_toolkit(): |
| 25 | + # Set environment variables for testing |
| 26 | + os.environ['WHATSAPP_ACCESS_TOKEN'] = 'test_token' |
| 27 | + os.environ['WHATSAPP_PHONE_NUMBER_ID'] = 'test_phone_number_id' |
| 28 | + return WhatsAppToolkit() |
| 29 | + |
| 30 | + |
| 31 | +def test_init_missing_credentials(): |
| 32 | + # Test initialization with missing credentials |
| 33 | + os.environ.pop('WHATSAPP_ACCESS_TOKEN', None) |
| 34 | + os.environ.pop('WHATSAPP_PHONE_NUMBER_ID', None) |
| 35 | + |
| 36 | + with pytest.raises(ValueError): |
| 37 | + WhatsAppToolkit() |
| 38 | + |
| 39 | + |
| 40 | +@patch('requests.post') |
| 41 | +def test_send_message_success(mock_post, whatsapp_toolkit): |
| 42 | + # Mock successful API response |
| 43 | + mock_response = MagicMock() |
| 44 | + mock_response.json.return_value = {"message_id": "test_message_id"} |
| 45 | + mock_response.raise_for_status.return_value = None |
| 46 | + mock_post.return_value = mock_response |
| 47 | + |
| 48 | + result = whatsapp_toolkit.send_message("1234567890", "Test message") |
| 49 | + |
| 50 | + assert result == {"message_id": "test_message_id"} |
| 51 | + mock_post.assert_called_once() |
| 52 | + |
| 53 | + |
| 54 | +@patch('requests.post') |
| 55 | +def test_send_message_failure(mock_post, whatsapp_toolkit): |
| 56 | + # Mock failed API response |
| 57 | + mock_response = MagicMock() |
| 58 | + mock_response.raise_for_status.side_effect = Exception("API Error") |
| 59 | + mock_post.return_value = mock_response |
| 60 | + |
| 61 | + result = whatsapp_toolkit.send_message("1234567890", "Test message") |
| 62 | + |
| 63 | + assert result == "Failed to send message: API Error" |
| 64 | + mock_post.assert_called_once() |
| 65 | + |
| 66 | + |
| 67 | +@patch('requests.get') |
| 68 | +def test_get_message_templates_success(mock_get, whatsapp_toolkit): |
| 69 | + # Mock successful API response |
| 70 | + mock_response = MagicMock() |
| 71 | + mock_response.json.return_value = { |
| 72 | + "data": [{"name": "template1"}, {"name": "template2"}] |
| 73 | + } |
| 74 | + mock_response.raise_for_status.return_value = None |
| 75 | + mock_get.return_value = mock_response |
| 76 | + |
| 77 | + result = whatsapp_toolkit.get_message_templates() |
| 78 | + |
| 79 | + assert result == [{"name": "template1"}, {"name": "template2"}] |
| 80 | + mock_get.assert_called_once() |
| 81 | + |
| 82 | + |
| 83 | +@patch('requests.get') |
| 84 | +def test_get_message_templates_failure(mock_get, whatsapp_toolkit): |
| 85 | + # Mock failed API response |
| 86 | + mock_response = MagicMock() |
| 87 | + mock_response.raise_for_status.side_effect = Exception("API Error") |
| 88 | + mock_response.json.return_value = { |
| 89 | + "error": "Failed to retrieve message templates" |
| 90 | + } |
| 91 | + mock_get.return_value = mock_response |
| 92 | + |
| 93 | + result = whatsapp_toolkit.get_message_templates() |
| 94 | + assert result == 'Failed to retrieve message templates: API Error' |
| 95 | + mock_get.assert_called_once() |
| 96 | + |
| 97 | + |
| 98 | +@patch('requests.get') |
| 99 | +def test_get_business_profile_success(mock_get, whatsapp_toolkit): |
| 100 | + # Mock successful API response |
| 101 | + mock_response = MagicMock() |
| 102 | + mock_response.json.return_value = { |
| 103 | + "name": "Test Business", |
| 104 | + "description": "Test Description", |
| 105 | + } |
| 106 | + mock_response.raise_for_status.return_value = None |
| 107 | + mock_get.return_value = mock_response |
| 108 | + |
| 109 | + result = whatsapp_toolkit.get_business_profile() |
| 110 | + |
| 111 | + assert result == { |
| 112 | + "name": "Test Business", |
| 113 | + "description": "Test Description", |
| 114 | + } |
| 115 | + mock_get.assert_called_once() |
| 116 | + |
| 117 | + |
| 118 | +@patch('requests.get') |
| 119 | +def test_get_business_profile_failure(mock_get, whatsapp_toolkit): |
| 120 | + # Mock failed API response |
| 121 | + mock_response = MagicMock() |
| 122 | + mock_response.raise_for_status.side_effect = Exception("API Error") |
| 123 | + mock_response.json.return_value = { |
| 124 | + "error": "Failed to retrieve message templates" |
| 125 | + } |
| 126 | + mock_get.return_value = mock_response |
| 127 | + |
| 128 | + result = whatsapp_toolkit.get_business_profile() |
| 129 | + assert isinstance(result, str) |
| 130 | + assert "Failed to retrieve business profile" in result |
| 131 | + assert "API Error" in result |
| 132 | + mock_get.assert_called_once() |
| 133 | + |
| 134 | + |
| 135 | +def test_get_tools(whatsapp_toolkit): |
| 136 | + tools = whatsapp_toolkit.get_tools() |
| 137 | + |
| 138 | + assert len(tools) == 3 |
| 139 | + for tool in tools: |
| 140 | + assert callable(tool) or hasattr(tool, 'func') |
| 141 | + assert callable(tool) or ( |
| 142 | + hasattr(tool, 'func') and callable(tool.func) |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +@patch('time.sleep') |
| 147 | +@patch('requests.post') |
| 148 | +def test_retry_mechanism(mock_post, mock_sleep, whatsapp_toolkit): |
| 149 | + # Mock failed API responses followed by a success |
| 150 | + mock_post.side_effect = [ |
| 151 | + RequestException("API Error"), |
| 152 | + RequestException("API Error"), |
| 153 | + MagicMock( |
| 154 | + json=lambda: {"message_id": "test_message_id"}, |
| 155 | + raise_for_status=lambda: None, |
| 156 | + ), |
| 157 | + ] |
| 158 | + |
| 159 | + result = whatsapp_toolkit.send_message("1234567890", "Test message") |
| 160 | + |
| 161 | + assert result == {"message_id": "test_message_id"} |
| 162 | + assert mock_post.call_count == 3 |
| 163 | + assert mock_sleep.call_count == 2 |
0 commit comments