Skip to content

Conversation

@y-onee
Copy link

@y-onee y-onee commented Nov 27, 2025

This PR introduces four targeted refactorings (via commits) to address code smells and improve code quality across the authentication, serialization, lobby, and factory modules.

Refactorings Applied:

1. Decompose Conditional - serializers.py
Problem: Complex nested conditional logic in the ResourceAccessSerializer.validate method made the code difficult to read and understand. The conditional combined update and create cases with multiple boolean expressions using nested and/or operators
Solution: Extracted helper methods to encapsulate distinct validation concerns:
_is_attempting_unauthorized_owner_assignment() - Checks if user is trying to assign owner role without permission
_is_attempting_to_remove_another_owner() - Checks if user is trying to modify a different owner
_violates_update_owner_rules() - Combines update-specific validation rules
_violates_create_owner_rules() - Handles create-specific validation rules
_raise_owner_permission_error() - Centralizes error raising
Benefit: Improved readability by replacing complex nested conditionals with well-named methods that clearly express intent. Each validation concern is now isolated and testable.

2. Extract Method - authentication.py
Problem: The authenticate_credentials method was 60+ lines long with multiple responsibilities: JWT decoding, claim validation, and user retrieval
Solution: Extracted three focused helper methods from the original implementation:
_decode_jwt_token() - Handles JWT token decoding and signature validation
_validate_token_claims() - Validates required claims (user_id, client_id, delegated flag)
_get_user_from_payload() - Retrieves and validates user from database
Benefit: Reduced main method to ~10 lines, improved code organization, and made each responsibility explicit and independently testable. The method now acts as a coordinator rather than implementing all details.

3. Rename Variable - lobby.py
Problem: Variable livekit_config was misleadingly named in the ACCEPTED status block (line 179) - it actually contains an access token, not configuration
Solution: Renamed livekit_config to access_token in the ACCEPTED status handling section of the request_entry method
Benefit: Eliminated confusion and improved code clarity by using a descriptive, accurate variable name that reflects the actual content (JWT access token)

4. Rename Variable - factories.py
Problem: Generic variable name item was used in both ResourceFactory.users() and RecordingFactory.users() post-generation methods, not clearly describing what it represents
Solution: Renamed item to user_entry in both factory methods to better convey that it represents either a User object or a (User, role) tuple
Benefit: Improved code readability and self-documentation. The variable name now clearly indicates it's an entry that can be either a single user or a user-role pair.

All test cases were successfully passed after the refactoring. The system was successfully run on local host after changes.

No changes to the functionality of the code have been made. Only refactoring to make the code easier to understand and maintainable.

…breaking it down into explanatory methods by giving them meaningful names and then used these methods to perform the conditional logic for easier-to-understand code
…le for multiple functionalities and broke it down into smaller and concise methods for better readability. Extracted 3 methods - _decode_jwt_token, _validate_token_claims and _get_user_from_payload from the main autheticate_credentials leaving this main method to act as a master method instead of implementing the details by itself
…the ACCEPTED status block of LobbyService.request_entry method. improved code clarity by using a more descriptive variable name that accurately represents the access token, addressing the 'wrongly named' comment in the code
…ory and RecordingFactory post_generation methods. improved code clarity by using a more descriptive variable name that accurately represents either a User object or a (User, role) tuple instead of the generic 'item' name
@sonarqubecloud
Copy link

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.

1 participant