-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#129: Promote User to Artist #204
base: master
Are you sure you want to change the base?
#129: Promote User to Artist #204
Conversation
Hey! I've been having issues with my python versions that I need to look into. I wasn't able to get pytest to work for me, so I didn't run the tests. Can you let me know if there's anything I should change or add? Thanks :) |
@xiomaraR From this summer we're using an updated Git convention as seen here. Branch name is OK because it was previous to the update but I think we can follow the convention for PR naming just to get used to it. I would suggest getting a look into the new Git Convention for following contributions :). |
Hi, I checked the pipelines and the new test is failing. Python 3.11 is recommended and previous versions could make the app not to run because of StrEnum type annotations that werent supported before. For running backend tests I wrote this docs. If the issue still persist tell me what error you're having so I can help you. Take a look at the docs and let me know if the problem was covered there :) |
1c1800f
to
9dfafe7
Compare
@AntonioMrtz Please let me know if I did too much and if there is a simpler solution. I couldn't figure out how to delete the user after passing the user into |
Hi @xiomaraR , first of all thanks for all the work you put into solving this issue. FormattingThis pipeline is failing because there's files that would be reformatted if ruff is used. So we could begin runing Overall solutionProbably I wasn't clear enough on my comments when I requested changes. The code looked good in the previous state and only required some minor changes for adjusting style and some minor docstring that weren't completly accurate. As said in the requested changes, this method will be called by user admins (it doesn't exists yet, it's a future task) so we can freely delete the user and create an artist and JWT treatment can be basicly excluded. With Admin user creation
Making atomic operations in database is not yet implemented so we can skip that part for the moment. Since User and Artist only differ on uploaded_songs attribute all the properties can be exported without losing info and breaking soft links like Playlist owners and so. The problem lives in adding attributes that If you have any doubts or questions feel free to let me know. :) |
@@ -90,6 +91,41 @@ def create_artist(name: str, photo: str, password: bytes, current_date: str) -> | |||
artist_repository_logger.info(f"Artist added to repository: {artist}") | |||
|
|||
|
|||
def create_artist_from_user(user_data: UserDAO) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use the create_artist
method from artist repo and upgrade the missing fields with a custom method
Raises: | ||
UserNotFoundException: if the user does not exist | ||
UserServiceException: unexpected error while upgrading user to artist | ||
UserBadNameException: if the user name is invalid |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually put more general exceptions under concrete exceptions. We also use the HTTP return code that the exception would return as a sorting mechanism. Example:
UserBadNameException -> Bad Parameter -> 400
UserNoutFoundException -> Not found -> 404
Repository,Service,Exceptions -> Internal Server Error -> 500
So based on the convention the order should look similar to the fragment above.
Also for generating docs in VSCODE we use this extension with the google convention for docstrings. This will generate docstring types and paramter automatically.
For Jetbrains I think its built in the editor itself.
} | ||
new_token = auth_service.create_access_token(new_token_data) | ||
|
||
except UserBadNameException as exception: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here the order is correct, 400, 404, 500 👍
status_code=HTTP_404_NOT_FOUND, | ||
content=PropertiesMessagesManager.userNotFound, | ||
) | ||
except UserUnauthorizedException: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any methods inside service that can throw this exception? I use it for users trying to create a song and so
@@ -124,6 +130,59 @@ def test_check_encrypted_password_different(): | |||
base_user_service.delete_user(name) | |||
|
|||
|
|||
def test_upgrade_user_to_artist_correct(clear_test_data_db): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like these tests 😁, good job. For extra style points we could check if the user stills exists on both as artist and as regular user. Since I don't think we have get_all_users
method anymore we can call service method get_user
from user_service
and check that it throws UserNotFoundException.
""" | ||
try: | ||
new_token = user_service.upgrade_user_to_artist(name, token) | ||
response_data = {"token": new_token} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's interesting. I didn't think about the JWT credentials. I expect this method to be called for an admin when the user is not logged.
The workflow I tought about was:
- User requests to be upgraded to artist
- Admin checks requests
- Admin run this endpoint for upgrading user to artist
In this issue we only care about the third point. So the JWT will be given to the user the next time he logs in.
return Response( | ||
content=response_json, | ||
media_type="application/json", | ||
status_code=HTTP_200_OK, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If its a patch method I would return a 204 code for having consistency between other patch methids.
raise UserRepositoryException from exception | ||
else: | ||
user_repository_logger.info(f"User added to repository: {user}") | ||
|
||
|
||
def update_user_role(user_name: str, new_role: str) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we will need this method anymore
try: | ||
base_user_service.validate_user_name_parameter(user_name) | ||
validate_user_exists(user_name) | ||
auth_service.validate_jwt_user_matches_user(token, user_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method would be exclusive for admins in the future, I should have give this context before. So checking jwt token is from user is not needed. Put a # TODO comment saying we will need to add user is admin check in the future
Hi, there's no need to hurry. I only wanted to know if you need help with anything. Feel free to ask :) |
Description
This pull request introduces a new service method to upgrade user accounts from basic users to artist accounts and a corresponding endpoint to facilitate this functionality.
Commit type
feat
: Indicates the addition of a new feature or functionality to the project.Issue
#129
Solution
Service
The
upgrade_user_to_artist
method has been implemented to handle the user account upgrade process. The function performs the following actions:user_repository
artist_repository
using the retrieved user data. This includes copying the user's playlists, playback history, and other relevant informationthe user_repository
to "artist"Endpoint
The
upgrade_to_artist
endpoint has been added to facilitate upgrading a user to an artist via an HTTP PATCH request. It performs the following:upgrade_user_to_artist
method to process the upgradeTests
Tests have been added for the
upgrade_user_to_artist
method and the new endpoint:Proposed Changes
upgrade_user_to_artist
function.PATCH /users/{name}/upgrade_to_artist
endpoint.create_artist_from_user
function to handle artist creation from existing user data.upgrade_to_artist
client endpoint for testing requestsupgrade_user_to_artist
function andupgrade_to_artist
endpointPotential Impact
Screenshots
N/A
Additional Tasks
Assigned
@AntonioMrtz