7
7
from broker_functions import BrokerFunctions
8
8
9
9
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 ):
16
12
mock_response = Mock ()
17
13
expected_token = {'token' : 'abc123' }
18
14
mock_response .json .return_value = expected_token
19
15
mock_response .status_code = 200
20
-
16
+ mock_post . return_value = mock_response
21
17
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 (
25
21
'https://my.farm.bot/api/tokens' ,
26
22
headers = {'content-type' : 'application/json' },
27
23
json = {
'user' : {
'email' :
'[email protected] ' ,
'password' :
'test_pass_123' }}
28
24
)
29
-
30
- self .assertEqual (token_data , expected_token )
31
25
self .assertEqual (fb .token , expected_token )
26
+ self .assertEqual (mock_post .return_value .status_code , 200 )
32
27
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
28
def test_get_token_custom_server (self , mock_post ):
59
29
mock_response = Mock ()
60
30
expected_token = {'token' : 'abc123' }
@@ -72,40 +42,6 @@ def test_get_token_custom_server(self, mock_post):
72
42
self .assertEqual (fb .token , expected_token )
73
43
self .assertEqual (mock_post .return_value .status_code , 200 )
74
44
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
45
# POSITIVE TEST: function called with endpoint only
110
46
@patch ('requests.get' )
111
47
def test_get_info_endpoint_only (self , mock_get ):
0 commit comments