diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 70eea3132..42a89055e 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -5,12 +5,14 @@ from __future__ import unicode_literals import base64 from django.conf.urls import include, url -from django.contrib.auth.models import User +from django.contrib.auth.models import AnonymousUser, User from django.db import models from django.http import HttpResponse from django.test import TestCase from django.utils import six +import pytest + from rest_framework import ( HTTP_HEADER_ENCODING, exceptions, permissions, renderers, status ) @@ -265,6 +267,26 @@ class CustomTokenAuthTests(BaseTokenAuthTests, TestCase): path = '/customtoken/' +class AttributeErrorInAuthenticateTests(TestCase): + def test_incorrect_credentials(self): + """ + Make sure AttributeErrors thrown in .authenticate() are not suppressed. + """ + class AttributeErrorThrowingAuth(BaseAuthentication): + def authenticate(self, request): + raise AttributeError('This exception should crash the authentication') + + request = factory.get('/') + request.user = AnonymousUser() + view = MockView.as_view( + authentication_classes=(AttributeErrorThrowingAuth,), + permission_classes=() + ) + + with pytest.raises(AttributeError): + view(request) + + class IncorrectCredentialsTests(TestCase): def test_incorrect_credentials(self): """