2022-11-13 17:42:47 -08:00
|
|
|
import pytest
|
|
|
|
from pytest_httpx import HTTPXMock
|
|
|
|
|
2022-11-27 10:09:46 -08:00
|
|
|
from activities.models import Post, PostStates
|
2022-12-28 21:47:28 -08:00
|
|
|
from users.models import Identity, InboxMessage
|
2022-11-13 17:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2022-12-04 20:30:18 -08:00
|
|
|
def test_fetch_post(httpx_mock: HTTPXMock, config_system):
|
2022-11-13 17:42:47 -08:00
|
|
|
"""
|
|
|
|
Tests that a post we don't have locally can be fetched by by_object_uri
|
|
|
|
"""
|
2022-12-20 03:39:45 -08:00
|
|
|
httpx_mock.add_response(
|
|
|
|
url="https://example.com/test-actor",
|
|
|
|
json={
|
|
|
|
"@context": [
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
|
|
|
],
|
|
|
|
"id": "https://example.com/test-actor",
|
|
|
|
"type": "Person",
|
|
|
|
},
|
|
|
|
)
|
2022-11-13 17:42:47 -08:00
|
|
|
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
|
2022-11-21 19:25:00 -08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2022-12-05 20:44:26 -08:00
|
|
|
def test_linkify_mentions_remote(
|
|
|
|
identity, identity2, remote_identity, remote_identity2
|
|
|
|
):
|
2022-11-21 19:25:00 -08:00
|
|
|
"""
|
2022-11-21 20:18:13 -08:00
|
|
|
Tests that we can linkify post mentions properly for remote use
|
2022-11-21 19:25:00 -08:00
|
|
|
"""
|
2022-11-21 20:18:13 -08:00
|
|
|
# Test a short username (remote)
|
2022-11-21 19:25:00 -08:00
|
|
|
post = Post.objects.create(
|
|
|
|
content="<p>Hello @test</p>",
|
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
2022-11-21 20:18:13 -08:00
|
|
|
post.mentions.add(remote_identity)
|
|
|
|
assert (
|
|
|
|
post.safe_content_remote()
|
|
|
|
== '<p>Hello <a href="https://remote.test/@test/">@test</a></p>'
|
|
|
|
)
|
|
|
|
# Test a full username (local)
|
2022-11-21 19:25:00 -08:00
|
|
|
post = Post.objects.create(
|
|
|
|
content="<p>@test@example.com, welcome!</p>",
|
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
2022-11-21 20:18:13 -08:00
|
|
|
post.mentions.add(identity)
|
2022-11-21 19:25:00 -08:00
|
|
|
assert (
|
2022-11-21 20:18:13 -08:00
|
|
|
post.safe_content_remote()
|
2022-12-05 19:26:21 -08:00
|
|
|
== '<p><a href="https://example.com/@test/">@test</a>, welcome!</p>'
|
2022-11-21 19:25:00 -08:00
|
|
|
)
|
2022-11-21 20:18:13 -08:00
|
|
|
# Test that they don't get touched without a mention
|
|
|
|
post = Post.objects.create(
|
|
|
|
content="<p>@test@example.com, welcome!</p>",
|
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
assert post.safe_content_remote() == "<p>@test@example.com, welcome!</p>"
|
2022-12-23 00:46:35 -08:00
|
|
|
|
2022-12-04 19:22:24 -08:00
|
|
|
# Test case insensitivity (remote)
|
|
|
|
post = Post.objects.create(
|
|
|
|
content="<p>Hey @TeSt</p>",
|
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
post.mentions.add(remote_identity)
|
|
|
|
assert (
|
|
|
|
post.safe_content_remote()
|
2022-12-24 20:04:25 -08:00
|
|
|
== '<p>Hey <a href="https://remote.test/@test/">@TeSt</a></p>'
|
2022-12-04 19:22:24 -08:00
|
|
|
)
|
2022-11-21 20:18:13 -08:00
|
|
|
|
2022-12-23 00:46:35 -08:00
|
|
|
# Test trailing dot (remote)
|
|
|
|
post = Post.objects.create(
|
|
|
|
content="<p>Hey @test@remote.test.</p>",
|
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
post.mentions.add(remote_identity)
|
|
|
|
assert (
|
|
|
|
post.safe_content_remote()
|
|
|
|
== '<p>Hey <a href="https://remote.test/@test/">@test</a>.</p>'
|
|
|
|
)
|
|
|
|
|
2022-12-05 20:44:26 -08:00
|
|
|
# Test that collapsing only applies to the first unique, short username
|
|
|
|
post = Post.objects.create(
|
|
|
|
content="<p>Hey @TeSt@remote.test and @test@remote2.test</p>",
|
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
post.mentions.set([remote_identity, remote_identity2])
|
|
|
|
assert post.safe_content_remote() == (
|
2022-12-24 20:04:25 -08:00
|
|
|
'<p>Hey <a href="https://remote.test/@test/">@TeSt</a> '
|
2022-12-05 20:44:26 -08:00
|
|
|
'and <a href="https://remote2.test/@test/">@test@remote2.test</a></p>'
|
|
|
|
)
|
|
|
|
|
|
|
|
post.content = "<p>Hey @TeSt, @Test@remote.test and @test</p>"
|
|
|
|
assert post.safe_content_remote() == (
|
2022-12-24 20:04:25 -08:00
|
|
|
'<p>Hey <a href="https://remote2.test/@test/">@TeSt</a>, '
|
|
|
|
'<a href="https://remote.test/@test/">@Test@remote.test</a> '
|
2022-12-05 20:44:26 -08:00
|
|
|
'and <a href="https://remote2.test/@test/">@test</a></p>'
|
|
|
|
)
|
|
|
|
|
2022-11-21 20:18:13 -08:00
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2022-12-22 08:55:31 -08:00
|
|
|
def test_linkify_mentions_local(config_system, identity, identity2, remote_identity):
|
2022-11-21 20:18:13 -08:00
|
|
|
"""
|
|
|
|
Tests that we can linkify post mentions properly for local use
|
|
|
|
"""
|
|
|
|
# Test a short username (remote)
|
2022-11-21 19:25:00 -08:00
|
|
|
post = Post.objects.create(
|
|
|
|
content="<p>Hello @test</p>",
|
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
post.mentions.add(remote_identity)
|
2022-11-21 20:18:13 -08:00
|
|
|
assert (
|
|
|
|
post.safe_content_local()
|
|
|
|
== '<p>Hello <a href="/@test@remote.test/">@test</a></p>'
|
|
|
|
)
|
|
|
|
# Test a full username (local)
|
|
|
|
post = Post.objects.create(
|
2022-12-05 20:44:26 -08:00
|
|
|
content="<p>@test@example.com, welcome! @test@example2.com @test@example.com</p>",
|
2022-11-21 20:18:13 -08:00
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
post.mentions.add(identity)
|
2022-12-05 20:44:26 -08:00
|
|
|
post.mentions.add(identity2)
|
|
|
|
assert post.safe_content_local() == (
|
|
|
|
'<p><a href="/@test@example.com/">@test</a>, welcome!'
|
|
|
|
' <a href="/@test@example2.com/">@test@example2.com</a>'
|
|
|
|
' <a href="/@test@example.com/">@test</a></p>'
|
2022-11-21 20:18:13 -08:00
|
|
|
)
|
2022-11-21 21:17:22 -08:00
|
|
|
# Test a full username (remote) with no <p>
|
|
|
|
post = Post.objects.create(
|
|
|
|
content="@test@remote.test hello!",
|
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
post.mentions.add(remote_identity)
|
2022-12-05 19:26:21 -08:00
|
|
|
assert post.safe_content_local() == '<a href="/@test@remote.test/">@test</a> hello!'
|
2022-11-21 20:18:13 -08:00
|
|
|
# Test that they don't get touched without a mention
|
|
|
|
post = Post.objects.create(
|
|
|
|
content="<p>@test@example.com, welcome!</p>",
|
|
|
|
author=identity,
|
|
|
|
local=True,
|
|
|
|
)
|
|
|
|
assert post.safe_content_local() == "<p>@test@example.com, welcome!</p>"
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2022-12-05 18:21:00 -08:00
|
|
|
def test_post_transitions(identity, stator):
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
# Create post
|
|
|
|
post = Post.objects.create(
|
|
|
|
content="<p>Hello!</p>",
|
|
|
|
author=identity,
|
|
|
|
local=False,
|
|
|
|
visibility=Post.Visibilities.mentioned,
|
|
|
|
)
|
|
|
|
# Test: | --> new --> fanned_out
|
|
|
|
assert post.state == str(PostStates.new)
|
2022-12-05 18:21:00 -08:00
|
|
|
stator.run_single_cycle_sync()
|
2022-11-27 10:09:46 -08:00
|
|
|
post = Post.objects.get(id=post.id)
|
|
|
|
assert post.state == str(PostStates.fanned_out)
|
|
|
|
|
|
|
|
# Test: fanned_out --> (forced) edited --> edited_fanned_out
|
|
|
|
Post.transition_perform(post, PostStates.edited)
|
2022-12-05 18:21:00 -08:00
|
|
|
stator.run_single_cycle_sync()
|
2022-11-27 10:09:46 -08:00
|
|
|
post = Post.objects.get(id=post.id)
|
|
|
|
assert post.state == str(PostStates.edited_fanned_out)
|
|
|
|
|
|
|
|
# Test: edited_fanned_out --> (forced) deleted --> deleted_fanned_out
|
|
|
|
Post.transition_perform(post, PostStates.deleted)
|
2022-12-05 18:21:00 -08:00
|
|
|
stator.run_single_cycle_sync()
|
2022-11-27 10:09:46 -08:00
|
|
|
post = Post.objects.get(id=post.id)
|
|
|
|
assert post.state == str(PostStates.deleted_fanned_out)
|
2022-12-21 21:29:49 -08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_content_map(remote_identity):
|
|
|
|
"""
|
|
|
|
Tests that post contentmap content also works
|
|
|
|
"""
|
|
|
|
post = Post.by_ap(
|
|
|
|
data={
|
|
|
|
"id": "https://remote.test/posts/1/",
|
|
|
|
"type": "Note",
|
|
|
|
"content": "Hi World",
|
|
|
|
"attributedTo": "https://remote.test/test-actor/",
|
|
|
|
"published": "2022-12-23T10:50:54Z",
|
|
|
|
},
|
|
|
|
create=True,
|
|
|
|
)
|
|
|
|
assert post.content == "Hi World"
|
|
|
|
|
|
|
|
post2 = Post.by_ap(
|
|
|
|
data={
|
|
|
|
"id": "https://remote.test/posts/2/",
|
|
|
|
"type": "Note",
|
|
|
|
"contentMap": {"und": "Hey World"},
|
|
|
|
"attributedTo": "https://remote.test/test-actor/",
|
|
|
|
"published": "2022-12-23T10:50:54Z",
|
|
|
|
},
|
|
|
|
create=True,
|
|
|
|
)
|
|
|
|
assert post2.content == "Hey World"
|
|
|
|
|
|
|
|
post3 = Post.by_ap(
|
|
|
|
data={
|
|
|
|
"id": "https://remote.test/posts/3/",
|
|
|
|
"type": "Note",
|
|
|
|
"contentMap": {"en-gb": "Hello World"},
|
|
|
|
"attributedTo": "https://remote.test/test-actor/",
|
|
|
|
"published": "2022-12-23T10:50:54Z",
|
|
|
|
},
|
|
|
|
create=True,
|
|
|
|
)
|
|
|
|
assert post3.content == "Hello World"
|
2022-12-28 21:47:28 -08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
@pytest.mark.parametrize("delete_type", ["note", "tombstone", "ref"])
|
|
|
|
def test_inbound_posts(
|
|
|
|
remote_identity: Identity,
|
|
|
|
stator,
|
|
|
|
delete_type: bool,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Ensures that a remote post can arrive via inbox message, be edited, and be
|
|
|
|
deleted.
|
|
|
|
"""
|
|
|
|
# Create an inbound new post message
|
|
|
|
message = {
|
|
|
|
"id": "test",
|
|
|
|
"type": "Create",
|
|
|
|
"actor": remote_identity.actor_uri,
|
|
|
|
"object": {
|
|
|
|
"id": "https://remote.test/test-post",
|
|
|
|
"type": "Note",
|
|
|
|
"published": "2022-11-13T23:20:16Z",
|
|
|
|
"attributedTo": remote_identity.actor_uri,
|
|
|
|
"content": "post version one",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
InboxMessage.objects.create(message=message)
|
|
|
|
|
|
|
|
# Run stator and ensure that made the post
|
|
|
|
stator.run_single_cycle_sync()
|
|
|
|
post = Post.objects.get(object_uri="https://remote.test/test-post")
|
|
|
|
assert post.content == "post version one"
|
|
|
|
assert post.published.day == 13
|
|
|
|
assert post.url == "https://remote.test/test-post"
|
|
|
|
|
|
|
|
# Create an inbound post edited message
|
|
|
|
message = {
|
|
|
|
"id": "test",
|
|
|
|
"type": "Update",
|
|
|
|
"actor": remote_identity.actor_uri,
|
|
|
|
"object": {
|
|
|
|
"id": "https://remote.test/test-post",
|
|
|
|
"type": "Note",
|
|
|
|
"published": "2022-11-13T23:20:16Z",
|
|
|
|
"updated": "2022-11-14T23:20:16Z",
|
|
|
|
"url": "https://remote.test/test-post/display",
|
|
|
|
"attributedTo": remote_identity.actor_uri,
|
|
|
|
"content": "post version two",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
InboxMessage.objects.create(message=message)
|
|
|
|
|
|
|
|
# Run stator and ensure that edited the post
|
|
|
|
stator.run_single_cycle_sync()
|
|
|
|
post = Post.objects.get(object_uri="https://remote.test/test-post")
|
|
|
|
assert post.content == "post version two"
|
|
|
|
assert post.edited.day == 14
|
|
|
|
assert post.url == "https://remote.test/test-post/display"
|
|
|
|
|
|
|
|
# Create an inbound post deleted message
|
|
|
|
if delete_type == "ref":
|
|
|
|
message = {
|
|
|
|
"id": "test",
|
|
|
|
"type": "Delete",
|
|
|
|
"actor": remote_identity.actor_uri,
|
|
|
|
"object": "https://remote.test/test-post",
|
|
|
|
}
|
|
|
|
elif delete_type == "tombstone":
|
|
|
|
message = {
|
|
|
|
"id": "test",
|
|
|
|
"type": "Delete",
|
|
|
|
"actor": remote_identity.actor_uri,
|
|
|
|
"object": {
|
|
|
|
"id": "https://remote.test/test-post",
|
|
|
|
"type": "Tombstone",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
message = {
|
|
|
|
"id": "test",
|
|
|
|
"type": "Delete",
|
|
|
|
"actor": remote_identity.actor_uri,
|
|
|
|
"object": {
|
|
|
|
"id": "https://remote.test/test-post",
|
|
|
|
"type": "Note",
|
|
|
|
"published": "2022-11-13T23:20:16Z",
|
|
|
|
"attributedTo": remote_identity.actor_uri,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
InboxMessage.objects.create(message=message)
|
|
|
|
|
|
|
|
# Run stator and ensure that deleted the post
|
|
|
|
stator.run_single_cycle_sync()
|
|
|
|
assert not Post.objects.filter(object_uri="https://remote.test/test-post").exists()
|