@context is now a simple string.

Added remote_object_is_recorded().

Added the on_fetch param to mock_remote_object()
and create_remote_person().
2019-08-17
Marnanel Thurman 2019-08-12 19:42:23 +01:00
rodzic d49ca4b14f
commit d482f50e78
1 zmienionych plików z 31 dodań i 29 usunięć

Wyświetl plik

@ -31,28 +31,6 @@ BOBS_FOLLOWERS = LOCAL_BOB+'/followers'
PUBLIC = "https://www.w3.org/ns/activitystreams#Public" PUBLIC = "https://www.w3.org/ns/activitystreams#Public"
CONTEXT_URL = "https://www.w3.org/ns/activitystreams" CONTEXT_URL = "https://www.w3.org/ns/activitystreams"
MESSAGE_CONTEXT = ["https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{"manuallyApprovesFollowers":"as:manuallyApprovesFollowers",
"sensitive":"as:sensitive",
"movedTo":{"@id":"as:movedTo",
"@type":"@id"},
"alsoKnownAs":{"@id":"as:alsoKnownAs",
"@type":"@id"},
"Hashtag":"as:Hashtag",
"ostatus":"http://ostatus.org#",
"atomUri":"ostatus:atomUri",
"inReplyToAtomUri":"ostatus:inReplyToAtomUri",
"conversation":"ostatus:conversation",
"toot":"http://joinmastodon.org/ns#",
"Emoji":"toot:Emoji",
"focalPoint":{"@container":"@list",
"@id":"toot:focalPoint"},
"featured":{"@id":"toot:featured",
"@type":"@id"},
"schema":"http://schema.org#",
"PropertyValue":"schema:PropertyValue",
"value":"schema:value"}]
logger = logging.getLogger(name='django_kepi') logger = logging.getLogger(name='django_kepi')
@ -124,6 +102,7 @@ def mock_remote_object(
content = '', content = '',
status = 200, status = 200,
as_post = False, as_post = False,
on_fetch = None,
): ):
headers = { headers = {
@ -140,12 +119,18 @@ def mock_remote_object(
else: else:
method = httpretty.GET method = httpretty.GET
def return_body(request, url, stuff):
logger.info('%s: fetched', url)
if on_fetch is not None:
on_fetch()
return status, stuff, body
httpretty.register_uri( httpretty.register_uri(
method, method,
url, url,
status=status, status=status,
headers=headers, headers=headers,
body = body, body = return_body,
match_querystring = True, match_querystring = True,
) )
@ -158,16 +143,21 @@ def create_remote_person(
url, url,
name, name,
publicKey, publicKey,
on_fetch = None,
**fields): **fields):
mock_remote_object( body = json.dumps(
url=url, remote_user(
content=json.dumps(remote_user(
url=url, url=url,
name=name, name=name,
publicKey = publicKey, publicKey = publicKey,
**fields, **fields,
)), ))
mock_remote_object(
url=url,
on_fetch=on_fetch,
content=body,
) )
def create_remote_collection( def create_remote_collection(
@ -219,7 +209,7 @@ def test_message_body_and_headers(secret='',
**fields): **fields):
body = dict([(f[2:],v) for f,v in fields.items() if f.startswith('f_')]) body = dict([(f[2:],v) for f,v in fields.items() if f.startswith('f_')])
body['@context'] = MESSAGE_CONTEXT body['@context'] = CONTEXT_URL
body['Host'] = host body['Host'] = host
headers = { headers = {
@ -256,6 +246,8 @@ def test_message_body_and_headers(secret='',
if 'id' not in body: if 'id' not in body:
body['id'] = ACTIVITY_ID body['id'] = ACTIVITY_ID
body = json.dumps(body, indent=2, sort_keys=True)
return body, headers return body, headers
def test_message(secret='', **fields): def test_message(secret='', **fields):
@ -324,7 +316,7 @@ def remote_user(url, name,
followers=None, followers=None,
): ):
result = { result = {
'@context': MESSAGE_CONTEXT, '@context': CONTEXT_URL,
'id': url, 'id': url,
'type': 'Person', 'type': 'Person',
'following': '', 'following': '',
@ -349,3 +341,13 @@ def remote_user(url, name,
} }
return result return result
def remote_object_is_recorded(url):
from django_kepi.models import Object
try:
result = Object.objects.get(remote_url=url)
return True
except Object.DoesNotExist:
return False