Repository navigation
fix: make in-memory user email lookups case-insensitive - #57
DevSidd2006 merged 4 commits into
Conversation
PostgresUserRepository stores emails lowercase and matches them with LOWER(email) = LOWER($1), but InMemoryUserRepository - the default for local development and the test suite - keyed users by the exact email string. In the default setup, signing up as "Alice@Example.com" and then logging in as "alice@example.com" returned 401, and the same address could register twice with different casing. Store the email lowercase on save and lowercase the lookup key in get_by_email so both backends behave the same, and document the case-insensitive contract on UserRepository.get_by_email.
Reviewer's GuideMakes the in-memory user repository normalize emails to lowercase and perform case-insensitive lookups, preserving consistent behavior with Postgres; adds layered regression tests covering repository, authentication service, and HTTP routes. Sequence diagram for case-insensitive in-memory user email lookupssequenceDiagram
actor Client
participant Repository as InMemoryUserRepository
Client->>Repository: save(record with Alice@example.com)
Repository->>Repository: email = record.email.lower()
Repository-->>Client: stored UserRecord with alice@example.com
Client->>Repository: get_by_email(alice@example.com)
Repository->>Repository: email.lower()
Repository-->>Client: matching UserRecord
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
👋 Thanks for opening this PR! A maintainer will review it soon. In the meantime, please make sure you've read CONTRIBUTING.md and filled out the PR template, including screenshots for any visual changes. |
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="repositories/memory.py" line_range="441" />
<code_context>
)
self._users[record.user_id] = stored
- self._email_index[record.email] = record.user_id
+ self._email_index[email] = record.user_id
return stored
</code_context>
<issue_to_address>
**Old email still resolves**
When a caller saves an existing `user_id` with a different email, `save` replaces the user record and adds the new email index entry but leaves the old entry in `_email_index`; `get_by_email` therefore returns that user for an email they no longer have.
Before adding the new email index entry, remove the previous email entry if it still points to this user.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and lowercasing emails changes which user record login lookup selects, so a mistaken identity collision could affect account access while this in-memory repository is running. Reverting removes the behavior, but it cannot undo any access that occurred before the revert.
Blocking findings: repositories/memory.py:441
Saving an existing user_id with a new email left the old address in _email_index, so get_by_email still resolved it. Remove the previous entry when it points to this user, matching the Postgres behaviour.
DevSidd2006
left a comment
There was a problem hiding this comment.
repositories/memory.py:431
InMemoryUserRepository.save() does not enforce email uniqueness when updating a user. If another user already owns the normalized email, _email_index is overwritten while both records remain in _users. PostgreSQL rejects this through its UNIQUE constraint, so the two backends behave differently.
Please check whether the normalized email belongs to another user_id and raise the appropriate conflict error. Please also add a regression test covering an update to an email already owned by another user.
|
🎉 Thanks for the contribution, @theLeverager1705! This PR has been merged. We appreciate you helping improve OpenHire. |
Description
InMemoryUserRepositorylooked up users by the exact email string, whilePostgresUserRepositorystores emails lowercase and matches them case-insensitively. In the default local setup (noDATABASE_URL), signing up asAlice@Example.comand logging in asalice@example.comreturned 401, and the same address could register twice with different casing.This PR makes the in-memory store match Postgres:
repositories/memory.py:savestores the email lowercase (same asPostgresUserRepository.save), andget_by_emaillowercases the lookup key.repositories/interfaces.py:UserRepository.get_by_emailnow documents that lookups are case-insensitive, so future backends keep the same contract.tests/test_auth_email_case.py: regression tests at the repository,AuthServiceand HTTP route level.Fixes #56
Type of change
How Has This Been Tested?
pytest tests/ -v): 1084 passed, 4 skipped (Postgres tests, noTEST_DATABASE_URL)LLM_PROVIDER=mock)Without the change to
repositories/memory.py, 7 of the 9 new tests fail; with it, all pass.Checklist:
Summary by Sourcery
Align in-memory user email handling with the case-insensitive behavior of the Postgres repository.
Bug Fixes:
Enhancements:
Tests: