test appi client

This commit is contained in:
simonwt 2026-04-07 13:08:01 +01:00
parent d136f37b5f
commit 9c372fd266
3 changed files with 50 additions and 2 deletions

View file

@ -177,5 +177,5 @@ from trustcafeapiwrapper.wrappers.post.create_post import create_post
print("-----------Get a list of trusted users for a specific user----------------") print("-----------Get a list of trusted users for a specific user----------------")
users = API.run_job('trust.listbyuserinit', "simon-little") users = API.run_job('trust.listbyuserinit', "simon-little")
print(users) print(users)
users = API.run_job('trust.listbyuserhas', "simon-little") users = API.run_job('trust.listbyuserhas', "simon-little ")
print(users) print(users)

47
tests/apiclient.py Normal file
View file

@ -0,0 +1,47 @@
import unittest
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")

View file

@ -8,10 +8,11 @@ from tests.utils.make_comment_sk import TestMakeCommentSk
from tests.utils.make_post_sk import TestMakePostSk from tests.utils.make_post_sk import TestMakePostSk
from tests.utils.get_child_spksk_from_paths import TestGetChildSpkskFromPaths from tests.utils.get_child_spksk_from_paths import TestGetChildSpkskFromPaths
from tests.wrappers.create_post import TestCreatePost from tests.wrappers.create_post import TestCreatePost
from tests.wrappers.create_comment import TestCreateComment from tests.wrappers.create_comment import TestCreateComment
from tests.wrappers.react import TestReact from tests.wrappers.react import TestReact
from tests.wrappers.vote import TestVoteCast from tests.wrappers.vote import TestVoteCast
from tests.apiclient import TestAPIClient
unittest.main() unittest.main()