github.com/amrnt/deis@v1.3.1/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 api 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('.', 2)[0], 32 HTTP_AUTHORIZATION='token {}'.format(self.token), 33 ) 34 self.assertEqual(response.status_code, 200) 35 self.assertEqual(response.has_header('X_DEIS_API_VERSION'), True) 36 self.assertEqual(response['X_DEIS_API_VERSION'], __version__.rsplit('.', 1)[0]) 37 38 def test_x_deis_version_header_bad(self): 39 """ 40 Test that when an improper version header is sent, the request is declined. 41 """ 42 response = self.client.get( 43 '/v1/apps', 44 HTTP_X_DEIS_VERSION='1234.5678', 45 HTTP_AUTHORIZATION='token {}'.format(self.token), 46 ) 47 self.assertEqual(response.status_code, 405) 48 49 def test_x_deis_version_header_not_present(self): 50 """ 51 Test that when the version header is not present, the request is accepted. 52 """ 53 response = self.client.get('/v1/apps', 54 HTTP_AUTHORIZATION='token {}'.format(self.token)) 55 self.assertEqual(response.status_code, 200)