diff --git a/src/controllers/login.py b/src/controllers/login.py index d44ab0a..bc353b2 100644 --- a/src/controllers/login.py +++ b/src/controllers/login.py @@ -12,23 +12,18 @@ class Controller: @classmethod def run(self): - # session = init_db() - - # collaborators = session.query(Collaborator).all() - - # if collaborators: - # self._menu(session) - # else: - # self._create_gestion_account(session) - # self._menu(session) - collaborators = self.session.query(Collaborator).all() if collaborators: self._menu(self.session) else: self._create_gestion_account(self.session) - self._menu(self.session) + + self.re_run() + + @classmethod + def re_run(self): + self.run() @classmethod def return_to_menu(self, session): diff --git a/src/models/contract_event.py b/src/models/contract_event.py index e69c8ad..10b7617 100644 --- a/src/models/contract_event.py +++ b/src/models/contract_event.py @@ -241,7 +241,7 @@ def support_name(self): @property def support_email(self): if self.support: - return self.support.email + return Fernet.decrypt(self.support.email) return unfilled diff --git a/src/utils/fernet.py b/src/utils/fernet.py index 5827285..1eb81c6 100644 --- a/src/utils/fernet.py +++ b/src/utils/fernet.py @@ -1,4 +1,4 @@ -import logging +from sentry_sdk import capture_exception from ..settings.settings import fernet @@ -13,7 +13,7 @@ def encrypt(value: str) -> str: except Exception as e: error_msg = f"encrypt error : {e}" print(error_msg) - logging.exception(error_msg) + capture_exception(error_msg) @staticmethod def decrypt(value: str) -> str: @@ -22,4 +22,4 @@ def decrypt(value: str) -> str: except Exception as e: error_msg = f"decrypt error : {e}" print(error_msg) - logging.exception(error_msg) + capture_exception(error_msg) diff --git a/src/views/event.py b/src/views/event.py index 092ae7d..a3bc0c6 100644 --- a/src/views/event.py +++ b/src/views/event.py @@ -10,7 +10,6 @@ from ..forms.location import LocationForm from ..models.collaborator import Collaborator from ..models.contract_event import Contract, Event -from ..utils.fernet import Fernet from .collaborator import View as CollaboratorView from .contract import View as ContractView from .mixin import MixinView @@ -104,7 +103,7 @@ def print_detail(self, obj: Event): table.add_row("Tel client", obj.customer_phone) table.add_row("", "") table.add_row("Nom support", obj.support_name) - table.add_row("Email support", Fernet.decrypt(obj.support_email)) + table.add_row("Email support", obj.support_email) table.add_row("Tel support", obj.support_phone) table.add_row("", "") table.add_row("Date de début", obj.formatted_start_date) diff --git a/src/views/mixin.py b/src/views/mixin.py index ec05b5d..8244cd8 100644 --- a/src/views/mixin.py +++ b/src/views/mixin.py @@ -1,7 +1,7 @@ -import logging import sys from rich.console import Console +from sentry_sdk import capture_message console = Console() @@ -29,7 +29,7 @@ def print_create_success(self, obj): success_msg = f"{self.name.title()} n°{obj.id} créé avec succès !\n" console.print(success_msg, style="bold green") - logging.info(success_msg) + capture_message(success_msg) @classmethod def print_update_success(self, obj): @@ -38,13 +38,13 @@ def print_update_success(self, obj): success_msg = f"{self.name.title()} n°{obj.id} modifié avec succès !\n" console.print(success_msg, style="bold green") - logging.info(success_msg) + capture_message(success_msg) @classmethod def print_delete_success(self, obj_id: int): success_msg = f"\n\n{self.name.title()} n°{obj_id} supprimé avec succès !\n\n" console.print(success_msg, style="bold green") - logging.info(success_msg) + capture_message(success_msg) @classmethod def print_signature_success(self, session, obj): @@ -54,7 +54,7 @@ def print_signature_success(self, session, obj): ) console.print(success_msg1, style="bold green") - logging.info(success_msg1) + capture_message(success_msg1) success_msg2 = f"Vous pouvez maintenant créer un événement pour le {self.name} n°{obj.id}.\n" diff --git a/tests/controllers/test_login.py b/tests/controllers/test_login.py index bff528a..73bc96c 100644 --- a/tests/controllers/test_login.py +++ b/tests/controllers/test_login.py @@ -12,26 +12,6 @@ class TestController(MixinSetup): - def test_run_and_quit(self, monkeypatch): - self.clear_db() - - # forbidden paths - monkeypatch.setattr( - Controller, "_create_gestion_account", self.mock_permission_denied - ) - - monkeypatch.setattr(Controller, "return_to_menu", self.mock_permission_denied) - - # allowed path - monkeypatch.setattr(Controller, "session", self.session) - monkeypatch.setattr(View, "print_menu", lambda *args, **kwargs: None) - monkeypatch.setattr(View, "get_user_choice", lambda *args, **kwargs: 0) - monkeypatch.setattr(View, "logout", lambda *args, **kwargs: None) - - # run paths - self.create_collaborator("Gestion") - Controller.run() - def test_create_the_first_gestion_account(self, monkeypatch): self.clear_db() @@ -54,6 +34,7 @@ def test_create_the_first_gestion_account(self, monkeypatch): form2 = PasswordForm(MultiDict(input_data2)) # allowed path + monkeypatch.setattr(Controller, "re_run", lambda *args, **kwargs: None) monkeypatch.setattr(Controller, "session", self.session) monkeypatch.setattr(View, "get_gestion_data", lambda *args, **kwargs: form1) monkeypatch.setattr(View, "create_password", lambda *args, **kwargs: form2) @@ -70,6 +51,27 @@ def test_create_the_first_gestion_account(self, monkeypatch): assert created_gestion assert Fernet.decrypt(created_gestion.email) == input_data.get("email") + def test_run_and_quit(self, monkeypatch): + self.clear_db() + + # forbidden paths + monkeypatch.setattr( + Controller, "_create_gestion_account", self.mock_permission_denied + ) + + monkeypatch.setattr(Controller, "return_to_menu", self.mock_permission_denied) + + # allowed path + monkeypatch.setattr(Controller, "re_run", lambda *args, **kwargs: None) + monkeypatch.setattr(Controller, "session", self.session) + monkeypatch.setattr(View, "print_menu", lambda *args, **kwargs: None) + monkeypatch.setattr(View, "get_user_choice", lambda *args, **kwargs: 0) + monkeypatch.setattr(View, "logout", lambda *args, **kwargs: None) + + # run paths + self.create_collaborator("Gestion") + Controller.run() + def test_login(self, monkeypatch): self.clear_db() # setup user data @@ -94,6 +96,7 @@ def test_login(self, monkeypatch): monkeypatch.setattr(View, "print_login_failure", self.mock_permission_denied) # allowed path + monkeypatch.setattr(Controller, "re_run", lambda *args, **kwargs: None) monkeypatch.setattr(Controller, "session", self.session) monkeypatch.setattr(View, "print_menu", lambda *args, **kwargs: None) monkeypatch.setattr(View, "get_user_choice", lambda *args, **kwargs: 1) @@ -134,6 +137,7 @@ def test_login_failure(self, monkeypatch): monkeypatch.setattr(HomeMenu, "run", self.mock_permission_denied) # allowed path + monkeypatch.setattr(Controller, "re_run", lambda *args, **kwargs: None) monkeypatch.setattr(Controller, "session", self.session) monkeypatch.setattr(View, "print_menu", lambda *args, **kwargs: None) monkeypatch.setattr(View, "get_user_choice", lambda *args, **kwargs: 1) @@ -178,6 +182,7 @@ def test_change_password(self, monkeypatch): monkeypatch.setattr(View, "print_forms_errors", self.mock_permission_denied) # allowed path + monkeypatch.setattr(Controller, "re_run", lambda *args, **kwargs: None) monkeypatch.setattr(Controller, "session", self.session) monkeypatch.setattr(View, "print_menu", lambda *args, **kwargs: None) monkeypatch.setattr(View, "get_user_choice", lambda *args, **kwargs: 2) @@ -236,6 +241,7 @@ def test_first_connexion(self, monkeypatch): monkeypatch.setattr(View, "print_forms_errors", self.mock_permission_denied) # allowed path + monkeypatch.setattr(Controller, "re_run", lambda *args, **kwargs: None) monkeypatch.setattr(Controller, "session", self.session) monkeypatch.setattr(View, "print_menu", lambda *args, **kwargs: None) monkeypatch.setattr(View, "get_user_choice", lambda *args, **kwargs: 3) @@ -292,6 +298,7 @@ def test_first_connexion_failure_with_wrong_id(self, monkeypatch): ) # allowed path + monkeypatch.setattr(Controller, "re_run", lambda *args, **kwargs: None) monkeypatch.setattr(Controller, "session", self.session) monkeypatch.setattr(View, "print_menu", lambda *args, **kwargs: None) monkeypatch.setattr(View, "get_user_choice", lambda *args, **kwargs: 3) diff --git a/tests/models/test_event.py b/tests/models/test_event.py index 576bc40..229f5bb 100644 --- a/tests/models/test_event.py +++ b/tests/models/test_event.py @@ -1,4 +1,5 @@ from src.models.contract_event import Event, slugify, unfilled +from src.utils.fernet import Fernet from tests import MixinSetup @@ -112,7 +113,7 @@ def test_support_email(self): self.clear_db() qs = self.create_event_qs() event = qs[0] - assert event.support_email == event.support.email + assert event.support_email == Fernet.decrypt(event.support.email) event.support = None assert event.support_email == unfilled diff --git a/tests/views/test_event.py b/tests/views/test_event.py index 05d4d28..3509590 100644 --- a/tests/views/test_event.py +++ b/tests/views/test_event.py @@ -2,7 +2,6 @@ from rich.prompt import Prompt -from src.utils.fernet import Fernet from src.views.event import Collaborator, Contract, Event, EventForm, LocationForm, View from tests import MixinSetup @@ -114,7 +113,7 @@ def test_print_detail(self, capsys): assert "Tel client" and obj.customer_phone in captured.out assert "Nom support" and obj.support_name in captured.out - assert "Email support" and Fernet.decrypt(obj.support_email) in captured.out + assert "Email support" and obj.support_email in captured.out assert "Tel support" and obj.support_phone in captured.out assert "Date de début" and obj.formatted_start_date in captured.out