Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Validate project type change #914

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading