Skip to content

Commit

Permalink
fix: remove call to _remove_guest_key (#181)
Browse files Browse the repository at this point in the history
* fix: remove call to _remove_guest_key

* fix: update guest_login to None but not last_login

* Add await args to expired guest key unit test

* fix: extract expired guest key removal function

---------

Co-authored-by: Taesung Hwang <[email protected]>
  • Loading branch information
samderanova and taesungh authored Jan 2, 2024
1 parent 4be0c4d commit e436ff2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion apps/api/src/auth/guest_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def _get_existing_key(email: EmailStr) -> Optional[str]:
# Reject expired key
now = utc_now()
if now > auth.exp:
await _remove_guest_key(uid)
await _remove_expired_guest_key(uid)
return None

return auth.key
Expand All @@ -112,6 +112,12 @@ async def _remove_guest_key(uid: str) -> None:
)


async def _remove_expired_guest_key(uid: str) -> None:
await mongodb_handler.update_one(
Collection.USERS, {"_id": uid}, {"guest_auth": None}
)


def _generate_confirmation_token() -> str:
"""Generate a confirmation token to use for guest authentication."""
return secrets.token_urlsafe()
Expand Down
6 changes: 5 additions & 1 deletion apps/api/tests/test_guest_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from unittest.mock import AsyncMock, patch

from auth import guest_auth
from services.mongodb_handler import Collection

guest_auth.AUTH_KEY_SALT = "not-a-good-idea".encode()
SAMPLE_EMAIL = "[email protected]"
SAMPLE_UID = "com.amazon.jeff"


@patch("services.mongodb_handler.retrieve_one", autospec=True)
Expand Down Expand Up @@ -73,7 +75,9 @@ async def test_expired_key_is_removed(

key = await guest_auth._get_existing_key(SAMPLE_EMAIL)
assert key is None
mock_mongodb_update_one.assert_awaited_once()
mock_mongodb_update_one.assert_awaited_once_with(
Collection.USERS, {"_id": SAMPLE_UID}, {"guest_auth": None}
)


@patch("services.mongodb_handler.retrieve_one", autospec=True)
Expand Down

0 comments on commit e436ff2

Please sign in to comment.