Tests for fastcgi; they all fail because I haven't implemented half of it

marnanel-wip
Marnanel Thurman 2023-10-29 18:31:13 +00:00
rodzic 2858cddec3
commit 45ca27e430
1 zmienionych plików z 171 dodań i 5 usunięć

Wyświetl plik

@ -1,14 +1,180 @@
# Many of these tests are based on tests from busby, and we should
# refactor those when the dust settles.
from kepi.fastcgi import despatch
from test import *
import logging
import email
import json
logger = logging.getLogger(name='kepi')
HTML_MIMETYPE = 'text/html'
HOST_META_URI = '/.well-known/host-meta'
HOST_META_MIMETYPE = 'application/xrd+xml'
NODEINFO_PART_1_URI = 'http://testserver/.well-known/nodeinfo'
NODEINFO_PART_2_URI = 'http://testserver/nodeinfo.json'
NODEINFO_MIMETYPE = (
'application/json; '
'profile=http://nodeinfo.diaspora.software/ns/schema/2.0#'
)
WEBFINGER_BASE_URI = 'https://altair.example.com/.well-known/webfinger'
WEBFINGER_URI = WEBFINGER_BASE_URI + '?resource={}'
WEBFINGER_MIMETYPE = 'application/jrd+json; charset=utf-8'
def call_despatch_and_parse_result(*args, **kwargs):
found = despatch(*args, **kwargs)
result = email.message_from_string(found)
return result
def test_fastcgi_simple():
found = despatch(
found = call_despatch_and_parse_result(
env = {
'DOCUMENT_URI': '',
'DOCUMENT_URI': '/this-is-not-a-real-URI',
},
)
assert found=="""Content-Type: text/html
Status: 404 Not found
assert found['Status'].startswith('404 ')
assert found['Content-Type']==HTML_MIMETYPE
def test_fastcgi_host_meta():
found = call_despatch_and_parse_result(
env = {
'DOCUMENT_URI': HOST_META_URI,
'ACCEPT': HOST_META_MIMETYPE,
},
)
assert found['Status'].startswith('200 ')
assert found['Content-Type']==HOST_META_MIMETYPE
assert (
# Not an f-string; the curly brackets are part of the string
"/.well-known/webfinger?resource={uri}",
) in found.body
def test_nodeinfo_part_1(self):
found = call_despatch_and_parse_result(
env = {
'DOCUMENT_URI': NODEINFO_PART_1_URI,
'ACCEPT': NODEINFO_MIMETYPE,
},
)
assert found['Status'].startswith('200 ')
assert found['Content-Type']==NODEINFO_MIMETYPE
response = json.loads(response.body)
assert 'links' in response
assert (
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
"href": NODEINFO_PART_2_URI,
},
response['links'],
) in response
def test_part_2(self):
found = call_despatch_and_parse_result(
env = {
'DOCUMENT_URI': NODEINFO_PART_1_URI,
'ACCEPT': NODEINFO_MIMETYPE,
},
)
assert found['Status'].startswith('200 ')
assert found['Content-Type']==NODEINFO_MIMETYPE
response = json.loads(response.body)
assert response['version']=='2.0'
assert response['software']['name']=='kepi'
assert 'activitypub' in response['software']['protocols']
That resource does not exist here.
"""
From the original: we need to do this before the tests which get 200
def setUp(self):
keys = json.load(open('kepi/bowler_pub/tests/keys/keys-0001.json', 'r'))
create_local_person(
name='alice',
publicKey=keys['public'],
privateKey=keys['private'],
)
self._alice_keys = keys
settings.ALLOWED_HOSTS = [
'altair.example.com',
'testserver',
]
settings.KEPI['LOCAL_OBJECT_HOSTNAME'] = 'testserver'
"""
def test_fastcgi_webfinger():
def webfinger(who,
expected_status = 200,
):
uri = WEBFINGER_BASE_URI
if who is not None:
uri += f'?resource={who}'
found = call_despatch_and_parse_result(
env = {
'DOCUMENT_URI': uri,
'ACCEPT': WEBFINGER_MIMETYPE,
},
)
assert found['Status'].startswith(
f'{expected_status} '), 'Status is correct'
response = webfinger(
who = None,
expected_status = 400,
)
response = webfinger(
who = 'I like coffee',
expected_status = 400,
)
response = webfinger(
who = 'jamie@magic-torch.example.net',
expected_status = 400,
)
response = webfinger(
who = 'lord_lucan@altair.example.com',
expected_status = 404,
)
response = webfinger(
who = 'alice@testserver',
expected_status = 200,
)
assert response['Content-Type']==WEBFINGER_MIMETYPE
assert response['Access-Control-Allow-Origin']=='*', (
'ACAO is *, per RFC'
)
parsed = json.loads(response.content)
assert parsed['subject']=='acct:alice@testserver'
assert 'https://testserver/users/alice' in parsed['aliases']
assert {
'rel': 'self',
'type': 'application/activity+json',
'href': 'https://testserver/users/alice',
} in parsed['links']