from unittest.mock import Mock import pytest from core.html import ContentRenderer, html_to_plaintext, sanitize_html def test_html_to_plaintext(): assert html_to_plaintext("
Hi!
") == "Hi!" assert html_to_plaintext("Hi!
There
Hi!
\n\nHow are you?
") == "Hi!\n\nHow are you?" ) assert ( html_to_plaintext("Hi!
\n\nHow are
you?
today
") == "Hi!\n\nHow are\n you?\n\ntoday" ) def test_sanitize_post(): assert sanitize_html("Hello!
") == "Hello!
" assert sanitize_html("It's great
") == "It's great
" # Note that we only want to linkify things with protocol prefixes to prevent # too many false positives. assert sanitize_html("test.com
") == "test.com
" assert ( sanitize_html("https://test.com
") == '' ) assert ( sanitize_html("@someone@subdomain.some-domain.com
") == "@someone@subdomain.some-domain.com
" ) def test_shorten_url(): full_url = ( "https://social.example.com/a-long/path/2023/01/16/that-should-be-shortened" ) assert ( sanitize_html(f"{full_url}
") == f'social.example.com/a-long/path
' ) assert ( sanitize_html( f'This is a long link text, but cannot be shortened as a URL
' ) == f'This is a long link text, but cannot be shortened as a URL
' ) @pytest.mark.django_db def test_link_preservation(): """ We want to: - Preserve incoming links from other servers - Linkify mentions and hashtags - Not have these all step on each other! """ renderer = ContentRenderer(local=True) fake_mention = Mock() fake_mention.username = "andrew" fake_mention.domain_id = "aeracode.org" fake_mention.urls.view = "/@andrew@aeracode.org/" fake_post = Mock() fake_post.mentions.all.return_value = [fake_mention] fake_post.author.domain.uri_domain = "example.com" fake_post.emojis.all.return_value = [] assert ( renderer.render_post( 'Hello @andrew, I want to link to this #hashtag: here and rewrite #thishashtag', fake_post, ) == 'Hello @andrew, I want to link to this #hashtag: here and rewrite #thishashtag' ) @pytest.mark.django_db def test_list_rendering(): """ We want to: - Preserve incoming links from other servers - Linkify mentions and hashtags - Not have these all step on each other! """ renderer = ContentRenderer(local=True) fake_mention = Mock() fake_mention.username = "andrew" fake_mention.domain_id = "aeracode.org" fake_mention.urls.view = "/@andrew@aeracode.org/" fake_post = Mock() fake_post.mentions.all.return_value = [fake_mention] fake_post.author.domain.uri_domain = "example.com" fake_post.emojis.all.return_value = [] assert ( renderer.render_post( "Ok. The roster so far is:
What’s next? I think I promised some people here bookwyrm
", fake_post, ) == "Ok. The roster so far is:
Infosec.exchange (mastodon)
pixel.Infosec.exchange (pixelfed)
video.Infosec.exchange (peertube)
relay.Infosec.exchange (activitypub relay)
risky.af (alt mastodon)
What’s next? I think I promised some people here bookwyrm
" ) @pytest.mark.django_db def test_link_mixcase_mentions(): renderer = ContentRenderer(local=True) fake_mention = Mock() fake_mention.username = "Manfre" fake_mention.domain_id = "manfre.net" fake_mention.urls.view = "/@Manfre@manfre.net/" fake_mention2 = Mock() fake_mention2.username = "manfre" fake_mention2.domain_id = "takahe.social" fake_mention2.urls.view = "https://takahe.social/@manfre@takahe.social/" unfetched_mention = Mock() unfetched_mention.username = None unfetched_mention.domain_id = None unfetched_mention.urls.view = "/None@None/" fake_post = Mock() fake_post.mentions.all.return_value = [ fake_mention, fake_mention2, unfetched_mention, ] fake_post.author.domain.uri_domain = "example.com" fake_post.emojis.all.return_value = [] assert renderer.render_post( "@Manfre@manfre.net @mAnFrE@takahe.social @manfre@manfre.net @unfetched@manfre.net", fake_post, ) == ( '@Manfre ' '@mAnFrE@takahe.social ' '@manfre ' "@unfetched@manfre.net" )