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
|
||||
.idea
|
||||
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,14 +3,12 @@ import socket
|
|||
from django.test import TestCase, RequestFactory
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import reverse
|
||||
import pytest
|
||||
|
||||
from umap.views import validate_url
|
||||
|
||||
|
||||
class TestsValidateProxyURL(TestCase):
|
||||
|
||||
def buildRequest(self, target="http://osm.org/georss.xml", verb="get",
|
||||
**kwargs):
|
||||
def get(target="http://osm.org/georss.xml", verb="get", **kwargs):
|
||||
defaults = {
|
||||
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
|
||||
'HTTP_REFERER': '%s/path/' % settings.SITE_URL
|
||||
|
@ -19,59 +17,65 @@ class TestsValidateProxyURL(TestCase):
|
|||
func = getattr(RequestFactory(**defaults), verb)
|
||||
return func('/', {'url': target})
|
||||
|
||||
def test_good_request_passes(self):
|
||||
|
||||
def test_good_request_passes():
|
||||
target = "http://osm.org/georss.xml"
|
||||
request = self.buildRequest(target)
|
||||
request = get(target)
|
||||
url = validate_url(request)
|
||||
self.assertEquals(url, target)
|
||||
assert url == target
|
||||
|
||||
def test_no_url_raises(self):
|
||||
request = self.buildRequest("")
|
||||
with self.assertRaises(AssertionError):
|
||||
|
||||
def test_no_url_raises():
|
||||
request = get("")
|
||||
with pytest.raises(AssertionError):
|
||||
validate_url(request)
|
||||
|
||||
def test_relative_url_raises(self):
|
||||
request = self.buildRequest("/just/a/path/")
|
||||
with self.assertRaises(AssertionError):
|
||||
|
||||
def test_relative_url_raises():
|
||||
request = get("/just/a/path/")
|
||||
with pytest.raises(AssertionError):
|
||||
validate_url(request)
|
||||
|
||||
def test_file_uri_raises(self):
|
||||
request = self.buildRequest("file:///etc/passwd")
|
||||
with self.assertRaises(AssertionError):
|
||||
|
||||
def test_file_uri_raises():
|
||||
request = get("file:///etc/passwd")
|
||||
with pytest.raises(AssertionError):
|
||||
validate_url(request)
|
||||
|
||||
def test_localhost_raises(self):
|
||||
request = self.buildRequest("http://localhost/path/")
|
||||
with self.assertRaises(AssertionError):
|
||||
|
||||
def test_localhost_raises():
|
||||
request = get("http://localhost/path/")
|
||||
with pytest.raises(AssertionError):
|
||||
validate_url(request)
|
||||
|
||||
def test_local_IP_raises(self):
|
||||
|
||||
def test_local_IP_raises():
|
||||
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):
|
||||
request = get(url)
|
||||
with pytest.raises(AssertionError):
|
||||
validate_url(request)
|
||||
|
||||
|
||||
class TestsProxy(TestCase):
|
||||
def test_POST_raises():
|
||||
request = get(verb="post")
|
||||
with pytest.raises(AssertionError):
|
||||
validate_url(request)
|
||||
|
||||
def test_valid_proxy_request(self):
|
||||
|
||||
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 = self.client.get(url, params, **headers)
|
||||
self.assertEquals(response.status_code, 200)
|
||||
self.assertIn('Example Domain', response.content.decode())
|
||||
self.assertNotIn("Cookie", response['Vary'])
|
||||
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