diff options
| author | Tom Christie | 2015-02-09 20:43:50 +0000 |
|---|---|---|
| committer | Tom Christie | 2015-02-09 20:43:50 +0000 |
| commit | fbb21caaaa01033bbd34b0c63ab48243ffb6310e (patch) | |
| tree | 5d0fdee18c9bf02733b1df913c4cddd9e3e86da7 /tests | |
| parent | 407480b4840990ff17f9a33b293cfcf15bb6f7c5 (diff) | |
| parent | 7b639c0cd0676172cc8502e833f5b708f39f9a83 (diff) | |
| download | django-rest-framework-fbb21caaaa01033bbd34b0c63ab48243ffb6310e.tar.bz2 | |
Merge master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_authentication.py | 9 | ||||
| -rw-r--r-- | tests/test_relations_hyperlink.py | 4 | ||||
| -rw-r--r-- | tests/test_renderers.py | 9 | ||||
| -rw-r--r-- | tests/test_request.py | 22 | ||||
| -rw-r--r-- | tests/test_response.py | 9 | ||||
| -rw-r--r-- | tests/test_throttling.py | 8 | ||||
| -rw-r--r-- | tests/test_versioning.py | 9 |
7 files changed, 60 insertions, 10 deletions
diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 04c5782e..91e49f9d 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -168,6 +168,15 @@ class TokenAuthTests(TestCase): response = self.csrf_client.post('/token/', {'example': 'example'}, format='json', HTTP_AUTHORIZATION=auth) self.assertEqual(response.status_code, status.HTTP_200_OK) + def test_post_json_makes_one_db_query(self): + """Ensure that authenticating a user using a token performs only one DB query""" + auth = "Token " + self.key + + def func_to_test(): + return self.csrf_client.post('/token/', {'example': 'example'}, format='json', HTTP_AUTHORIZATION=auth) + + self.assertNumQueries(1, func_to_test) + def test_post_form_failing_token_auth(self): """Ensure POSTing form over token auth without correct credentials fails""" response = self.csrf_client.post('/token/', {'example': 'example'}) diff --git a/tests/test_relations_hyperlink.py b/tests/test_relations_hyperlink.py index aede61d2..33b09713 100644 --- a/tests/test_relations_hyperlink.py +++ b/tests/test_relations_hyperlink.py @@ -12,7 +12,9 @@ factory = APIRequestFactory() request = factory.get('/') # Just to ensure we have a request in the serializer context -dummy_view = lambda request, pk: None +def dummy_view(request, pk): + pass + urlpatterns = [ url(r'^dummyurl/(?P<pk>[0-9]+)/$', dummy_view, name='dummy-url'), diff --git a/tests/test_renderers.py b/tests/test_renderers.py index f68405f0..60a08225 100644 --- a/tests/test_renderers.py +++ b/tests/test_renderers.py @@ -21,8 +21,13 @@ import re DUMMYSTATUS = status.HTTP_200_OK DUMMYCONTENT = 'dummycontent' -RENDERER_A_SERIALIZER = lambda x: ('Renderer A: %s' % x).encode('ascii') -RENDERER_B_SERIALIZER = lambda x: ('Renderer B: %s' % x).encode('ascii') + +def RENDERER_A_SERIALIZER(x): + return ('Renderer A: %s' % x).encode('ascii') + + +def RENDERER_B_SERIALIZER(x): + return ('Renderer B: %s' % x).encode('ascii') expected_results = [ diff --git a/tests/test_request.py b/tests/test_request.py index 02a9b1e2..c274ab69 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -249,9 +249,29 @@ class TestUserSetter(TestCase): login(self.request, self.user) self.assertEqual(self.wrapped_request.user, self.user) + def test_calling_user_fails_when_attribute_error_is_raised(self): + """ + This proves that when an AttributeError is raised inside of the request.user + property, that we can handle this and report the true, underlying error. + """ + class AuthRaisesAttributeError(object): + def authenticate(self, request): + import rest_framework + rest_framework.MISSPELLED_NAME_THAT_DOESNT_EXIST -class TestAuthSetter(TestCase): + self.request = Request(factory.get('/'), authenticators=(AuthRaisesAttributeError(),)) + SessionMiddleware().process_request(self.request) + login(self.request, self.user) + try: + self.request.user + except AttributeError as error: + self.assertEqual(str(error), "'module' object has no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'") + else: + assert False, 'AttributeError not raised' + + +class TestAuthSetter(TestCase): def test_auth_can_be_set(self): request = Request(factory.get('/')) request.auth = 'DUMMY' diff --git a/tests/test_response.py b/tests/test_response.py index f233ae33..4a9deaa2 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -38,8 +38,13 @@ class MockTextMediaRenderer(BaseRenderer): DUMMYSTATUS = status.HTTP_200_OK DUMMYCONTENT = 'dummycontent' -RENDERER_A_SERIALIZER = lambda x: ('Renderer A: %s' % x).encode('ascii') -RENDERER_B_SERIALIZER = lambda x: ('Renderer B: %s' % x).encode('ascii') + +def RENDERER_A_SERIALIZER(x): + return ('Renderer A: %s' % x).encode('ascii') + + +def RENDERER_B_SERIALIZER(x): + return ('Renderer B: %s' % x).encode('ascii') class RendererA(BaseRenderer): diff --git a/tests/test_throttling.py b/tests/test_throttling.py index cc36a004..50a53b3e 100644 --- a/tests/test_throttling.py +++ b/tests/test_throttling.py @@ -188,7 +188,9 @@ class ScopedRateThrottleTests(TestCase): class XYScopedRateThrottle(ScopedRateThrottle): TIMER_SECONDS = 0 THROTTLE_RATES = {'x': '3/min', 'y': '1/min'} - timer = lambda self: self.TIMER_SECONDS + + def timer(self): + return self.TIMER_SECONDS class XView(APIView): throttle_classes = (XYScopedRateThrottle,) @@ -290,7 +292,9 @@ class XffTestingBase(TestCase): class Throttle(ScopedRateThrottle): THROTTLE_RATES = {'test_limit': '1/day'} TIMER_SECONDS = 0 - timer = lambda self: self.TIMER_SECONDS + + def timer(self): + return self.TIMER_SECONDS class View(APIView): throttle_classes = (Throttle,) diff --git a/tests/test_versioning.py b/tests/test_versioning.py index 553463d1..90ad8afd 100644 --- a/tests/test_versioning.py +++ b/tests/test_versioning.py @@ -32,8 +32,13 @@ class RequestInvalidVersionView(APIView): factory = APIRequestFactory() -dummy_view = lambda request: None -dummy_pk_view = lambda request, pk: None + +def dummy_view(request): + pass + + +def dummy_pk_view(request, pk): + pass class TestRequestVersion: |
