Skip to content

Commit

Permalink
Merge pull request #55 from OnroerendErfgoed/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
claeyswo authored Dec 7, 2020
2 parents 6d656c3 + fc99fc7 commit f40cb87
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 175 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: python
python:
- "2.7"
- "3.4"
- "3.5"
- "3.8"
env:
- LC_ALL=en_US.utf-8
install:
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.4.0 (07-12-2020)
------------------

- Py3 migratie (#53)

0.3.0 (12-04-2016)
------------------

Expand Down
18 changes: 7 additions & 11 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# Runtime requirements
--requirement requirements.txt

# Documentation
Sphinx==1.7.1
sphinxcontrib-httpdomain==1.6.1

waitress==1.1.0
pyramid_debugtoolbar==4.4
waitress==1.4.4
pyramid_debugtoolbar==4.9

#testing
pytest==3.4.2
pytest-cov==2.5.1
webtest==2.0.29
coveralls==1.3.0
pytest==6.1.2
pytest-cov==2.10.1
webtest==2.0.35
coveralls==2.2.0

# Wheel
wheel==0.30.0
wheel==0.36.1
5 changes: 2 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pyramid==1.9.2

PyYAML==3.13
pyramid==1.10.5
PyYAML==5.3.1
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
]

setup(name='urihandler',
version='0.3.0',
version='0.4.0',
description='A tiny application that handles (cool) uri\'s.',
long_description=README + '\n\n' + CHANGES,
classifiers=[
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Programming Language :: Python",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.8",
"Framework :: Pyramid",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
Expand Down
45 changes: 25 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,39 @@

from urihandler.handler import UriHandler


@pytest.fixture(scope="session")
def handlerconfig():
cfg = {
'uris': [
"uris": [
{
"match": r"^/foobar/(?P<id>\d+)$",
"mount": True,
"redirect": "http://localhost:5555/foobar/{id}",
},
{
"match": r"^/bar/(?P<name>\w+)$",
"redirect": "http://localhost:5555/bar/{name}",
},
{
"match": r"^urn:x-barbar:(?P<namespace>\w+):(?P<id>\d+)$",
"mount": False,
"redirect": "http://localhost:2222/{namespace}/{id}",
},
{
"match": r"override/(?P<namespace>\w+)/(?P<id>\d+)$",
"redirect": "http://localhost:2222/{namespace}/{id}",
},
{
'match': '^/foobar/(?P<id>\d+)$',
'mount': True,
'redirect': 'http://localhost:5555/foobar/{id}'
}, {
'match': '^/bar/(?P<name>\w+)$',
'redirect': 'http://localhost:5555/bar/{name}'
} , {
'match': '^urn:x-barbar:(?P<namespace>\w+):(?P<id>\d+)$',
'mount': False,
'redirect': 'http://localhost:2222/{namespace}/{id}'
}, {
'match': 'override/(?P<namespace>\w+)/(?P<id>\d+)$',
'redirect': 'http://localhost:2222/{namespace}/{id}'
}, {
'match': '^/foo/(?P<foo_id>\d+)/bar/(?P<bar_id>\d+)$',
'mount': True,
'redirect': 'http://localhost:5555/foo/{foo_id}/bar/{bar_id}'
}
"match": r"^/foo/(?P<foo_id>\d+)/bar/(?P<bar_id>\d+)$",
"mount": True,
"redirect": "http://localhost:5555/foo/{foo_id}/bar/{bar_id}",
},
]
}
return cfg


@pytest.fixture(scope="session")
def urihandler(handlerconfig):
return UriHandler(handlerconfig['uris'])
return UriHandler(handlerconfig["uris"])
84 changes: 43 additions & 41 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,79 @@
# -*- coding: utf-8 -*-
import os
import json

import pytest

from webtest import TestApp


@pytest.fixture()
def app():
settings = {
'urihandler.config': os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test.yaml')
"urihandler.config": os.path.join(
os.path.dirname(os.path.realpath(__file__)), "test.yaml"
)
}
from urihandler import main

return TestApp(main({}, **settings))

class TestFunctional:

class TestFunctional:
def test_redirect(self, app):
res = app.get('/foobar/18', status=303)
assert res.status == '303 See Other'
res = app.get("/foobar/18", status=303)
assert res.status == "303 See Other"

def test_redirect_no_match(self, app):
res = app.get('/test', status=404)
assert res.status == '404 Not Found'
res = app.get("/test", status=404)
assert res.status == "404 Not Found"

def test_handle(self, app):
res = app.get('/handle?uri=http://localhost/foobar/18', status=303)
assert res.status == '303 See Other'
res = app.get("/handle?uri=http://localhost/foobar/18", status=303)
assert res.status == "303 See Other"

def test_handle_no_match(self, app):
res = app.get('/handle?uri=http://id.erfgoed.net/foo/1', status=404)
assert res.status == '404 Not Found'
res = app.get("/handle?uri=http://id.erfgoed.net/foo/1", status=404)
assert res.status == "404 Not Found"

def test_handle_no_uri(self, app):
res = app.get('/handle?url=http://id.erfgoed.net/foo/1', status=400)
assert res.status == '400 Bad Request'
res = app.get("/handle?url=http://id.erfgoed.net/foo/1", status=400)
assert res.status == "400 Bad Request"

def test_uris(self, app):
res = app.get('/uris?uri=http://localhost/foobar/18')
assert res.status == '200 OK'
assert 'application/json' in res.headers['Content-Type']
data = json.loads(res.body.decode('utf-8'))
assert data['uri'] == 'http://localhost/foobar/18'
assert data['location'] == 'http://localhost:5555/foobar/18'
res = app.get("/uris?uri=http://localhost/foobar/18")
assert res.status == "200 OK"
assert "application/json" in res.headers["Content-Type"]
data = json.loads(res.body.decode("utf-8"))
assert data["uri"] == "http://localhost/foobar/18"
assert data["location"] == "http://localhost:5555/foobar/18"

def test_uris_no_match(self, app):
res = app.get('/uris?uri=http://id.erfgoed.net/foo/1')
assert res.status == '200 OK'
assert 'application/json' in res.headers['Content-Type']
data = json.loads(res.body.decode('utf-8'))
assert data['uri'] == 'http://id.erfgoed.net/foo/1'
assert data['location'] is None
assert not data['success']
res = app.get("/uris?uri=http://id.erfgoed.net/foo/1")
assert res.status == "200 OK"
assert "application/json" in res.headers["Content-Type"]
data = json.loads(res.body.decode("utf-8"))
assert data["uri"] == "http://id.erfgoed.net/foo/1"
assert data["location"] is None
assert not data["success"]

def test_uris_no_uri(self, app):
res = app.get('/uris?url=http://localhost/foobar/18', status=400)
assert res.status == '400 Bad Request'
res = app.get("/uris?url=http://localhost/foobar/18", status=400)
assert res.status == "400 Bad Request"

def test_uris_two_parameters(self, app):
res = app.get('/uris?uri=urn:x-barbar:area:51')
assert res.status == '200 OK'
assert 'application/json' in res.headers['Content-Type']
data = json.loads(res.body.decode('utf-8'))
assert data['uri'] == 'urn:x-barbar:area:51'
assert data['location'] == 'http://localhost:2222/area/51'
res = app.get("/uris?uri=urn:x-barbar:area:51")
assert res.status == "200 OK"
assert "application/json" in res.headers["Content-Type"]
data = json.loads(res.body.decode("utf-8"))
assert data["uri"] == "urn:x-barbar:area:51"
assert data["location"] == "http://localhost:2222/area/51"

def test_uris_caching(self, app):
res = app.get('/uris?uri=http://localhost/foobar/18')
assert 'Cache-Control' in res.headers
assert 'ETag' in res.headers
etag = res.headers['ETag']
res = app.get("/uris?uri=http://localhost/foobar/18")
assert "Cache-Control" in res.headers
assert "ETag" in res.headers
etag = res.headers["ETag"]
res2 = app.get(
'/uris?uri=http://localhost/foobar/18',
headers={'If-None-Match': etag}
"/uris?uri=http://localhost/foobar/18", headers={"If-None-Match": etag}
)
assert res2.status == '304 Not Modified'
assert res2.status == "304 Not Modified"
20 changes: 12 additions & 8 deletions tests/test_general.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import pytest

import os

from urihandler import (
_parse_settings,
_load_configuration
)

class TestGeneral:

class TestGeneral:
def test_parse_settings(self):
settings = {
'urihandler.config': os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test.yaml')
"urihandler.config": os.path.join(
os.path.dirname(os.path.realpath(__file__)), "test.yaml"
)
}
args = _parse_settings(settings)
assert 'config' in args
assert args['config'] == os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test.yaml')
assert "config" in args
assert args["config"] == os.path.join(
os.path.dirname(os.path.realpath(__file__)), "test.yaml"
)

def test_load_configuration(self):
cfg = _load_configuration(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test.yaml'))
assert 'uris' in cfg
cfg = _load_configuration(
os.path.join(os.path.dirname(os.path.realpath(__file__)), "test.yaml")
)
assert "uris" in cfg
40 changes: 21 additions & 19 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

import logging

logging.basicConfig(level=logging.DEBUG)

from pyramid import testing
Expand All @@ -16,47 +17,49 @@
get_uri_handler
)

class TestHandler:

class TestHandler:
def test_urihandler_exists(self, urihandler):
assert urihandler

def test_no_match(self, urihandler):
req = testing.DummyRequest()
req.host_url = 'http://test.urihandler.org'
res = urihandler.handle('http://test.urihandler.org/bunnies/koen', req)
req.host_url = "http://test.urihandler.org"
res = urihandler.handle("http://test.urihandler.org/bunnies/koen", req)
assert res is None

def test_mounted_redirect(self, urihandler):
req = testing.DummyRequest()
req.host_url = 'http://test.urihandler.org'
res = urihandler.handle('http://test.urihandler.org/foobar/18', req)
assert res == 'http://localhost:5555/foobar/18'
req.host_url = "http://test.urihandler.org"
res = urihandler.handle("http://test.urihandler.org/foobar/18", req)
assert res == "http://localhost:5555/foobar/18"

def test_unanchored_redirect(self, urihandler):
req = testing.DummyRequest()
req.host_url = 'http://test.urihandler.org'
res = urihandler.handle('http://test.urihandler.org/something/override/me/987', req)
assert res == 'http://localhost:2222/me/987'
req.host_url = "http://test.urihandler.org"
res = urihandler.handle(
"http://test.urihandler.org/something/override/me/987", req
)
assert res == "http://localhost:2222/me/987"

def test_urn_redirect(self, urihandler):
req = testing.DummyRequest()
res = urihandler.handle('urn:x-barbar:area:51', req)
assert res == 'http://localhost:2222/area/51'
res = urihandler.handle("urn:x-barbar:area:51", req)
assert res == "http://localhost:2222/area/51"

def test_two_matches(self, urihandler):
req = testing.DummyRequest()
req.host_url = 'http://test.urihandler.org'
res = urihandler.handle('http://test.urihandler.org/foo/6/bar/66', req)
assert res == 'http://localhost:5555/foo/6/bar/66'
req.host_url = "http://test.urihandler.org"
res = urihandler.handle("http://test.urihandler.org/foo/6/bar/66", req)
assert res == "http://localhost:5555/foo/6/bar/66"

class MockRegistry:

class MockRegistry:
def __init__(self, settings=None):

if settings is None:
self.settings = {}
else: # pragma NO COVER
else: # pragma NO COVER
self.settings = settings

self.uri_handler = None
Expand All @@ -69,17 +72,16 @@ def registerUtility(self, uri_handler, iface):


class TestGetAndBuild:

def test_get_uri_handler(self, handlerconfig):
r = MockRegistry()
UH = UriHandler(handlerconfig['uris'])
UH = UriHandler(handlerconfig["uris"])
r.registerUtility(UH, IUriHandler)
UH2 = get_uri_handler(r)
assert UH == UH2

def test_build_uri_handler_already_exists(self, handlerconfig):
r = MockRegistry()
UH = UriHandler(handlerconfig['uris'])
UH = UriHandler(handlerconfig["uris"])
r.registerUtility(UH, IUriHandler)
UH2 = _build_uri_handler(r, handlerconfig)
assert UH == UH2
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py27, py34, py35, cover
envlist = py38, cover

[testenv]
commands =
Expand All @@ -11,7 +11,7 @@ setenv =

[testenv:cover]
basepython =
python2.7
python3.8
commands =
pip install -r requirements-dev.txt
python setup.py develop
Expand Down
Loading

0 comments on commit f40cb87

Please sign in to comment.