From 4b6f3bf5edee0ff514e38960c52bf0d8552bb1e7 Mon Sep 17 00:00:00 2001 From: Sandro Meireles Date: Thu, 24 Aug 2023 10:36:02 -0300 Subject: [PATCH] fix: Adjusts the method that get user by email to create it if it doesn't exist --- marketplace/projects/usecases/project_creation.py | 10 +++------- .../usecases/tests/test_project_creation.py | 14 +++++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/marketplace/projects/usecases/project_creation.py b/marketplace/projects/usecases/project_creation.py index 69949ca0..99ed6346 100644 --- a/marketplace/projects/usecases/project_creation.py +++ b/marketplace/projects/usecases/project_creation.py @@ -3,7 +3,6 @@ from ..models import Project from .interfaces import TemplateTypeIntegrationInterface -from .exceptions import InvalidProjectData User = get_user_model() @@ -23,11 +22,8 @@ class ProjectCreationUseCase: def __init__(self, template_type_integration: TemplateTypeIntegrationInterface): self.__template_type_integration = template_type_integration - def get_user_by_email(self, email: str) -> User: - try: - return User.objects.get(email=email) - except User.DoesNotExist: - raise InvalidProjectData(f"User with email `{email}` does not exist!") + def get_or_create_user_by_email(self, email: str) -> tuple: + return User.objects.get_or_create(email=email) def get_or_create_project(self, project_dto: ProjectCreationDTO, user: User) -> tuple: return Project.objects.get_or_create( @@ -42,7 +38,7 @@ def get_or_create_project(self, project_dto: ProjectCreationDTO, user: User) -> ) def create_project(self, project_dto: ProjectCreationDTO, user_email: str) -> None: - user = self.get_user_by_email(user_email) + user, _ = self.get_or_create_user_by_email(user_email) project, _ = self.get_or_create_project(project_dto, user) if project_dto.is_template: diff --git a/marketplace/projects/usecases/tests/test_project_creation.py b/marketplace/projects/usecases/tests/test_project_creation.py index f9510131..c8b3d1b6 100644 --- a/marketplace/projects/usecases/tests/test_project_creation.py +++ b/marketplace/projects/usecases/tests/test_project_creation.py @@ -6,7 +6,6 @@ from ...models import Project from ..project_creation import ProjectCreationDTO, ProjectCreationUseCase -from ..exceptions import InvalidProjectData User = get_user_model() @@ -18,17 +17,18 @@ def setUp(self) -> None: self.user = User.objects.create_superuser(email="user@marketplace.ai") self.project = Project.objects.create(uuid=uuid.uuid4(), name="Test Project", created_by=self.user) - def test_get_user_by_email_raises_invalid_project_data(self): + def test_get_or_create_user_by_email(self): usecase = ProjectCreationUseCase(self.template_type_integration) + user, _ = usecase.get_or_create_user_by_email("user@marketplace.ai") - with self.assertRaisesMessage(InvalidProjectData, "User with email `fake@email.com` does not exist!"): - usecase.get_user_by_email("fake@email.com") + self.assertEqual(user, self.user) - def test_get_user_by_email(self): + def test_get_or_create_user_by_email_creates_new_user(self): + new_user_email = "newuser@marketplace.ai" usecase = ProjectCreationUseCase(self.template_type_integration) - user = usecase.get_user_by_email("user@marketplace.ai") + user, _ = usecase.get_or_create_user_by_email(new_user_email) - self.assertEqual(user, self.user) + self.assertEqual(user.email, new_user_email) def test_creating_template_project_integrate_template_type_in_project_is_called(self): project_uuid = uuid.uuid4()