88 lines
No EOL
3.5 KiB
Python
88 lines
No EOL
3.5 KiB
Python
import unittest, simplejson as json
|
|
from unittest.mock import patch
|
|
from trustcafeapiwrapper.apiclient import APIClient
|
|
class TestAPIClient(unittest.TestCase):
|
|
def setUp(self):
|
|
self.api_client = APIClient(
|
|
client_id="test_client_id",
|
|
client_secret="test_client_secret",
|
|
debug=False
|
|
)
|
|
|
|
def test_initialization(self):
|
|
self.assertEqual(self.api_client.client_id.get_secret_value(), "test_client_id")
|
|
self.assertEqual(self.api_client.client_secret.get_secret_value(), "test_client_secret")
|
|
self.assertFalse(self.api_client.debug)
|
|
|
|
def test_token_management(self):
|
|
# Mock token data
|
|
token_data = {
|
|
"access_token": "test_access_token",
|
|
"access_token_timeout": 9999999999 # Far future timestamp
|
|
}
|
|
self.api_client.set_token(token_data)
|
|
self.assertEqual(self.api_client._access_token, "test_access_token")
|
|
self.assertEqual(self.api_client._access_token_timeout, 9999999999)
|
|
self.assertTrue(self.api_client.is_token_valid())
|
|
|
|
def test_run_job_with_string(self):
|
|
# Mock a job function in the jobs module
|
|
def mock_job(api_client, arg1, arg2):
|
|
return f"Job executed with {arg1} and {arg2}"
|
|
|
|
# Dynamically add the mock job to the jobs module for testing
|
|
import types
|
|
import sys
|
|
mock_module = types.ModuleType("mock_jobs")
|
|
setattr(mock_module, "mock_job", mock_job)
|
|
sys.modules["trustcafeapiwrapper.jobs.mock_jobs"] = mock_module
|
|
|
|
result = self.api_client.run_job("mock_jobs.mock_job", "value1", "value2")
|
|
self.assertEqual(result, "Job executed with value1 and value2")
|
|
|
|
def test_run_job_with_function(self):
|
|
def mock_job(api_client, arg1):
|
|
return f"Job executed with {arg1}"
|
|
|
|
result = self.api_client.run_job(mock_job, "value1")
|
|
self.assertEqual(result, "Job executed with value1")
|
|
|
|
|
|
def test_set_production_env(self):
|
|
Prod_API = APIClient(
|
|
client_id="prod_client_id",
|
|
client_secret="prod_client_secret",
|
|
debug=False,
|
|
environment="production"
|
|
)
|
|
self.assertEqual(Prod_API.environment, "production")
|
|
|
|
def test_set_bad_env(self):
|
|
with self.assertRaises(ValueError):
|
|
APIClient(
|
|
client_id="test_client_id",
|
|
client_secret="test_client_secret",
|
|
debug=False,
|
|
environment="invalid_env"
|
|
)
|
|
|
|
def test_set_environment_method(self):
|
|
self.api_client.set_environment("production")
|
|
self.assertEqual(self.api_client.environment, "production")
|
|
|
|
def test_make_non_supported_request_type(self):
|
|
with self.assertRaises(ValueError):
|
|
self.api_client.make_request("PATCH", "content", "test")
|
|
|
|
def test_make_request_with_bad_endpoint(self):
|
|
with self.assertRaises(ValueError):
|
|
self.api_client.make_request("GET", "invalid_endpoint", "test")
|
|
|
|
|
|
@patch('trustcafeapiwrapper.apiclient.requests.Session.request')
|
|
def test_make_request(self, mock_request):
|
|
# This test should be expanded
|
|
# Or the the functions should be broken up more to be more easily testable
|
|
response = self.api_client.make_request("GET", "content", "test")
|
|
#
|
|
mock_request.assert_called_once() |