Files
romm/backend/tests/utils/test_context.py
Georges-Antoine Assi 1ab3a3625a feat(auth): use the OIDC picture claim as the user's avatar (#5730)
* feat(auth): use the OIDC picture claim as the user's avatar

On each OIDC sign-in, a user with no avatar, or one synced from the
provider earlier, gets the picture claim stored as their avatar. An
uploaded avatar is never replaced. The download goes through the
SSRF-guarded client (the configured provider's origin may reach a LAN
address), is capped at 5 MB, must sniff as a supported image, and any
failure is logged without blocking the sign-in.

Closes #4297

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(auth): address review on the OIDC avatar sync

Addresses Greptile's review on #5730:
- ask the UserInfo endpoint when the ID token has no picture claim
- keep an avatar uploaded while the picture downloaded
- allow only the OIDC provider's origins to reach private addresses
- trim docstrings and narrow await_args in the callback test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(auth): type the OIDC avatar test helpers for mypy

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Bound the OIDC picture download with an overall deadline, trust the
OIDC CA bundle, catch hostname encoding errors so sign-in never 500s,
and set the synced avatar with a conditional update so a concurrent
upload wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: review-polish

Type the pytest fixtures and helpers in the OIDC avatar tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(auth): remove the synced OIDC avatar when the user uploads one

Once avatar_path points at the uploaded avatar.<ext>, the sync never
touches the user's avatar again, so the old oidc-avatar.<ext> file
would otherwise stay on disk for good.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:23:21 -05:00

61 lines
1.8 KiB
Python

import ssl
from typing import Any
from config import has_proxy_env
from utils.context import (
create_aiohttp_session,
create_httpx_async_client,
create_httpx_client,
)
class TestProxyAwareHttpClients:
def test_has_proxy_env_detects_uppercase_proxy_vars(self, monkeypatch):
monkeypatch.setenv("HTTP_PROXY", "http://proxy.internal:8080")
assert has_proxy_env() is True
def test_has_proxy_env_returns_false_without_proxy_vars(self, monkeypatch):
for var in (
"HTTP_PROXY",
"HTTPS_PROXY",
"NO_PROXY",
):
monkeypatch.delenv(var, raising=False)
assert has_proxy_env() is False
async def test_create_aiohttp_session_uses_config_proxy_env(self, monkeypatch):
monkeypatch.setattr("utils.context.has_proxy_env", lambda: True)
session = create_aiohttp_session()
try:
assert session.trust_env is True
finally:
await session.close()
async def test_create_httpx_clients_use_config_proxy_env(self, monkeypatch):
monkeypatch.setattr("utils.context.has_proxy_env", lambda: True)
async_client = create_httpx_async_client()
client = create_httpx_client()
try:
assert async_client._trust_env is True
assert client._trust_env is True
finally:
await async_client.aclose()
client.close()
async def test_create_httpx_async_client_uses_the_given_ssl_context(self):
ssl_context = ssl.create_default_context()
client = create_httpx_async_client(verify=ssl_context)
try:
transport: Any = client._transport
assert transport._pool._ssl_context is ssl_context
finally:
await client.aclose()