def setUp(self): self.target_url = testfunc.get_simple_url() self.webmention_source_url = testfunc.random_url() self.simplemention_source_url = testfunc.random_url() Webmention.objects.create( source_url=self.webmention_source_url, target_url=self.target_url, validated=True, approved=True, ) SimpleMention.objects.create( source_url=self.simplemention_source_url, target_url=self.target_url, )
def test_random_url(self): """Randomly generated URLs are valid.""" urls = [testfunc.random_url()] * 100 validator = URLValidator() for url in urls: validator(url) # Throws ValidationError if invalid
def setUp(self) -> None: super().setUp() obj = testfunc.create_mentionable_object() self.source = testfunc.random_url() self.target = testfunc.get_absolute_url_for_object(obj) self.http_post = QueryDict( f"source={self.source}&target={self.target}") self.sent_by = "localhost"
def test_primary_endpoint_rejects_invalid_target(self): """Invalid `target` rejects webmention and returns 400.""" data = { "source": testfunc.random_url(), "target": "htt://bad-url.or", } response_code = self._mock_post(data, accept=False) self.assertEqual(400, response_code)
def test_primary_endpoint_accepts_webmentions(self): """Valid POST request accepts webmention for processing and returns successful HTTP code 202.""" data = { "source": testfunc.random_url(), "target": self.target_url, } response_code = self._mock_post(data, accept=True) self.assertEqual(202, response_code)
def setUp(self) -> None: super().setUp() source = testfunc.random_url() obj = testfunc.create_mentionable_object(content=testfunc.random_str()) PendingIncomingWebmention.objects.create( source_url=source, target_url=testfunc.get_absolute_url_for_object(obj), sent_by="localhost", ) PendingOutgoingContent.objects.create( absolute_url=obj.get_absolute_url(), text=obj.content, )
def test_process_incoming_webmention_no_mentions_in_source(self): """process_incoming_webmention creates unvalidated Webmention object when target link not found in source text.""" with _patch_get(text=snippets.build_html(body=SOURCE_TEXT_NO_MENTION)): incoming_webmentions.process_incoming_webmention( source_url=SOURCE_URL_NO_MENTIONS, target_url=TARGET_URL, sent_by=testfunc.random_url(), ) webmentions = Webmention.objects.all() self.assertEqual(1, webmentions.count()) mention = webmentions.first() self.assertFalse(mention.validated)
def test_process_incoming_webmention(self): """process_incoming_webmention targeting a URL creates a validated Webmention object when successful.""" with _patch_get(): incoming_webmentions.process_incoming_webmention( source_url=SOURCE_URL_OK, target_url=TARGET_URL, sent_by=testfunc.random_url(), ) webmentions = Webmention.objects.all() self.assertEqual(1, webmentions.count()) mention = webmentions.first() self.assertEqual(mention.source_url, SOURCE_URL_OK) self.assertEqual(mention.target_url, TARGET_URL) self.assertTrue(mention.validated) hcard = mention.hcard self.assertIsNotNone(hcard) self.assertEqual(hcard.name, "Jane")
def test_find_links_in_text(self): """Outgoing links are found correctly.""" urls = { f"https://{testfunc.random_domain()}", f"http://{testfunc.random_domain()}/some-path", f"{testfunc.random_domain()}/some-path/something_else_04/", f"https://subdomain.{testfunc.random_domain()}/blah-blah/", *{testfunc.random_url() for _ in range(0, 5)}, } outgoing_content = "".join([ f'This is some content that mentions <a href="{url}">this page</a>' for url in urls ]) outgoing_links = outgoing_webmentions._find_links_in_text( outgoing_content) self.assertSetEqual(outgoing_links, urls)
def test_process_incoming_webmention_with_target_object(self): """process_incoming_webmention targeting an object creates a validated Webmention object when successful.""" with _patch_get(text=SOURCE_TEXT_FOR_OBJECT.format( url=self.target_url)): incoming_webmentions.process_incoming_webmention( source_url=SOURCE_URL_OK, target_url=self.target_url, sent_by=testfunc.random_url(), ) webmentions = Webmention.objects.all() self.assertEqual(1, webmentions.count()) mention = webmentions.first() self.assertEqual(mention.source_url, SOURCE_URL_OK) self.assertEqual(mention.target_url, self.target_url) self.assertTrue(mention.validated) hcard = mention.hcard self.assertIsNotNone(hcard) self.assertEqual(hcard.name, "Jane")
def test_get_target_object_wrong_domain_raises_exception(self): """Target URL with wrong domain raises TargetWrongDomain.""" with self.assertRaises(TargetWrongDomain): incoming_webmentions._get_target_object(testfunc.random_url())
""" import logging from unittest.mock import Mock, patch import requests from mentions.exceptions import SourceNotAccessible, TargetWrongDomain from mentions.models import Webmention from mentions.tasks import incoming_webmentions from tests import MockResponse, WebmentionTestCase from tests.util import snippets, testfunc log = logging.getLogger(__name__) SOURCE_URL_OK = testfunc.random_url() SOURCE_URL_NOT_FOUND = testfunc.random_url() SOURCE_URL_UNSUPPORTED_CONTENT_TYPE = testfunc.random_url() SOURCE_URL_NO_MENTIONS = testfunc.random_url() TARGET_URL = testfunc.get_simple_url(absolute=True) SOURCE_TEXT = f"""<div> <a href="{TARGET_URL}">link to target url</a> <div class="h-card"> <a class="u-url" href="https://janebloggs.org">Jane</a> </div> """ SOURCE_TEXT_FOR_OBJECT = """<div> <a href="{url}">link to target object</a> <div class="h-card">
def test_primary_endpoint_rejects_missing_source(self): """Missing `source` parameter rejects webmention and returns HTTP 400.""" data = {"target": testfunc.random_url()} response_code = self._mock_post(data, accept=False) self.assertEqual(400, response_code)