Skip to content

Commit 9ce3607

Browse files
committed
cleanup
1 parent 7f78097 commit 9ce3607

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

services/web/server/src/simcore_service_webserver/users/schemas.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,20 @@ class ProfileUpdate(BaseModel):
119119

120120
@field_validator("user_name")
121121
@classmethod
122-
def _validate_user_name(cls, value):
122+
def _validate_user_name(cls, value: str):
123123
# Ensure valid characters (alphanumeric + . _ -)
124124
if not re.match(r"^[a-zA-Z][a-zA-Z0-9_]*$", value):
125-
msg = "Username must start with a letter and can only contain letters, numbers and '_'."
125+
msg = f"Username '{value}' must start with a letter and can only contain letters, numbers and '_'."
126126
raise ValueError(msg)
127127

128128
# Ensure no consecutive special characters
129129
if re.search(r"[_]{2,}", value):
130-
msg = "Username cannot contain consecutive special characters like '__'."
130+
msg = f"Username '{value}' cannot contain consecutive special characters like '__'."
131131
raise ValueError(msg)
132132

133133
# Ensure it doesn't end with a special character
134-
if value[-1] in {".", "_", "-"}:
135-
msg = "Username cannot end with a special character."
134+
if {value[0], value[-1]}.intersection({"_"}):
135+
msg = f"Username '{value}' cannot end or start with a special character."
136136
raise ValueError(msg)
137137

138138
# Check reserved words (example list; extend as needed)
@@ -144,15 +144,10 @@ def _validate_user_name(cls, value):
144144
"undefined",
145145
"support",
146146
"moderator",
147+
# NOTE: add here extra via env vars
147148
}
148-
if value.lower() in reserved_words:
149-
msg = f"Username '{value}' is reserved and cannot be used."
150-
raise ValueError(msg)
151-
152-
# Ensure no offensive content
153-
offensive_terms = {"badword1", "badword2"}
154-
if any(term in value.lower() for term in offensive_terms):
155-
msg = "Username contains prohibited or offensive content"
149+
if any(w in value.lower() for w in reserved_words):
150+
msg = f"Username '{value}' cannot be used."
156151
raise ValueError(msg)
157152

158153
return value

services/web/server/tests/unit/with_dbs/03/test_users.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ async def test_access_update_profile(
100100
await assert_status(resp, expected)
101101

102102

103-
@pytest.mark.parametrize(
104-
"user_role",
105-
[UserRole.USER],
106-
)
103+
@pytest.mark.parametrize("user_role", [UserRole.USER])
107104
async def test_get_profile(
108105
logged_user: UserInfoDict,
109106
client: TestClient,
@@ -156,10 +153,7 @@ async def test_get_profile(
156153
)
157154

158155

159-
@pytest.mark.parametrize(
160-
"user_role",
161-
[UserRole.USER],
162-
)
156+
@pytest.mark.parametrize("user_role", [UserRole.USER])
163157
async def test_update_profile(
164158
logged_user: UserInfoDict,
165159
client: TestClient,
@@ -197,10 +191,7 @@ def _copy(data: dict, exclude: set) -> dict:
197191
assert _copy(data, exclude) == _copy(before, exclude)
198192

199193

200-
@pytest.mark.parametrize(
201-
"user_role",
202-
[UserRole.USER],
203-
)
194+
@pytest.mark.parametrize("user_role", [UserRole.USER])
204195
async def test_profile_workflow(
205196
logged_user: UserInfoDict,
206197
client: TestClient,
@@ -217,7 +208,7 @@ async def test_profile_workflow(
217208
resp = await client.patch(
218209
f"{url}",
219210
json={
220-
"firstName": "Odei",
211+
"first_name": "Odei", # NOTE: still not camecase!
221212
"userName": "odei123",
222213
"privacy": {"hide_fullname": False},
223214
},
@@ -241,34 +232,41 @@ async def test_profile_workflow(
241232
assert updated_profile.privacy.hide_fullname != my_profile.privacy.hide_fullname
242233

243234

244-
@pytest.mark.parametrize(
245-
"user_role",
246-
[UserRole.USER],
247-
)
235+
@pytest.mark.parametrize("user_role", [UserRole.USER])
236+
@pytest.mark.parametrize("invalid_username", ["", "_foo", "superadmin", "foo..-123"])
248237
async def test_update_wrong_user_name(
249238
logged_user: UserInfoDict,
250239
client: TestClient,
251240
user_role: UserRole,
241+
invalid_username: str,
252242
):
253243
assert client.app
254244

255-
# update with INVALID username
256245
url = client.app.router["update_my_profile"].url_for()
246+
resp = await client.patch(
247+
f"{url}",
248+
json={
249+
"userName": invalid_username,
250+
},
251+
)
252+
await assert_status(resp, status.HTTP_422_UNPROCESSABLE_ENTITY)
257253

258-
for invalid_username in ("_foo", "superadmin", "foo..-123"):
259-
resp = await client.patch(
260-
f"{url}",
261-
json={
262-
"userName": invalid_username,
263-
},
264-
)
265-
await assert_status(resp, status.HTTP_422_UNPROCESSABLE_ENTITY)
254+
255+
@pytest.mark.parametrize("user_role", [UserRole.USER])
256+
async def test_update_existing_user_name(
257+
logged_user: UserInfoDict,
258+
client: TestClient,
259+
user_role: UserRole,
260+
):
261+
assert client.app
266262

267263
# update with SAME username (i.e. existing)
268264
url = client.app.router["get_my_profile"].url_for()
269265
resp = await client.get(f"{url}")
270266
data, _ = await assert_status(resp, status.HTTP_200_OK)
271267

268+
assert data["userName"] == logged_user["name"]
269+
272270
url = client.app.router["update_my_profile"].url_for()
273271
resp = await client.patch(
274272
f"{url}",

0 commit comments

Comments
 (0)