Repository navigation
fix: make in-memory user email lookups case-insensitive #57
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
Merged
DevSidd2006
merged 4 commits into
DevSidd2006:main
from
theLeverager1705:fix/case-insensitive-email-memory-repo
Oct 28, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
89c9707
fix: make in-memory user email lookups case-insensitive
theLeverager1705 583addb
fix: drop stale email index entry when a user's email changes
theLeverager1705 04c5282
Merge branch 'main' into fix/case-insensitive-email-memory-repo
DevSidd2006 e7a8208
fix: resolve merge damage in InMemoryUserRepository (lowercase emails…
DevSidd2006 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """ | ||
| Email case-insensitivity for user accounts. | ||
|
|
||
| `PostgresUserRepository` stores emails lowercase and looks them up with | ||
| `LOWER(email) = LOWER($1)`, so "Alice@example.com" and "alice@example.com" | ||
| are the same account. `InMemoryUserRepository` - the default for local | ||
| development and the test suite - must behave the same way, otherwise | ||
| signup/login semantics silently change depending on whether DATABASE_URL | ||
| is set. | ||
| """ | ||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
|
|
||
| from api.app import create_app | ||
| from core.config import AppSettings | ||
| from core.errors import ConflictError | ||
| from repositories.interfaces import UserRecord | ||
| from repositories.memory import InMemoryUserRepository | ||
| from services.auth_service import AuthService | ||
|
|
||
|
|
||
| def _user(user_id="user_1", email="Alice@Example.com"): | ||
| return UserRecord( | ||
| user_id=user_id, | ||
| email=email, | ||
| password_hash="hash", | ||
| user_type="candidate", | ||
| ) | ||
|
|
||
|
|
||
| class TestInMemoryUserRepositoryEmailCase: | ||
| @pytest.mark.asyncio | ||
| async def test_save_stores_email_lowercase(self): | ||
| repo = InMemoryUserRepository() | ||
| stored = await repo.save(_user()) | ||
| assert stored.email == "alice@example.com" | ||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize( | ||
| "lookup", ["alice@example.com", "ALICE@EXAMPLE.COM", "Alice@Example.com"] | ||
| ) | ||
| async def test_get_by_email_is_case_insensitive(self, lookup): | ||
| repo = InMemoryUserRepository() | ||
| await repo.save(_user()) | ||
| found = await repo.get_by_email(lookup) | ||
| assert found is not None | ||
| assert found.user_id == "user_1" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_changing_email_stops_old_email_resolving(self): | ||
| repo = InMemoryUserRepository() | ||
| await repo.save(_user()) | ||
| await repo.save(_user(email="Alice.New@Example.com")) | ||
| assert await repo.get_by_email("alice@example.com") is None | ||
| found = await repo.get_by_email("alice.new@example.com") | ||
| assert found is not None | ||
| assert found.user_id == "user_1" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_by_email_unknown_still_returns_none(self): | ||
| repo = InMemoryUserRepository() | ||
| await repo.save(_user()) | ||
| assert await repo.get_by_email("bob@example.com") is None | ||
|
|
||
|
|
||
| class TestAuthServiceEmailCase: | ||
| @pytest.fixture | ||
| def service(self): | ||
| return AuthService(user_repository=InMemoryUserRepository(), settings=AppSettings()) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_login_accepts_different_casing_from_signup(self, service): | ||
| signed_up, _, _ = await service.signup( | ||
| email="Alice@Example.com", password="password123", user_type="candidate" | ||
| ) | ||
| logged_in, _, _ = await service.login(email="alice@example.com", password="password123") | ||
| assert logged_in.user_id == signed_up.user_id | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_signup_rejects_same_email_with_different_casing(self, service): | ||
| await service.signup( | ||
| email="alice@example.com", password="password123", user_type="candidate" | ||
| ) | ||
| with pytest.raises(ConflictError): | ||
| await service.signup( | ||
| email="ALICE@example.com", password="password123", user_type="recruiter" | ||
| ) | ||
|
|
||
|
|
||
| class TestAuthRoutesEmailCase: | ||
| @pytest.fixture | ||
| def client(self): | ||
| with TestClient(create_app()) as c: | ||
| yield c | ||
|
|
||
| def test_signup_then_login_with_different_casing(self, client): | ||
| signup = client.post( | ||
| "/auth/signup", | ||
| json={"email": "Alice@Example.com", "password": "password123", "user_type": "candidate"}, | ||
| ) | ||
| assert signup.status_code == 200, signup.text | ||
| assert signup.json()["user"]["email"] == "alice@example.com" | ||
|
|
||
| login = client.post( | ||
| "/auth/login", json={"email": "alice@example.com", "password": "password123"} | ||
| ) | ||
| assert login.status_code == 200, login.text | ||
| assert login.json()["user"]["user_id"] == signup.json()["user"]["user_id"] | ||
|
|
||
| def test_duplicate_signup_with_different_casing_conflicts(self, client): | ||
| first = client.post( | ||
| "/auth/signup", | ||
| json={"email": "alice@example.com", "password": "password123", "user_type": "candidate"}, | ||
| ) | ||
| assert first.status_code == 200, first.text | ||
|
|
||
| second = client.post( | ||
| "/auth/signup", | ||
| json={"email": "ALICE@example.com", "password": "password123", "user_type": "candidate"}, | ||
| ) | ||
| assert second.status_code == 409, second.text |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.