Skip to content

Add unit tests for Global HMAC authentication methods#164

Merged
DasBasti merged 5 commits into
retry-international-authfrom
copilot/sub-pr-161-again
Feb 4, 2026
Merged

Add unit tests for Global HMAC authentication methods#164
DasBasti merged 5 commits into
retry-international-authfrom
copilot/sub-pr-161-again

Conversation

Copilot AI commented Feb 4, 2026

Copy link
Copy Markdown
Contributor
  • Create mock response files for global login and refresh endpoints
  • Add unit tests for _login_global() method
  • Add unit tests for _refresh_access_token_global() method
  • Add integration tests verifying auth_mode selection triggers correct code paths
  • Verify token extraction (accessToken, refreshToken, userId, expiresIn)
  • Run tests and fix any issues
  • Fix linting and formatting issues
  • Use specific SmartAPIError exception type in tests
  • Remove unnecessary API session endpoint mocks

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 4, 2026 21:24
Co-authored-by: DasBasti <1713093+DasBasti@users.noreply.github.com>
Co-authored-by: DasBasti <1713093+DasBasti@users.noreply.github.com>
Copilot AI changed the title [WIP] Update implementation for global authentication mode Add unit tests for Global HMAC authentication methods Feb 4, 2026
Copilot AI requested a review from DasBasti February 4, 2026 21:27
@DasBasti DasBasti marked this pull request as ready for review February 4, 2026 21:35
Copilot AI review requested due to automatic review settings February 4, 2026 21:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds comprehensive unit test coverage for the Global HMAC authentication methods introduced in PR #161. The tests validate the _login_global() and _refresh_access_token_global() authentication flows that were previously untested.

Changes:

  • Added 8 unit tests covering success paths, error conditions, and auth mode routing for Global HMAC authentication
  • Created mock JSON responses (global_login_result.json and global_refresh_result.json) to simulate Global API endpoints
  • Tests verify proper token extraction, error handling for missing fields, and correct routing based on SmartAuthMode.GLOBAL_HMAC

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
pysmarthashtag/tests/test_authentication.py Adds comprehensive test suite with 8 tests covering Global HMAC login, token refresh, error handling, and auth mode routing
pysmarthashtag/tests/replys/global_login_result.json Mock response for successful global login with all required token fields
pysmarthashtag/tests/replys/global_refresh_result.json Mock response for successful global token refresh with refreshed token values

)

# Should raise SmartAPIError when access_token is missing
with pytest.raises(Exception) as exc_info:

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should catch the specific exception type SmartAPIError rather than the generic Exception. The implementation in api/authentication.py lines 406-407 raises SmartAPIError, so the test should verify that specific exception type is raised. This provides more precise testing and better documents the expected behavior.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed all three exception tests to use specific SmartAPIError type and removed unnecessary API session endpoint mocks from global authentication tests in commit c4b8fd9.

)

# Should raise SmartAPIError when userId is missing
with pytest.raises(Exception) as exc_info:

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should catch the specific exception type SmartAPIError rather than the generic Exception. The implementation in api/authentication.py lines 406-407 raises SmartAPIError, so the test should verify that specific exception type is raised. This provides more precise testing and better documents the expected behavior.

Copilot uses AI. Check for mistakes.
)

# Should raise SmartAPIError when data is missing
with pytest.raises(Exception) as exc_info:

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should catch the specific exception type SmartAPIError rather than the generic Exception. The implementation in api/authentication.py lines 396-399 raises SmartAPIError, so the test should verify that specific exception type is raised. This provides more precise testing and better documents the expected behavior.

Copilot uses AI. Check for mistakes.
SmartAuthMode,
SmartRegion,
get_endpoint_urls_for_region,
)

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use SmartAPIError in the exception tests (lines 233, 260, 284), you need to add it to the imports. Add from pysmarthashtag.models import SmartAPIError to the imports section.

Suggested change
)
)
from pysmarthashtag.models import SmartAPIError

Copilot uses AI. Check for mistakes.
Comment thread pysmarthashtag/tests/test_authentication.py Outdated
Comment on lines +156 to +159
# Mock API session endpoint
respx.post(GLOBAL_API_BASE_URL + "/iam/service/api/v1/session?identity_type=smart").mock(
return_value=Response(200, json=load_response(RESPONSE_DIR / "api_access.json"))
)

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mock for the API session endpoint is unnecessary for the global login flow. Unlike the EU OAuth flow, _login_global() does not call _get_api_session() - it returns the access token directly as the api_access_token (see lines 413-421 in api/authentication.py). This mock can be safely removed as it adds confusion about what endpoints are actually being called during global authentication.

Copilot uses AI. Check for mistakes.
DasBasti and others added 2 commits February 4, 2026 22:40
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: DasBasti <1713093+DasBasti@users.noreply.github.com>
@DasBasti DasBasti merged commit 621838d into retry-international-auth Feb 4, 2026
@DasBasti DasBasti deleted the copilot/sub-pr-161-again branch February 4, 2026 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants