Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
NidalChateur committed Feb 19, 2024
2 parents b668989 + 2fb6965 commit e4e1e81
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 45 deletions.
17 changes: 6 additions & 11 deletions src/controllers/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion src/models/contract_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/utils/fernet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from sentry_sdk import capture_exception

from ..settings.settings import fernet

Expand All @@ -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:
Expand All @@ -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)
3 changes: 1 addition & 2 deletions src/views/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/views/mixin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import sys

from rich.console import Console
from sentry_sdk import capture_message

console = Console()

Expand Down Expand Up @@ -29,7 +29,7 @@ def print_create_success(self, obj):
success_msg = f"{self.name.title()}{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):
Expand All @@ -38,13 +38,13 @@ def print_update_success(self, obj):
success_msg = f"{self.name.title()}{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()}{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):
Expand All @@ -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}{obj.id}.\n"

Expand Down
47 changes: 27 additions & 20 deletions tests/controllers/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tests/models/test_event.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from src.models.contract_event import Event, slugify, unfilled
from src.utils.fernet import Fernet
from tests import MixinSetup


Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/views/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e4e1e81

Please sign in to comment.