Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/test_request_response_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest
from unittest.mock import patch
from importlib.metadata import version, PackageNotFoundError

from uid2_client.request_response_util import auth_headers


class TestRequestResponseUtil(unittest.TestCase):

def test_auth_headers_packaged_mode(self):
"""Test that the version is correctly retrieved in packaged mode."""
try:
# In a test environment, the package might not be fully installed.
# We get the version directly if available.
expected_version = version("uid2_client")
except PackageNotFoundError:
# If not found, we can't run this specific check, so we skip it.
self.skipTest("uid2_client package not found, skipping packaged mode test.")

headers = auth_headers("test_auth_key")
self.assertEqual(headers['Authorization'], 'Bearer test_auth_key')
self.assertEqual(headers['X-UID2-Client-Version'], f"uid2-client-python-{expected_version}")

@patch('uid2_client.request_response_util.metadata.version')
def test_auth_headers_non_packaged_mode(self, mock_version):
"""Test that the version is set to non-packaged-mode when the package is not found."""
mock_version.side_effect = PackageNotFoundError
headers = auth_headers("test_auth_key")
self.assertEqual(headers['Authorization'], 'Bearer test_auth_key')
self.assertEqual(headers['X-UID2-Client-Version'], "uid2-client-python-non-packaged-mode")
7 changes: 4 additions & 3 deletions uid2_client/request_response_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
from datetime import datetime
from typing import Dict, Optional
from urllib import request

import pkg_resources
from importlib import metadata

from uid2_client.encryption import _encrypt_gcm, _decrypt_gcm
from .envelope import Envelope
from .uid2_response import Uid2Response

BINARY = 'application/octet-stream'


def _make_url(base_url: str, path: str) -> str:
return base_url + path


def auth_headers(auth_key: str) -> Dict[str, str]:
try:
version = pkg_resources.get_distribution("uid2_client").version
version = metadata.version("uid2_client")
except Exception:
version = "non-packaged-mode"

Expand Down