Skip to content

Commit

Permalink
Merge pull request #34 from em1208/func-tests
Browse files Browse the repository at this point in the history
Added function based view tests for authentication
  • Loading branch information
em1208 committed Apr 6, 2024
2 parents 61feeaa + b02eaf8 commit 5f882b6
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from django.test import TestCase, override_settings

from adrf.views import APIView
from adrf.decorators import api_view
from rest_framework import permissions, status
from rest_framework.decorators import permission_classes, authentication_classes
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.test import APIRequestFactory
Expand Down Expand Up @@ -53,21 +55,37 @@ async def get(self, request):
return HttpResponse({"a": 1, "b": 2, "c": 3})


@api_view(("GET",))
@permission_classes([permissions.IsAuthenticated])
@authentication_classes([AsyncAuthentication])
async def mock_view_func(request):
return HttpResponse({"a": 1, "b": 2, "c": 3})


@override_settings(ROOT_URLCONF=__name__)
class TestAsyncAuthentication(TestCase):
async def test_admit_customtoken(self):
async def test_admit_customtoken_class_view(self):
auth = "Bearer admitme"
request = factory.get("/view/", HTTP_AUTHORIZATION=auth)

response = await MockView.as_view()(request)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(request.user, faked_user)

async def test_reject_customtoken(self):
async def test_reject_customtoken_class_view(self):
auth = "Bearer expired"
request = factory.get("/view/", HTTP_AUTHORIZATION=auth)

response = await MockView.as_view()(request)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

async def test_admit_customtoken_func_view(self):
auth = "Bearer admitme"
request = factory.get("/view/", HTTP_AUTHORIZATION=auth)
response = await mock_view_func(request)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(request.user, faked_user)

async def test_reject_customtoken_func_view(self):
auth = "Bearer expired"
request = factory.get("/view/", HTTP_AUTHORIZATION=auth)
response = await mock_view_func(request)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

0 comments on commit 5f882b6

Please sign in to comment.