github.com/inflatablewoman/deis@v1.0.1-0.20141111034523-a4511c46a6ce/controller/api/tests/test_api_middleware.py (about) 1 """ 2 Unit tests for the Deis api app. 3 4 Run the tests with "./manage.py test api" 5 """ 6 from __future__ import unicode_literals 7 8 from django.contrib.auth.models import User 9 from django.test import TestCase 10 from rest_framework.authtoken.models import Token 11 12 from deis import __version__ 13 14 15 class APIMiddlewareTest(TestCase): 16 17 """Tests middleware.py's business logic""" 18 19 fixtures = ['tests.json'] 20 21 def setUp(self): 22 self.user = User.objects.get(username='autotest') 23 self.token = Token.objects.get(user=self.user).key 24 25 def test_x_deis_version_header_good(self): 26 """ 27 Test that when the version header is sent, the request is accepted. 28 """ 29 response = self.client.get( 30 '/v1/apps', 31 HTTP_X_DEIS_VERSION=__version__.rsplit('.', 1)[0], 32 HTTP_AUTHORIZATION='token {}'.format(self.token), 33 ) 34 self.assertEqual(response.status_code, 200) 35 36 def test_x_deis_version_header_bad(self): 37 """ 38 Test that when an improper version header is sent, the request is declined. 39 """ 40 response = self.client.get( 41 '/v1/apps', 42 HTTP_X_DEIS_VERSION='1234.5678', 43 HTTP_AUTHORIZATION='token {}'.format(self.token), 44 ) 45 self.assertEqual(response.status_code, 405) 46 47 def test_x_deis_version_header_not_present(self): 48 """ 49 Test that when the version header is not present, the request is accepted. 50 """ 51 response = self.client.get('/v1/apps', 52 HTTP_AUTHORIZATION='token {}'.format(self.token)) 53 self.assertEqual(response.status_code, 200)