Skip to content

Commit

Permalink
Merge pull request #908 from weni-ai/fix/organization-name-field-leng…
Browse files Browse the repository at this point in the history
…th-validation

Fix/organization name field length validation
  • Loading branch information
kallilsouza authored Dec 26, 2024
2 parents b8f7a85 + 8d7d102 commit cd0accb
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
86 changes: 84 additions & 2 deletions connect/api/v2/organizations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from rest_framework.test import APIRequestFactory, force_authenticate

from django.test import TestCase

from django.utils.crypto import get_random_string

from unittest.mock import patch

from connect.api.v1.tests.utils import create_user_and_token
from connect.common.models import Organization, BillingPlan, OrganizationRole
from connect.common.models import Project, Organization, BillingPlan, OrganizationRole
from connect.authentication.models import UserEmailSetup
from connect.api.v2.organizations.views import OrganizationViewSet
from connect.common.mocks import StripeMockGateway
Expand Down Expand Up @@ -184,6 +184,88 @@ def test_create_organization_project(

self.assertEquals(response.status_code, status.HTTP_201_CREATED)

@patch("connect.billing.get_gateway")
@patch("connect.authentication.models.User.send_request_flow_user_info")
@patch(
"connect.internals.event_driven.producer.rabbitmq_publisher.RabbitmqPublisher"
)
def test_cannot_create_organization_with_invalid_name_length(
self, mock_publisher, send_request_flow_user_info, mock_get_gateway
):
mock_get_gateway.return_value = StripeMockGateway()
send_request_flow_user_info.side_effect = [True]
mock_publisher.side_effect = [True]

invalid_name_length = Organization.name.field.max_length + 1

org_data = {
"name": get_random_string(invalid_name_length),
"description": "V2 desc",
"organization_billing_plan": BillingPlan.PLAN_TRIAL,
"authorizations": [
{"user_email": "e@mail.com", "role": 3},
{"user_email": "user_1@user.com", "role": 3},
],
}

project_data = {
"date_format": "D",
"name": "Test Project",
"timezone": "America/Argentina/Buenos_Aires",
}

data = {"organization": org_data, "project": project_data}

path = "/v2/organizations/"
method = {"post": "create"}
user = self.user

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

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data["organization"]["name"][0].code, "max_length")

@patch("connect.billing.get_gateway")
@patch("connect.authentication.models.User.send_request_flow_user_info")
@patch(
"connect.internals.event_driven.producer.rabbitmq_publisher.RabbitmqPublisher"
)
def test_cannot_create_organization_project_with_invalid_name_length(
self, mock_publisher, send_request_flow_user_info, mock_get_gateway
):
mock_get_gateway.return_value = StripeMockGateway()
send_request_flow_user_info.side_effect = [True]
mock_publisher.side_effect = [True]

org_data = {
"name": "Test",
"description": "V2 desc",
"organization_billing_plan": BillingPlan.PLAN_TRIAL,
"authorizations": [
{"user_email": "e@mail.com", "role": 3},
{"user_email": "user_1@user.com", "role": 3},
],
}

invalid_name_length = Project.name.field.max_length + 1

project_data = {
"date_format": "D",
"name": get_random_string(invalid_name_length),
"timezone": "America/Argentina/Buenos_Aires",
}

data = {"organization": org_data, "project": project_data}

path = "/v2/organizations/"
method = {"post": "create"}
user = self.user

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

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data["project"]["name"][0].code, "max_length")

@patch("connect.internals.event_driven.producer.rabbitmq_publisher.RabbitmqPublisher.send_message")
@patch("connect.authentication.models.User.send_request_flow_user_info")
def test_user_email_setup(
Expand Down
16 changes: 13 additions & 3 deletions connect/api/v2/organizations/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pendulum
from rest_framework import mixins, status
from rest_framework import mixins, status, exceptions
from rest_framework.decorators import action
from rest_framework.viewsets import GenericViewSet
from rest_framework.response import Response
Expand Down Expand Up @@ -83,7 +83,12 @@ def create(self, request, *args, **kwargs):

# Organization
serializer = self.get_serializer(data=org_data)
serializer.is_valid()

try:
serializer.is_valid(raise_exception=True)
except exceptions.ValidationError as e:
raise exceptions.ValidationError({"organization": e.detail}, code=e.get_codes())

instance = serializer.save()

if type(instance) == dict:
Expand All @@ -94,7 +99,12 @@ def create(self, request, *args, **kwargs):
{"organization": instance.uuid}
)
project_serializer = ProjectSerializer(data=project_data, context={"request": request})
project_serializer.is_valid()

try:
project_serializer.is_valid(raise_exception=True)
except exceptions.ValidationError as e:
raise exceptions.ValidationError({"project": e.detail}, code=e.get_codes())

project_instance = project_serializer.save()

if type(project_instance) == dict:
Expand Down
2 changes: 1 addition & 1 deletion connect/api/v2/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Meta:
ref_name = None

uuid = serializers.UUIDField(style={"show": False}, read_only=True)
name = serializers.CharField(max_length=500, required=True)
name = serializers.CharField(max_length=150, required=True)
description = serializers.CharField(max_length=1000, required=False)
organization = serializers.PrimaryKeyRelatedField(
queryset=Organization.objects,
Expand Down

0 comments on commit cd0accb

Please sign in to comment.