Switch to pytest

This commit is contained in:
Yohan Boniface 2016-08-20 12:32:23 +02:00
parent 1a54df5a3d
commit 805bca7a97
5 changed files with 76 additions and 65 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@ docs/_build
umap/remote_static umap/remote_static
.idea .idea
tmp/* tmp/*
.cache

2
Makefile Normal file
View file

@ -0,0 +1,2 @@
test:
py.test

2
pytest.ini Normal file
View file

@ -0,0 +1,2 @@
[pytest]
DJANGO_SETTINGS_MODULE=umap.settings.local

2
requirements-dev.txt Normal file
View file

@ -0,0 +1,2 @@
pytest
pytest-django

View file

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