-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Juliana Mashon
authored and
Juliana Mashon
committed
Jul 18, 2024
1 parent
3ebdf5d
commit 6ce47e8
Showing
1 changed file
with
7 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,54 +7,24 @@ | |
from broker_functions import BrokerFunctions | ||
|
||
class TestFarmbot(unittest.TestCase): | ||
@patch('main.ApiFunctions') | ||
@patch('main.BrokerFunctions') | ||
def test_get_token_default_server(self, MockBrokerFunctions, MockApiFunctions): | ||
mock_api_instance = MockApiFunctions.return_value | ||
mock_broker_instance = MockBrokerFunctions.return_value | ||
|
||
@patch('main.Farmbot.get_token') | ||
def test_get_token_default_server(self, mock_post): | ||
mock_response = Mock() | ||
expected_token = {'token': 'abc123'} | ||
mock_response.json.return_value = expected_token | ||
mock_response.status_code = 200 | ||
|
||
mock_post.return_value = mock_response | ||
fb = Farmbot() | ||
token_data = fb.get_token('[email protected]', 'test_pass_123') | ||
|
||
mock_api_instance.post.assert_called_once_with( | ||
# Call with default server | ||
fb.get_token('[email protected]', 'test_pass_123') | ||
mock_post.assert_called_once_with( | ||
'https://my.farm.bot/api/tokens', | ||
headers={'content-type': 'application/json'}, | ||
json={'user': {'email': '[email protected]', 'password': 'test_pass_123'}} | ||
) | ||
|
||
self.assertEqual(token_data, expected_token) | ||
self.assertEqual(fb.token, expected_token) | ||
self.assertEqual(mock_post.return_value.status_code, 200) | ||
|
||
mock_api_instance.get_token.assert_called_once_with('[email protected]', 'test_pass_123', 'https://my.farm.bot') | ||
|
||
mock_broker_instance.broker_connect.token = expected_token | ||
mock_broker_instance.api.api_connect.token = expected_token | ||
|
||
# ## POSITIVE TEST: function called with email, password, and default server | ||
# @patch('requests.post') | ||
# def test_get_token_default_server(self, mock_post): | ||
# mock_response = Mock() | ||
# expected_token = {'token': 'abc123'} | ||
# mock_response.json.return_value = expected_token | ||
# mock_response.status_code = 200 | ||
# mock_post.return_value = mock_response | ||
# fb = Farmbot() | ||
# fb.get_token('[email protected]', 'test_pass_123') | ||
# mock_post.assert_called_once_with( | ||
# 'https://my.farm.bot/api/tokens', | ||
# headers={'content-type': 'application/json'}, | ||
# json={'user': {'email': '[email protected]', 'password': 'test_pass_123'}} | ||
# ) | ||
# self.assertEqual(fb.token, expected_token) | ||
# self.assertEqual(mock_post.return_value.status_code, 200) | ||
|
||
# POSITIVE TEST: function called with email, password, and custom server | ||
@patch('requests.post') | ||
def test_get_token_custom_server(self, mock_post): | ||
mock_response = Mock() | ||
expected_token = {'token': 'abc123'} | ||
|
@@ -72,40 +42,6 @@ def test_get_token_custom_server(self, mock_post): | |
self.assertEqual(fb.token, expected_token) | ||
self.assertEqual(mock_post.return_value.status_code, 200) | ||
|
||
# NEGATIVE TEST: function called with bad email or password (HTTP error) | ||
@patch('requests.post') | ||
def test_get_token_bad_email(self, mock_post): | ||
mock_response = Mock() | ||
mock_response.status_code = 422 | ||
mock_post.return_value = mock_response | ||
fb = Farmbot() | ||
# Call with bad email | ||
fb.get_token('[email protected]', 'test_pass_123', 'https://staging.farm.bot') | ||
mock_post.assert_called_once_with( | ||
'https://staging.farm.bot/api/tokens', | ||
headers={'content-type': 'application/json'}, | ||
json={'user': {'email': '[email protected]', 'password': 'test_pass_123'}} | ||
) | ||
self.assertIsNone(fb.token) | ||
self.assertEqual(mock_post.return_value.status_code, 422) | ||
|
||
# NEGATIVE TEST: function called with bad server address (HTTP error) | ||
@patch('requests.post') | ||
def test_get_token_bad_email(self, mock_post): | ||
mock_response = Mock() | ||
mock_response.status_code = 404 | ||
mock_post.return_value = mock_response | ||
fb = Farmbot() | ||
# Call with bad email | ||
fb.get_token('[email protected]', 'test_pass_123', 'https://bad.farm.bot') | ||
mock_post.assert_called_once_with( | ||
'https://bad.farm.bot/api/tokens', | ||
headers={'content-type': 'application/json'}, | ||
json={'user': {'email': '[email protected]', 'password': 'test_pass_123'}} | ||
) | ||
self.assertIsNone(fb.token) | ||
self.assertEqual(mock_post.return_value.status_code, 404) | ||
|
||
# POSITIVE TEST: function called with endpoint only | ||
@patch('requests.get') | ||
def test_get_info_endpoint_only(self, mock_get): | ||
|