Skip to content

Commit

Permalink
fix: Adjusts the method that get user by email to create it if it doe…
Browse files Browse the repository at this point in the history
…sn't exist
  • Loading branch information
Sandro-Meireles committed Aug 24, 2023
1 parent 5822172 commit 4b6f3bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
10 changes: 3 additions & 7 deletions marketplace/projects/usecases/project_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from ..models import Project
from .interfaces import TemplateTypeIntegrationInterface
from .exceptions import InvalidProjectData


User = get_user_model()
Expand All @@ -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(
Expand All @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions marketplace/projects/usecases/tests/test_project_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from ...models import Project
from ..project_creation import ProjectCreationDTO, ProjectCreationUseCase
from ..exceptions import InvalidProjectData


User = get_user_model()
Expand All @@ -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()
Expand Down

0 comments on commit 4b6f3bf

Please sign in to comment.