Skip to content

Conversation

@seedspirit
Copy link
Contributor

resolves #6639 (BA-2966)

Checklist: (if applicable)

  • Milestone metadata specifying the target backport version
  • Mention to the original issue
  • Installer updates including:
    • Fixtures for db schema changes
    • New mandatory config options
  • Update of end-to-end CLI integration tests in ai.backend.test
  • API server-client counterparts (e.g., manager API -> client SDK)
  • Test case(s) to:
    • Demonstrate the difference of before/after
    • Demonstrate the flow of abstract/conceptual models with a concrete implementation
  • Documentation
    • Contents in the docs directory
    • docstrings in public interfaces and type annotations

@github-actions github-actions bot added size:L 100~500 LoC comp:manager Related to Manager component labels Dec 8, 2025
@seedspirit seedspirit marked this pull request as ready for review December 8, 2025 09:03
Copilot AI review requested due to automatic review settings December 8, 2025 09:03
@github-actions github-actions bot added the area:docs Documentations label Dec 8, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the authentication repository layer to explicitly raise exceptions when operations fail, replacing the previous pattern of returning None or False to indicate failure. This makes error handling more explicit and moves it closer to the source of the error.

Key Changes

  • Modified check_credential_with_migration and check_credential_without_migration to raise AuthorizationFailed instead of returning None
  • Changed update_user_full_name to raise UserNotFound instead of returning False
  • Updated get_user_row_by_uuid to raise UserNotFound instead of returning None
  • Removed redundant null checks in the service layer since exceptions are now raised at the repository level
  • Updated all tests to expect exceptions using pytest.raises() and mock side_effect instead of return values

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/ai/backend/manager/models/user.py Modified check_credential_with_migration and check_credential to raise AuthorizationFailed instead of returning None. Updated docstrings to document the new exception behavior. Removed the query_user_by_uuid method.
src/ai/backend/manager/repositories/auth/db_source/db_source.py Added new imports (Any, joinedload, selectinload, UserNotFound). Updated modify_user_full_name to raise UserNotFound instead of returning bool. Moved fetch_user_row_by_uuid implementation inline (from removed UserRow.query_user_by_uuid) and added exception raising. Changed return type annotations from Optional to non-optional.
src/ai/backend/manager/repositories/auth/repository.py Updated method signatures to remove Optional return types for update_user_full_name, check_credential_with_migration, check_credential_without_migration, and get_user_row_by_uuid.
src/ai/backend/manager/services/auth/service.py Removed UserNotFound import. Removed null checks after credential checks and user queries since exceptions are now raised. Updated update_password to use try-except for credential check. Removed redundant error handling in authorize, signout, update_full_name, and update_password_no_auth.
src/ai/backend/manager/api/auth.py Removed error handling for update_full_name since exceptions are now propagated from the service layer.
tests/manager/services/auth/test_authorize.py Updated to use side_effect with AuthorizationFailed exception instead of return_value = None.
tests/manager/services/auth/test_signout.py Updated to expect AuthorizationFailed exception instead of GenericBadRequest. Changed mock to use side_effect instead of return_value.
tests/manager/services/auth/test_update_full_name.py Updated test to expect UserNotFound exception. Changed mock to use side_effect. Removed @pytest.mark.asyncio decorator (issue identified).
tests/manager/services/auth/test_update_password.py Updated to use side_effect with AuthorizationFailed exception.
tests/manager/services/auth/test_update_password_no_auth.py Updated to use side_effect with AuthorizationFailed exception.
tests/manager/repositories/auth/test_auth_repository.py Added new test cases for error scenarios: test_update_user_full_name_not_found and test_get_user_row_by_uuid_not_found. Added redundant local import of UserNotFound (issue identified).
changes/7177.enhance.md Added changelog entry for this enhancement.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@pytest.mark.asyncio
async def test_get_user_row_by_uuid_not_found(self, auth_repository: AuthRepository) -> None:
"""Test getting user row by UUID when user doesn't exist"""
from ai.backend.common.exception import UserNotFound
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

Redundant import. UserNotFound is already imported at the top of the file (line 14), so this local import is unnecessary.

Suggested change
from ai.backend.common.exception import UserNotFound

Copilot uses AI. Check for mistakes.
)
assert result.success is True


Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

Missing @pytest.mark.asyncio decorator. This async test function requires the decorator to be properly executed by pytest. Without it, the test will not run correctly.

Suggested change
@pytest.mark.asyncio

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs Documentations comp:manager Related to Manager component size:L 100~500 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explicitly raise Error when object not found in Auth DB Source

2 participants