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

Add test and use skip_authentication decorator #497

Merged
merged 1 commit into from
Feb 4, 2025
Merged
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
28 changes: 18 additions & 10 deletions temba/api/v2/internals/contacts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ def test_get_contact_fields_with_multiple_fields(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(data.get("results"), expected_result)

@skip_authentication(endpoint_path=CONTACT_FIELDS_ENDPOINT_PATH)
def test_get_contact_fields_with_key_filter(self):
ContactField.get_or_create(self.org, self.admin, "test1", value_type="T")

url = f"/api/v2/internals/contacts_fields?project={self.org.proj_uuid}&key=test1"
response = self.client.get(url)

data = response.json()
expected_result = [{"key": "test1", "label": "Test1", "pinned": False, "value_type": "text"}]

self.assertEqual(response.status_code, 200)
self.assertEqual(data.get("results"), expected_result)


class UpdateContactFieldsViewTest(TembaTest):
@patch.object(LambdaURLValidator, "protected_resource")
Expand Down Expand Up @@ -246,17 +259,15 @@ 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", [])
@skip_authentication(endpoint_path=CONTACT_FIELDS_ENDPOINT_PATH)
def test_request_without_body(self):
url = "/api/v2/internals/contacts_fields"
response = self.client.post(url)

self.assertEqual(response.status_code, 400)
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", [])
@skip_authentication(endpoint_path=CONTACT_FIELDS_ENDPOINT_PATH)
def test_project_not_found(self):
url = "/api/v2/internals/contacts_fields"
body = {
Expand All @@ -269,8 +280,7 @@ def test_project_not_found(self):
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", [])
@skip_authentication(endpoint_path=CONTACT_FIELDS_ENDPOINT_PATH)
def test_user_not_found(self):
mock_user = MagicMock(spec=User)
mock_user.is_authenticated = False
Expand All @@ -289,8 +299,7 @@ def test_user_not_found(self):
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", [])
@skip_authentication(endpoint_path=CONTACT_FIELDS_ENDPOINT_PATH)
def test_serializer_error(self):
mock_user = MagicMock(spec=User)
mock_user.is_authenticated = True
Expand All @@ -308,8 +317,7 @@ def test_serializer_error(self):

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", [])
@skip_authentication(endpoint_path=CONTACT_FIELDS_ENDPOINT_PATH)
def test_success(self):
mock_user = MagicMock(spec=User)
mock_user.is_authenticated = True
Expand Down