Browse Source

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

Danielle McLean 1 year ago
parent
commit
cbc24d4774
Signed by: Danielle McLean <dani@00dani.me> GPG Key ID: 5A5D2D1AFF12EEC5

+ 4
- 0
lemoncurry/settings/test.py View File

@@ -0,0 +1,4 @@
1
+from .base import *
2
+
3
+ALLOWED_HOSTS = ['*']
4
+SECURE_SSL_REDIRECT = False

+ 1
- 1
pytest.ini View File

@@ -1,3 +1,3 @@
1 1
 [pytest]
2
-DJANGO_SETTINGS_MODULE = lemoncurry.settings.dev
2
+DJANGO_SETTINGS_MODULE = lemoncurry.settings.test
3 3
 python_files = tests/*.py

+ 0
- 0
wellknowns/tests/__init__.py View File


+ 0
- 0
wellknowns/tests/views/__init__.py View File


+ 26
- 0
wellknowns/tests/views/host_meta.py View File

@@ -0,0 +1,26 @@
1
+import json
2
+from lxml import etree
3
+import pytest
4
+
5
+
6
+@pytest.mark.django_db
7
+def test_host_meta_json(client):
8
+    res = client.get('/.well-known/host-meta.json')
9
+    assert res.status_code == 200
10
+    assert res['Content-Type'] == 'application/json'
11
+    meta = json.loads(res.content)
12
+    assert meta.keys() == {'links', 'subject'}
13
+    assert meta['subject'] == 'https://example.com'
14
+    assert len(meta['links']) == 13
15
+
16
+
17
+@pytest.mark.django_db
18
+def test_host_meta_xml(client):
19
+    res = client.get('/.well-known/host-meta')
20
+    assert res.status_code == 200
21
+    assert res['Content-Type'] == 'application/xrd+xml'
22
+    root = etree.XML(res.content)
23
+    ns = '{http://docs.oasis-open.org/ns/xri/xrd-1.0}'
24
+    assert root.tag == (ns + 'XRD')
25
+    assert root.findtext(ns + 'Subject') == 'https://example.com'
26
+    assert len(root.findall(ns + 'Link')) == 13

+ 13
- 0
wellknowns/tests/views/static.py View File

@@ -0,0 +1,13 @@
1
+from ...views import static
2
+
3
+
4
+def test_redirect_to_static(rf):
5
+    res = static.redirect_to_static('abcd')(rf.get('/'))
6
+    assert res.status_code == 302
7
+    assert res.url == '/static/wellknowns/abcd'
8
+
9
+
10
+def test_keybase(rf):
11
+    res = static.keybase(rf.get('/.well-knowns/keybase.txt'))
12
+    assert res.status_code == 302
13
+    assert res.url == '/static/wellknowns/keybase.txt'

Loading…
Cancel
Save