From 038d0ca2d137ed6500abdd5159218dea837add4d Mon Sep 17 00:00:00 2001 From: Marnanel Thurman Date: Wed, 29 May 2019 09:32:20 +0100 Subject: [PATCH] post_test_message() --- tests/__init__.py | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_inbox.py | 36 +++++++++++------------------------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index d8492b5..778e440 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,6 +1,7 @@ from django_kepi.models import create from django_kepi.validation import IncomingMessage, validate from django_kepi.models.actor import Actor +import django.test import httpretty import logging import httpsig @@ -155,6 +156,43 @@ def test_message(secret='', **fields): result.save() return result +def post_test_message( + path, host, + secret, + f_id, f_type, f_actor, f_object, + client = None, + ): + + if client is None: + client = django.test.Client() + + body, headers = test_message_body_and_headers( + f_id=f_id, + f_type=f_type, + f_actor=f_actor, + f_object=f_object, + secret = secret, + path = path, + host = host, + ) + + logger.debug("Test message is %s", + body) + logger.debug(" -- with headers %s", + headers) + + client.post( + path = path, + content_type = headers['content-type'], # XXX why twice? + data = json.dumps(body), + CONTENT_TYPE = headers['content-type'], + HTTP_DATE = headers['date'], + HOST = headers['host'], + HTTP_SIGNATURE = headers['signature'], + ) + + return client + def remote_user(url, name, publicKey='', inbox=None, diff --git a/tests/test_inbox.py b/tests/test_inbox.py index fa199be..b5569b5 100644 --- a/tests/test_inbox.py +++ b/tests/test_inbox.py @@ -30,32 +30,18 @@ class TestInbox(TestCase): publicKey = keys['public'], )), ) - - body, headers = test_message_body_and_headers( - f_id=ACTIVITY_ID, - f_type="Follow", - f_actor=REMOTE_FRED, - f_object=LOCAL_ALICE, - secret = keys['private'], - path = ALICE_INBOX, - host = 'europa.example.com', - ) - - logger.debug("Test message is %s", - body) - logger.debug(" -- with headers %s", - headers) - c = Client() - c.post( - ALICE_INBOX, - content_type = 'application/activity+json', - data = json.dumps(body), - CONTENT_TYPE = headers['content-type'], - HTTP_DATE = headers['date'], - HOST = headers['host'], - HTTP_SIGNATURE = headers['signature'], - ) + + post_test_message( + client = c, + path = ALICE_INBOX, + host = 'europa.example.com', + secret = keys['private'], + f_id = ACTIVITY_ID, + f_type = "Follow", + f_actor = REMOTE_FRED, + f_object = LOCAL_ALICE, + ) @httpretty.activate def test_shared_post(self):