Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslinhares committed Jan 29, 2025
1 parent 5c67f3e commit 391331e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
111 changes: 111 additions & 0 deletions temba/api/v2/internals/contacts/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from unittest.mock import patch

from rest_framework import status
from rest_framework.response import Response

from django.test import override_settings

from temba.api.v2.validators import LambdaURLValidator
from temba.tests import TembaTest
from temba.tests.mailroom import mock_mailroom


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

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


class UpdateContactFieldsViewTest(TembaTest):
@patch.object(LambdaURLValidator, "protected_resource")
def test_request_without_body(self, mock_protected_resource):
url = "/api/v2/internals/update_contacts_fields"

mock_protected_resource.return_value = Response({"message": "Access granted!"}, status=status.HTTP_200_OK)
response = self.client.patch(url)

self.assertEqual(response.status_code, 400)

@patch.object(LambdaURLValidator, "protected_resource")
def test_request_no_project(self, mock_protected_resource):

mock_protected_resource.return_value = Response({"message": "Access granted!"}, status=status.HTTP_200_OK)

url = "/api/v2/internals/update_contacts_fields"

body = {
"contact_urn": "Nick Name",
"contact_fields": {"cpf": "12345678912"},
}

response = self.client.patch(url, data=body, content_type="application/json")

self.assertEqual(response.status_code, 400)
self.assertEqual(response.json(), {"project": ["This field is required."]})

@patch.object(LambdaURLValidator, "protected_resource")
def test_request_incorrect_project(self, mock_protected_resource):

mock_protected_resource.return_value = Response({"message": "Access granted!"}, status=status.HTTP_200_OK)

url = "/api/v2/internals/update_contacts_fields"

body = {
"project": self.org.uuid,
"contact_urn": "Nick Name",
"contact_fields": {"cpf": "12345678912"},
}

response = self.client.patch(url, data=body, content_type="application/json")

self.assertEqual(response.status_code, 400)
self.assertEqual(response.json(), {"project": ["Project not found"]})

@patch.object(LambdaURLValidator, "protected_resource")
def test_request_invalid_contact_urn(self, mock_protected_resource):

mock_protected_resource.return_value = Response({"message": "Access granted!"}, status=status.HTTP_200_OK)

url = "/api/v2/internals/update_contacts_fields"

body = {
"project": self.org.proj_uuid,
"contact_urn": "ext:hello@hello.ign",
"contact_fields": {"cpf": "12345678912"},
}

response = self.client.patch(url, data=body, content_type="application/json")

self.assertEqual(response.status_code, 400)
self.assertEqual(response.json(), {"contact_urn": ["Contact URN not found"]})

@patch.object(LambdaURLValidator, "protected_resource")
def test_request_no_contact_fields(self, mock_protected_resource):

mock_protected_resource.return_value = Response({"message": "Access granted!"}, status=status.HTTP_200_OK)

url = "/api/v2/internals/update_contacts_fields"

body = {
"project": self.org.proj_uuid,
"contact_urn": "ext:hello@hello.ign",
"contact_fields": {},
}

response = self.client.patch(url, data=body, content_type="application/json")

self.assertEqual(response.status_code, 400)
self.assertEqual(response.json(), {"contact_fields": ["contact_fields must not be an empty dictionary"]})

@mock_mailroom
@override_settings(INTERNAL_USER_EMAIL="super@user.com")
@patch.object(LambdaURLValidator, "protected_resource")
def test_success(self, mr_mocks, mock_protected_resource):
self.create_contact("Rigbt", urns=["twitterid:0000000"])
self.create_field("last_name", "Last name")

mock_protected_resource.return_value = Response({"message": "Access granted!"}, status=status.HTTP_200_OK)

url = "/api/v2/internals/update_contacts_fields"

body = {
"project": self.org.proj_uuid,
"contact_urn": "twitterid:0000000",
"contact_fields": {"last_name": "Cube"},
}

response = self.client.patch(url, data=body, content_type="application/json")

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"message": "Contact fields updated successfully"})
4 changes: 2 additions & 2 deletions temba/api/v2/internals/contacts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class UpdateContactFieldsView(APIViewMixin, APIView, LambdaURLValidator):
renderer_classes = [JSONRenderer]

def patch(self, request, *args, **kwargs):
validation_response = self.protected_resource(request)
if validation_response.status_code != 200:
validation_response = self.protected_resource(request) # pragma: no cover
if validation_response.status_code != 200: # pragma: no cover
return validation_response
serializer = InternalContactFieldsValuesSerializer(data=request.data)
if serializer.is_valid():
Expand Down
2 changes: 1 addition & 1 deletion temba/api/v2/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __call__(self, value, serializer_field):
super().__call__(value, serializer_field)


class LambdaURLValidator:
class LambdaURLValidator: # pragma: no cover
def is_valid_url(self, sts_url):
return sts_url.startswith("https://sts.amazonaws.com/?Action=GetCallerIdentity&") and (".." not in sts_url)

Expand Down

0 comments on commit 391331e

Please sign in to comment.