import pytest from pytest_httpx import HTTPXMock from activities.models import Post @pytest.mark.django_db def test_fetch_post(httpx_mock: HTTPXMock): """ Tests that a post we don't have locally can be fetched by by_object_uri """ httpx_mock.add_response( url="https://example.com/test-post", json={ "@context": [ "https://www.w3.org/ns/activitystreams", ], "id": "https://example.com/test-post", "type": "Note", "published": "2022-11-13T23:20:16Z", "url": "https://example.com/test-post", "attributedTo": "https://example.com/test-actor", "content": "BEEEEEES", }, ) # Fetch with a HTTP access post = Post.by_object_uri("https://example.com/test-post", fetch=True) assert post.content == "BEEEEEES" assert post.author.actor_uri == "https://example.com/test-actor" # Fetch again with a DB hit assert Post.by_object_uri("https://example.com/test-post").id == post.id @pytest.mark.django_db def test_linkify_mentions_remote(identity, remote_identity): """ Tests that we can linkify post mentions properly for remote use """ # Test a short username (remote) post = Post.objects.create( content="
Hello @test
", author=identity, local=True, ) post.mentions.add(remote_identity) assert ( post.safe_content_remote() == 'Hello @test
' ) # Test a full username (local) post = Post.objects.create( content="@test@example.com, welcome!
", author=identity, local=True, ) post.mentions.add(identity) assert ( post.safe_content_remote() == '@test@example.com, welcome!
' ) # Test that they don't get touched without a mention post = Post.objects.create( content="@test@example.com, welcome!
", author=identity, local=True, ) assert post.safe_content_remote() == "@test@example.com, welcome!
" @pytest.mark.django_db def test_linkify_mentions_local(identity, remote_identity): """ Tests that we can linkify post mentions properly for local use """ # Test a short username (remote) post = Post.objects.create( content="Hello @test
", author=identity, local=True, ) post.mentions.add(remote_identity) assert ( post.safe_content_local() == 'Hello @test
' ) # Test a full username (local) post = Post.objects.create( content="@test@example.com, welcome!
", author=identity, local=True, ) post.mentions.add(identity) assert ( post.safe_content_local() == '@test@example.com, welcome!
' ) # Test a full username (remote) with nopost = Post.objects.create( content="@test@remote.test hello!", author=identity, local=True, ) post.mentions.add(remote_identity) assert ( post.safe_content_local() == '@test@remote.test hello!' ) # Test that they don't get touched without a mention post = Post.objects.create( content="
@test@example.com, welcome!
", author=identity, local=True, ) assert post.safe_content_local() == "@test@example.com, welcome!
"