From 94a19afaccf3397a5f775d559f08137de76937d8 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Sun, 14 Nov 2021 20:39:14 +0100 Subject: [PATCH 1/8] optim premat --- papi/config.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/papi/config.py b/papi/config.py index 7f9ada2..2bf1bd4 100644 --- a/papi/config.py +++ b/papi/config.py @@ -3,13 +3,16 @@ import glob import configparser +import logging as logger + def read_config(*, pattern="*"): paths = [] for relpath in glob.glob(f"confs/{pattern}.ini"): if "/api_client" in relpath: - paths.append(os.path.abspath(relpath)) + logger.info("Found api client {relpath} to add") + paths.append(os.path.abspath(relpath)) res = [] for path in paths: conf = configparser.ConfigParser() From bac406c980a3e90d4e63db7888205868c8f4db96 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Sun, 14 Nov 2021 21:11:41 +0100 Subject: [PATCH 2/8] cosmit --- papi/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/papi/config.py b/papi/config.py index 2bf1bd4..b476452 100644 --- a/papi/config.py +++ b/papi/config.py @@ -11,7 +11,7 @@ def read_config(*, pattern="*"): paths = [] for relpath in glob.glob(f"confs/{pattern}.ini"): if "/api_client" in relpath: - logger.info("Found api client {relpath} to add") + logger.info(f"Found api client {relpath} to add") paths.append(os.path.abspath(relpath)) res = [] for path in paths: From de76383cfaa92b2f23ad2566271d1952970ef3cf Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Sun, 14 Nov 2021 21:15:21 +0100 Subject: [PATCH 3/8] Add documentation --- README.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.rst b/README.rst index e69de29..e10427a 100644 --- a/README.rst +++ b/README.rst @@ -0,0 +1,5 @@ +Config +====== + +Les configs pour prendre en compte les données transmises sont préfixées par api_client +Pour les relais par relais. From 0b5103df1b60b29e53296404ec072b1824ba301d Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Sun, 14 Nov 2021 21:15:53 +0100 Subject: [PATCH 4/8] add testiles --- tests/test_config.py | 22 ++++++++++++++++++++++ tests/test_papi.py | 8 ++++---- tests/test_relais.py | 6 ++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 tests/test_config.py create mode 100644 tests/test_relais.py diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..831b432 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,22 @@ +from unittest import TestCase +from papi import main + + +class ConfigurationTestCase(TestCase): + """ Check configuration parsing """ + + def test_get_config(self): + configs = main.read_config(pattern="*test_sonde") + assert len(configs) == 1 + config = configs[0] + assert config["identifiant_sonde"] == "838266b2-fc3a-4430-95e8-f7f0d0fc9871" + assert config["nom_sonde"] == "SondeTest" + assert config["emails"] == ["1@1", "2@2"] + + def test_get_config1mail(self): + configs = main.read_config(pattern="*1mail") + assert len(configs) == 1 + config = configs[0] + assert config["emails"] == [ + "1@1", + ] diff --git a/tests/test_papi.py b/tests/test_papi.py index 4c52084..67d920d 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -248,7 +248,7 @@ class CodeCoverageTestCase(TestCase): import string id_sonde = "".join(choices(string.ascii_lowercase, k=8)) - r = client.get(f"/sonde/{id_sonde}/rapport") - r = client.post(f"/sonde/{id_sonde}/", json={}) - - assert r.ok + with pytest.raises(LookupError): + r = client.get(f"/sonde/{id_sonde}/rapport") + with pytest.raises(LookupError): + r = client.post(f"/sonde/{id_sonde}/", json={}) diff --git a/tests/test_relais.py b/tests/test_relais.py new file mode 100644 index 0000000..ac61aec --- /dev/null +++ b/tests/test_relais.py @@ -0,0 +1,6 @@ +from papi import relais + + +def test_main(): + + relais.main(maxloop=1, pattern="relais_test_sonde") From 4dce2bd91d5b4adb578fd3493698797bc33e124d Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Sun, 14 Nov 2021 21:16:18 +0100 Subject: [PATCH 5/8] Cosmit whitespeces --- papi/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/papi/main.py b/papi/main.py index d909461..093d444 100644 --- a/papi/main.py +++ b/papi/main.py @@ -105,7 +105,7 @@ class Notifier: ) @staticmethod - def error_sonde(idsonde, kind="perte_contact_api"): + def error_sonde(idsonde, kind="perte contact api"): notifications[idsonde].append( { "changes": ["perte contact api"], From 148e6416c98ed52050853d61ef530680a0ba2bdb Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Sun, 14 Nov 2021 21:21:29 +0100 Subject: [PATCH 6/8] fixme: Bypas various bad designe choices --- papi/main.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/papi/main.py b/papi/main.py index 093d444..dad945e 100644 --- a/papi/main.py +++ b/papi/main.py @@ -112,8 +112,11 @@ class Notifier: "status": ["Le monitoring est inacessible"], } ) - conf = PROBES[idsonde] - + try: + conf = PROBES[idsonde] + except KeyError: + logger.info(f"{idsonde} not found in probe") + return subject = "Probleme api supervision" content = kind from papi.mail_sendermodel import sendmail @@ -179,7 +182,10 @@ def last_notif_text(request: Request, idsonde: str): try: recipients = sondeid2notifsemails(idsonde) except KeyError: # pragma: no cover - recipients = ["mail1@xxx", "mail2@xxx"] + + # breakpoint() + + recipients = ["mail1@xxx", "mail2@xxx", f"{idsonde}@fqdn"] changements = content["changes"] status = content["status"] @@ -212,7 +218,7 @@ def post_sonde_error( if idsonde != "test": raise LookupError(f"{idsonde} not found") - Notifier.error_sonde(idsonde, body["msg"]) + Notifier.error_sonde(idsonde, body.get("msg")) # create fake sample try: From 094803cc8dd1ba95d8c62dfeb026ead5d34955a2 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Sun, 14 Nov 2021 21:21:57 +0100 Subject: [PATCH 7/8] forward db session dependency --- papi/main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/papi/main.py b/papi/main.py index dad945e..34badb1 100644 --- a/papi/main.py +++ b/papi/main.py @@ -173,7 +173,9 @@ def list_notification(idsonde: str): @app.get("/notifications/{idsonde}/text") -def last_notif_text(request: Request, idsonde: str): +def last_notif_text(request: Request, idsonde: str, db: Session = Depends(get_db)): + db_sonde = crud.get_sonde(db, identifiant=idsonde) + notifs = notifications[idsonde] try: content = notifs[-1] @@ -272,7 +274,7 @@ def post_sonde_data( ) if content: Notifier(db, idsonde, content) - res = last_notif_text(request, idsonde) + res = last_notif_text(request, idsonde, db) html = res.body.decode("utf-8") from papi.mail_sendermodel import sendmail From 41e9de25c72d33aaaafbf2e076832b32a8687d93 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Sun, 14 Nov 2021 22:06:14 +0100 Subject: [PATCH 8/8] Fix cnfig parsing --- confs/relais_test_sonde.ini | 10 +++++----- papi/config.py | 35 +++++++++++++++++++++++++++-------- papi/relais.py | 7 +++++-- tests/test_config.py | 3 ++- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/confs/relais_test_sonde.ini b/confs/relais_test_sonde.ini index fbe5ac9..a59505d 100644 --- a/confs/relais_test_sonde.ini +++ b/confs/relais_test_sonde.ini @@ -1,7 +1,7 @@ [relais] identitifant_sonde = 838266b2-fc3a-4430-95e8-f7f0d0fc9871 -api_login_url = 'https://merlin.savediffusion.fr/api/1.1/user/login' -api_status_url = 'https://merlin.savediffusion.fr/api/1.1/status" -api_user_login = 'admin' -api_user_password = 'Watermark35' -forward_api_address = 'https:/papi.silib.re/' +api_login_url = https://merlin.savediffusion.fr/api/1.1/user/login +api_status_url = https://merlin.savediffusion.fr/api/1.1/status +api_user_login =admin +api_user_password = Watermark35 +forward_api_address = https:/papi.silib.re/ diff --git a/papi/config.py b/papi/config.py index b476452..474182a 100644 --- a/papi/config.py +++ b/papi/config.py @@ -6,23 +6,42 @@ import configparser import logging as logger -def read_config(*, pattern="*"): +def read_config(*, kind=None, pattern="*"): + if kind is None: + kind = "api" paths = [] + for relpath in glob.glob(f"confs/{pattern}.ini"): if "/api_client" in relpath: logger.info(f"Found api client {relpath} to add") paths.append(os.path.abspath(relpath)) res = [] - for path in paths: + # TODO: use case/switch + if kind == "api": + for path in paths: + conf = configparser.ConfigParser() + conf.read(path) + probe_settings = dict(conf["probe"]) + emails = probe_settings["emails"] + stripped = [x.strip() for x in emails.split()] + probe_settings["emails"] = stripped + probe_settings["debug_source_path"] = path + res.append(probe_settings) + + elif kind == "relais": + assert len(paths) == 1 # for now only one path is allowed conf = configparser.ConfigParser() + path = paths[0] conf.read(path) - probe_settings = dict(conf["probe"]) - emails = probe_settings["emails"] - stripped = [x.strip() for x in emails.split()] - probe_settings["emails"] = stripped - probe_settings["debug_source_path"] = path - res.append(probe_settings) + gg = dict(conf["relais"]) + for k, v in gg.items(): + gg[k] = v.strip() + if "'" in v or '"' in v: + raise ValueError( + f"{k}:{v} from {path} containts forbidden char (escaping issue)" + ) + res.append(gg) return res diff --git a/papi/relais.py b/papi/relais.py index 3815d76..1beb62c 100644 --- a/papi/relais.py +++ b/papi/relais.py @@ -23,7 +23,10 @@ def api_login(config): post_url = config["api_login_url"] login_r = session.post( post_url, - json={"login": api_credentials.user, "password": api_credentials.password}, + json={ + "login": config["api_user_login"], + "password": config["api_user_password"], + }, ) logged.append(True) @@ -77,7 +80,7 @@ def main( pattern="relais_*" ): loopcount = itertools.count().__next__ - config = read_config(pattern=pattern) + config = read_config(kind="relais", pattern=pattern) assert len(config) == 1 config = config[0] login(config) diff --git a/tests/test_config.py b/tests/test_config.py index 831b432..cbe02ed 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -6,7 +6,8 @@ class ConfigurationTestCase(TestCase): """ Check configuration parsing """ def test_get_config(self): - configs = main.read_config(pattern="*test_sonde") + + configs = main.read_config(pattern="api_client*test_sonde") assert len(configs) == 1 config = configs[0] assert config["identifiant_sonde"] == "838266b2-fc3a-4430-95e8-f7f0d0fc9871"