From 9c372fd266f30c8ef1814bc07a00cfddcf291c81 Mon Sep 17 00:00:00 2001 From: simonwt Date: Tue, 7 Apr 2026 13:08:01 +0100 Subject: [PATCH] test appi client --- testing.py | 2 +- tests/apiclient.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ unittests.py | 3 ++- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/apiclient.py diff --git a/testing.py b/testing.py index 7719b58..a618cf9 100644 --- a/testing.py +++ b/testing.py @@ -177,5 +177,5 @@ from trustcafeapiwrapper.wrappers.post.create_post import create_post print("-----------Get a list of trusted users for a specific user----------------") users = API.run_job('trust.listbyuserinit', "simon-little") print(users) -users = API.run_job('trust.listbyuserhas', "simon-little") +users = API.run_job('trust.listbyuserhas', "simon-little ") print(users) \ No newline at end of file diff --git a/tests/apiclient.py b/tests/apiclient.py new file mode 100644 index 0000000..b8447da --- /dev/null +++ b/tests/apiclient.py @@ -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") diff --git a/unittests.py b/unittests.py index e98234f..e409dff 100644 --- a/unittests.py +++ b/unittests.py @@ -8,10 +8,11 @@ from tests.utils.make_comment_sk import TestMakeCommentSk from tests.utils.make_post_sk import TestMakePostSk from tests.utils.get_child_spksk_from_paths import TestGetChildSpkskFromPaths - from tests.wrappers.create_post import TestCreatePost from tests.wrappers.create_comment import TestCreateComment from tests.wrappers.react import TestReact from tests.wrappers.vote import TestVoteCast +from tests.apiclient import TestAPIClient + unittest.main()