зеркало из https://github.com/mozilla/commonware.git
python3 improvements
* add tox for testing * StrictTransportMiddleware deprecated Squashed and rebased from pull request #22
This commit is contained in:
Родитель
392213bb3a
Коммит
7b5ae5a6d5
|
@ -8,3 +8,6 @@ pip-log.txt
|
||||||
*.egg-info
|
*.egg-info
|
||||||
dist
|
dist
|
||||||
.coverage
|
.coverage
|
||||||
|
venv/
|
||||||
|
src/
|
||||||
|
.tox/
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
sudo: false
|
sudo: false
|
||||||
language: python
|
language: python
|
||||||
python:
|
python:
|
||||||
- "2.6"
|
|
||||||
- "2.7"
|
- "2.7"
|
||||||
install: pip install -r requirements.txt --use-mirrors
|
- "3.6"
|
||||||
script: fab test
|
install: pip install tox-travis
|
||||||
|
script: tox
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
import mock
|
try:
|
||||||
|
from unittest import mock
|
||||||
|
except ImportError:
|
||||||
|
import mock
|
||||||
from nose.tools import eq_
|
from nose.tools import eq_
|
||||||
from test_utils import RequestFactory
|
from test_utils import RequestFactory
|
||||||
|
|
||||||
|
@ -24,7 +27,7 @@ def test_xff():
|
||||||
eq_('127.0.0.1', req.META['REMOTE_ADDR'])
|
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():
|
def test_xff_known():
|
||||||
req = get_req()
|
req = get_req()
|
||||||
mw.process_request(req)
|
mw.process_request(req)
|
||||||
|
@ -36,7 +39,7 @@ def test_xff_known():
|
||||||
eq_('127.0.0.1', req.META['REMOTE_ADDR'])
|
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'])
|
['127.0.0.1', '2.3.4.5'])
|
||||||
def test_xff_multiknown():
|
def test_xff_multiknown():
|
||||||
req = get_req()
|
req = get_req()
|
||||||
|
@ -44,7 +47,7 @@ def test_xff_multiknown():
|
||||||
eq_('1.2.3.4', req.META['REMOTE_ADDR'])
|
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():
|
def test_xff_bad_address():
|
||||||
req = get_req()
|
req = get_req()
|
||||||
req.META['HTTP_X_FORWARDED_FOR'] += ',foobar'
|
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'])
|
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'])
|
['127.0.0.1', '2.3.4.5'])
|
||||||
def test_xff_all_known():
|
def test_xff_all_known():
|
||||||
"""If all the remotes are known, use the last one."""
|
"""If all the remotes are known, use the last one."""
|
||||||
|
|
|
@ -41,21 +41,11 @@ class RobotsTagHeader(object):
|
||||||
|
|
||||||
class StrictTransportMiddleware(object):
|
class StrictTransportMiddleware(object):
|
||||||
"""
|
"""
|
||||||
Set the Strict-Transport-Security header on responses. Use the
|
DEPRECATED: https://docs.djangoproject.com/en/1.8/ref/middleware/#security-middleware
|
||||||
STS_MAX_AGE setting to control the max-age value. (Default: 1 year.)
|
|
||||||
Use the STS_SUBDOMAINS boolean to add includeSubdomains.
|
|
||||||
(Default: False.)
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def process_response(self, request, response):
|
def __init__(self):
|
||||||
if request.is_secure():
|
raise DeprecationWarning("https://docs.djangoproject.com/en/1.8/ref/middleware/#security-middleware")
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class XSSProtectionHeader(object):
|
class XSSProtectionHeader(object):
|
||||||
|
|
|
@ -2,7 +2,10 @@ from django.conf import settings
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.test.client import RequestFactory
|
from django.test.client import RequestFactory
|
||||||
|
|
||||||
import mock
|
try:
|
||||||
|
from unittest import mock
|
||||||
|
except ImportError:
|
||||||
|
import mock
|
||||||
from nose.tools import eq_
|
from nose.tools import eq_
|
||||||
|
|
||||||
from commonware.response import decorators, middleware
|
from commonware.response import decorators, middleware
|
||||||
|
@ -30,21 +33,6 @@ def _make_resp(mw_cls, secure=False):
|
||||||
return resp
|
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():
|
def test_xframe_middleware():
|
||||||
resp = _make_resp(middleware.FrameOptionsHeader)
|
resp = _make_resp(middleware.FrameOptionsHeader)
|
||||||
assert 'X-Frame-Options' in resp
|
assert 'X-Frame-Options' in resp
|
||||||
|
|
|
@ -2,16 +2,16 @@
|
||||||
"name": "Commonware",
|
"name": "Commonware",
|
||||||
"description": "A collection of small but useful tools for Django.",
|
"description": "A collection of small but useful tools for Django.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"url": "https://github.com/jsocol/commonware",
|
"url": "https://github.com/mozilla/commonware",
|
||||||
"license": "BSD 3-Clause"
|
"license": "BSD 3-Clause"
|
||||||
},
|
},
|
||||||
"participate": {
|
"participate": {
|
||||||
"home": "https://github.com/jsocol/commonware",
|
"home": "https://github.com/mozilla/commonware",
|
||||||
"docs": "https://github.com/jsocol/commonware"
|
"docs": "https://github.com/mozilla/commonware"
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"list": "https://github.com/jsocol/commonware/issues",
|
"list": "https://github.com/mozilla/commonware/issues",
|
||||||
"report": "https://github.com/jsocol/commonware/issues/new"
|
"report": "https://github.com/mozilla/commonware/issues/new"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"django",
|
"django",
|
||||||
|
|
|
@ -6,8 +6,6 @@ path = lambda *a: os.path.join(ROOT, *a)
|
||||||
|
|
||||||
JINJA_CONFIG = {}
|
JINJA_CONFIG = {}
|
||||||
|
|
||||||
STS_SUBDOMAINS = False
|
|
||||||
|
|
||||||
KNOWN_PROXIES = []
|
KNOWN_PROXIES = []
|
||||||
|
|
||||||
SECRET_KEY = 'not so secret key for testing'
|
SECRET_KEY = 'not so secret key for testing'
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# These are required to run the tests.
|
# These are required to run the tests.
|
||||||
nose
|
nose
|
||||||
mock
|
mock
|
||||||
fabric
|
fabric3
|
||||||
Django<1.7
|
-e git+git://github.com/mastizada/test-utils.git#egg=test_utils
|
||||||
-e git+git://github.com/jbalogh/test-utils.git#egg=test_utils
|
|
||||||
-e git+git://github.com/jbalogh/django-nose.git#egg=django_nose
|
-e git+git://github.com/jbalogh/django-nose.git#egg=django_nose
|
||||||
|
|
|
@ -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
|
Загрузка…
Ссылка в новой задаче