Plus aucun fichier de secret : la valeur est lue au moment de l'appel, jamais copiee, jamais affichee (--config indique seulement definie/absente). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lignes
3.7 KiB
Python
88 lignes
3.7 KiB
Python
import datetime as dt
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from meteo_collecte.commun import (UTC, Config, Journal, ecrire_json, lire_json, paris_vers_utc, scrub,
|
|
vers_paris)
|
|
|
|
|
|
class TestHeureParis(unittest.TestCase):
|
|
def test_ete_moins_deux_heures(self):
|
|
self.assertEqual(paris_vers_utc(dt.datetime(2026, 10, 14, 23, 50)),
|
|
dt.datetime(2026, 10, 14, 21, 50, tzinfo=UTC))
|
|
|
|
def test_hiver_moins_une_heure(self):
|
|
self.assertEqual(paris_vers_utc(dt.datetime(2026, 12, 1, 12, 0)), dt.datetime(2026, 12, 1, 11, 0, tzinfo=UTC))
|
|
|
|
def test_heure_inexistante_de_mars(self):
|
|
self.assertIsNone(paris_vers_utc(dt.datetime(2026, 3, 29, 2, 30)))
|
|
|
|
def test_heure_doublee_d_octobre(self):
|
|
local = dt.datetime(2026, 10, 25, 2, 30)
|
|
# Avant la bascule (00:40 UTC), seule la première occurrence (00:30 UTC) est passée.
|
|
self.assertEqual(paris_vers_utc(local, dt.datetime(2026, 10, 25, 0, 40, tzinfo=UTC)),
|
|
dt.datetime(2026, 10, 25, 0, 30, tzinfo=UTC))
|
|
# Après (01:40 UTC), on prend la seconde.
|
|
self.assertEqual(paris_vers_utc(local, dt.datetime(2026, 10, 25, 1, 40, tzinfo=UTC)),
|
|
dt.datetime(2026, 10, 25, 1, 30, tzinfo=UTC))
|
|
|
|
def test_minuit_paris(self):
|
|
self.assertEqual(str(vers_paris(dt.datetime(2026, 10, 16, 22, 30, tzinfo=UTC)).date()), "2026-10-17")
|
|
|
|
|
|
class TestSecrets(unittest.TestCase):
|
|
def test_scrub(self):
|
|
m = "error for url (https://x/?a=1&token=AB%2BC&b=2): https://y/?apikey=K1 ; brut AB+C"
|
|
v = scrub(m, "AB+C")
|
|
self.assertNotIn("AB", v)
|
|
self.assertNotIn("K1", v)
|
|
self.assertIn("token=***&b=2", v)
|
|
|
|
def test_journal_masque(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
j = Journal(Path(d) / "j.log")
|
|
j.masquer("SECRET123")
|
|
j("erreur SECRET123")
|
|
self.assertNotIn("SECRET123", (Path(d) / "j.log").read_text(encoding="utf-8"))
|
|
|
|
def test_secret_lu_seulement_dans_l_environnement(self):
|
|
cfg = Config.depuis_env({"MC_BASE": "x", "MC_JETON_INFOCLIMAT": " VAL "})
|
|
self.assertEqual(cfg.secret("MC_JETON_INFOCLIMAT"), "VAL")
|
|
with self.assertRaises(LookupError):
|
|
cfg.secret("MC_CLE_ROMMA")
|
|
self.assertNotIn("VAL", repr(cfg)) # l'environnement n'apparaît pas dans repr()
|
|
|
|
def test_config_n_affiche_pas_les_secrets(self):
|
|
import contextlib
|
|
import io
|
|
from unittest import mock
|
|
from meteo_collecte import ordonnanceur
|
|
env = {"MC_BASE": "x", "MC_JETON_INFOCLIMAT": "JETON_TRES_SECRET", "MC_CLE_ROMMA": "CLE_TRES_SECRETE"}
|
|
sortie = io.StringIO()
|
|
with mock.patch.dict("os.environ", env), contextlib.redirect_stdout(sortie):
|
|
ordonnanceur.main(["--config"])
|
|
texte = sortie.getvalue()
|
|
self.assertNotIn("TRES_SECRET", texte)
|
|
self.assertIn("MC_JETON_INFOCLIMAT", texte)
|
|
self.assertIn("valeur masquée", texte)
|
|
|
|
|
|
class TestFichiers(unittest.TestCase):
|
|
def test_ecriture_atomique(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p = Path(d) / "a" / "b.json"
|
|
ecrire_json(p, {"é": 1})
|
|
self.assertEqual(lire_json(p, None), {"é": 1})
|
|
self.assertEqual([x.name for x in p.parent.iterdir()], ["b.json"])
|
|
|
|
def test_config_sans_chemin_en_dur(self):
|
|
c = Config.depuis_env({"MC_BASE": "/tmp/x", "MC_PLAFOND_SITE": "800"})
|
|
self.assertEqual(c.plafond_site, 800)
|
|
self.assertEqual(c.public, Path("/tmp/x") / "public")
|
|
self.assertFalse(c.alerte_active)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|