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
2 changes: 1 addition & 1 deletion qase-api-client/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "qase-api-client"
version = "2.0.5"
version = "2.0.6"
description = "Qase TestOps API V1 client for Python"
readme = "README.md"
authors = [{name = "Qase Team", email = "support@qase.io"}]
Expand Down
11 changes: 10 additions & 1 deletion qase-api-client/src/qase/api_client_v1/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(
self.default_headers[header_name] = header_value
self.cookie = cookie
# Set default User-Agent.
self.user_agent = 'OpenAPI-Generator/1.0.0/python'
self.user_agent = f'qase-api-client-python/{self._get_package_version()}'
self.client_side_validation = configuration.client_side_validation

def __enter__(self):
Expand All @@ -112,6 +112,15 @@ def user_agent(self, value):
def set_default_header(self, header_name, header_value):
self.default_headers[header_name] = header_value

@staticmethod
def _get_package_version() -> str:
"""Get package version dynamically from installed metadata."""
try:
from importlib.metadata import version
return version('qase-api-client')
except Exception:
return 'unknown'


_default = None

Expand Down
38 changes: 38 additions & 0 deletions qase-api-client/test/test_user_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Tests for User-Agent header on API V1 client."""
import re
from unittest.mock import patch

from qase.api_client_v1.api_client import ApiClient


class TestUserAgentHeader:

def test_user_agent_contains_qase_api_client(self):
"""User-Agent must contain 'qase-api-client' substring."""
client = ApiClient()
assert "qase-api-client" in client.user_agent

def test_user_agent_format(self):
"""User-Agent must match qase-api-client-python/<version>."""
client = ApiClient()
assert re.match(r"qase-api-client-python/.+", client.user_agent)

def test_user_agent_backend_regex_matches(self):
"""Backend regex /qase-api-client[\\w-]*/i must match."""
client = ApiClient()
match = re.search(r"qase-api-client[\w-]*", client.user_agent, re.IGNORECASE)
assert match is not None
assert match.group(0) == "qase-api-client-python"

def test_user_agent_version_is_dynamic(self):
"""User-Agent version must come from importlib.metadata, not hardcoded."""
client = ApiClient()
version = client.user_agent.split("/")[1]
assert version != "1.0.0", "Version should not be the old OpenAPI-Generator default"
assert version != "", "Version should not be empty"

@patch("importlib.metadata.version", side_effect=Exception("not installed"))
def test_user_agent_fallback_on_missing_metadata(self, mock_version):
"""When package metadata is unavailable, version falls back to 'unknown'."""
client = ApiClient()
assert client.user_agent == "qase-api-client-python/unknown"
2 changes: 1 addition & 1 deletion qase-api-v2-client/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "qase-api-v2-client"
version = "2.0.4"
version = "2.0.5"
description = "Qase TestOps API V2 client for Python"
readme = "README.md"
authors = [{name = "Qase Team", email = "support@qase.io"}]
Expand Down
11 changes: 10 additions & 1 deletion qase-api-v2-client/src/qase/api_client_v2/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(
self.default_headers[header_name] = header_value
self.cookie = cookie
# Set default User-Agent.
self.user_agent = 'OpenAPI-Generator/1.0.0/python'
self.user_agent = f'qase-api-client-python/{self._get_package_version()}'
self.client_side_validation = configuration.client_side_validation

def __enter__(self):
Expand All @@ -112,6 +112,15 @@ def user_agent(self, value):
def set_default_header(self, header_name, header_value):
self.default_headers[header_name] = header_value

@staticmethod
def _get_package_version() -> str:
"""Get package version dynamically from installed metadata."""
try:
from importlib.metadata import version
return version('qase-api-v2-client')
except Exception:
return 'unknown'


_default = None

Expand Down
38 changes: 38 additions & 0 deletions qase-api-v2-client/test/test_user_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Tests for User-Agent header on API V2 client."""
import re
from unittest.mock import patch

from qase.api_client_v2.api_client import ApiClient


class TestUserAgentHeader:

def test_user_agent_contains_qase_api_client(self):
"""User-Agent must contain 'qase-api-client' substring."""
client = ApiClient()
assert "qase-api-client" in client.user_agent

def test_user_agent_format(self):
"""User-Agent must match qase-api-client-python/<version>."""
client = ApiClient()
assert re.match(r"qase-api-client-python/.+", client.user_agent)

def test_user_agent_backend_regex_matches(self):
"""Backend regex /qase-api-client[\\w-]*/i must match."""
client = ApiClient()
match = re.search(r"qase-api-client[\w-]*", client.user_agent, re.IGNORECASE)
assert match is not None
assert match.group(0) == "qase-api-client-python"

def test_user_agent_version_is_dynamic(self):
"""User-Agent version must come from importlib.metadata, not hardcoded."""
client = ApiClient()
version = client.user_agent.split("/")[1]
assert version != "1.0.0", "Version should not be the old OpenAPI-Generator default"
assert version != "", "Version should not be empty"

@patch("importlib.metadata.version", side_effect=Exception("not installed"))
def test_user_agent_fallback_on_missing_metadata(self, mock_version):
"""When package metadata is unavailable, version falls back to 'unknown'."""
client = ApiClient()
assert client.user_agent == "qase-api-client-python/unknown"
Loading