feat(admin): add is_admin() access guard

Implement access control function for admin panel. Returns True only if
ADMIN_EMAIL env var is set, user is authenticated, and email matches
case-insensitively.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Colin Maudry
2026-07-03 09:41:57 +02:00
parent c4851ff0ae
commit 2f2cd151fb
2 changed files with 54 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
from unittest.mock import patch
from src.admin import guard
def _fake_user(email: str, authenticated: bool = True):
user = type("U", (), {})()
user.is_authenticated = authenticated
user.email = email
return user
def test_is_admin_true_for_matching_email(monkeypatch):
monkeypatch.setenv("ADMIN_EMAIL", "admin@ex.fr")
with patch("src.admin.guard.current_user", _fake_user("admin@ex.fr")):
assert guard.is_admin() is True
def test_is_admin_true_case_insensitive(monkeypatch):
monkeypatch.setenv("ADMIN_EMAIL", "Admin@Ex.fr")
with patch("src.admin.guard.current_user", _fake_user("admin@ex.fr")):
assert guard.is_admin() is True
def test_is_admin_false_for_different_email(monkeypatch):
monkeypatch.setenv("ADMIN_EMAIL", "admin@ex.fr")
with patch("src.admin.guard.current_user", _fake_user("someone@ex.fr")):
assert guard.is_admin() is False
def test_is_admin_false_for_anonymous(monkeypatch):
monkeypatch.setenv("ADMIN_EMAIL", "admin@ex.fr")
with patch(
"src.admin.guard.current_user", _fake_user("admin@ex.fr", authenticated=False)
):
assert guard.is_admin() is False
def test_is_admin_false_when_admin_email_unset(monkeypatch):
monkeypatch.delenv("ADMIN_EMAIL", raising=False)
with patch("src.admin.guard.current_user", _fake_user("admin@ex.fr")):
assert guard.is_admin() is False