Skip to content

Commit 5a78f58

Browse files
author
Matthew Fisher
committed
test(controller): add django middleware tests
1 parent 6bb034c commit 5a78f58

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

controller/api/middleware.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import json
22

33
from django.http import HttpResponse
4+
from rest_framework import status
45

56
from deis import __version__
67

78

89
class VersionMiddleware:
910

1011
def process_request(self, request):
11-
# server and client version must match "x.y"
12-
client_version = request.META['HTTP_X_DEIS_VERSION']
13-
server_version = __version__.rsplit('.', 1)[0]
1412
try:
13+
# server and client version must match "x.y"
14+
client_version = request.META['HTTP_X_DEIS_VERSION']
15+
server_version = __version__.rsplit('.', 1)[0]
1516
if client_version != server_version:
1617
message = {
1718
'error': 'Client and server versions do not match.\n' +
@@ -20,7 +21,8 @@ def process_request(self, request):
2021
}
2122
return HttpResponse(
2223
json.dumps(message),
23-
content_type='application/json'
24+
content_type='application/json',
25+
status=status.HTTP_405_METHOD_NOT_ALLOWED
2426
)
2527
except KeyError:
2628
pass

controller/api/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
4141
test_labels, extra_tests, **kwargs)
4242

4343

44+
from .test_api_middleware import * # noqa
4445
from .test_app import * # noqa
4546
from .test_auth import * # noqa
4647
from .test_build import * # noqa
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.test import TestCase
9+
10+
from deis import __version__
11+
12+
13+
class APIMiddlewareTest(TestCase):
14+
15+
"""Tests middleware.py's business logic"""
16+
17+
fixtures = ['tests.json']
18+
19+
def setUp(self):
20+
self.assertTrue(
21+
self.client.login(username='autotest', password='password'))
22+
23+
def test_x_deis_version_header_good(self):
24+
"""
25+
Test that when the version header is sent, the request is accepted.
26+
"""
27+
response = self.client.get(
28+
'/api/apps',
29+
HTTP_X_DEIS_VERSION=__version__.rsplit('.', 1)[0]
30+
)
31+
self.assertEqual(response.status_code, 200)
32+
33+
def test_x_deis_version_header_bad(self):
34+
"""
35+
Test that when an improper version header is sent, the request is declined.
36+
"""
37+
response = self.client.get(
38+
'/api/apps',
39+
HTTP_X_DEIS_VERSION='1234.5678'
40+
)
41+
self.assertEqual(response.status_code, 405)
42+
43+
def test_x_deis_version_header_not_present(self):
44+
"""
45+
Test that when the version header is not present, the request is accepted.
46+
"""
47+
response = self.client.get('/api/apps')
48+
self.assertEqual(response.status_code, 200)

0 commit comments

Comments
 (0)