Skip to content

Commit

Permalink
Add validation to prevent a general project from having its type chan…
Browse files Browse the repository at this point in the history
…ged to commerce
  • Loading branch information
kallilsouza committed Jan 20, 2025
1 parent 24f33ad commit aaa280f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
34 changes: 31 additions & 3 deletions connect/api/v2/projects/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@ def test_set_type_success(self):
"""Test successfully setting a valid project type"""
project_uuid = str(self.project1.uuid)

path = f"/v2/projects/{project_uuid}/set-type"
method = {"post": "set_type"}
data = {"project_type": TypeProject.GENERAL}
user = self.user

response, content_data = self.request(
path,
method,
user=user,
project_uuid=project_uuid,
data=data,
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(content_data.get("project_type"), TypeProject.GENERAL)

self.project1.refresh_from_db()
self.assertEqual(self.project1.project_type, TypeProject.GENERAL)

def test_cannot_set_type_to_commerce_once_is_general(self):
"""Test successfully setting a valid project type"""
self.project1.project_type = TypeProject.GENERAL
self.project1.save()
project_uuid = str(self.project1.uuid)

path = f"/v2/projects/{project_uuid}/set-type"
method = {"post": "set_type"}
data = {"project_type": TypeProject.COMMERCE}
Expand All @@ -241,11 +266,14 @@ def test_set_type_success(self):
data=data,
)

self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals(content_data.get("project_type"), TypeProject.COMMERCE)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn(
f"It is not possible to change the type from {TypeProject.GENERAL} to {TypeProject.COMMERCE}",
content_data.get("detail"),
)

self.project1.refresh_from_db()
self.assertEquals(self.project1.project_type, TypeProject.COMMERCE)
self.assertNotEqual(self.project1.project_type, TypeProject.COMMERCE)

def test_set_type_invalid_type(self):
"""Test setting an invalid project type returns error"""
Expand Down
14 changes: 13 additions & 1 deletion connect/api/v2/projects/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import mixins, status
from rest_framework.exceptions import ValidationError
from rest_framework.viewsets import GenericViewSet
from rest_framework.response import Response
from rest_framework.decorators import action
Expand Down Expand Up @@ -116,7 +117,7 @@ def perform_destroy(self, instance):

@action(detail=True, methods=["POST"], url_name="set-type")
def set_type(self, request, **kwargs):
instance = self.get_object()
instance: Project = self.get_object()
try:
project_type = request.data.get("project_type")

Expand All @@ -129,6 +130,17 @@ def set_type(self, request, **kwargs):
status=status.HTTP_400_BAD_REQUEST,
)

if (
instance.project_type == TypeProject.GENERAL
and project_type == TypeProject.COMMERCE
):
return Response(
{
"detail": f"It is not possible to change the type from {TypeProject.GENERAL} to {TypeProject.COMMERCE}"
},
status=status.HTTP_400_BAD_REQUEST,
)

instance.project_type = project_type
instance.save(update_fields=["project_type"])

Expand Down

0 comments on commit aaa280f

Please sign in to comment.