From 08f95cfd58474e304976cf187e7244d22d557195 Mon Sep 17 00:00:00 2001 From: Cecilia Stevens <63068179+ceciliastevens@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:32:57 -0500 Subject: [PATCH] update user-agent --- CHANGELOG.md | 7 +++++++ src/_incydr_cli/main.py | 3 +++ src/_incydr_sdk/core/client.py | 6 +++--- tests/test_cli_core.py | 28 ++++++++++++++++++++++++++++ tests/test_core.py | 5 +++++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ccdb1b9..82446a7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,13 @@ how a consumer would use the library or CLI tool (e.g. adding unit tests, updating documentation, etc) are not captured here. +## Unreleased + +### Updated + +- The CLI and SDK now have user-agent headers consistent with Code42 current standards. + + ## 2.2.0 - 2024-11-18 ### Updated diff --git a/src/_incydr_cli/main.py b/src/_incydr_cli/main.py index 5e33c17a..74cc2a0e 100644 --- a/src/_incydr_cli/main.py +++ b/src/_incydr_cli/main.py @@ -52,6 +52,9 @@ ) @logging_options def incydr(version, python, script_dir): + # Configure SDK settings + os.environ["INCYDR_USER_AGENT_PREFIX"] = "incydrCLI (Code42; code42.com) " + if version: console.print(__version__, highlight=False) if python: diff --git a/src/_incydr_sdk/core/client.py b/src/_incydr_sdk/core/client.py index 4d0b7e91..9d598ca3 100644 --- a/src/_incydr_sdk/core/client.py +++ b/src/_incydr_sdk/core/client.py @@ -28,7 +28,7 @@ from _incydr_sdk.users.client import UsersClient from _incydr_sdk.watchlists.client import WatchlistsClient -_base_user_agent = user_agent("incydr", __version__) +_base_user_agent = user_agent("incydrSDK", __version__) class Client: @@ -75,8 +75,8 @@ def __init__( self._session = BaseUrlSession(base_url=self._settings.url) self._session.headers["User-Agent"] = ( - self._settings.user_agent_prefix or "" + _base_user_agent - ) + self._settings.user_agent_prefix or "" + ) + _base_user_agent self._session.auth = APIClientAuth( session=self._session, api_client_id=self._settings.api_client_id, diff --git a/tests/test_cli_core.py b/tests/test_cli_core.py index 704dec52..f60140b8 100644 --- a/tests/test_cli_core.py +++ b/tests/test_cli_core.py @@ -1,3 +1,7 @@ +from typing import Optional + +import pytest + from _incydr_cli.main import incydr @@ -9,3 +13,27 @@ def test_cli_auth_missing_error_prints_missing_vars(runner, monkeypatch): assert "INCYDR_API_CLIENT_SECRET" in result.output assert "INCYDR_URL" not in result.output assert "INCYDR_API_CLIENT_ID" not in result.output + + +@pytest.mark.disable_autouse +def test_cli_user_agent(runner, httpserver_auth): + def starts_with_matcher( + header_name: str, actual: Optional[str], expected: str + ) -> bool: + if actual is None: + return False + + return actual.startswith(expected) + + httpserver_auth.expect_ordered_request( + "/v1/users", + method="GET", + headers={"User-Agent": "incydrCLI"}, + header_value_matcher=starts_with_matcher, + ).respond_with_json({"users": [], "totalCount": 0}) + result = runner.invoke( + incydr, ["users", "list", "--log-stderr", "--log-level", "DEBUG"] + ) + httpserver_auth.check() + assert result.exit_code == 0 + assert "No results found" in result.output diff --git a/tests/test_core.py b/tests/test_core.py index 9be1e88a..d145bf54 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -117,3 +117,8 @@ class Test(Model): err.value ) assert "value is not a valid integer" in str(err.value) + + +def test_user_agent(httpserver_auth: HTTPServer): + c = Client() + assert c._session.headers["User-Agent"].startswith("incydrSDK")