* add tox for testing
* StrictTransportMiddleware deprecated

Squashed and rebased from pull request #22
This commit is contained in:
Emin Mastizada 2017-07-15 22:43:05 +03:00 коммит произвёл Paul McLanahan
Родитель 392213bb3a
Коммит 7b5ae5a6d5
9 изменённых файлов: 42 добавлений и 47 удалений

3
.gitignore поставляемый
Просмотреть файл

@ -8,3 +8,6 @@ pip-log.txt
*.egg-info
dist
.coverage
venv/
src/
.tox/

Просмотреть файл

@ -1,7 +1,7 @@
sudo: false
language: python
python:
- "2.6"
- "2.7"
install: pip install -r requirements.txt --use-mirrors
script: fab test
- "3.6"
install: pip install tox-travis
script: tox

Просмотреть файл

@ -1,6 +1,9 @@
from django.conf import settings
import mock
try:
from unittest import mock
except ImportError:
import mock
from nose.tools import eq_
from test_utils import RequestFactory
@ -24,7 +27,7 @@ def test_xff():
eq_('127.0.0.1', req.META['REMOTE_ADDR'])
@mock.patch.object(settings._wrapped, 'KNOWN_PROXIES', ['127.0.0.1'])
@mock.patch.object(settings, 'KNOWN_PROXIES', ['127.0.0.1'])
def test_xff_known():
req = get_req()
mw.process_request(req)
@ -36,7 +39,7 @@ def test_xff_known():
eq_('127.0.0.1', req.META['REMOTE_ADDR'])
@mock.patch.object(settings._wrapped, 'KNOWN_PROXIES',
@mock.patch.object(settings, 'KNOWN_PROXIES',
['127.0.0.1', '2.3.4.5'])
def test_xff_multiknown():
req = get_req()
@ -44,7 +47,7 @@ def test_xff_multiknown():
eq_('1.2.3.4', req.META['REMOTE_ADDR'])
@mock.patch.object(settings._wrapped, 'KNOWN_PROXIES', ['127.0.0.1'])
@mock.patch.object(settings, 'KNOWN_PROXIES', ['127.0.0.1'])
def test_xff_bad_address():
req = get_req()
req.META['HTTP_X_FORWARDED_FOR'] += ',foobar'
@ -52,7 +55,7 @@ def test_xff_bad_address():
eq_('2.3.4.5', req.META['REMOTE_ADDR'])
@mock.patch.object(settings._wrapped, 'KNOWN_PROXIES',
@mock.patch.object(settings, 'KNOWN_PROXIES',
['127.0.0.1', '2.3.4.5'])
def test_xff_all_known():
"""If all the remotes are known, use the last one."""

Просмотреть файл

@ -41,21 +41,11 @@ class RobotsTagHeader(object):
class StrictTransportMiddleware(object):
"""
Set the Strict-Transport-Security header on responses. Use the
STS_MAX_AGE setting to control the max-age value. (Default: 1 year.)
Use the STS_SUBDOMAINS boolean to add includeSubdomains.
(Default: False.)
DEPRECATED: https://docs.djangoproject.com/en/1.8/ref/middleware/#security-middleware
"""
def process_response(self, request, response):
if request.is_secure():
age = getattr(settings, 'STS_MAX_AGE', 31536000) # 365 days.
subdomains = getattr(settings, 'STS_SUBDOMAINS', False)
val = 'max-age=%d' % age
if subdomains:
val += '; includeSubDomains'
response['Strict-Transport-Security'] = val
return response
def __init__(self):
raise DeprecationWarning("https://docs.djangoproject.com/en/1.8/ref/middleware/#security-middleware")
class XSSProtectionHeader(object):

Просмотреть файл

@ -2,7 +2,10 @@ from django.conf import settings
from django.http import HttpResponse
from django.test.client import RequestFactory
import mock
try:
from unittest import mock
except ImportError:
import mock
from nose.tools import eq_
from commonware.response import decorators, middleware
@ -30,21 +33,6 @@ def _make_resp(mw_cls, secure=False):
return resp
def test_sts_middleware():
resp = _make_resp(middleware.StrictTransportMiddleware)
assert 'Strict-Transport-Security' not in resp
resp = _make_resp(middleware.StrictTransportMiddleware, secure=True)
assert 'Strict-Transport-Security' in resp
eq_('max-age=31536000', resp['Strict-Transport-Security'])
@mock.patch.object(settings._wrapped, 'STS_SUBDOMAINS', True)
def test_sts_middleware_subdomains():
resp = _make_resp(middleware.StrictTransportMiddleware, secure=True)
assert 'Strict-Transport-Security' in resp
assert resp['Strict-Transport-Security'].endswith('includeSubDomains')
def test_xframe_middleware():
resp = _make_resp(middleware.FrameOptionsHeader)
assert 'X-Frame-Options' in resp

Просмотреть файл

@ -2,16 +2,16 @@
"name": "Commonware",
"description": "A collection of small but useful tools for Django.",
"repository": {
"url": "https://github.com/jsocol/commonware",
"url": "https://github.com/mozilla/commonware",
"license": "BSD 3-Clause"
},
"participate": {
"home": "https://github.com/jsocol/commonware",
"docs": "https://github.com/jsocol/commonware"
"home": "https://github.com/mozilla/commonware",
"docs": "https://github.com/mozilla/commonware"
},
"bugs": {
"list": "https://github.com/jsocol/commonware/issues",
"report": "https://github.com/jsocol/commonware/issues/new"
"list": "https://github.com/mozilla/commonware/issues",
"report": "https://github.com/mozilla/commonware/issues/new"
},
"keywords": [
"django",

Просмотреть файл

@ -6,8 +6,6 @@ path = lambda *a: os.path.join(ROOT, *a)
JINJA_CONFIG = {}
STS_SUBDOMAINS = False
KNOWN_PROXIES = []
SECRET_KEY = 'not so secret key for testing'

Просмотреть файл

@ -1,7 +1,6 @@
# These are required to run the tests.
nose
mock
fabric
Django<1.7
-e git+git://github.com/jbalogh/test-utils.git#egg=test_utils
fabric3
-e git+git://github.com/mastizada/test-utils.git#egg=test_utils
-e git+git://github.com/jbalogh/django-nose.git#egg=django_nose

14
tox.ini Normal file
Просмотреть файл

@ -0,0 +1,14 @@
[tox]
envlist =
py27-django{18}-extra,
py36-django{18,19,110,111}-extra,
[testenv]
commands = fab test
deps =
django18: Django>=1.8,<1.9
django19: Django>=1.9,<1.10
django110: Django>=1.10,<1.11
django111: Django>=1.11,<1.12
extra: -rrequirements.txt