Skip to content

Commit 6ce47e8

Browse files
Juliana MashonJuliana Mashon
Juliana Mashon
authored and
Juliana Mashon
committed
Bookmark
1 parent 3ebdf5d commit 6ce47e8

File tree

1 file changed

+7
-71
lines changed

1 file changed

+7
-71
lines changed

testing.py

+7-71
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,24 @@
77
from broker_functions import BrokerFunctions
88

99
class TestFarmbot(unittest.TestCase):
10-
@patch('main.ApiFunctions')
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-
10+
@patch('main.Farmbot.get_token')
11+
def test_get_token_default_server(self, mock_post):
1612
mock_response = Mock()
1713
expected_token = {'token': 'abc123'}
1814
mock_response.json.return_value = expected_token
1915
mock_response.status_code = 200
20-
16+
mock_post.return_value = mock_response
2117
fb = Farmbot()
22-
token_data = fb.get_token('[email protected]', 'test_pass_123')
23-
24-
mock_api_instance.post.assert_called_once_with(
18+
# Call with default server
19+
fb.get_token('[email protected]', 'test_pass_123')
20+
mock_post.assert_called_once_with(
2521
'https://my.farm.bot/api/tokens',
2622
headers={'content-type': 'application/json'},
2723
json={'user': {'email': '[email protected]', 'password': 'test_pass_123'}}
2824
)
29-
30-
self.assertEqual(token_data, expected_token)
3125
self.assertEqual(fb.token, expected_token)
26+
self.assertEqual(mock_post.return_value.status_code, 200)
3227

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')
5828
def test_get_token_custom_server(self, mock_post):
5929
mock_response = Mock()
6030
expected_token = {'token': 'abc123'}
@@ -72,40 +42,6 @@ def test_get_token_custom_server(self, mock_post):
7242
self.assertEqual(fb.token, expected_token)
7343
self.assertEqual(mock_post.return_value.status_code, 200)
7444

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-
10945
# POSITIVE TEST: function called with endpoint only
11046
@patch('requests.get')
11147
def test_get_info_endpoint_only(self, mock_get):

0 commit comments

Comments
 (0)