2020-10-19 09:32:37 +02:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.UpdateTest do
|
|
|
|
use Mobilizon.DataCase
|
|
|
|
use Oban.Testing, repo: Mobilizon.Storage.Repo
|
|
|
|
import Mobilizon.Factory
|
2021-11-13 18:45:01 +01:00
|
|
|
import Mox
|
2022-04-20 11:33:13 +02:00
|
|
|
import ExUnit.CaptureLog
|
2020-10-19 09:32:37 +02:00
|
|
|
|
|
|
|
alias Mobilizon.{Actors, Events, Posts}
|
2020-10-19 19:21:39 +02:00
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
2020-10-19 09:32:37 +02:00
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Posts.Post
|
2022-04-20 11:33:13 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Activity, Relay, Transmogrifier}
|
2020-10-19 09:32:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Convertible
|
2021-11-13 18:45:01 +01:00
|
|
|
alias Mobilizon.Service.HTTP.ActivityPub.Mock
|
2020-10-19 09:32:37 +02:00
|
|
|
|
|
|
|
describe "handle incoming update activities" do
|
|
|
|
test "it works for incoming update activities on actors" do
|
2021-11-13 18:45:01 +01:00
|
|
|
data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
actor_data =
|
|
|
|
File.read!("test/fixtures/mastodon-actor.json")
|
|
|
|
|> Jason.decode!()
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
Mock
|
|
|
|
|> expect(:call, 2, fn
|
|
|
|
%{method: :get, url: "https://framapiaf.org/users/admin"}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: actor_data}}
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
%{method: :get, url: "https://framapiaf.org/users/tcit"}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: actor_data}}
|
|
|
|
end)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(data)
|
|
|
|
update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
object =
|
|
|
|
update_data["object"]
|
|
|
|
|> Map.put("actor", data["actor"])
|
|
|
|
|> Map.put("id", data["actor"])
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
update_data =
|
|
|
|
update_data
|
|
|
|
|> Map.put("actor", data["actor"])
|
|
|
|
|> Map.put("object", object)
|
|
|
|
|
|
|
|
{:ok, %Activity{data: _data, local: false}, _} = Transmogrifier.handle_incoming(update_data)
|
|
|
|
|
|
|
|
{:ok, %Actor{} = actor} = Actors.get_actor_by_url(update_data["actor"])
|
|
|
|
assert actor.name == "nextsoft"
|
|
|
|
|
|
|
|
assert actor.summary == "<p>Some bio</p>"
|
2020-10-19 09:32:37 +02:00
|
|
|
end
|
|
|
|
|
2022-04-20 11:33:13 +02:00
|
|
|
test "it fails for incoming update activies on local actors" do
|
|
|
|
%Actor{url: relay_actor_url} = Relay.get_actor()
|
|
|
|
|
|
|
|
update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
|
|
|
|
|
|
|
|
object =
|
|
|
|
update_data["object"]
|
|
|
|
|> Map.put("actor", relay_actor_url)
|
|
|
|
|> Map.put("id", relay_actor_url)
|
|
|
|
|
|
|
|
update_data =
|
|
|
|
update_data
|
|
|
|
|> Map.put("actor", relay_actor_url)
|
|
|
|
|> Map.put("object", object)
|
|
|
|
|
|
|
|
assert capture_log([level: :warn], fn ->
|
|
|
|
:error = Transmogrifier.handle_incoming(update_data)
|
|
|
|
end) =~ "[warning] Activity tried to update an actor that's local or not a group"
|
|
|
|
|
|
|
|
{:ok, %Actor{keys: keys}} = Actors.get_actor_by_url(relay_actor_url)
|
|
|
|
assert Regex.match?(~r/BEGIN RSA PRIVATE KEY/, keys)
|
|
|
|
end
|
|
|
|
|
2020-10-19 09:32:37 +02:00
|
|
|
test "it works for incoming update activities on events" do
|
2021-11-13 18:45:01 +01:00
|
|
|
data = File.read!("test/fixtures/mobilizon-post-activity.json") |> Jason.decode!()
|
|
|
|
|
|
|
|
actor_data =
|
|
|
|
File.read!("test/fixtures/mastodon-actor.json")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|
|
|
|
Mock
|
|
|
|
|> expect(:call, 2, fn
|
|
|
|
%{method: :get, url: "https://mobilizon.fr/@metacartes"}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: actor_data}}
|
|
|
|
|
|
|
|
%{method: :get, url: "https://framapiaf.org/users/tcit"}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: actor_data}}
|
|
|
|
end)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
{:ok, %Activity{data: data, local: false}, %Event{id: event_id}} =
|
|
|
|
Transmogrifier.handle_incoming(data)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
assert_enqueued(
|
|
|
|
worker: Mobilizon.Service.Workers.BuildSearch,
|
|
|
|
args: %{event_id: event_id, op: :insert_search_event}
|
|
|
|
)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2022-09-21 08:32:42 +02:00
|
|
|
assert %{success: 1, snoozed: 0, failure: 0, discard: 0, cancelled: 0} ==
|
|
|
|
Oban.drain_queue(queue: :search)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
object =
|
|
|
|
data["object"]
|
|
|
|
|> Map.put("actor", data["actor"])
|
|
|
|
|> Map.put("name", "My updated event")
|
|
|
|
|> Map.put("id", data["object"]["id"])
|
|
|
|
|> Map.put("type", "Event")
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
update_data =
|
|
|
|
update_data
|
|
|
|
|> Map.put("actor", data["actor"])
|
|
|
|
|> Map.put("object", object)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(update_data)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
%Event{} = event = Events.get_event_by_url(data["object"]["id"])
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
assert_enqueued(
|
|
|
|
worker: Mobilizon.Service.Workers.BuildSearch,
|
|
|
|
args: %{event_id: event_id, op: :update_search_event}
|
|
|
|
)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2022-09-21 08:32:42 +02:00
|
|
|
assert %{success: 1, snoozed: 0, failure: 0, discard: 0, cancelled: 0} ==
|
|
|
|
Oban.drain_queue(queue: :search)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
assert event.title == "My updated event"
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
assert event.description == data["object"]["content"]
|
2020-10-19 09:32:37 +02:00
|
|
|
end
|
|
|
|
|
2020-10-19 19:21:39 +02:00
|
|
|
# test "it works for incoming update activities which lock the account" do
|
|
|
|
# data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
|
|
|
|
|
|
|
|
# {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
|
|
|
|
# update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
|
|
|
|
|
|
|
|
# object =
|
|
|
|
# update_data["object"]
|
|
|
|
# |> Map.put("actor", data["actor"])
|
|
|
|
# |> Map.put("id", data["actor"])
|
|
|
|
# |> Map.put("manuallyApprovesFollowers", true)
|
|
|
|
|
|
|
|
# update_data =
|
|
|
|
# update_data
|
|
|
|
# |> Map.put("actor", data["actor"])
|
|
|
|
# |> Map.put("object", object)
|
|
|
|
|
|
|
|
# {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
|
|
|
|
|
|
|
|
# user = User.get_cached_by_ap_id(data["actor"])
|
|
|
|
# assert user.info["locked"] == true
|
|
|
|
# end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "handle incoming updates activities for group posts" do
|
|
|
|
test "it works for incoming update activities on group posts when remote actor is a moderator" do
|
2020-12-16 11:14:24 +01:00
|
|
|
%Actor{url: remote_actor_url} =
|
|
|
|
remote_actor =
|
|
|
|
insert(:actor,
|
|
|
|
domain: "remote.domain",
|
|
|
|
url: "https://remote.domain/@remote",
|
|
|
|
preferred_username: "remote"
|
|
|
|
)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
group = insert(:group)
|
2021-09-24 16:46:42 +02:00
|
|
|
%Member{} = _member = insert(:member, actor: remote_actor, parent: group, role: :moderator)
|
2020-12-16 11:14:24 +01:00
|
|
|
%Post{} = post = insert(:post, attributed_to: group)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
data = Convertible.model_to_as(post)
|
|
|
|
refute is_nil(Posts.get_post_by_url(data["id"]))
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
object =
|
|
|
|
data
|
|
|
|
|> Map.put("name", "My updated post")
|
|
|
|
|> Map.put("type", "Article")
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
update_data =
|
|
|
|
update_data
|
|
|
|
|> Map.put("actor", remote_actor_url)
|
|
|
|
|> Map.put("object", object)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(update_data)
|
|
|
|
|
|
|
|
%Post{id: updated_post_id, title: updated_post_title} =
|
|
|
|
Posts.get_post_by_url(data["object"]["id"])
|
|
|
|
|
|
|
|
assert updated_post_id == post.id
|
|
|
|
assert updated_post_title == "My updated post"
|
2020-10-19 09:32:37 +02:00
|
|
|
end
|
|
|
|
|
2020-10-19 19:21:39 +02:00
|
|
|
test "it works for incoming update activities on group posts" do
|
2020-12-16 11:14:24 +01:00
|
|
|
%Actor{url: remote_actor_url} =
|
|
|
|
remote_actor =
|
|
|
|
insert(:actor,
|
|
|
|
domain: "remote.domain",
|
|
|
|
url: "https://remote.domain/@remote",
|
|
|
|
preferred_username: "remote"
|
|
|
|
)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
group = insert(:group)
|
2021-09-24 16:46:42 +02:00
|
|
|
%Member{} = _member = insert(:member, actor: remote_actor, parent: group)
|
2020-12-16 11:14:24 +01:00
|
|
|
%Post{} = post = insert(:post, attributed_to: group)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
data = Convertible.model_to_as(post)
|
|
|
|
refute is_nil(Posts.get_post_by_url(data["id"]))
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
object =
|
|
|
|
data
|
|
|
|
|> Map.put("name", "My updated post")
|
|
|
|
|> Map.put("type", "Article")
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
update_data =
|
|
|
|
update_data
|
|
|
|
|> Map.put("actor", remote_actor_url)
|
|
|
|
|> Map.put("object", object)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
:error = Transmogrifier.handle_incoming(update_data)
|
|
|
|
|
|
|
|
%Post{id: updated_post_id, title: updated_post_title} = Posts.get_post_by_url(data["id"])
|
|
|
|
|
|
|
|
assert updated_post_id == post.id
|
|
|
|
refute updated_post_title == "My updated post"
|
2020-10-19 09:32:37 +02:00
|
|
|
end
|
|
|
|
|
2020-10-19 19:21:39 +02:00
|
|
|
test "it fails for incoming update activities on group posts when the actor is not a member from the group" do
|
2020-12-16 11:14:24 +01:00
|
|
|
%Actor{url: remote_actor_url} =
|
|
|
|
insert(:actor,
|
|
|
|
domain: "remote.domain",
|
|
|
|
url: "https://remote.domain/@remote",
|
|
|
|
preferred_username: "remote"
|
|
|
|
)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
group = insert(:group)
|
|
|
|
%Post{} = post = insert(:post, attributed_to: group)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
data = Convertible.model_to_as(post)
|
|
|
|
refute is_nil(Posts.get_post_by_url(data["id"]))
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
object =
|
|
|
|
data
|
|
|
|
|> Map.put("name", "My updated post")
|
|
|
|
|> Map.put("type", "Article")
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
update_data =
|
|
|
|
update_data
|
|
|
|
|> Map.put("actor", remote_actor_url)
|
|
|
|
|> Map.put("object", object)
|
2020-10-19 19:21:39 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
:error = Transmogrifier.handle_incoming(update_data)
|
2020-10-19 19:21:39 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
%Post{id: updated_post_id, title: updated_post_title} = Posts.get_post_by_url(data["id"])
|
2020-10-19 19:21:39 +02:00
|
|
|
|
2020-12-16 11:14:24 +01:00
|
|
|
assert updated_post_id == post.id
|
|
|
|
refute updated_post_title == "My updated post"
|
2020-10-19 19:21:39 +02:00
|
|
|
end
|
2020-10-19 09:32:37 +02:00
|
|
|
end
|
|
|
|
end
|