Skip to content

Commit ae558a3

Browse files
committed
Enhance team registration to check for duplicate admin emails case insensitively
- Updated the team registration logic to ensure that admin email checks are case insensitive, preventing duplicate registrations with varying cases. - Added new tests to verify that registration fails when attempting to register a team with an admin email that matches an existing one, regardless of case.
1 parent 51c8807 commit ae558a3

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

app/api/teams.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from fastapi import APIRouter, Depends, HTTPException, status
22
from sqlalchemy.orm import Session
3+
from sqlalchemy import func
34
from typing import List, Optional
45
from datetime import datetime, UTC
56
import logging
@@ -33,8 +34,8 @@ async def register_team(
3334
"""
3435
Register a new team. This endpoint is publicly accessible.
3536
"""
36-
# Check if team email already exists
37-
db_team = db.query(DBTeam).filter(DBTeam.admin_email == team.admin_email).first()
37+
# Check if team email already exists (case insensitive)
38+
db_team = db.query(DBTeam).filter(func.lower(DBTeam.admin_email) == func.lower(team.admin_email)).first()
3839
if db_team:
3940
raise HTTPException(
4041
status_code=status.HTTP_400_BAD_REQUEST,

tests/test_teams.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,70 @@ def test_register_team_duplicate_admin_email(client, db):
5757
assert response.status_code == 400
5858
assert response.json()["detail"] == "Email already registered"
5959

60+
def test_register_team_duplicate_admin_email_case_insensitive(client, db):
61+
"""
62+
Given a team with admin_email "existing@example.com" exists
63+
When registering a new team with admin_email "EXISTING@EXAMPLE.COM"
64+
Then the registration should fail with "Email already registered" error
65+
"""
66+
# First, create a team
67+
team = DBTeam(
68+
name="Existing Team",
69+
admin_email="existing@example.com",
70+
phone="1234567890",
71+
billing_address="123 Test St, Test City, 12345",
72+
is_active=True,
73+
created_at=datetime.now(UTC)
74+
)
75+
db.add(team)
76+
db.commit()
77+
db.refresh(team)
78+
79+
# Try to register a new team with the same admin_email but different case
80+
response = client.post(
81+
"/teams/",
82+
json={
83+
"name": "New Team",
84+
"admin_email": "EXISTING@EXAMPLE.COM",
85+
"phone": "0987654321",
86+
"billing_address": "456 New St, New City, 54321"
87+
}
88+
)
89+
assert response.status_code == 400
90+
assert response.json()["detail"] == "Email already registered"
91+
92+
def test_register_team_duplicate_admin_email_case_insensitive_reverse(client, db):
93+
"""
94+
Given a team with admin_email "EXISTING@EXAMPLE.COM" exists
95+
When registering a new team with admin_email "existing@example.com"
96+
Then the registration should fail with "Email already registered" error
97+
"""
98+
# First, create a team with uppercase email
99+
team = DBTeam(
100+
name="Existing Team",
101+
admin_email="EXISTING@EXAMPLE.COM",
102+
phone="1234567890",
103+
billing_address="123 Test St, Test City, 12345",
104+
is_active=True,
105+
created_at=datetime.now(UTC)
106+
)
107+
db.add(team)
108+
db.commit()
109+
db.refresh(team)
110+
111+
# Try to register a new team with the same admin_email but lowercase
112+
response = client.post(
113+
"/teams/",
114+
json={
115+
"name": "New Team",
116+
"admin_email": "existing@example.com",
117+
"phone": "0987654321",
118+
"billing_address": "456 New St, New City, 54321"
119+
}
120+
)
121+
assert response.status_code == 400
122+
assert response.json()["detail"] == "Email already registered"
123+
60124
def test_list_teams(client, admin_token, db, test_team):
61125
"""Test listing all teams (admin only)"""
62126
# List teams as admin

0 commit comments

Comments
 (0)