trustcafe-api-wrapper/tests/apiclient.py
2026-04-07 22:32:13 +01:00

75 lines
No EOL
2.9 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"
)
@patch('trustcafeapiwrapper.apiclient.requests.request')
def test_make_request(self, mock_request):
# This is a placeholder test. In a real test, you'd mock the HTTP request and response.
response = self.api_client.make_request("GET", "content", "https://api.example.com/test")
#
mock_request.assert_called_once()