from django.test import TestCase, Client from django_kepi.views import InboxView from django_kepi.models import Activity from things_for_testing.models import ThingArticle, ThingUser from things_for_testing import KepiTestCase import json import httpretty from django_kepi import logger class TestInbox(KepiTestCase): @httpretty.activate def test_specific_post(self): HUMAN_URL = 'https://users.example.net/mary' ANIMAL_URL = 'https://things.example.org/lamb' self._mock_remote_object(HUMAN_URL, ftype='Person') self._mock_remote_object(ANIMAL_URL, ftype='Person') c = Client() c.post('/users/alice/inbox', content_type = 'application/activity+json', data = { "id": "https://example.net/hello-world", "actor": HUMAN_URL, "object": ANIMAL_URL, "type": "Like", }, ) self.assertTrue( QuarantinedMessage.objects.filter(username='alice').exists()) @httpretty.activate def test_shared_post(self): HUMAN_URL = 'https://users.example.net/mary' ANIMAL_URL = 'https://things.example.org/another-lamb' self._mock_remote_object(HUMAN_URL, ftype='Person') self._mock_remote_object(ANIMAL_URL, ftype='Person') c = Client() c.post('/sharedInbox', content_type = 'application/activity+json', data = { "id": "https://example.net/hello-world", "actor": HUMAN_URL, "object": ANIMAL_URL, "type": "Like", }, ) self.assertTrue( QuarantinedMessage.objects.filter(username=None).exists()) def test_non_json(self): QuarantinedMessage.objects.all().delete() c = Client() c.post('/sharedInbox', content_type = 'text/plain', data = 'Hello', ) self.assertFalse( QuarantinedMessage.objects.all().exists()) @httpretty.activate def test_malformed_json(self): HUMAN_URL = 'https://users.example.com/my-dame' ANIMAL_URL = 'https://animals.example.com/a-lame-tame-crane' self._mock_remote_object(HUMAN_URL, ftype='Person') self._mock_remote_object(ANIMAL_URL, ftype='Person') c = Client() c.post('/sharedInbox', content_type = 'application/activity+json', data = { "id": "https://example.net/hello-world", "actor": HUMAN_URL, "object": ANIMAL_URL, "type": "Like", }, ) return self.assertTrue( QuarantinedMessage.objects.all().exists()) QuarantinedMessage.objects.all().delete() text = text[1:] # remove leading {, so the JSON is invalid c.post('/sharedInbox', content_type = 'application/activity+json', data = text, ) self.assertFalse( QuarantinedMessage.objects.all().exists()) def test_all_parts_known(self): user = ThingUser(name="margaret") user.save() article = ThingArticle(title="dragons") article.save() QuarantinedMessage.objects.all().delete() c = Client() c.post('/users/alice/inbox', content_type = 'application/activity+json', data = { "id": "https://example.net/hello-world", "actor": user.activity_id, "object": article.activity_id, "type": "Like", }, ) # This should go through immediately, because # all parts are known and verifiable. self.assertTrue( Activity.objects.filter(identifier='https://example.net/hello-world').exists()) self.assertFalse( QuarantinedMessage.objects.all().exists())