vincbeck commented on code in PR #77234:
URL: https://github.com/apache/airflow/pull/77234#discussion_r4615847483
##########
providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/services/users.py:
##########
@@ -176,6 +162,24 @@ def update_user(cls, username: str, body: UserPatchBody,
update_mask: str | None
else:
fields_to_update = all_fields
+ # The lookups can return this user itself, e.g. usernames match
case-insensitively,
+ # so only a different user is a conflict.
+ if "username" in fields_to_update and body.username is not None:
+ existing = security_manager.find_user(username=body.username)
+ if existing is not None and existing.id != user.id:
+ raise HTTPException(
+ status_code=status.HTTP_409_CONFLICT,
+ detail=f"The username `{body.username}` already exists",
+ )
+
+ if "email" in fields_to_update and body.email is not None:
Review Comment:
nit: this block looks very similar to the one above, any chance to factorize?
##########
providers/fab/tests/unit/fab/auth_manager/api_fastapi/services/test_users.py:
##########
@@ -508,6 +516,107 @@ def test_update_user_conflict_email(self,
get_fab_auth_manager, fab_auth_manager
assert ex.value.status_code == 409
assert "email" in ex.value.detail
+ @pytest.mark.parametrize(
+ ("field", "value"),
+ [
+ pytest.param("username", "Alice",
id="username-differs-in-case-only"),
+ pytest.param("email", "[email protected]",
id="email-differs-in-case-only"),
+ ],
+ )
+ def test_update_user_lookup_returning_the_same_user_is_not_a_conflict(
+ self, get_fab_auth_manager, fab_auth_manager, security_manager, field,
value
+ ):
+ user_obj = _make_user_obj(
+ username="alice",
+ email="[email protected]",
+ first_name="Alice",
+ last_name="Liddell",
+ )
+ security_manager.find_user.return_value = user_obj
+ fab_auth_manager.security_manager = security_manager
+ get_fab_auth_manager.return_value = fab_auth_manager
+
+ patch_body = types.SimpleNamespace(
+ username=None,
+ email=None,
+ first_name=None,
+ last_name=None,
+ roles=None,
+ password=None,
+ )
+ setattr(patch_body, field, value)
+
+ out = FABAuthManagerUsers.update_user("alice", patch_body)
+
+ assert getattr(out, field) == value
+ security_manager.update_user.assert_called_once_with(user_obj)
Review Comment:
nit: `find_user` returns the same user for every call, so this test still
passes if the conflict lookup is removed. Maybe also check the lookup ran?
```suggestion
security_manager.update_user.assert_called_once_with(user_obj)
security_manager.find_user.assert_any_call(**{field: value})
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]