From 4329c095c7b1ef4534ffae9e4d18456366e858b3 Mon Sep 17 00:00:00 2001 From: sanchegm Date: Wed, 11 Dec 2024 16:58:40 -0800 Subject: [PATCH] Update tests.py - Remove user login authentication and replace with API Key authentication in tests. --- src/affiliations/tests.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/affiliations/tests.py b/src/affiliations/tests.py index 3ac91c1..3899554 100644 --- a/src/affiliations/tests.py +++ b/src/affiliations/tests.py @@ -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 @@ -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 @@ -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(), [ @@ -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)