1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
6 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.Tests.ObanHelpers
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.AdminAPI.AccountView
15 alias Pleroma.Web.CommonAPI
18 import Pleroma.Factory
19 import ExUnit.CaptureLog
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 setup do: clear_config([:instance, :max_remote_account_fields])
28 describe "handle_incoming" do
29 test "it works for incoming notices with tag not being an array (kroeg)" do
30 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
32 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
33 object = Object.normalize(data["object"])
35 assert object.data["emoji"] == %{
36 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
39 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
41 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
42 object = Object.normalize(data["object"])
44 assert "test" in object.data["tag"]
47 test "it cleans up incoming notices which are not really DMs" do
49 other_user = insert(:user)
51 to = [user.ap_id, other_user.ap_id]
54 File.read!("test/fixtures/mastodon-post-activity.json")
64 data = Map.put(data, "object", object)
66 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
68 assert data["to"] == []
69 assert data["cc"] == to
71 object_data = Object.normalize(activity).data
73 assert object_data["to"] == []
74 assert object_data["cc"] == to
77 test "it ignores an incoming notice if we already have it" do
78 activity = insert(:note_activity)
81 File.read!("test/fixtures/mastodon-post-activity.json")
83 |> Map.put("object", Object.normalize(activity).data)
85 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
87 assert activity == returned_activity
90 @tag capture_log: true
91 test "it fetches reply-to activities if we don't have them" do
93 File.read!("test/fixtures/mastodon-post-activity.json")
98 |> Map.put("inReplyTo", "https://mstdn.io/users/mayuutann/statuses/99568293732299394")
100 data = Map.put(data, "object", object)
101 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
102 returned_object = Object.normalize(returned_activity, false)
105 Activity.get_create_by_object_ap_id(
106 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
109 assert returned_object.data["inReplyTo"] ==
110 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
113 test "it does not fetch reply-to activities beyond max replies depth limit" do
115 File.read!("test/fixtures/mastodon-post-activity.json")
120 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
122 data = Map.put(data, "object", object)
124 with_mock Pleroma.Web.Federator,
125 allowed_thread_distance?: fn _ -> false end do
126 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
128 returned_object = Object.normalize(returned_activity, false)
130 refute Activity.get_create_by_object_ap_id(
131 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
134 assert returned_object.data["inReplyTo"] == "https://shitposter.club/notice/2827873"
138 test "it does not crash if the object in inReplyTo can't be fetched" do
140 File.read!("test/fixtures/mastodon-post-activity.json")
145 |> Map.put("inReplyTo", "https://404.site/whatever")
149 |> Map.put("object", object)
151 assert capture_log(fn ->
152 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
153 end) =~ "[warn] Couldn't fetch \"https://404.site/whatever\", error: nil"
156 test "it does not work for deactivated users" do
157 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
159 insert(:user, ap_id: data["actor"], deactivated: true)
161 assert {:error, _} = Transmogrifier.handle_incoming(data)
164 test "it works for incoming notices" do
165 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
167 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
170 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
172 assert data["context"] ==
173 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
175 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
177 assert data["cc"] == [
178 "http://mastodon.example.org/users/admin/followers",
179 "http://localtesting.pleroma.lol/users/lain"
182 assert data["actor"] == "http://mastodon.example.org/users/admin"
184 object_data = Object.normalize(data["object"]).data
186 assert object_data["id"] ==
187 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
189 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
191 assert object_data["cc"] == [
192 "http://mastodon.example.org/users/admin/followers",
193 "http://localtesting.pleroma.lol/users/lain"
196 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
197 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
199 assert object_data["context"] ==
200 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
202 assert object_data["sensitive"] == true
204 user = User.get_cached_by_ap_id(object_data["actor"])
206 assert user.note_count == 1
209 test "it works for incoming notices without the sensitive property but an nsfw hashtag" do
210 data = File.read!("test/fixtures/mastodon-post-activity-nsfw.json") |> Poison.decode!()
212 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
214 object_data = Object.normalize(data["object"], false).data
216 assert object_data["sensitive"] == true
219 test "it works for incoming notices with hashtags" do
220 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
222 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
223 object = Object.normalize(data["object"])
225 assert Enum.at(object.data["tag"], 2) == "moo"
228 test "it works for incoming notices with contentMap" do
230 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
232 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
233 object = Object.normalize(data["object"])
235 assert object.data["content"] ==
236 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
239 test "it works for incoming notices with to/cc not being an array (kroeg)" do
240 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
242 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
243 object = Object.normalize(data["object"])
245 assert object.data["content"] ==
246 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
249 test "it ensures that as:Public activities make it to their followers collection" do
253 File.read!("test/fixtures/mastodon-post-activity.json")
255 |> Map.put("actor", user.ap_id)
256 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
261 |> Map.put("attributedTo", user.ap_id)
262 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
264 |> Map.put("id", user.ap_id <> "/activities/12345678")
266 data = Map.put(data, "object", object)
268 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
270 assert data["cc"] == [User.ap_followers(user)]
273 test "it ensures that address fields become lists" do
277 File.read!("test/fixtures/mastodon-post-activity.json")
279 |> Map.put("actor", user.ap_id)
280 |> Map.put("to", nil)
281 |> Map.put("cc", nil)
285 |> Map.put("attributedTo", user.ap_id)
286 |> Map.put("to", nil)
287 |> Map.put("cc", nil)
288 |> Map.put("id", user.ap_id <> "/activities/12345678")
290 data = Map.put(data, "object", object)
292 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
294 assert !is_nil(data["to"])
295 assert !is_nil(data["cc"])
298 test "it strips internal likes" do
300 File.read!("test/fixtures/mastodon-post-activity.json")
305 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
306 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
308 "type" => "OrderedCollection"
311 object = Map.put(data["object"], "likes", likes)
312 data = Map.put(data, "object", object)
314 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
316 refute Map.has_key?(object.data, "likes")
319 test "it strips internal reactions" do
321 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
322 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
324 %{object: object} = Activity.get_by_id_with_object(activity.id)
325 assert Map.has_key?(object.data, "reactions")
326 assert Map.has_key?(object.data, "reaction_count")
328 object_data = Transmogrifier.strip_internal_fields(object.data)
329 refute Map.has_key?(object_data, "reactions")
330 refute Map.has_key?(object_data, "reaction_count")
333 test "it works for incoming unfollows with an existing follow" do
337 File.read!("test/fixtures/mastodon-follow-activity.json")
339 |> Map.put("object", user.ap_id)
341 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
344 File.read!("test/fixtures/mastodon-unfollow-activity.json")
346 |> Map.put("object", follow_data)
348 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
350 assert data["type"] == "Undo"
351 assert data["object"]["type"] == "Follow"
352 assert data["object"]["object"] == user.ap_id
353 assert data["actor"] == "http://mastodon.example.org/users/admin"
355 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
358 test "it accepts Flag activities" do
360 other_user = insert(:user)
362 {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
363 object = Object.normalize(activity)
367 "id" => activity.data["id"],
368 "content" => "test post",
369 "published" => object.data["published"],
370 "actor" => AccountView.render("show.json", %{user: user, skip_visibility_check: true})
374 "@context" => "https://www.w3.org/ns/activitystreams",
375 "cc" => [user.ap_id],
376 "object" => [user.ap_id, activity.data["id"]],
378 "content" => "blocked AND reported!!!",
379 "actor" => other_user.ap_id
382 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
384 assert activity.data["object"] == [user.ap_id, note_obj]
385 assert activity.data["content"] == "blocked AND reported!!!"
386 assert activity.data["actor"] == other_user.ap_id
387 assert activity.data["cc"] == [user.ap_id]
390 test "it correctly processes messages with non-array to field" do
394 "@context" => "https://www.w3.org/ns/activitystreams",
395 "to" => "https://www.w3.org/ns/activitystreams#Public",
398 "content" => "blah blah blah",
400 "attributedTo" => user.ap_id,
403 "actor" => user.ap_id
406 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
408 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
411 test "it correctly processes messages with non-array cc field" do
415 "@context" => "https://www.w3.org/ns/activitystreams",
416 "to" => user.follower_address,
417 "cc" => "https://www.w3.org/ns/activitystreams#Public",
420 "content" => "blah blah blah",
422 "attributedTo" => user.ap_id,
425 "actor" => user.ap_id
428 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
430 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
431 assert [user.follower_address] == activity.data["to"]
434 test "it correctly processes messages with weirdness in address fields" do
438 "@context" => "https://www.w3.org/ns/activitystreams",
439 "to" => [nil, user.follower_address],
440 "cc" => ["https://www.w3.org/ns/activitystreams#Public", ["¿"]],
445 "attributedTo" => user.ap_id,
448 "actor" => user.ap_id
451 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
453 assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"]
454 assert [user.follower_address] == activity.data["to"]
457 test "it accepts Move activities" do
458 old_user = insert(:user)
459 new_user = insert(:user)
462 "@context" => "https://www.w3.org/ns/activitystreams",
464 "actor" => old_user.ap_id,
465 "object" => old_user.ap_id,
466 "target" => new_user.ap_id
469 assert :error = Transmogrifier.handle_incoming(message)
471 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
473 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
474 assert activity.actor == old_user.ap_id
475 assert activity.data["actor"] == old_user.ap_id
476 assert activity.data["object"] == old_user.ap_id
477 assert activity.data["target"] == new_user.ap_id
478 assert activity.data["type"] == "Move"
482 describe "`handle_incoming/2`, Mastodon format `replies` handling" do
483 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
484 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
488 "test/fixtures/mastodon-post-activity.json"
492 items = get_in(data, ["object", "replies", "first", "items"])
493 assert length(items) > 0
495 %{data: data, items: items}
498 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
502 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
504 {:ok, _activity} = Transmogrifier.handle_incoming(data)
507 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
508 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
512 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
514 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
516 {:ok, _activity} = Transmogrifier.handle_incoming(data)
518 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
522 describe "`handle_incoming/2`, Pleroma format `replies` handling" do
523 setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
524 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
529 {:ok, activity} = CommonAPI.post(user, %{status: "post1"})
532 CommonAPI.post(user, %{status: "reply1", in_reply_to_status_id: activity.id})
535 CommonAPI.post(user, %{status: "reply2", in_reply_to_status_id: activity.id})
537 replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end)
539 {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data)
541 Repo.delete(activity.object)
542 Repo.delete(activity)
544 %{federation_output: federation_output, replies_uris: replies_uris}
547 test "schedules background fetching of `replies` items if max thread depth limit allows", %{
548 federation_output: federation_output,
549 replies_uris: replies_uris
551 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
553 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
555 for id <- replies_uris do
556 job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
557 assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
561 test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
562 %{federation_output: federation_output} do
563 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
565 {:ok, _activity} = Transmogrifier.handle_incoming(federation_output)
567 assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
571 describe "prepare outgoing" do
572 test "it inlines private announced objects" do
575 {:ok, activity} = CommonAPI.post(user, %{status: "hey", visibility: "private"})
577 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
579 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
581 assert modified["object"]["content"] == "hey"
582 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
585 test "it turns mentions into tags" do
587 other_user = insert(:user)
590 CommonAPI.post(user, %{status: "hey, @#{other_user.nickname}, how are ya? #2hu"})
592 with_mock Pleroma.Notification,
593 get_notified_from_activity: fn _, _ -> [] end do
594 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
596 object = modified["object"]
598 expected_mention = %{
599 "href" => other_user.ap_id,
600 "name" => "@#{other_user.nickname}",
605 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
610 refute called(Pleroma.Notification.get_notified_from_activity(:_, :_))
611 assert Enum.member?(object["tag"], expected_tag)
612 assert Enum.member?(object["tag"], expected_mention)
616 test "it adds the sensitive property" do
619 {:ok, activity} = CommonAPI.post(user, %{status: "#nsfw hey"})
620 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
622 assert modified["object"]["sensitive"]
625 test "it adds the json-ld context and the conversation property" do
628 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
629 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
631 assert modified["@context"] ==
632 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
634 assert modified["object"]["conversation"] == modified["context"]
637 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
640 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
641 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
643 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
646 test "it strips internal hashtag data" do
649 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu"})
652 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
657 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
659 assert modified["object"]["tag"] == [expected_tag]
662 test "it strips internal fields" do
665 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu :firefox:"})
667 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
669 assert length(modified["object"]["tag"]) == 2
671 assert is_nil(modified["object"]["emoji"])
672 assert is_nil(modified["object"]["like_count"])
673 assert is_nil(modified["object"]["announcements"])
674 assert is_nil(modified["object"]["announcement_count"])
675 assert is_nil(modified["object"]["context_id"])
678 test "it strips internal fields of article" do
679 activity = insert(:article_activity)
681 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
683 assert length(modified["object"]["tag"]) == 2
685 assert is_nil(modified["object"]["emoji"])
686 assert is_nil(modified["object"]["like_count"])
687 assert is_nil(modified["object"]["announcements"])
688 assert is_nil(modified["object"]["announcement_count"])
689 assert is_nil(modified["object"]["context_id"])
690 assert is_nil(modified["object"]["likes"])
693 test "the directMessage flag is present" do
695 other_user = insert(:user)
697 {:ok, activity} = CommonAPI.post(user, %{status: "2hu :moominmamma:"})
699 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
701 assert modified["directMessage"] == false
703 {:ok, activity} = CommonAPI.post(user, %{status: "@#{other_user.nickname} :moominmamma:"})
705 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
707 assert modified["directMessage"] == false
710 CommonAPI.post(user, %{
711 status: "@#{other_user.nickname} :moominmamma:",
715 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
717 assert modified["directMessage"] == true
720 test "it strips BCC field" do
722 {:ok, list} = Pleroma.List.create("foo", user)
724 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
726 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
728 assert is_nil(modified["bcc"])
731 test "it can handle Listen activities" do
732 listen_activity = insert(:listen)
734 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
736 assert modified["type"] == "Listen"
740 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
742 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
746 describe "user upgrade" do
747 test "it upgrades a user to activitypub" do
750 nickname: "rye@niu.moe",
752 ap_id: "https://niu.moe/users/rye",
753 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
756 user_two = insert(:user)
757 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
759 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
760 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{status: "test"})
761 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
763 user = User.get_cached_by_id(user.id)
764 assert user.note_count == 1
766 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
767 ObanHelpers.perform_all()
769 assert user.ap_enabled
770 assert user.note_count == 1
771 assert user.follower_address == "https://niu.moe/users/rye/followers"
772 assert user.following_address == "https://niu.moe/users/rye/following"
774 user = User.get_cached_by_id(user.id)
775 assert user.note_count == 1
777 activity = Activity.get_by_id(activity.id)
778 assert user.follower_address in activity.recipients
784 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
793 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
798 refute "..." in activity.recipients
800 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
801 refute user.follower_address in unrelated_activity.recipients
803 user_two = User.get_cached_by_id(user_two.id)
804 assert User.following?(user_two, user)
805 refute "..." in User.following(user_two)
809 describe "actor rewriting" do
810 test "it fixes the actor URL property to be a proper URI" do
812 "url" => %{"href" => "http://example.com"}
815 rewritten = Transmogrifier.maybe_fix_user_object(data)
816 assert rewritten["url"] == "http://example.com"
820 describe "actor origin containment" do
821 test "it rejects activities which reference objects with bogus origins" do
823 "@context" => "https://www.w3.org/ns/activitystreams",
824 "id" => "http://mastodon.example.org/users/admin/activities/1234",
825 "actor" => "http://mastodon.example.org/users/admin",
826 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
827 "object" => "https://info.pleroma.site/activity.json",
831 assert capture_log(fn ->
832 {:error, _} = Transmogrifier.handle_incoming(data)
833 end) =~ "Object containment failed"
836 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
838 "@context" => "https://www.w3.org/ns/activitystreams",
839 "id" => "http://mastodon.example.org/users/admin/activities/1234",
840 "actor" => "http://mastodon.example.org/users/admin",
841 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
842 "object" => "https://info.pleroma.site/activity2.json",
846 assert capture_log(fn ->
847 {:error, _} = Transmogrifier.handle_incoming(data)
848 end) =~ "Object containment failed"
851 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
853 "@context" => "https://www.w3.org/ns/activitystreams",
854 "id" => "http://mastodon.example.org/users/admin/activities/1234",
855 "actor" => "http://mastodon.example.org/users/admin",
856 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
857 "object" => "https://info.pleroma.site/activity3.json",
861 assert capture_log(fn ->
862 {:error, _} = Transmogrifier.handle_incoming(data)
863 end) =~ "Object containment failed"
867 describe "reserialization" do
868 test "successfully reserializes a message with inReplyTo == nil" do
872 "@context" => "https://www.w3.org/ns/activitystreams",
873 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
877 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
882 "attributedTo" => user.ap_id
884 "actor" => user.ap_id
887 {:ok, activity} = Transmogrifier.handle_incoming(message)
889 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
892 test "successfully reserializes a message with AS2 objects in IR" do
896 "@context" => "https://www.w3.org/ns/activitystreams",
897 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
901 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
906 "attributedTo" => user.ap_id,
908 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
909 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
912 "actor" => user.ap_id
915 {:ok, activity} = Transmogrifier.handle_incoming(message)
917 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
921 describe "fix_explicit_addressing" do
927 test "moves non-explicitly mentioned actors to cc", %{user: user} do
928 explicitly_mentioned_actors = [
929 "https://pleroma.gold/users/user1",
930 "https://pleroma.gold/user2"
934 "actor" => user.ap_id,
935 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
938 Enum.map(explicitly_mentioned_actors, fn href ->
939 %{"type" => "Mention", "href" => href}
943 fixed_object = Transmogrifier.fix_explicit_addressing(object)
944 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
945 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
946 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
949 test "does not move actor's follower collection to cc", %{user: user} do
951 "actor" => user.ap_id,
952 "to" => [user.follower_address],
956 fixed_object = Transmogrifier.fix_explicit_addressing(object)
957 assert user.follower_address in fixed_object["to"]
958 refute user.follower_address in fixed_object["cc"]
961 test "removes recipient's follower collection from cc", %{user: user} do
962 recipient = insert(:user)
965 "actor" => user.ap_id,
966 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
967 "cc" => [user.follower_address, recipient.follower_address]
970 fixed_object = Transmogrifier.fix_explicit_addressing(object)
972 assert user.follower_address in fixed_object["cc"]
973 refute recipient.follower_address in fixed_object["cc"]
974 refute recipient.follower_address in fixed_object["to"]
978 describe "fix_summary/1" do
979 test "returns fixed object" do
980 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
981 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
982 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
986 describe "fix_in_reply_to/2" do
987 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
990 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
994 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
995 assert Transmogrifier.fix_in_reply_to(data) == data
998 test "returns object with inReplyTo when denied incoming reply", %{data: data} do
999 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1002 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1004 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1005 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1008 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1010 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1011 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1014 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1016 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1017 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1019 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1020 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1021 assert modified_object["inReplyTo"] == []
1024 @tag capture_log: true
1025 test "returns modified object when allowed incoming reply", %{data: data} do
1030 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
1033 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1034 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1036 assert modified_object["inReplyTo"] ==
1037 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
1039 assert modified_object["context"] ==
1040 "tag:shitposter.club,2018-02-22:objectType=thread:nonce=e5a7c72d60a9c0e4"
1044 describe "fix_url/1" do
1045 test "fixes data for object when url is map" do
1049 "mimeType" => "video/mp4",
1050 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1054 assert Transmogrifier.fix_url(object) == %{
1055 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1059 test "returns non-modified object" do
1060 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1064 describe "get_obj_helper/2" do
1065 test "returns nil when cannot normalize object" do
1066 assert capture_log(fn ->
1067 refute Transmogrifier.get_obj_helper("test-obj-id")
1068 end) =~ "Unsupported URI scheme"
1071 @tag capture_log: true
1072 test "returns {:ok, %Object{}} for success case" do
1073 assert {:ok, %Object{}} =
1074 Transmogrifier.get_obj_helper(
1075 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
1080 describe "fix_attachments/1" do
1081 test "returns not modified object" do
1082 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1083 assert Transmogrifier.fix_attachments(data) == data
1086 test "returns modified object when attachment is map" do
1087 assert Transmogrifier.fix_attachments(%{
1089 "mediaType" => "video/mp4",
1090 "url" => "https://peertube.moe/stat-480.mp4"
1095 "mediaType" => "video/mp4",
1096 "type" => "Document",
1099 "href" => "https://peertube.moe/stat-480.mp4",
1100 "mediaType" => "video/mp4",
1109 test "returns modified object when attachment is list" do
1110 assert Transmogrifier.fix_attachments(%{
1112 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1113 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1118 "mediaType" => "video/mp4",
1119 "type" => "Document",
1122 "href" => "https://pe.er/stat-480.mp4",
1123 "mediaType" => "video/mp4",
1129 "mediaType" => "video/mp4",
1130 "type" => "Document",
1133 "href" => "https://pe.er/stat-480.mp4",
1134 "mediaType" => "video/mp4",
1144 describe "fix_emoji/1" do
1145 test "returns not modified object when object not contains tags" do
1146 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1147 assert Transmogrifier.fix_emoji(data) == data
1150 test "returns object with emoji when object contains list tags" do
1151 assert Transmogrifier.fix_emoji(%{
1153 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1154 %{"type" => "Hashtag"}
1157 "emoji" => %{"bib" => "/test"},
1159 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1160 %{"type" => "Hashtag"}
1165 test "returns object with emoji when object contains map tag" do
1166 assert Transmogrifier.fix_emoji(%{
1167 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1169 "emoji" => %{"bib" => "/test"},
1170 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1175 describe "set_replies/1" do
1176 setup do: clear_config([:activitypub, :note_replies_output_limit], 2)
1178 test "returns unmodified object if activity doesn't have self-replies" do
1179 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1180 assert Transmogrifier.set_replies(data) == data
1183 test "sets `replies` collection with a limited number of self-replies" do
1184 [user, another_user] = insert_list(2, :user)
1186 {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{status: "1"})
1188 {:ok, %{id: id2} = self_reply1} =
1189 CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: id1})
1191 {:ok, self_reply2} =
1192 CommonAPI.post(user, %{status: "self-reply 2", in_reply_to_status_id: id1})
1194 # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
1195 {:ok, _} = CommonAPI.post(user, %{status: "self-reply 3", in_reply_to_status_id: id1})
1198 CommonAPI.post(user, %{
1199 status: "self-reply to self-reply",
1200 in_reply_to_status_id: id2
1204 CommonAPI.post(another_user, %{
1205 status: "another user's reply",
1206 in_reply_to_status_id: id1
1209 object = Object.normalize(activity)
1210 replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
1212 assert %{"type" => "Collection", "items" => ^replies_uris} =
1213 Transmogrifier.set_replies(object.data)["replies"]
1217 test "take_emoji_tags/1" do
1218 user = insert(:user, %{emoji: %{"firefox" => "https://example.org/firefox.png"}})
1220 assert Transmogrifier.take_emoji_tags(user) == [
1222 "icon" => %{"type" => "Image", "url" => "https://example.org/firefox.png"},
1223 "id" => "https://example.org/firefox.png",
1224 "name" => ":firefox:",
1226 "updated" => "1970-01-01T00:00:00Z"