From 2d778dae464eccfe5d828dfa8a4838cbb7a9e2be Mon Sep 17 00:00:00 2001 From: JustinBacher <92jbach@gmail.com> Date: Wed, 15 Jul 2026 20:37:54 -0400 Subject: [PATCH] test: make API key status checks deterministic --- unit-tests/test_command_enterprise_api_keys.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/unit-tests/test_command_enterprise_api_keys.py b/unit-tests/test_command_enterprise_api_keys.py index e90108927..c8f8eda08 100644 --- a/unit-tests/test_command_enterprise_api_keys.py +++ b/unit-tests/test_command_enterprise_api_keys.py @@ -29,6 +29,14 @@ def setUp(self): def tearDown(self): mock.patch.stopall() + @staticmethod + def execute_with_fixed_time(cmd, params, **kwargs): + """Execute a command with time fixed before the active test token expires.""" + fixed_now = datetime.datetime(2026, 1, 1) + with mock.patch.object(enterprise_api_keys, 'datetime', wraps=datetime) as mocked_datetime: + mocked_datetime.datetime.now.return_value = fixed_now + return cmd.execute(params, **kwargs) + def test_api_key_list_success(self): """Test successful listing of API keys matching Commander terminal output""" params = get_connected_params() @@ -65,7 +73,7 @@ def test_api_key_list_json_format(self): cmd = enterprise_api_keys.ApiKeyListCommand() TestEnterpriseApiKeys.expected_commands = ['list_token'] - result = cmd.execute(params, format='json') + result = self.execute_with_fixed_time(cmd, params, format='json') self.assertEqual(len(TestEnterpriseApiKeys.expected_commands), 0) self.assertIsNotNone(result) @@ -582,7 +590,7 @@ def test_api_key_status_detection_expired_vs_active(self): captured_output = io.StringIO() with mock.patch('sys.stdout', captured_output): - result = cmd.execute(params) + result = self.execute_with_fixed_time(cmd, params) output = captured_output.getvalue()