|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | +from importlib.metadata import version, PackageNotFoundError |
| 4 | + |
| 5 | +from uid2_client.request_response_util import auth_headers |
| 6 | + |
| 7 | + |
| 8 | +class TestRequestResponseUtil(unittest.TestCase): |
| 9 | + |
| 10 | + def test_auth_headers_packaged_mode(self): |
| 11 | + """Test that the version is correctly retrieved in packaged mode.""" |
| 12 | + try: |
| 13 | + # In a test environment, the package might not be fully installed. |
| 14 | + # We get the version directly if available. |
| 15 | + expected_version = version("uid2_client") |
| 16 | + except PackageNotFoundError: |
| 17 | + # If not found, we can't run this specific check, so we skip it. |
| 18 | + self.skipTest("uid2_client package not found, skipping packaged mode test.") |
| 19 | + |
| 20 | + headers = auth_headers("test_auth_key") |
| 21 | + self.assertEqual(headers['Authorization'], 'Bearer test_auth_key') |
| 22 | + self.assertEqual(headers['X-UID2-Client-Version'], f"uid2-client-python-{expected_version}") |
| 23 | + |
| 24 | + @patch('uid2_client.request_response_util.metadata.version') |
| 25 | + def test_auth_headers_non_packaged_mode(self, mock_version): |
| 26 | + """Test that the version is set to non-packaged-mode when the package is not found.""" |
| 27 | + mock_version.side_effect = PackageNotFoundError |
| 28 | + headers = auth_headers("test_auth_key") |
| 29 | + self.assertEqual(headers['Authorization'], 'Bearer test_auth_key') |
| 30 | + self.assertEqual(headers['X-UID2-Client-Version'], "uid2-client-python-non-packaged-mode") |
0 commit comments