Skip to content

Commit 803935f

Browse files
committed
Implement team name uniqueness check in registration process
- Added logic to the team registration endpoint to check for existing team names in a case-insensitive manner, preventing duplicate team registrations. - Introduced new tests to verify that registration fails when attempting to register a team with a name that already exists, regardless of case.
1 parent ae558a3 commit 803935f

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

app/api/teams.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ async def register_team(
4242
detail="Email already registered"
4343
)
4444

45+
# Check if team name already exists (case insensitive)
46+
db_team = db.query(DBTeam).filter(func.lower(DBTeam.name) == func.lower(team.name)).first()
47+
if db_team:
48+
raise HTTPException(
49+
status_code=status.HTTP_400_BAD_REQUEST,
50+
detail="Team name already exists"
51+
)
52+
4553
# Create the team
4654
db_team = DBTeam(
4755
name=team.name,

tests/test_teams.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,70 @@ def test_register_team_duplicate_admin_email_case_insensitive_reverse(client, db
121121
assert response.status_code == 400
122122
assert response.json()["detail"] == "Email already registered"
123123

124+
def test_register_team_duplicate_name(client, db):
125+
"""
126+
Given a team with name "Existing Team" exists
127+
When registering a new team with name "Existing Team"
128+
Then the registration should fail with "Team name already exists" error
129+
"""
130+
# First, create a team
131+
team = DBTeam(
132+
name="Existing Team",
133+
admin_email="existing@example.com",
134+
phone="1234567890",
135+
billing_address="123 Test St, Test City, 12345",
136+
is_active=True,
137+
created_at=datetime.now(UTC)
138+
)
139+
db.add(team)
140+
db.commit()
141+
db.refresh(team)
142+
143+
# Try to register a new team with the same name
144+
response = client.post(
145+
"/teams/",
146+
json={
147+
"name": "Existing Team",
148+
"admin_email": "newteam@example.com",
149+
"phone": "0987654321",
150+
"billing_address": "456 New St, New City, 54321"
151+
}
152+
)
153+
assert response.status_code == 400
154+
assert response.json()["detail"] == "Team name already exists"
155+
156+
def test_register_team_duplicate_name_case_insensitive(client, db):
157+
"""
158+
Given a team with name "Existing Team" exists
159+
When registering a new team with name "existing team"
160+
Then the registration should fail with "Team name already exists" error
161+
"""
162+
# First, create a team
163+
team = DBTeam(
164+
name="Existing Team",
165+
admin_email="existing@example.com",
166+
phone="1234567890",
167+
billing_address="123 Test St, Test City, 12345",
168+
is_active=True,
169+
created_at=datetime.now(UTC)
170+
)
171+
db.add(team)
172+
db.commit()
173+
db.refresh(team)
174+
175+
# Try to register a new team with the same name but different case
176+
response = client.post(
177+
"/teams/",
178+
json={
179+
"name": "existing team",
180+
"admin_email": "newteam@example.com",
181+
"phone": "0987654321",
182+
"billing_address": "456 New St, New City, 54321"
183+
}
184+
)
185+
assert response.status_code == 400
186+
assert response.json()["detail"] == "Team name already exists"
187+
124188
def test_list_teams(client, admin_token, db, test_team):
125189
"""Test listing all teams (admin only)"""
126190
# List teams as admin

0 commit comments

Comments
 (0)