|
1 | 1 | import unittest
|
2 |
| -from unittest.mock import MagicMock, patch |
| 2 | +import json |
| 3 | +from unittest.mock import Mock, patch |
3 | 4 |
|
4 | 5 | from main import Farmbot
|
5 | 6 | from api_functions import ApiFunctions
|
6 | 7 | from broker_functions import BrokerFunctions
|
7 | 8 |
|
8 |
| -class TestApiFunctions(unittest.TestCase): |
9 |
| - |
| 9 | +class TestFarmbot(unittest.TestCase): |
10 | 10 | @patch('main.ApiFunctions')
|
11 |
| - def setUp(self, MockApiFunctions): |
12 |
| - self.mock_api = MockApiFunctions.return_value |
13 |
| - self.farmbot = Farmbot() |
| 11 | + @patch('main.BrokerFunctions') |
| 12 | + def test_get_token_default_server(self, MockBrokerFunctions, MockApiFunctions): |
| 13 | + mock_api_instance = MockApiFunctions.return_value |
| 14 | + mock_broker_instance = MockBrokerFunctions.return_value |
| 15 | + |
| 16 | + mock_response = Mock() |
| 17 | + expected_token = {'token': 'abc123'} |
| 18 | + mock_response.json.return_value = expected_token |
| 19 | + mock_response.status_code = 200 |
| 20 | + |
| 21 | + fb = Farmbot() |
| 22 | + token_data = fb. get_token( '[email protected]', 'test_pass_123') |
| 23 | + |
| 24 | + mock_api_instance.post.assert_called_once_with( |
| 25 | + 'https://my.farm.bot/api/tokens', |
| 26 | + headers={'content-type': 'application/json'}, |
| 27 | + json={ 'user': { 'email': '[email protected]', 'password': 'test_pass_123'}} |
| 28 | + ) |
| 29 | + |
| 30 | + self.assertEqual(token_data, expected_token) |
| 31 | + self.assertEqual(fb.token, expected_token) |
| 32 | + |
| 33 | + mock_api_instance. get_token. assert_called_once_with( '[email protected]', 'test_pass_123', 'https://my.farm.bot') |
| 34 | + |
| 35 | + mock_broker_instance.broker_connect.token = expected_token |
| 36 | + mock_broker_instance.api.api_connect.token = expected_token |
| 37 | + |
| 38 | + # ## POSITIVE TEST: function called with email, password, and default server |
| 39 | + # @patch('requests.post') |
| 40 | + # def test_get_token_default_server(self, mock_post): |
| 41 | + # mock_response = Mock() |
| 42 | + # expected_token = {'token': 'abc123'} |
| 43 | + # mock_response.json.return_value = expected_token |
| 44 | + # mock_response.status_code = 200 |
| 45 | + # mock_post.return_value = mock_response |
| 46 | + # fb = Farmbot() |
| 47 | + # fb.get_token('[email protected]', 'test_pass_123') |
| 48 | + # mock_post.assert_called_once_with( |
| 49 | + # 'https://my.farm.bot/api/tokens', |
| 50 | + # headers={'content-type': 'application/json'}, |
| 51 | + # json={'user': {'email': '[email protected]', 'password': 'test_pass_123'}} |
| 52 | + # ) |
| 53 | + # self.assertEqual(fb.token, expected_token) |
| 54 | + # self.assertEqual(mock_post.return_value.status_code, 200) |
| 55 | + |
| 56 | + # POSITIVE TEST: function called with email, password, and custom server |
| 57 | + @patch('requests.post') |
| 58 | + def test_get_token_custom_server(self, mock_post): |
| 59 | + mock_response = Mock() |
| 60 | + expected_token = {'token': 'abc123'} |
| 61 | + mock_response.json.return_value = expected_token |
| 62 | + mock_response.status_code = 200 |
| 63 | + mock_post.return_value = mock_response |
| 64 | + fb = Farmbot() |
| 65 | + # Call with custom server |
| 66 | + fb. get_token( '[email protected]', 'test_pass_123', 'https://staging.farm.bot') |
| 67 | + mock_post.assert_called_once_with( |
| 68 | + 'https://staging.farm.bot/api/tokens', |
| 69 | + headers={'content-type': 'application/json'}, |
| 70 | + json={ 'user': { 'email': '[email protected]', 'password': 'test_pass_123'}} |
| 71 | + ) |
| 72 | + self.assertEqual(fb.token, expected_token) |
| 73 | + self.assertEqual(mock_post.return_value.status_code, 200) |
| 74 | + |
| 75 | + # NEGATIVE TEST: function called with bad email or password (HTTP error) |
| 76 | + @patch('requests.post') |
| 77 | + def test_get_token_bad_email(self, mock_post): |
| 78 | + mock_response = Mock() |
| 79 | + mock_response.status_code = 422 |
| 80 | + mock_post.return_value = mock_response |
| 81 | + fb = Farmbot() |
| 82 | + # Call with bad email |
| 83 | + fb. get_token( '[email protected]', 'test_pass_123', 'https://staging.farm.bot') |
| 84 | + mock_post.assert_called_once_with( |
| 85 | + 'https://staging.farm.bot/api/tokens', |
| 86 | + headers={'content-type': 'application/json'}, |
| 87 | + json={ 'user': { 'email': '[email protected]', 'password': 'test_pass_123'}} |
| 88 | + ) |
| 89 | + self.assertIsNone(fb.token) |
| 90 | + self.assertEqual(mock_post.return_value.status_code, 422) |
| 91 | + |
| 92 | + # NEGATIVE TEST: function called with bad server address (HTTP error) |
| 93 | + @patch('requests.post') |
| 94 | + def test_get_token_bad_email(self, mock_post): |
| 95 | + mock_response = Mock() |
| 96 | + mock_response.status_code = 404 |
| 97 | + mock_post.return_value = mock_response |
| 98 | + fb = Farmbot() |
| 99 | + # Call with bad email |
| 100 | + fb. get_token( '[email protected]', 'test_pass_123', 'https://bad.farm.bot') |
| 101 | + mock_post.assert_called_once_with( |
| 102 | + 'https://bad.farm.bot/api/tokens', |
| 103 | + headers={'content-type': 'application/json'}, |
| 104 | + json={ 'user': { 'email': '[email protected]', 'password': 'test_pass_123'}} |
| 105 | + ) |
| 106 | + self.assertIsNone(fb.token) |
| 107 | + self.assertEqual(mock_post.return_value.status_code, 404) |
| 108 | + |
| 109 | + # POSITIVE TEST: function called with endpoint only |
| 110 | + @patch('requests.get') |
| 111 | + def test_get_info_endpoint_only(self, mock_get): |
| 112 | + mock_token = { |
| 113 | + 'token': { |
| 114 | + 'unencoded': {'iss': '//my.farm.bot'}, |
| 115 | + 'encoded': 'encoded_token_value' |
| 116 | + } |
| 117 | + } |
| 118 | + mock_response = Mock() |
| 119 | + expected_response = {'device': 'info'} |
| 120 | + mock_response.json.return_value = expected_response |
| 121 | + mock_response.status_code = 200 |
| 122 | + mock_get.return_value = mock_response |
| 123 | + fb = Farmbot() |
| 124 | + fb.token = mock_token |
| 125 | + # Call with endpoint only |
| 126 | + response = fb.get_info('device') |
| 127 | + mock_get.assert_called_once_with( |
| 128 | + 'https://my.farm.bot/api/device/', |
| 129 | + headers={ |
| 130 | + 'authorization': 'encoded_token_value', |
| 131 | + 'content-type': 'application/json' |
| 132 | + } |
| 133 | + ) |
| 134 | + self.assertEqual(response, json.dumps(expected_response, indent=2)) |
| 135 | + self.assertEqual(mock_get.return_value.status_code, 200) |
| 136 | + |
| 137 | + # POSITIVE TEST: function called with endpoint and ID value |
| 138 | + @patch('requests.get') |
| 139 | + def test_get_info_with_id(self, mock_get): |
| 140 | + mock_token = { |
| 141 | + 'token': { |
| 142 | + 'unencoded': {'iss': '//my.farm.bot'}, |
| 143 | + 'encoded': 'encoded_token_value' |
| 144 | + } |
| 145 | + } |
| 146 | + mock_response = Mock() |
| 147 | + expected_response = {'peripheral': 'info'} |
| 148 | + mock_response.json.return_value = expected_response |
| 149 | + mock_response.status_code = 200 |
| 150 | + mock_get.return_value = mock_response |
| 151 | + fb = Farmbot() |
| 152 | + fb.token = mock_token |
| 153 | + # Call with specific ID |
| 154 | + response = fb.get_info('peripherals', '12345') |
| 155 | + mock_get.assert_called_once_with( |
| 156 | + 'https://my.farm.bot/api/peripherals/12345', |
| 157 | + headers={ |
| 158 | + 'authorization': 'encoded_token_value', |
| 159 | + 'content-type': 'application/json' |
| 160 | + } |
| 161 | + ) |
| 162 | + self.assertEqual(response, json.dumps(expected_response, indent=2)) |
| 163 | + self.assertEqual(mock_get.return_value.status_code, 200) |
| 164 | + |
| 165 | +# class TestFarmbot_api(unittest.TestCase): |
14 | 166 |
|
15 |
| - def test_get_token(self): |
16 |
| - self.mock_api.get_token.return_value = 'mock_token' |
| 167 | +# def setUp(self): |
| 168 | +# self.farmbot = Farmbot() |
17 | 169 |
|
18 |
| - |
19 |
| - password = 'password' |
20 |
| - server = 'https://my.farm.bot' |
| 170 | +# @patch('farmbot_util_PORT.FarmbotAPI.get_token') |
| 171 | +# def test_get_token(self, mock_get_token): |
| 172 | +# mock_get_token.return_value = 'fake_token' |
| 173 | +# self.farmbot.get_token('[email protected]', 'password123') |
| 174 | +# self.assertEqual(self.farmbot.token, 'fake_token') |
| 175 | +# mock_get_token.assert_called_once_with('[email protected]', 'password123', 'https://my.farm.bot') |
21 | 176 |
|
22 |
| - token = self.farmbot.get_token(email, password, server) |
| 177 | +# @patch('farmbot_util_PORT.FarmbotAPI.get_info') |
| 178 | +# def test_get_info(self, mock_get_info): |
| 179 | +# mock_get_info.return_value = {'info': 'fake_info'} |
| 180 | +# result = self.farmbot.get_info() |
| 181 | +# self.assertEqual(result, {'info': 'fake_info'}) |
| 182 | +# mock_get_info.assert_called_once() |
23 | 183 |
|
24 |
| - self.assertEqual(token, 'mock_token') |
25 |
| - self.assertEqual(self.farmbot.token, 'mock_token') |
26 |
| - self.assertEqual(self.farmbot.api.token, 'mock_token') |
27 |
| - self.assertEqual(self.farmbot.api.api_connect.token, 'mock_token') |
| 184 | +# @patch('farmbot_util_PORT.FarmbotAPI.set_info') |
| 185 | +# def test_set_info(self, mock_set_info): |
| 186 | +# self.farmbot.set_info('label', 'value') |
| 187 | +# mock_set_info.assert_called_once_with('label', 'value') |
28 | 188 |
|
29 |
| - self.mock_api.get_token.assert_called_with(email, password, server) |
| 189 | +# @patch('farmbot_util_PORT.FarmbotAPI.log') |
| 190 | +# def test_log(self, mock_log): |
| 191 | +# self.farmbot.log('message', 'info') |
| 192 | +# mock_log.assert_called_once_with('message', 'info') |
30 | 193 |
|
31 |
| - def test_get_info(self): |
32 |
| - self.mock_api.get_info.return_value = 'info_data' |
| 194 | +# @patch('farmbot_util_PORT.FarmbotAPI.safe_z') |
| 195 | +# def test_safe_z(self, mock_safe_z): |
| 196 | +# mock_safe_z.return_value = 10 |
| 197 | +# result = self.farmbot.safe_z() |
| 198 | +# self.assertEqual(result, 10) |
| 199 | +# mock_safe_z.assert_called_once() |
33 | 200 |
|
34 |
| - endpoint = 'endpoint' |
35 |
| - id = 123 |
| 201 | +# @patch('farmbot_util_PORT.FarmbotAPI.garden_size') |
| 202 | +# def test_garden_size(self, mock_garden_size): |
| 203 | +# mock_garden_size.return_value = {'x': 1000, 'y': 2000} |
| 204 | +# result = self.farmbot.garden_size() |
| 205 | +# self.assertEqual(result, {'x': 1000, 'y': 2000}) |
| 206 | +# mock_garden_size.assert_called_once() |
36 | 207 |
|
37 |
| - data = self.farmbot.get_info(endpoint, id) |
| 208 | +# @patch('farmbot_util_PORT.FarmbotAPI.group') |
| 209 | +# def test_group(self, mock_group): |
| 210 | +# sequences = ['seq1', 'seq2'] |
| 211 | +# mock_group.return_value = {'grouped': True} |
| 212 | +# result = self.farmbot.group(sequences) |
| 213 | +# self.assertEqual(result, {'grouped': True}) |
| 214 | +# mock_group.assert_called_once_with(sequences) |
38 | 215 |
|
39 |
| - self.assertEqual(data, 'info_data') |
40 |
| - self.mock_api.get_info.assert_called_with(endpoint, id) |
| 216 | +# @patch('farmbot_util_PORT.FarmbotAPI.curve') |
| 217 | +# def test_curve(self, mock_curve): |
| 218 | +# mock_curve.return_value = {'curve': True} |
| 219 | +# result = self.farmbot.curve('seq', 0, 0, 10, 10, 5, 5) |
| 220 | +# self.assertEqual(result, {'curve': True}) |
| 221 | +# mock_curve.assert_called_once_with('seq', 0, 0, 10, 10, 5, 5) |
41 | 222 |
|
42 | 223 | if __name__ == '__main__':
|
43 | 224 | unittest.main()
|
0 commit comments