From a80e0f117a0271d428abd939e2896857a8da1b5c Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Mon, 21 Nov 2022 20:25:00 -0700 Subject: Mentionify tests and some fixtures --- tests/activities/models/test_post.py | 32 ++++++++++++++++++++++++++++++++ tests/conftest.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) (limited to 'tests') diff --git a/tests/activities/models/test_post.py b/tests/activities/models/test_post.py index 5c7fca2..fb2e7a6 100644 --- a/tests/activities/models/test_post.py +++ b/tests/activities/models/test_post.py @@ -29,3 +29,35 @@ def test_fetch_post(httpx_mock: HTTPXMock): 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(identity, remote_identity): + """ + Tests that we can linkify post mentions properly + """ + # Test a short username without a mention (presumed local) + post = Post.objects.create( + content="

Hello @test

", + author=identity, + local=True, + ) + assert post.safe_content == '

Hello @test

' + # Test a full username + post = Post.objects.create( + content="

@test@example.com, welcome!

", + author=identity, + local=True, + ) + assert ( + post.safe_content + == '

@test@example.com, welcome!

' + ) + # Test a short username with a mention resolving to remote + post = Post.objects.create( + content="

Hello @test

", + author=identity, + local=True, + ) + post.mentions.add(remote_identity) + assert post.safe_content == '

Hello @test

' diff --git a/tests/conftest.py b/tests/conftest.py index 79bdf60..24fac9a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ import pytest from core.models import Config +from users.models import Domain, Identity, User @pytest.fixture @@ -57,3 +58,37 @@ def config_system(keypair): system_actor_public_key=keypair["public_key"], ) yield Config.system + + +@pytest.fixture +@pytest.mark.django_db +def identity(): + """ + Creates a basic test identity with a user and domain. + """ + user = User.objects.create(email="test@example.com") + domain = Domain.objects.create(domain="example.com", local=True, public=True) + return Identity.objects.create( + actor_uri="https://example.com/test-actor/", + username="test", + domain=domain, + user=user, + name="Test User", + local=True, + ) + + +@pytest.fixture +@pytest.mark.django_db +def remote_identity(): + """ + Creates a basic remote test identity with a domain. + """ + domain = Domain.objects.create(domain="remote.test", local=False) + return Identity.objects.create( + actor_uri="https://remote.test/test-actor/", + username="test", + domain=domain, + name="Test Remote User", + local=False, + ) -- cgit v1.2.3