Skip to content

Commit

Permalink
handling logic from google doc and created get_user_info function
Browse files Browse the repository at this point in the history
  • Loading branch information
BigOleHealz committed Oct 29, 2024
1 parent 78c8d16 commit b7aa70c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
22 changes: 0 additions & 22 deletions .vscode/launch.json

This file was deleted.

25 changes: 21 additions & 4 deletions services/supabase/users_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,27 @@ def user_exists(self, user_id: int) -> bool:
return True
return False


@handle_exceptions(default_return_value=False, raise_on_error=False)
def get_user_info(self, user_id: int) -> dict:
"""Get user info from the users table"""
data, _ = (
self.client.table(table_name="users")
.select("*")
.eq(column="user_id", value=user_id)
.execute()
)
if len(data[1]) > 0:
return data[1][0]
return {}


@handle_exceptions(default_return_value=None, raise_on_error=True)
def handle_user_email_update(self, user_id: int, email: str) -> None:
"""Update user email in the users table if email is valid and not None"""
"""Update user email in the users table if email is valid and not None and different from the current email"""
if self.check_email_is_valid(email=email):
self.client.table("users").update(
json={"email": email}
).eq("user_id", user_id).execute()
user_info = self.get_user_info(user_id=user_id)
if user_info.get("email") != email:
self.client.table("users").update(
json={"email": email}
).eq("user_id", user_id).execute()
31 changes: 8 additions & 23 deletions tests/services/supabase/test_users_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,40 +431,25 @@ def test_handle_user_email_update() -> None:
no_reply_email = f"no_reply_email@{GITHUB_NOREPLY_EMAIL_DOMAIN}"
supabase_manager.handle_user_email_update(user_id=USER_ID, email=no_reply_email)

# Verify the email was updated
users_data, _ = (
supabase_manager.client.table(table_name="users")
.select("email")
.eq(column="user_id", value=USER_ID)
.execute()
)
assert users_data[1][0]["email"] == EMAIL
# Verify the email was not updated
user_data = supabase_manager.get_user_info(user_id=USER_ID)
assert user_data["email"] == EMAIL

# Update the user's email
none_email = None
supabase_manager.handle_user_email_update(user_id=USER_ID, email=none_email)

# Verify the email was updated
users_data, _ = (
supabase_manager.client.table(table_name="users")
.select("email")
.eq(column="user_id", value=USER_ID)
.execute()
)
assert users_data[1][0]["email"] == EMAIL
# Verify the email was not updated
user_data = supabase_manager.get_user_info(user_id=USER_ID)
assert user_data["email"] == EMAIL

# Update the user's email
new_email = "new_email@example.com"
supabase_manager.handle_user_email_update(user_id=USER_ID, email=new_email)

# Verify the email was updated
users_data, _ = (
supabase_manager.client.table(table_name="users")
.select("email")
.eq(column="user_id", value=USER_ID)
.execute()
)
assert users_data[1][0]["email"] == new_email
user_data = supabase_manager.get_user_info(user_id=USER_ID)
assert user_data["email"] == new_email

# Clean Up
wipe_installation_owner_user_data()
Expand Down

0 comments on commit b7aa70c

Please sign in to comment.