Skip to content

Commit 59f178b

Browse files
Juliana MashonJuliana Mashon
authored andcommitted
Fixed test_get_token() for updated code
1 parent 6a0bff7 commit 59f178b

File tree

2 files changed

+31
-97
lines changed

2 files changed

+31
-97
lines changed

main.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ def get_token(self, email, password, server="https://my.farm.bot"):
1313

1414
self.token = token_data
1515

16+
# Set API tokens
1617
self.api.token = token_data
1718
self.api.api_connect.token = token_data
1819

19-
# self.broker.token = token_data
20+
# Set broker tokens
2021
self.broker.broker_connect.token = token_data
2122
self.broker.api.api_connect.token = token_data
2223

@@ -30,28 +31,28 @@ def set_info(self, endpoint, field, value, id=None):
3031

3132
def env(self, id=None, field=None, new_val=None):
3233
return self.api.env(id, field, new_val)
33-
34+
3435
def log(self, message, type=None, channel=None):
3536
return self.api.log(message, type, channel)
36-
37+
3738
def safe_z(self):
3839
return self.api.safe_z()
39-
40+
4041
def garden_size(self):
4142
return self.api.garden_size()
42-
43+
4344
def group(self, id):
4445
return self.api.group(id)
45-
46+
4647
def curve(self, id):
4748
return self.api.curve(id)
48-
49+
4950
def read_status(self):
5051
return self.broker.read_status()
51-
52+
5253
def read_sensor(self, id, mode, label='---'):
5354
return self.broker.read_sensor(id, mode, label)
54-
55+
5556
def message(self, message, type=None, channel=None):
5657
return self.broker.message(message, type, channel)
5758

testing.py

Lines changed: 21 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,30 @@
11
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
413

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
657

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):
719

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()
7414

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'
8120

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)
9222

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)
9528

9629
# POSITIVE TEST: function called with endpoint only
9730
@patch('requests.get')
@@ -216,4 +149,4 @@ def test_get_info_with_id(self, mock_get):
216149
# mock_curve.assert_called_once_with('seq', 0, 0, 10, 10, 5, 5)
217150

218151
if __name__ == '__main__':
219-
unittest.main()
152+
unittest.main()

0 commit comments

Comments
 (0)