d8faccab46
src/db.py : le remplacement des noms d'organisation nuls utilisait .name.keep() sur une expression concat_str référençant *_id ; Polars nommait alors le résultat d'après *_id, écrasant la colonne titulaire_id/acheteur_id et laissant *_nom inchangée. Remplacé par .alias(col). Suite de tests — élimination de la pollution inter-tests (callbacks/conn globaux Dash) qui faisait échouer ~15 tests Selenium en exécution complète : - tests/conftest.py : DUCKDB_PATH et DATA_SCHEMA_CACHE pointent sur des chemins de test isolés (tests/decp.duckdb, tests/schema.cache.json, gitignorés) — on ne touche plus jamais aux fichiers versionnés decp.duckdb et schema.fixture.json (qui étaient mutés et cassaient la collecte au run suivant). Viewport Chrome élargi à 1600px. - tests/test_db.py : fixture module-scoped qui recharge src.db après le module (test_query_marches faisait importlib.reload vers une DB temporaire au schéma réduit, polluant le conn global → ColumnNotFoundError ensuite). Assertion mise à jour pour le nouveau libellé "[Inconnu de l'INSEE (...)]". Tests périmés / fragiles corrigés : - tests/auth/test_oauth_routes.py : mock LinkedIn aligné sur la route (userinfo récupéré via .get() séparé) ; destination post-login /compte/abonnement. - tests/test_main.py : test_002 filtre sur dateNotification (uid retiré du tableau), scrollIntoView (colonne hors viewport), attentes de l'application du filtre et de la restauration de la persistance ; test_015 via wait_for_no_elements. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
120 lines
3.8 KiB
Python
120 lines
3.8 KiB
Python
import pytest
|
|
|
|
from src.auth import db
|
|
from src.auth import oauth as oauth_module
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_userinfo(monkeypatch):
|
|
"""Évite tout appel réseau LinkedIn.
|
|
|
|
La route `linkedin_callback` échange d'abord le code via
|
|
`authorize_access_token()` (dont la valeur de retour est ignorée), puis
|
|
récupère le profil par un appel séparé `oauth.linkedin.get(.../userinfo)`.
|
|
On mocke donc les deux : `authorize_access_token` pour réussir/échouer
|
|
l'échange, et `.get()` pour renvoyer le userinfo.
|
|
"""
|
|
|
|
state = {"userinfo": None, "raise": False}
|
|
|
|
def _authorize_access_token():
|
|
if state["raise"]:
|
|
raise RuntimeError("token exchange failed")
|
|
return {"access_token": "fake-token"}
|
|
|
|
class _Resp:
|
|
def raise_for_status(self):
|
|
pass
|
|
|
|
def json(self):
|
|
return state["userinfo"]
|
|
|
|
def _get(url, *args, **kwargs):
|
|
return _Resp()
|
|
|
|
monkeypatch.setattr(
|
|
oauth_module.oauth.linkedin,
|
|
"authorize_access_token",
|
|
_authorize_access_token,
|
|
raising=False,
|
|
)
|
|
monkeypatch.setattr(oauth_module.oauth.linkedin, "get", _get, raising=False)
|
|
return state
|
|
|
|
|
|
def test_login_route_redirects_to_linkedin(client, monkeypatch):
|
|
from flask import redirect
|
|
|
|
monkeypatch.setattr(
|
|
oauth_module.oauth.linkedin,
|
|
"authorize_redirect",
|
|
lambda redirect_uri, **kw: redirect("https://www.linkedin.com/oauth/authorize"),
|
|
raising=False,
|
|
)
|
|
resp = client.get("/auth/linkedin")
|
|
assert resp.status_code == 302
|
|
assert "linkedin.com" in resp.headers["Location"]
|
|
|
|
|
|
def test_callback_creates_user_and_logs_in(client, fake_userinfo, users_db_path):
|
|
db.init_schema()
|
|
fake_userinfo["userinfo"] = {
|
|
"sub": "sub-xyz",
|
|
"email": "newbie@example.com",
|
|
"email_verified": True,
|
|
}
|
|
resp = client.get("/auth/linkedin/callback")
|
|
assert resp.status_code == 302
|
|
# Nouvel utilisateur sans abonnement → _post_login_url renvoie vers
|
|
# /compte/abonnement (et non /compte/admin, réservé aux abonnés actifs).
|
|
assert resp.headers["Location"].endswith("/compte/abonnement")
|
|
assert db.get_user_by_email("newbie@example.com") is not None
|
|
|
|
|
|
def test_callback_without_email_fails(client, fake_userinfo, users_db_path):
|
|
db.init_schema()
|
|
fake_userinfo["userinfo"] = {"sub": "sub-noemail", "email_verified": True}
|
|
resp = client.get("/auth/linkedin/callback")
|
|
assert resp.status_code == 302
|
|
assert "error=oauth_failed" in resp.headers["Location"]
|
|
|
|
|
|
def test_callback_token_error_redirects(client, fake_userinfo, users_db_path):
|
|
db.init_schema()
|
|
fake_userinfo["raise"] = True
|
|
resp = client.get("/auth/linkedin/callback")
|
|
assert resp.status_code == 302
|
|
assert "error=oauth_failed" in resp.headers["Location"]
|
|
|
|
|
|
def test_callback_user_cancelled_redirects(client, users_db_path):
|
|
db.init_schema()
|
|
resp = client.get("/auth/linkedin/callback?error=user_cancelled_login")
|
|
assert resp.status_code == 302
|
|
assert "error=oauth_cancelled" in resp.headers["Location"]
|
|
|
|
|
|
def test_connexion_layout_has_linkedin_button():
|
|
import src.app # noqa: F401 - Initialize Dash app before importing pages
|
|
from src.pages.connexion import layout
|
|
|
|
html_str = str(layout())
|
|
assert "Connexion avec LinkedIn" in html_str
|
|
assert "/auth/linkedin" in html_str
|
|
|
|
|
|
def test_connexion_layout_shows_oauth_error():
|
|
import src.app # noqa: F401 - Initialize Dash app before importing pages
|
|
from src.pages.connexion import layout
|
|
|
|
assert "LinkedIn" in str(layout(error="oauth_failed"))
|
|
|
|
|
|
def test_inscription_layout_has_linkedin_button():
|
|
import src.app # noqa: F401 - Initialize Dash app before importing pages
|
|
from src.pages.inscription import layout
|
|
|
|
html_str = str(layout())
|
|
assert "Connexion avec LinkedIn" in html_str
|
|
assert "/auth/linkedin" in html_str
|