From cbc24d47741a74ba2c0de33beef4b348dafa99bb Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Tue, 6 Feb 2018 16:12:36 +1100 Subject: [PATCH] Add support for tests that use django.test.Client and that require a database, and use this support to test some of the wellknowns.views modules --- lemoncurry/settings/test.py | 4 ++++ pytest.ini | 2 +- wellknowns/tests/__init__.py | 0 wellknowns/tests/views/__init__.py | 0 wellknowns/tests/views/host_meta.py | 26 ++++++++++++++++++++++++++ wellknowns/tests/views/static.py | 13 +++++++++++++ 6 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 lemoncurry/settings/test.py create mode 100644 wellknowns/tests/__init__.py create mode 100644 wellknowns/tests/views/__init__.py create mode 100644 wellknowns/tests/views/host_meta.py create mode 100644 wellknowns/tests/views/static.py diff --git a/lemoncurry/settings/test.py b/lemoncurry/settings/test.py new file mode 100644 index 0000000..2e94a81 --- /dev/null +++ b/lemoncurry/settings/test.py @@ -0,0 +1,4 @@ +from .base import * + +ALLOWED_HOSTS = ['*'] +SECURE_SSL_REDIRECT = False diff --git a/pytest.ini b/pytest.ini index 0f0251b..0df535e 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,3 @@ [pytest] -DJANGO_SETTINGS_MODULE = lemoncurry.settings.dev +DJANGO_SETTINGS_MODULE = lemoncurry.settings.test python_files = tests/*.py diff --git a/wellknowns/tests/__init__.py b/wellknowns/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wellknowns/tests/views/__init__.py b/wellknowns/tests/views/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wellknowns/tests/views/host_meta.py b/wellknowns/tests/views/host_meta.py new file mode 100644 index 0000000..e8e332f --- /dev/null +++ b/wellknowns/tests/views/host_meta.py @@ -0,0 +1,26 @@ +import json +from lxml import etree +import pytest + + +@pytest.mark.django_db +def test_host_meta_json(client): + res = client.get('/.well-known/host-meta.json') + assert res.status_code == 200 + assert res['Content-Type'] == 'application/json' + meta = json.loads(res.content) + assert meta.keys() == {'links', 'subject'} + assert meta['subject'] == 'https://example.com' + assert len(meta['links']) == 13 + + +@pytest.mark.django_db +def test_host_meta_xml(client): + res = client.get('/.well-known/host-meta') + assert res.status_code == 200 + assert res['Content-Type'] == 'application/xrd+xml' + root = etree.XML(res.content) + ns = '{http://docs.oasis-open.org/ns/xri/xrd-1.0}' + assert root.tag == (ns + 'XRD') + assert root.findtext(ns + 'Subject') == 'https://example.com' + assert len(root.findall(ns + 'Link')) == 13 diff --git a/wellknowns/tests/views/static.py b/wellknowns/tests/views/static.py new file mode 100644 index 0000000..eb7beb2 --- /dev/null +++ b/wellknowns/tests/views/static.py @@ -0,0 +1,13 @@ +from ...views import static + + +def test_redirect_to_static(rf): + res = static.redirect_to_static('abcd')(rf.get('/')) + assert res.status_code == 302 + assert res.url == '/static/wellknowns/abcd' + + +def test_keybase(rf): + res = static.keybase(rf.get('/.well-knowns/keybase.txt')) + assert res.status_code == 302 + assert res.url == '/static/wellknowns/keybase.txt'