1
1
import unittest
2
- import json
3
- from main import Farmbot
4
- from unittest .mock import patch , Mock
5
-
6
- class TestFarmbot (unittest .TestCase ):
7
-
8
- ## POSITIVE TEST: function called with email, password, and default server
9
- @patch ('requests.post' )
10
- def test_get_token_default_server (self , mock_post ):
11
- mock_response = Mock ()
12
- expected_token = {'token' : 'abc123' }
13
- mock_response .json .return_value = expected_token
14
- mock_response .status_code = 200
15
- mock_post .return_value = mock_response
16
-
17
- fb = Farmbot ()
18
-
19
- # Call with default server
20
- fb .
get_token (
'[email protected] ' ,
'test_pass_123' )
21
-
22
- mock_post .assert_called_once_with (
23
- 'https://my.farm.bot/api/tokens' ,
24
- headers = {'content-type' : 'application/json' },
25
- json = {
'user' : {
'email' :
'[email protected] ' ,
'password' :
'test_pass_123' }}
26
- )
27
-
28
- self .assertEqual (fb .token , expected_token )
29
- self .assertEqual (mock_post .return_value .status_code , 200 )
30
-
31
- # POSITIVE TEST: function called with email, password, and custom server
32
- @patch ('requests.post' )
33
- def test_get_token_custom_server (self , mock_post ):
34
- mock_response = Mock ()
35
- expected_token = {'token' : 'abc123' }
36
- mock_response .json .return_value = expected_token
37
- mock_response .status_code = 200
38
- mock_post .return_value = mock_response
39
-
40
- fb = Farmbot ()
2
+ from unittest .mock import MagicMock , patch
41
3
42
- # Call with custom server
43
- fb .
get_token (
'[email protected] ' ,
'test_pass_123' ,
'https://staging.farm.bot' )
44
-
45
- mock_post .assert_called_once_with (
46
- 'https://staging.farm.bot/api/tokens' ,
47
- headers = {'content-type' : 'application/json' },
48
- json = {
'user' : {
'email' :
'[email protected] ' ,
'password' :
'test_pass_123' }}
49
- )
50
-
51
- self .assertEqual (fb .token , expected_token )
52
- self .assertEqual (mock_post .return_value .status_code , 200 )
53
-
54
- # NEGATIVE TEST: function called with bad email or password (HTTP error)
55
- @patch ('requests.post' )
56
- def test_get_token_bad_email (self , mock_post ):
57
- mock_response = Mock ()
58
- mock_response .status_code = 422
59
- mock_post .return_value = mock_response
60
-
61
- fb = Farmbot ()
62
-
63
- # Call with bad email
64
- fb .
get_token (
'[email protected] ' ,
'test_pass_123' ,
'https://staging.farm.bot' )
4
+ from main import Farmbot
5
+ from api_functions import ApiFunctions
6
+ from broker_functions import BrokerFunctions
65
7
66
- mock_post .assert_called_once_with (
67
- 'https://staging.farm.bot/api/tokens' ,
68
- headers = {'content-type' : 'application/json' },
69
- json = {
'user' : {
'email' :
'[email protected] ' ,
'password' :
'test_pass_123' }}
70
- )
8
+ class TestApiFunctions (unittest .TestCase ):
71
9
72
- self .assertIsNone (fb .token )
73
- self .assertEqual (mock_post .return_value .status_code , 422 )
10
+ @patch ('main.ApiFunctions' )
11
+ def setUp (self , MockApiFunctions ):
12
+ self .mock_api = MockApiFunctions .return_value
13
+ self .farmbot = Farmbot ()
74
14
75
- # NEGATIVE TEST: function called with bad server address (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 = 404
80
- mock_post .return_value = mock_response
15
+ def test_get_token (self ):
16
+ self .mock_api .get_token .return_value = 'mock_token'
17
+
18
+ password = 'password'
19
+ server = 'https://my.farm.bot'
81
20
82
- fb = Farmbot ()
83
-
84
- # Call with bad email
85
- fb .
get_token (
'[email protected] ' ,
'test_pass_123' ,
'https://bad.farm.bot' )
86
-
87
- mock_post .assert_called_once_with (
88
- 'https://bad.farm.bot/api/tokens' ,
89
- headers = {'content-type' : 'application/json' },
90
- json = {
'user' : {
'email' :
'[email protected] ' ,
'password' :
'test_pass_123' }}
91
- )
21
+ token = self .farmbot .get_token (email , password , server )
92
22
93
- self .assertIsNone (fb .token )
94
- self .assertEqual (mock_post .return_value .status_code , 404 )
23
+ self .assertEqual (token , 'mock_token' )
24
+ self .assertEqual (self .farmbot .token , 'mock_token' )
25
+ self .assertEqual (self .farmbot .api .token , 'mock_token' )
26
+ self .assertEqual (self .farmbot .api .api_connect .token , 'mock_token' )
27
+ self .mock_api .get_token .assert_called_with (email , password , server )
95
28
96
29
# POSITIVE TEST: function called with endpoint only
97
30
@patch ('requests.get' )
@@ -216,4 +149,4 @@ def test_get_info_with_id(self, mock_get):
216
149
# mock_curve.assert_called_once_with('seq', 0, 0, 10, 10, 5, 5)
217
150
218
151
if __name__ == '__main__' :
219
- unittest .main ()
152
+ unittest .main ()
0 commit comments