Shortcut to run coveraged tests

This commit is contained in:
Martin Mahner 2015-08-12 06:56:34 +02:00
parent e50e1db73e
commit 516cea933f
3 changed files with 8 additions and 7 deletions

View file

@ -3,6 +3,7 @@ source = dpaste
branch = True branch = True
omit = omit =
dpaste/migrations/* dpaste/migrations/*
dpaste/south_migrations/*
dpaste/tests/* dpaste/tests/*
dpaste/settings/* dpaste/settings/*

View file

@ -6,7 +6,8 @@ tor_bl = (
'{remote_addr}.{server_port}.{server_ip}' '{remote_addr}.{server_port}.{server_ip}'
'.ip-port.exitlist.torproject.org') '.ip-port.exitlist.torproject.org')
open_proxy_bl = ('{remote_addr}.dnsbl.proxybl.org') open_proxy_bl = ('{remote_addr}.dnsbl.proxybl.org')
rev_ip = lambda ip: '.'.join(reversed(ip.split('.')))
rev_ip = lambda ip: '.'.join(reversed(ip.split('.'))) # pragma: no cover
response = """<html><body><h1>Access denied</h1> response = """<html><body><h1>Access denied</h1>
<p>It appears you're requesting this page from an open proxy or <p>It appears you're requesting this page from an open proxy or
@ -15,7 +16,7 @@ statutory violation related posts in the past.</p>
<p>If you think this is wrong, <a href="https://github.com/bartTC/dpaste">file <p>If you think this is wrong, <a href="https://github.com/bartTC/dpaste">file
a bug on Github please</a>.</p></body></html>""" a bug on Github please</a>.</p></body></html>"""
def in_blacklist(request, bl, ip=None): def in_blacklist(request, bl, ip=None): # pragma: no cover
ip = ip or request.META['REMOTE_ADDR'] ip = ip or request.META['REMOTE_ADDR']
try: try:
server_ip = socket.gethostbyname(request.META['SERVER_NAME']) server_ip = socket.gethostbyname(request.META['SERVER_NAME'])
@ -37,15 +38,13 @@ def in_blacklist(request, bl, ip=None):
return lookup == '127.0.0.2' return lookup == '127.0.0.2'
class SuspiciousIPMiddleware(object): class SuspiciousIPMiddleware(object): # pragma: no cover
def process_request(self, request): def process_request(self, request):
def check_tor(): def check_tor():
if not hasattr(request, '_is_tor_exit_node'): if not hasattr(request, '_is_tor_exit_node'):
request._is_tor_exit_node = in_blacklist(request, tor_bl) request._is_tor_exit_node = in_blacklist(request, tor_bl)
return request._is_tor_exit_node return request._is_tor_exit_node
request.is_tor_exit_node = check_tor request.is_tor_exit_node = check_tor
def check_open_proxy(): def check_open_proxy():
@ -53,13 +52,11 @@ class SuspiciousIPMiddleware(object):
request._is_open_proxy = in_blacklist( request._is_open_proxy = in_blacklist(
request, open_proxy_bl) request, open_proxy_bl)
return request._is_open_proxy return request._is_open_proxy
request.is_open_proxy = check_open_proxy request.is_open_proxy = check_open_proxy
def check_suspicious(): def check_suspicious():
return request.is_tor_exit_node() or request.is_open_proxy() return request.is_tor_exit_node() or request.is_open_proxy()
request.is_suspicious = check_suspicious request.is_suspicious = check_suspicious
if request.method == 'POST' and request.is_suspicious(): if request.method == 'POST' and request.is_suspicious():
return HttpResponseBadRequest(response) return HttpResponseBadRequest(response)

3
runtests.sh Normal file
View file

@ -0,0 +1,3 @@
coverage run --rcfile=./.coverage.rc runtests.py && \
coverage html --rcfile=./.coverage.rc