Skip to content

Commit

Permalink
Update tests.py
Browse files Browse the repository at this point in the history
- Remove user login authentication and replace with API Key authentication in tests.
  • Loading branch information
sanchegm committed Dec 12, 2024
1 parent 525ea7e commit 4329c09
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/affiliations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Third-party dependencies:
from unittest import mock
from django.test import TestCase
from django.contrib.auth.models import User

from django.core.exceptions import ValidationError
from rest_framework.test import APIRequestFactory
from rest_framework.test import APIRequestFactory, APITestCase
from rest_framework_api_key.models import APIKey

# In-house code:
from affiliations.views import AffiliationsList
Expand All @@ -16,7 +16,7 @@
from affiliations.admin import AffiliationForm


class AffiliationsViewsBaseTestCase(TestCase):
class AffiliationsViewsBaseTestCase(APITestCase):
"""A base test class with setup for testing affiliations views."""

maxDiff = None
Expand Down Expand Up @@ -184,9 +184,11 @@ def test_should_be_able_to_view_single_affiliation_detail(self):
def test_detail_affiliation_json_call(self):
"""Make sure the API response of a single affiliation is returned
in the original JSON format ."""
_ = User.objects.create_user(username="test_user", password="secret")
self.client.login(username="test_user", password="secret")
response = self.client.get("/api/affiliation_detail/?affil_id=10000")
_, key = APIKey.objects.create_key(name="my-remote-service")
auth_headers = {"HTTP_X_API_KEY": key}
response = self.client.get(
"/api/affiliation_detail/?affil_id=10000", **auth_headers
)
self.assertEqual(
response.json(),
[
Expand All @@ -210,9 +212,9 @@ def test_detail_affiliation_json_call(self):
def test_list_affiliation_json_call(self):
"""Make sure the API response of all the affiliations in the db is
returned in the original JSON format ."""
_ = User.objects.create_user(username="test_user", password="secret")
self.client.login(username="test_user", password="secret")
response = self.client.get("/api/affiliations_list/")
_, key = APIKey.objects.create_key(name="my-remote-service")
auth_headers = {"HTTP_X_API_KEY": key}
response = self.client.get("/api/affiliations_list/", **auth_headers)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 2)

Expand Down

0 comments on commit 4329c09

Please sign in to comment.