Skip to content

Commit

Permalink
Merge pull request #491 from weni-ai/feature/internal-endpoint-create…
Browse files Browse the repository at this point in the history
…-contact-field

Endpoint to create ContactField internally
  • Loading branch information
lucaslinhares authored Jan 29, 2025
2 parents ff97348 + aafe618 commit e4bfc8d
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 2 deletions.
93 changes: 93 additions & 0 deletions temba/api/v2/internals/contacts/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from unittest.mock import MagicMock, patch

from django.contrib.auth import get_user_model
from django.test import override_settings

from temba.tests import TembaTest

User = get_user_model()


class InternalContactViewTest(TembaTest):
def test_request_without_token(self):
Expand Down Expand Up @@ -39,3 +44,91 @@ def test_get_contacts(self):
self.assertEqual(len(data.get("results")), 2)

self.assertContains(response, str(contact1.uuid))


class InternalContactFieldsEndpointTest(TembaTest):
def setUp(self):
super().setUp()
User.objects.create(username="Mordecai", email="mordecai@msn.com")

@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.authentication_classes", [])
@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.permission_classes", [])
def test_request_without_body(self):
url = "/api/v2/internals/contacts_fields"
response = self.client.post(url)

self.assertEqual(response.status_code, 401)
self.assertEqual(response.json(), {"error": "Project not provided"})

@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.authentication_classes", [])
@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.permission_classes", [])
def test_project_not_found(self):
url = "/api/v2/internals/contacts_fields"
body = {
"project": self.org.uuid,
"label": "Nick Name",
"value_type": "text",
}
response = self.client.post(url, data=body, content_type="application/json")

self.assertEqual(response.status_code, 404)
self.assertEqual(response.json(), {"error": "Project not found"})

@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.authentication_classes", [])
@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.permission_classes", [])
def test_user_not_found(self):
mock_user = MagicMock(spec=User)
mock_user.is_authenticated = False
mock_user.email = "mockuser@example.com"

with patch("rest_framework.request.Request.user", mock_user):

url = "/api/v2/internals/contacts_fields"
body = {
"project": self.org.proj_uuid,
"label": "Nick Name",
"value_type": "text",
}
response = self.client.post(url, data=body, content_type="application/json")

self.assertEqual(response.status_code, 404)
self.assertEqual(response.json(), {"error": "User not found"})

@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.authentication_classes", [])
@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.permission_classes", [])
def test_serializer_error(self):
mock_user = MagicMock(spec=User)
mock_user.is_authenticated = True
mock_user.email = "mordecai@msn.com"

with patch("rest_framework.request.Request.user", mock_user):

url = "/api/v2/internals/contacts_fields"
body = {
"project": self.org.proj_uuid,
"label": "Nick Name",
"value_type": "T",
}
response = self.client.post(url, data=body, content_type="application/json")

self.assertEqual(response.status_code, 400)

@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.authentication_classes", [])
@patch("temba.api.v2.internals.contacts.views.InternalContactFieldsEndpoint.permission_classes", [])
def test_success(self):
mock_user = MagicMock(spec=User)
mock_user.is_authenticated = True
mock_user.email = "mordecai@msn.com"

with patch("rest_framework.request.Request.user", mock_user):

url = "/api/v2/internals/contacts_fields"
body = {
"project": self.org.proj_uuid,
"label": "Nick Name",
"value_type": "text",
}
response = self.client.post(url, data=body, content_type="application/json")

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"message": "Success"})
7 changes: 5 additions & 2 deletions temba/api/v2/internals/contacts/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.urls import path

from .views import InternalContactView
from .views import InternalContactFieldsEndpoint, InternalContactView

urlpatterns = [path("contacts", InternalContactView.as_view(), name="internal_contacts")]
urlpatterns = [
path("contacts", InternalContactView.as_view(), name="internal_contacts"),
path("contacts_fields", InternalContactFieldsEndpoint.as_view(), name="internal_contacts_fields"),
]
38 changes: 38 additions & 0 deletions temba/api/v2/internals/contacts/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from rest_framework.exceptions import AuthenticationFailed, NotAuthenticated
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from weni.internal.authenticators import InternalOIDCAuthentication
from weni.internal.permissions import CanCommunicateInternally

from django.conf import settings
from django.contrib.auth import get_user_model

from temba.api.v2.internals.contacts.serializers import InternalContactSerializer
from temba.api.v2.internals.views import APIViewMixin
from temba.api.v2.serializers import ContactFieldWriteSerializer
from temba.contacts.models import Contact
from temba.orgs.models import Org

User = get_user_model()


class InternalContactView(APIViewMixin, APIView):
Expand Down Expand Up @@ -44,3 +52,33 @@ def post(self, request: Request):
response["results"].append(contact_data)

return Response(response)


class InternalContactFieldsEndpoint(APIViewMixin, APIView):
authentication_classes = [InternalOIDCAuthentication]
permission_classes = [IsAuthenticated, CanCommunicateInternally]

def post(self, request, *args, **kwargs):
project_uuid = request.data.get("project")

if not project_uuid:
return Response({"error": "Project not provided"}, status=401)

try:
org = Org.objects.get(proj_uuid=project_uuid)
user = User.objects.get(email=request.user.email)
except Org.DoesNotExist:
return Response({"error": "Project not found"}, status=404)

except User.DoesNotExist:
return Response({"error": "User not found"}, status=404)

serializer = ContactFieldWriteSerializer(
data=request.data, context={"request": request, "org": org, "user": user}
)

if serializer.is_valid():
serializer.save()
return Response({"message": "Success"})

return Response(serializer.errors, status=400)

0 comments on commit e4bfc8d

Please sign in to comment.