-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_api_middleware.py
More file actions
55 lines (44 loc) · 1.78 KB
/
test_api_middleware.py
File metadata and controls
55 lines (44 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Unit tests for the Deis api app.
Run the tests with "./manage.py test api"
"""
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.test import TestCase
from rest_framework.authtoken.models import Token
from api import __version__
class APIMiddlewareTest(TestCase):
"""Tests middleware.py's business logic"""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = Token.objects.get(user=self.user).key
def test_x_deis_version_header_good(self):
"""
Test that when the version header is sent, the request is accepted.
"""
response = self.client.get(
'/v1/apps',
HTTP_X_DEIS_VERSION=__version__.rsplit('.', 2)[0],
HTTP_AUTHORIZATION='token {}'.format(self.token),
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.has_header('X_DEIS_API_VERSION'), True)
self.assertEqual(response['X_DEIS_API_VERSION'], __version__.rsplit('.', 1)[0])
def test_x_deis_version_header_bad(self):
"""
Test that when an improper version header is sent, the request is declined.
"""
response = self.client.get(
'/v1/apps',
HTTP_X_DEIS_VERSION='1234.5678',
HTTP_AUTHORIZATION='token {}'.format(self.token),
)
self.assertEqual(response.status_code, 405)
def test_x_deis_version_header_not_present(self):
"""
Test that when the version header is not present, the request is accepted.
"""
response = self.client.get('/v1/apps',
HTTP_AUTHORIZATION='token {}'.format(self.token))
self.assertEqual(response.status_code, 200)