Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
test(controller): add django middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Fisher committed Jun 20, 2014
1 parent d66e7d1 commit 47cce0d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
10 changes: 6 additions & 4 deletions controller/api/middleware.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import json

from django.http import HttpResponse
from rest_framework import status

from deis import __version__


class VersionMiddleware:

def process_request(self, request):
# server and client version must match "x.y"
client_version = request.META['HTTP_X_DEIS_VERSION']
server_version = __version__.rsplit('.', 1)[0]
try:
# server and client version must match "x.y"
client_version = request.META['HTTP_X_DEIS_VERSION']
server_version = __version__.rsplit('.', 1)[0]
if client_version != server_version:
message = {
'error': 'Client and server versions do not match.\n' +
Expand All @@ -20,7 +21,8 @@ def process_request(self, request):
}
return HttpResponse(
json.dumps(message),
content_type='application/json'
content_type='application/json',
status=status.HTTP_405_METHOD_NOT_ALLOWED
)
except KeyError:
pass
1 change: 1 addition & 0 deletions controller/api/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
test_labels, extra_tests, **kwargs)


from .test_api_middleware import * # noqa
from .test_app import * # noqa
from .test_auth import * # noqa
from .test_build import * # noqa
Expand Down
48 changes: 48 additions & 0 deletions controller/api/tests/test_api_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Unit tests for the Deis api app.
Run the tests with "./manage.py test api"
"""
from __future__ import unicode_literals

from django.test import TestCase

from deis import __version__


class APIMiddlewareTest(TestCase):

"""Tests middleware.py's business logic"""

fixtures = ['tests.json']

def setUp(self):
self.assertTrue(
self.client.login(username='autotest', password='password'))

def test_x_deis_version_header_good(self):
"""
Test that when the version header is sent, the request is accepted.
"""
response = self.client.get(
'/api/apps',
HTTP_X_DEIS_VERSION=__version__.rsplit('.', 1)[0]
)
self.assertEqual(response.status_code, 200)

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(
'/api/apps',
HTTP_X_DEIS_VERSION='1234.5678'
)
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('/api/apps')
self.assertEqual(response.status_code, 200)

0 comments on commit 47cce0d

Please sign in to comment.