ChickenBenny opened a new pull request, #77445:
URL: https://github.com/apache/airflow/pull/77445

   ## Why
   
   #77234 fixed `PATCH /auth/fab/v1/users/{username}` returning 409 for 
requests that should work, like
   changing only the case of your own username. The existing tests didn't catch 
it, because none of
   them run the real lookup:
   
   | Tests | What they mock | What they miss |
   |---|---|---|
   | `routes/test_users.py` | the whole `FABAuthManagerUsers` service | 
everything the service does |
   | `services/test_users.py` | `find_user` and `update_user` | that 
`find_user` matches usernames ignoring case, and what actually gets written |
   
   The bug was in how the service and the real `find_user` work together, so a 
mocked `find_user`
   can't show it. This PR adds tests that run `PATCH /users` through the real 
FAB app and database.
   For roles, `TestPatchRoleWithDatabase` does the same at the service level.
   
   This PR only adds tests. They pass on `main`.
   
   ## What
   
   A new `TestPatchUserWithDatabase` class in `routes/test_users.py`. It uses 
the existing
   `real_app_client` fixture and creates two users, `patch_db_alice` and 
`Patch_DB_Carol`. Each test
   sends a request, then reads the users back from the database:
   
   | Scenario | Request | Expected |
   |---|---|---|
   | Change only the case of your own username | `PATCH /users/patch_db_alice` 
`{"username": "Patch_DB_Alice"}` | 200, stored name is now `Patch_DB_Alice` |
   | Keep your username, but the URL uses lower case | `PATCH 
/users/patch_db_carol` `{"username": "Patch_DB_Carol", "first_name": "Caro"}` | 
200, `first_name` updated |
   | Take another user's username, in a different case | `PATCH 
/users/patch_db_alice` `{"username": "patch_db_carol"}` | 409, both users 
unchanged |
   | Take another user's email | `PATCH /users/patch_db_alice` `{"email": 
"[email protected]"}` | 409, both users unchanged |
   | Conflicting email outside `update_mask` | `PATCH 
/users/patch_db_alice?update_mask=first_name` with Carol's email in the body | 
200, only `first_name` changed |
   
   There's no test for changing only the case of an email. Unlike usernames, 
which have been
   case-insensitive since #27266 (`find_user` compares `lower()` on both sides, 
backed by a unique
   index on `lower(username)`), emails are compared with the database's own 
collation. With a
   case-insensitive collation such as MySQL's `utf8mb4_unicode_ci` (what Breeze 
uses),
   `[email protected]` and `[email protected]` are the same email; on Postgres and SQLite 
by default they are
   not. One assertion can't hold on every backend. Making emails 
case-insensitive everywhere would
   need a migration like #27266's, so it's out of scope here.
   
   ## Testing
   
   - `breeze run pytest 
providers/fab/tests/unit/fab/auth_manager/api_fastapi/routes/test_users.py`:
     28 passed on SQLite.
   - The new class on `--backend mysql` and `--backend postgres`: 5 passed on 
each.
   - Checked that the tests catch the bugs they're for:
     - With `services/users.py` from before #77234, 3 fail with `assert 409 == 
200`: the two
       "keep your own username" cases and the `update_mask` case. The two real 
conflicts still get 409.
     - With the conflict check turned off, both conflict cases fail with 
`assert 500 == 409`: the
       database's unique constraint rejects the write, so the API returns 500 
instead of a clean 409.
   - The 5 cases add about 3s.
   - `prek` hooks passed on commit.
   
   ## Related
   
   - #77234: the fix these tests cover.
   - #76972: added `TestPatchRoleWithDatabase`, a service-level DB test for 
`PATCH /roles`.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Opus 5.5) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


-- 
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]

Reply via email to