Switch to pytest
This commit is contained in:
parent
1a54df5a3d
commit
805bca7a97
5 changed files with 76 additions and 65 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -6,3 +6,4 @@ docs/_build
|
||||||
umap/remote_static
|
umap/remote_static
|
||||||
.idea
|
.idea
|
||||||
tmp/*
|
tmp/*
|
||||||
|
.cache
|
||||||
|
|
2
Makefile
Normal file
2
Makefile
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
test:
|
||||||
|
py.test
|
2
pytest.ini
Normal file
2
pytest.ini
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
[pytest]
|
||||||
|
DJANGO_SETTINGS_MODULE=umap.settings.local
|
2
requirements-dev.txt
Normal file
2
requirements-dev.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
pytest
|
||||||
|
pytest-django
|
|
@ -3,75 +3,79 @@ import socket
|
||||||
from django.test import TestCase, RequestFactory
|
from django.test import TestCase, RequestFactory
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
|
import pytest
|
||||||
|
|
||||||
from umap.views import validate_url
|
from umap.views import validate_url
|
||||||
|
|
||||||
|
|
||||||
class TestsValidateProxyURL(TestCase):
|
def get(target="http://osm.org/georss.xml", verb="get", **kwargs):
|
||||||
|
defaults = {
|
||||||
def buildRequest(self, target="http://osm.org/georss.xml", verb="get",
|
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
|
||||||
**kwargs):
|
'HTTP_REFERER': '%s/path/' % settings.SITE_URL
|
||||||
defaults = {
|
}
|
||||||
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
|
defaults.update(kwargs)
|
||||||
'HTTP_REFERER': '%s/path/' % settings.SITE_URL
|
func = getattr(RequestFactory(**defaults), verb)
|
||||||
}
|
return func('/', {'url': target})
|
||||||
defaults.update(kwargs)
|
|
||||||
func = getattr(RequestFactory(**defaults), verb)
|
|
||||||
return func('/', {'url': target})
|
|
||||||
|
|
||||||
def test_good_request_passes(self):
|
|
||||||
target = "http://osm.org/georss.xml"
|
|
||||||
request = self.buildRequest(target)
|
|
||||||
url = validate_url(request)
|
|
||||||
self.assertEquals(url, target)
|
|
||||||
|
|
||||||
def test_no_url_raises(self):
|
|
||||||
request = self.buildRequest("")
|
|
||||||
with self.assertRaises(AssertionError):
|
|
||||||
validate_url(request)
|
|
||||||
|
|
||||||
def test_relative_url_raises(self):
|
|
||||||
request = self.buildRequest("/just/a/path/")
|
|
||||||
with self.assertRaises(AssertionError):
|
|
||||||
validate_url(request)
|
|
||||||
|
|
||||||
def test_file_uri_raises(self):
|
|
||||||
request = self.buildRequest("file:///etc/passwd")
|
|
||||||
with self.assertRaises(AssertionError):
|
|
||||||
validate_url(request)
|
|
||||||
|
|
||||||
def test_localhost_raises(self):
|
|
||||||
request = self.buildRequest("http://localhost/path/")
|
|
||||||
with self.assertRaises(AssertionError):
|
|
||||||
validate_url(request)
|
|
||||||
|
|
||||||
def test_local_IP_raises(self):
|
|
||||||
url = "http://{}/path/".format(socket.gethostname())
|
|
||||||
request = self.buildRequest(url)
|
|
||||||
with self.assertRaises(AssertionError):
|
|
||||||
validate_url(request)
|
|
||||||
|
|
||||||
def test_POST_raises(self):
|
|
||||||
request = self.buildRequest(verb="post")
|
|
||||||
with self.assertRaises(AssertionError):
|
|
||||||
validate_url(request)
|
|
||||||
|
|
||||||
def test_unkown_domain_raises(self):
|
|
||||||
request = self.buildRequest("http://xlkjdkjsdlkjfd.com")
|
|
||||||
with self.assertRaises(AssertionError):
|
|
||||||
validate_url(request)
|
|
||||||
|
|
||||||
|
|
||||||
class TestsProxy(TestCase):
|
def test_good_request_passes():
|
||||||
|
target = "http://osm.org/georss.xml"
|
||||||
|
request = get(target)
|
||||||
|
url = validate_url(request)
|
||||||
|
assert url == target
|
||||||
|
|
||||||
def test_valid_proxy_request(self):
|
|
||||||
url = reverse('ajax-proxy')
|
def test_no_url_raises():
|
||||||
params = {'url': 'http://example.org'}
|
request = get("")
|
||||||
headers = {
|
with pytest.raises(AssertionError):
|
||||||
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
|
validate_url(request)
|
||||||
'HTTP_REFERER': settings.SITE_URL
|
|
||||||
}
|
|
||||||
response = self.client.get(url, params, **headers)
|
def test_relative_url_raises():
|
||||||
self.assertEquals(response.status_code, 200)
|
request = get("/just/a/path/")
|
||||||
self.assertIn('Example Domain', response.content.decode())
|
with pytest.raises(AssertionError):
|
||||||
self.assertNotIn("Cookie", response['Vary'])
|
validate_url(request)
|
||||||
|
|
||||||
|
|
||||||
|
def test_file_uri_raises():
|
||||||
|
request = get("file:///etc/passwd")
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
validate_url(request)
|
||||||
|
|
||||||
|
|
||||||
|
def test_localhost_raises():
|
||||||
|
request = get("http://localhost/path/")
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
validate_url(request)
|
||||||
|
|
||||||
|
|
||||||
|
def test_local_IP_raises():
|
||||||
|
url = "http://{}/path/".format(socket.gethostname())
|
||||||
|
request = get(url)
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
validate_url(request)
|
||||||
|
|
||||||
|
|
||||||
|
def test_POST_raises():
|
||||||
|
request = get(verb="post")
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
validate_url(request)
|
||||||
|
|
||||||
|
|
||||||
|
def test_unkown_domain_raises():
|
||||||
|
request = get("http://xlkjdkjsdlkjfd.com")
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
validate_url(request)
|
||||||
|
|
||||||
|
|
||||||
|
def test_valid_proxy_request(client):
|
||||||
|
url = reverse('ajax-proxy')
|
||||||
|
params = {'url': 'http://example.org'}
|
||||||
|
headers = {
|
||||||
|
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
|
||||||
|
'HTTP_REFERER': settings.SITE_URL
|
||||||
|
}
|
||||||
|
response = client.get(url, params, **headers)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert 'Example Domain' in response.content.decode()
|
||||||
|
assert 'Cookie' not in response['Vary']
|
||||||
|
|
Loading…
Reference in a new issue