Skip to content

fix: make in-memory user email lookups case-insensitive - #57

Merged
DevSidd2006 merged 4 commits into
DevSidd2006:mainfrom
theLeverager1705:fix/case-insensitive-email-memory-repo
Oct 28, 2026
Merged

DevSidd2006 merged 4 commits into
DevSidd2006:mainfrom
theLeverager1705:fix/case-insensitive-email-memory-repo

Conversation

@theLeverager1705

@theLeverager1705 theLeverager1705 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

InMemoryUserRepository looked up users by the exact email string, while PostgresUserRepository stores emails lowercase and matches them case-insensitively. In the default local setup (no DATABASE_URL), signing up as Alice@Example.com and logging in as alice@example.com returned 401, and the same address could register twice with different casing.

This PR makes the in-memory store match Postgres:

  • repositories/memory.py: save stores the email lowercase (same as PostgresUserRepository.save), and get_by_email lowercases the lookup key.
  • repositories/interfaces.py: UserRepository.get_by_email now documents that lookups are case-insensitive, so future backends keep the same contract.
  • tests/test_auth_email_case.py: regression tests at the repository, AuthService and HTTP route level.

Fixes #56

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Automated tests run locally (pytest tests/ -v): 1084 passed, 4 skipped (Postgres tests, no TEST_DATABASE_URL)
  • Tested in mock mode without external API keys (LLM_PROVIDER=mock)

Without the change to repositories/memory.py, 7 of the 9 new tests fail; with it, all pass.

Checklist:

  • I have performed a self-review of my own code
  • New and existing unit tests pass locally with my changes
  • I have not committed any secrets, API keys, credentials, or personal candidate data
  • AI tools are welcome, but I've reviewed and verified all AI-assisted content myself

Summary by Sourcery

Align in-memory user email handling with the case-insensitive behavior of the Postgres repository.

Bug Fixes:

  • Make in-memory user email storage and lookups case-insensitive so signup and login behavior matches the Postgres repository.
  • Prevent outdated email aliases from resolving after an account email is changed.

Enhancements:

  • Document case-insensitive email lookup as part of the user repository contract.

Tests:

  • Add repository, authentication service, and HTTP regression coverage for case-insensitive email handling and email updates.

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.
@sourcery-ai

sourcery-ai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Makes 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 lookups

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Aligned in-memory email storage and lookup semantics with the case-insensitive Postgres contract.
  • Lowercase emails before storing user records and indexing them.
  • Lowercase lookup keys in get_by_email.
  • Document case-insensitive behavior in the repository interface and implementation.
repositories/memory.py
repositories/interfaces.py
Added regression coverage across repository, service, and HTTP layers.
  • Verify lowercase persistence and case-insensitive repository lookups.
  • Verify mixed-case login succeeds and differently cased duplicate signup conflicts.
  • Verify HTTP signup normalizes the response, login succeeds, and duplicate signup returns 409.
tests/test_auth_email_case.py

Assessment against linked issues

Issue Objective Addressed Explanation
#56 Make the in-memory user repository treat email addresses case-insensitively, so users can log in regardless of email casing and cannot create duplicate accounts by changing the casing. ✅
#56 Document case-insensitive email lookup as part of the UserRepository contract so repository backends have consistent behavior. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions

Copy link
Copy Markdown

👋 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.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread repositories/memory.py Resolved
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 DevSidd2006 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@DevSidd2006
DevSidd2006 merged commit 58b35be into DevSidd2006:main Oct 28, 2026
5 of 6 checks passed
@github-actions

Copy link
Copy Markdown

🎉 Thanks for the contribution, @theLeverager1705! This PR has been merged. We appreciate you helping improve OpenHire.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Email login is case-sensitive with the default in-memory user store

2 participants