1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.ActivityPub.Utils
14 alias Pleroma.Web.OStatus
15 alias Pleroma.Web.Websub.WebsubClientSubscription
17 import Pleroma.Factory
18 alias Pleroma.Web.CommonAPI
21 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
25 describe "handle_incoming" do
26 test "it ignores an incoming notice if we already have it" do
27 activity = insert(:note_activity)
30 File.read!("test/fixtures/mastodon-post-activity.json")
32 |> Map.put("object", activity.data["object"])
34 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
36 assert activity == returned_activity
39 test "it fetches replied-to activities if we don't have them" do
41 File.read!("test/fixtures/mastodon-post-activity.json")
46 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
50 |> Map.put("object", object)
52 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
53 returned_object = Object.normalize(returned_activity.data["object"])
56 Activity.get_create_by_object_ap_id(
57 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
60 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
63 test "it works for incoming notices" do
64 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
66 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
69 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
71 assert data["context"] ==
72 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
74 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
76 assert data["cc"] == [
77 "http://mastodon.example.org/users/admin/followers",
78 "http://localtesting.pleroma.lol/users/lain"
81 assert data["actor"] == "http://mastodon.example.org/users/admin"
83 object = Object.normalize(data["object"]).data
84 assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
86 assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
88 assert object["cc"] == [
89 "http://mastodon.example.org/users/admin/followers",
90 "http://localtesting.pleroma.lol/users/lain"
93 assert object["actor"] == "http://mastodon.example.org/users/admin"
94 assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
96 assert object["context"] ==
97 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
99 assert object["sensitive"] == true
101 user = User.get_by_ap_id(object["actor"])
103 assert user.info.note_count == 1
106 test "it works for incoming notices with hashtags" do
107 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
109 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
110 object = Object.normalize(data["object"])
112 assert Enum.at(object.data["tag"], 2) == "moo"
115 test "it works for incoming notices with contentMap" do
117 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
119 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
120 object = Object.normalize(data["object"])
122 assert object.data["content"] ==
123 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
126 test "it works for incoming notices with to/cc not being an array (kroeg)" do
127 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
129 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
130 object = Object.normalize(data["object"])
132 assert object.data["content"] ==
133 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
136 test "it works for incoming announces with actor being inlined (kroeg)" do
137 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
139 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
141 assert data["actor"] == "https://puckipedia.com/"
144 test "it works for incoming notices with tag not being an array (kroeg)" do
145 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
147 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
148 object = Object.normalize(data["object"])
150 assert object.data["emoji"] == %{
151 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
154 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
156 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
157 object = Object.normalize(data["object"])
159 assert "test" in object.data["tag"]
162 test "it works for incoming notices with url not being a string (prismo)" do
163 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
165 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
166 object = Object.normalize(data["object"])
168 assert object.data["url"] == "https://prismo.news/posts/83"
171 test "it cleans up incoming notices which are not really DMs" do
173 other_user = insert(:user)
175 to = [user.ap_id, other_user.ap_id]
178 File.read!("test/fixtures/mastodon-post-activity.json")
188 data = Map.put(data, "object", object)
190 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
192 assert data["to"] == []
193 assert data["cc"] == to
195 object_data = Object.normalize(activity).data
197 assert object_data["to"] == []
198 assert object_data["cc"] == to
201 test "it works for incoming follow requests" do
205 File.read!("test/fixtures/mastodon-follow-activity.json")
207 |> Map.put("object", user.ap_id)
209 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
211 assert data["actor"] == "http://mastodon.example.org/users/admin"
212 assert data["type"] == "Follow"
213 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
214 assert User.following?(User.get_by_ap_id(data["actor"]), user)
217 test "it works for incoming follow requests from hubzilla" do
221 File.read!("test/fixtures/hubzilla-follow-activity.json")
223 |> Map.put("object", user.ap_id)
224 |> Utils.normalize_params()
226 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
228 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
229 assert data["type"] == "Follow"
230 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
231 assert User.following?(User.get_by_ap_id(data["actor"]), user)
234 test "it works for incoming likes" do
236 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
239 File.read!("test/fixtures/mastodon-like.json")
241 |> Map.put("object", activity.data["object"])
243 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
245 assert data["actor"] == "http://mastodon.example.org/users/admin"
246 assert data["type"] == "Like"
247 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
248 assert data["object"] == activity.data["object"]
251 test "it returns an error for incoming unlikes wihout a like activity" do
253 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
256 File.read!("test/fixtures/mastodon-undo-like.json")
258 |> Map.put("object", activity.data["object"])
260 assert Transmogrifier.handle_incoming(data) == :error
263 test "it works for incoming unlikes with an existing like activity" do
265 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
268 File.read!("test/fixtures/mastodon-like.json")
270 |> Map.put("object", activity.data["object"])
272 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
275 File.read!("test/fixtures/mastodon-undo-like.json")
277 |> Map.put("object", like_data)
278 |> Map.put("actor", like_data["actor"])
280 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
282 assert data["actor"] == "http://mastodon.example.org/users/admin"
283 assert data["type"] == "Undo"
284 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
285 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
288 test "it works for incoming announces" do
289 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
291 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
293 assert data["actor"] == "http://mastodon.example.org/users/admin"
294 assert data["type"] == "Announce"
297 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
299 assert data["object"] ==
300 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
302 assert Activity.get_create_by_object_ap_id(data["object"])
305 test "it works for incoming announces with an existing activity" do
307 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
310 File.read!("test/fixtures/mastodon-announce.json")
312 |> Map.put("object", activity.data["object"])
314 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
316 assert data["actor"] == "http://mastodon.example.org/users/admin"
317 assert data["type"] == "Announce"
320 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
322 assert data["object"] == activity.data["object"]
324 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
327 test "it does not clobber the addressing on announce activities" do
329 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
332 File.read!("test/fixtures/mastodon-announce.json")
334 |> Map.put("object", Object.normalize(activity).data["id"])
335 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
338 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
340 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
343 test "it ensures that as:Public activities make it to their followers collection" do
347 File.read!("test/fixtures/mastodon-post-activity.json")
349 |> Map.put("actor", user.ap_id)
350 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
355 |> Map.put("attributedTo", user.ap_id)
356 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
359 data = Map.put(data, "object", object)
361 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
363 assert data["cc"] == [User.ap_followers(user)]
366 test "it ensures that address fields become lists" do
370 File.read!("test/fixtures/mastodon-post-activity.json")
372 |> Map.put("actor", user.ap_id)
373 |> Map.put("to", nil)
374 |> Map.put("cc", nil)
378 |> Map.put("attributedTo", user.ap_id)
379 |> Map.put("to", nil)
380 |> Map.put("cc", nil)
382 data = Map.put(data, "object", object)
384 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
386 assert !is_nil(data["to"])
387 assert !is_nil(data["cc"])
390 test "it works for incoming update activities" do
391 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
393 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
394 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
397 update_data["object"]
398 |> Map.put("actor", data["actor"])
399 |> Map.put("id", data["actor"])
403 |> Map.put("actor", data["actor"])
404 |> Map.put("object", object)
406 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
408 user = User.get_cached_by_ap_id(data["actor"])
409 assert user.name == "gargle"
411 assert user.avatar["url"] == [
414 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
418 assert user.info.banner["url"] == [
421 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
425 assert user.bio == "<p>Some bio</p>"
428 test "it works for incoming update activities which lock the account" do
429 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
431 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
432 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
435 update_data["object"]
436 |> Map.put("actor", data["actor"])
437 |> Map.put("id", data["actor"])
438 |> Map.put("manuallyApprovesFollowers", true)
442 |> Map.put("actor", data["actor"])
443 |> Map.put("object", object)
445 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
447 user = User.get_cached_by_ap_id(data["actor"])
448 assert user.info.locked == true
451 test "it works for incoming deletes" do
452 activity = insert(:note_activity)
455 File.read!("test/fixtures/mastodon-delete.json")
460 |> Map.put("id", activity.data["object"])
464 |> Map.put("object", object)
465 |> Map.put("actor", activity.data["actor"])
467 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
469 refute Activity.get_by_id(activity.id)
472 test "it fails for incoming deletes with spoofed origin" do
473 activity = insert(:note_activity)
476 File.read!("test/fixtures/mastodon-delete.json")
481 |> Map.put("id", activity.data["object"])
485 |> Map.put("object", object)
487 :error = Transmogrifier.handle_incoming(data)
489 assert Activity.get_by_id(activity.id)
492 test "it works for incoming unannounces with an existing notice" do
494 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
497 File.read!("test/fixtures/mastodon-announce.json")
499 |> Map.put("object", activity.data["object"])
501 {:ok, %Activity{data: announce_data, local: false}} =
502 Transmogrifier.handle_incoming(announce_data)
505 File.read!("test/fixtures/mastodon-undo-announce.json")
507 |> Map.put("object", announce_data)
508 |> Map.put("actor", announce_data["actor"])
510 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
512 assert data["type"] == "Undo"
513 assert data["object"]["type"] == "Announce"
514 assert data["object"]["object"] == activity.data["object"]
516 assert data["object"]["id"] ==
517 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
520 test "it works for incomming unfollows with an existing follow" do
524 File.read!("test/fixtures/mastodon-follow-activity.json")
526 |> Map.put("object", user.ap_id)
528 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
531 File.read!("test/fixtures/mastodon-unfollow-activity.json")
533 |> Map.put("object", follow_data)
535 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
537 assert data["type"] == "Undo"
538 assert data["object"]["type"] == "Follow"
539 assert data["object"]["object"] == user.ap_id
540 assert data["actor"] == "http://mastodon.example.org/users/admin"
542 refute User.following?(User.get_by_ap_id(data["actor"]), user)
545 test "it works for incoming blocks" do
549 File.read!("test/fixtures/mastodon-block-activity.json")
551 |> Map.put("object", user.ap_id)
553 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
555 assert data["type"] == "Block"
556 assert data["object"] == user.ap_id
557 assert data["actor"] == "http://mastodon.example.org/users/admin"
559 blocker = User.get_by_ap_id(data["actor"])
561 assert User.blocks?(blocker, user)
564 test "incoming blocks successfully tear down any follow relationship" do
565 blocker = insert(:user)
566 blocked = insert(:user)
569 File.read!("test/fixtures/mastodon-block-activity.json")
571 |> Map.put("object", blocked.ap_id)
572 |> Map.put("actor", blocker.ap_id)
574 {:ok, blocker} = User.follow(blocker, blocked)
575 {:ok, blocked} = User.follow(blocked, blocker)
577 assert User.following?(blocker, blocked)
578 assert User.following?(blocked, blocker)
580 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
582 assert data["type"] == "Block"
583 assert data["object"] == blocked.ap_id
584 assert data["actor"] == blocker.ap_id
586 blocker = User.get_by_ap_id(data["actor"])
587 blocked = User.get_by_ap_id(data["object"])
589 assert User.blocks?(blocker, blocked)
591 refute User.following?(blocker, blocked)
592 refute User.following?(blocked, blocker)
595 test "it works for incoming unblocks with an existing block" do
599 File.read!("test/fixtures/mastodon-block-activity.json")
601 |> Map.put("object", user.ap_id)
603 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
606 File.read!("test/fixtures/mastodon-unblock-activity.json")
608 |> Map.put("object", block_data)
610 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
611 assert data["type"] == "Undo"
612 assert data["object"]["type"] == "Block"
613 assert data["object"]["object"] == user.ap_id
614 assert data["actor"] == "http://mastodon.example.org/users/admin"
616 blocker = User.get_by_ap_id(data["actor"])
618 refute User.blocks?(blocker, user)
621 test "it works for incoming accepts which were pre-accepted" do
622 follower = insert(:user)
623 followed = insert(:user)
625 {:ok, follower} = User.follow(follower, followed)
626 assert User.following?(follower, followed) == true
628 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
631 File.read!("test/fixtures/mastodon-accept-activity.json")
633 |> Map.put("actor", followed.ap_id)
636 accept_data["object"]
637 |> Map.put("actor", follower.ap_id)
638 |> Map.put("id", follow_activity.data["id"])
640 accept_data = Map.put(accept_data, "object", object)
642 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
643 refute activity.local
645 assert activity.data["object"] == follow_activity.data["id"]
647 follower = User.get_by_id(follower.id)
649 assert User.following?(follower, followed) == true
652 test "it works for incoming accepts which were orphaned" do
653 follower = insert(:user)
654 followed = insert(:user, %{info: %User.Info{locked: true}})
656 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
659 File.read!("test/fixtures/mastodon-accept-activity.json")
661 |> Map.put("actor", followed.ap_id)
664 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
666 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
667 assert activity.data["object"] == follow_activity.data["id"]
669 follower = User.get_by_id(follower.id)
671 assert User.following?(follower, followed) == true
674 test "it works for incoming accepts which are referenced by IRI only" do
675 follower = insert(:user)
676 followed = insert(:user, %{info: %User.Info{locked: true}})
678 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
681 File.read!("test/fixtures/mastodon-accept-activity.json")
683 |> Map.put("actor", followed.ap_id)
684 |> Map.put("object", follow_activity.data["id"])
686 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
687 assert activity.data["object"] == follow_activity.data["id"]
689 follower = User.get_by_id(follower.id)
691 assert User.following?(follower, followed) == true
694 test "it fails for incoming accepts which cannot be correlated" do
695 follower = insert(:user)
696 followed = insert(:user, %{info: %User.Info{locked: true}})
699 File.read!("test/fixtures/mastodon-accept-activity.json")
701 |> Map.put("actor", followed.ap_id)
704 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
706 :error = Transmogrifier.handle_incoming(accept_data)
708 follower = User.get_by_id(follower.id)
710 refute User.following?(follower, followed) == true
713 test "it fails for incoming rejects which cannot be correlated" do
714 follower = insert(:user)
715 followed = insert(:user, %{info: %User.Info{locked: true}})
718 File.read!("test/fixtures/mastodon-reject-activity.json")
720 |> Map.put("actor", followed.ap_id)
723 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
725 :error = Transmogrifier.handle_incoming(accept_data)
727 follower = User.get_by_id(follower.id)
729 refute User.following?(follower, followed) == true
732 test "it works for incoming rejects which are orphaned" do
733 follower = insert(:user)
734 followed = insert(:user, %{info: %User.Info{locked: true}})
736 {:ok, follower} = User.follow(follower, followed)
737 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
739 assert User.following?(follower, followed) == true
742 File.read!("test/fixtures/mastodon-reject-activity.json")
744 |> Map.put("actor", followed.ap_id)
747 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
749 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
750 refute activity.local
752 follower = User.get_by_id(follower.id)
754 assert User.following?(follower, followed) == false
757 test "it works for incoming rejects which are referenced by IRI only" do
758 follower = insert(:user)
759 followed = insert(:user, %{info: %User.Info{locked: true}})
761 {:ok, follower} = User.follow(follower, followed)
762 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
764 assert User.following?(follower, followed) == true
767 File.read!("test/fixtures/mastodon-reject-activity.json")
769 |> Map.put("actor", followed.ap_id)
770 |> Map.put("object", follow_activity.data["id"])
772 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
774 follower = User.get_by_id(follower.id)
776 assert User.following?(follower, followed) == false
779 test "it rejects activities without a valid ID" do
783 File.read!("test/fixtures/mastodon-follow-activity.json")
785 |> Map.put("object", user.ap_id)
788 :error = Transmogrifier.handle_incoming(data)
791 test "it remaps video URLs as attachments if necessary" do
793 ActivityPub.fetch_object_from_id(
794 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
799 "mediaType" => "video/mp4",
801 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
802 "mimeType" => "video/mp4",
807 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
808 "mediaType" => "video/mp4",
815 assert object.data["url"] ==
816 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
818 assert object.data["attachment"] == [attachment]
821 test "it accepts Flag activities" do
823 other_user = insert(:user)
825 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
826 object = Object.normalize(activity.data["object"])
829 "@context" => "https://www.w3.org/ns/activitystreams",
830 "cc" => [user.ap_id],
831 "object" => [user.ap_id, object.data["id"]],
833 "content" => "blocked AND reported!!!",
834 "actor" => other_user.ap_id
837 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
839 assert activity.data["object"] == [user.ap_id, object.data["id"]]
840 assert activity.data["content"] == "blocked AND reported!!!"
841 assert activity.data["actor"] == other_user.ap_id
842 assert activity.data["cc"] == [user.ap_id]
846 describe "prepare outgoing" do
847 test "it turns mentions into tags" do
849 other_user = insert(:user)
852 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
854 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
855 object = modified["object"]
857 expected_mention = %{
858 "href" => other_user.ap_id,
859 "name" => "@#{other_user.nickname}",
864 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
869 assert Enum.member?(object["tag"], expected_tag)
870 assert Enum.member?(object["tag"], expected_mention)
873 test "it adds the sensitive property" do
876 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
877 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
879 assert modified["object"]["sensitive"]
882 test "it adds the json-ld context and the conversation property" do
885 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
886 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
888 assert modified["@context"] ==
889 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
891 assert modified["object"]["conversation"] == modified["context"]
894 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
897 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
898 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
900 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
903 test "it translates ostatus IDs to external URLs" do
904 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
905 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
909 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
910 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
912 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
915 test "it translates ostatus reply_to IDs to external URLs" do
916 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
917 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
922 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
924 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
926 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
929 test "it strips internal hashtag data" do
932 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
935 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
940 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
942 assert modified["object"]["tag"] == [expected_tag]
945 test "it strips internal fields" do
948 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :moominmamma:"})
950 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
952 assert length(modified["object"]["tag"]) == 2
954 assert is_nil(modified["object"]["emoji"])
955 assert is_nil(modified["object"]["like_count"])
956 assert is_nil(modified["object"]["announcements"])
957 assert is_nil(modified["object"]["announcement_count"])
958 assert is_nil(modified["object"]["context_id"])
961 test "it strips internal fields of article" do
962 activity = insert(:article_activity)
964 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
966 assert length(modified["object"]["tag"]) == 2
968 assert is_nil(modified["object"]["emoji"])
969 assert is_nil(modified["object"]["like_count"])
970 assert is_nil(modified["object"]["announcements"])
971 assert is_nil(modified["object"]["announcement_count"])
972 assert is_nil(modified["object"]["context_id"])
975 test "it adds like collection to object" do
976 activity = insert(:note_activity)
977 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
979 assert modified["object"]["likes"]["type"] == "OrderedCollection"
980 assert modified["object"]["likes"]["totalItems"] == 0
983 test "the directMessage flag is present" do
985 other_user = insert(:user)
987 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
989 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
991 assert modified["directMessage"] == false
994 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
996 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
998 assert modified["directMessage"] == false
1001 CommonAPI.post(user, %{
1002 "status" => "@#{other_user.nickname} :moominmamma:",
1003 "visibility" => "direct"
1006 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1008 assert modified["directMessage"] == true
1012 describe "user upgrade" do
1013 test "it upgrades a user to activitypub" do
1016 nickname: "rye@niu.moe",
1018 ap_id: "https://niu.moe/users/rye",
1019 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1022 user_two = insert(:user, %{following: [user.follower_address]})
1024 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1025 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1026 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1028 user = User.get_by_id(user.id)
1029 assert user.info.note_count == 1
1031 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1032 assert user.info.ap_enabled
1033 assert user.info.note_count == 1
1034 assert user.follower_address == "https://niu.moe/users/rye/followers"
1036 user = User.get_by_id(user.id)
1037 assert user.info.note_count == 1
1039 activity = Activity.get_by_id(activity.id)
1040 assert user.follower_address in activity.recipients
1046 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1055 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1058 } = user.info.banner
1060 refute "..." in activity.recipients
1062 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1063 refute user.follower_address in unrelated_activity.recipients
1065 user_two = User.get_by_id(user_two.id)
1066 assert user.follower_address in user_two.following
1067 refute "..." in user_two.following
1071 describe "maybe_retire_websub" do
1072 test "it deletes all websub client subscripitions with the user as topic" do
1073 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1074 {:ok, ws} = Repo.insert(subscription)
1076 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1077 {:ok, ws2} = Repo.insert(subscription)
1079 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1081 refute Repo.get(WebsubClientSubscription, ws.id)
1082 assert Repo.get(WebsubClientSubscription, ws2.id)
1086 describe "actor rewriting" do
1087 test "it fixes the actor URL property to be a proper URI" do
1089 "url" => %{"href" => "http://example.com"}
1092 rewritten = Transmogrifier.maybe_fix_user_object(data)
1093 assert rewritten["url"] == "http://example.com"
1097 describe "actor origin containment" do
1098 test "it rejects activities which reference objects with bogus origins" do
1100 "@context" => "https://www.w3.org/ns/activitystreams",
1101 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1102 "actor" => "http://mastodon.example.org/users/admin",
1103 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1104 "object" => "https://info.pleroma.site/activity.json",
1105 "type" => "Announce"
1108 :error = Transmogrifier.handle_incoming(data)
1111 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1113 "@context" => "https://www.w3.org/ns/activitystreams",
1114 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1115 "actor" => "http://mastodon.example.org/users/admin",
1116 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1117 "object" => "https://info.pleroma.site/activity2.json",
1118 "type" => "Announce"
1121 :error = Transmogrifier.handle_incoming(data)
1124 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1126 "@context" => "https://www.w3.org/ns/activitystreams",
1127 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1128 "actor" => "http://mastodon.example.org/users/admin",
1129 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1130 "object" => "https://info.pleroma.site/activity3.json",
1131 "type" => "Announce"
1134 :error = Transmogrifier.handle_incoming(data)
1138 describe "general origin containment" do
1139 test "contain_origin_from_id() catches obvious spoofing attempts" do
1141 "id" => "http://example.com/~alyssa/activities/1234.json"
1145 Transmogrifier.contain_origin_from_id(
1146 "http://example.org/~alyssa/activities/1234.json",
1151 test "contain_origin_from_id() allows alternate IDs within the same origin domain" do
1153 "id" => "http://example.com/~alyssa/activities/1234.json"
1157 Transmogrifier.contain_origin_from_id(
1158 "http://example.com/~alyssa/activities/1234",
1163 test "contain_origin_from_id() allows matching IDs" do
1165 "id" => "http://example.com/~alyssa/activities/1234.json"
1169 Transmogrifier.contain_origin_from_id(
1170 "http://example.com/~alyssa/activities/1234.json",
1175 test "users cannot be collided through fake direction spoofing attempts" do
1177 nickname: "rye@niu.moe",
1179 ap_id: "https://niu.moe/users/rye",
1180 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1183 {:error, _} = User.get_or_fetch_by_ap_id("https://n1u.moe/users/rye")
1186 test "all objects with fake directions are rejected by the object fetcher" do
1188 ActivityPub.fetch_and_contain_remote_object_from_id(
1189 "https://info.pleroma.site/activity4.json"
1194 describe "reserialization" do
1195 test "successfully reserializes a message with inReplyTo == nil" do
1196 user = insert(:user)
1199 "@context" => "https://www.w3.org/ns/activitystreams",
1200 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1204 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1209 "attributedTo" => user.ap_id
1211 "actor" => user.ap_id
1214 {:ok, activity} = Transmogrifier.handle_incoming(message)
1216 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1219 test "successfully reserializes a message with AS2 objects in IR" do
1220 user = insert(:user)
1223 "@context" => "https://www.w3.org/ns/activitystreams",
1224 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1228 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1233 "attributedTo" => user.ap_id,
1235 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1236 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1239 "actor" => user.ap_id
1242 {:ok, activity} = Transmogrifier.handle_incoming(message)
1244 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)