Files
courrier/tests/test_piece_068.py
T

74 lines
3.5 KiB
Python

import base64
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app.main import app, mirror_client, MAX_PIECE, _nom_sur
STATIC = Path(__file__).resolve().parent.parent / "app" / "static"
def fausse(contenu=b"%PDF-1.4 test", nom="offre.pdf", taille=None):
return lambda mid, f: {"donnees": {"message_id": mid, "nom": nom, "type_mime": "application/pdf",
"taille": taille if taille is not None else len(contenu),
"encodage": "base64", "contenu_base64": base64.b64encode(contenu).decode()}}
def test_piece_exige_le_jeton():
assert TestClient(app).get("/api/v1/mail/piece?id=m-1&nom=offre.pdf").status_code == 401
def test_piece_telechargee_toujours_en_piece_jointe(client, monkeypatch):
monkeypatch.setattr(mirror_client, "get_attachment", fausse())
r = client.get("/api/v1/mail/piece?id=m-1&nom=offre.pdf")
assert r.status_code == 200 and r.content == b"%PDF-1.4 test"
assert r.headers["content-type"].startswith("application/octet-stream")
assert r.headers["content-disposition"].startswith("attachment;")
assert r.headers["x-content-type-options"] == "nosniff" and r.headers["cache-control"] == "no-store"
def test_piece_nom_accentue_dans_l_entete(client, monkeypatch):
monkeypatch.setattr(mirror_client, "get_attachment", fausse(nom="Offre financière.pdf"))
r = client.get("/api/v1/mail/piece", params={"id": "m-1", "nom": "Offre financière.pdf"})
assert "filename*=UTF-8''Offre%20financi%C3%A8re.pdf" in r.headers["content-disposition"]
@pytest.mark.parametrize("nom", ["setup.exe", "page.HTML", "macro.xlsm", "script.js", "image.svg", "a.b.bat"])
def test_extensions_actives_refusees_sans_appel_miroir(client, monkeypatch, nom):
def boom(*a): raise AssertionError("le miroir ne doit pas etre appele")
monkeypatch.setattr(mirror_client, "get_attachment", boom)
assert client.get("/api/v1/mail/piece", params={"id": "m-1", "nom": nom}).status_code == 403
def test_piece_trop_volumineuse(client, monkeypatch):
monkeypatch.setattr(mirror_client, "get_attachment", fausse(taille=MAX_PIECE + 1))
assert client.get("/api/v1/mail/piece?id=m-1&nom=gros.pdf").status_code == 413
def test_piece_introuvable_et_miroir_indisponible(client, monkeypatch):
monkeypatch.setattr(mirror_client, "get_attachment", lambda mid, f: {"donnees": {}})
assert client.get("/api/v1/mail/piece?id=m-1&nom=x.pdf").status_code == 404
def casse(mid, f): raise ConnectionError("x")
monkeypatch.setattr(mirror_client, "get_attachment", casse)
assert client.get("/api/v1/mail/piece?id=m-1&nom=x.pdf").status_code == 502
def test_nom_nettoye():
assert _nom_sur('../../etc/pass"wd.pdf') == "pass_wd.pdf"
assert _nom_sur("..") == "piece-jointe"
def test_indicateur_bloquee_dans_la_lecture(client, monkeypatch):
d = {"message_id": "m-1", "en_tetes": {}, "corps_texte": "x",
"pieces_jointes": [{"filename": "a.exe", "content_type": "application/x", "size": 5},
{"filename": "b.pdf", "content_type": "application/pdf", "size": 5}]}
monkeypatch.setattr(mirror_client, "get_mail", lambda mid: {"donnees": d})
pj = client.get("/api/v1/mail?id=m-1").json()["pieces_jointes"]
assert [p["bloquee"] for p in pj] == [True, False]
def test_interface_telechargement_sans_innerhtml():
js = (STATIC / "app.js").read_text(encoding="utf-8")
assert "telechargerPiece" in js and "/mail/piece?id=" in js and "innerHTML" not in js