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
9 alias Pleroma.Object.Fetcher
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.OStatus
16 alias Pleroma.Web.Websub.WebsubClientSubscription
18 import Pleroma.Factory
19 alias Pleroma.Web.CommonAPI
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 describe "handle_incoming" do
27 test "it ignores an incoming notice if we already have it" do
28 activity = insert(:note_activity)
31 File.read!("test/fixtures/mastodon-post-activity.json")
33 |> Map.put("object", activity.data["object"])
35 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
37 assert activity == returned_activity
40 test "it fetches replied-to activities if we don't have them" do
42 File.read!("test/fixtures/mastodon-post-activity.json")
47 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
51 |> Map.put("object", object)
53 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
54 returned_object = Object.normalize(returned_activity.data["object"])
57 Activity.get_create_by_object_ap_id(
58 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
61 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
64 test "it works for incoming notices" do
65 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
67 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
70 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
72 assert data["context"] ==
73 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
75 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
77 assert data["cc"] == [
78 "http://mastodon.example.org/users/admin/followers",
79 "http://localtesting.pleroma.lol/users/lain"
82 assert data["actor"] == "http://mastodon.example.org/users/admin"
84 object = Object.normalize(data["object"]).data
85 assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
87 assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
89 assert object["cc"] == [
90 "http://mastodon.example.org/users/admin/followers",
91 "http://localtesting.pleroma.lol/users/lain"
94 assert object["actor"] == "http://mastodon.example.org/users/admin"
95 assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
97 assert object["context"] ==
98 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
100 assert object["sensitive"] == true
102 user = User.get_cached_by_ap_id(object["actor"])
104 assert user.info.note_count == 1
107 test "it works for incoming notices with hashtags" do
108 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
110 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
111 object = Object.normalize(data["object"])
113 assert Enum.at(object.data["tag"], 2) == "moo"
116 test "it works for incoming questions" do
117 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
119 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
121 object = Object.normalize(activity)
123 assert Enum.all?(object.data["oneOf"], fn choice ->
126 "Everyone knows that!",
127 "25 char limit is dumb",
128 "I can't even fit a funny"
133 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
137 CommonAPI.post(user, %{
138 "status" => "suya...",
139 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
142 object = Object.normalize(activity)
145 File.read!("test/fixtures/mastodon-vote.json")
147 |> Kernel.put_in(["to"], user.ap_id)
148 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
149 |> Kernel.put_in(["object", "to"], user.ap_id)
151 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
152 answer_object = Object.normalize(activity)
153 assert answer_object.data["type"] == "Answer"
154 object = Object.get_by_ap_id(object.data["id"])
157 object.data["oneOf"],
159 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
165 test "it works for incoming notices with contentMap" do
167 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
169 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
170 object = Object.normalize(data["object"])
172 assert object.data["content"] ==
173 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
176 test "it works for incoming notices with to/cc not being an array (kroeg)" do
177 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
179 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
180 object = Object.normalize(data["object"])
182 assert object.data["content"] ==
183 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
186 test "it works for incoming announces with actor being inlined (kroeg)" do
187 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
189 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
191 assert data["actor"] == "https://puckipedia.com/"
194 test "it works for incoming notices with tag not being an array (kroeg)" do
195 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
197 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
198 object = Object.normalize(data["object"])
200 assert object.data["emoji"] == %{
201 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
204 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
206 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
207 object = Object.normalize(data["object"])
209 assert "test" in object.data["tag"]
212 test "it works for incoming notices with url not being a string (prismo)" do
213 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
215 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
216 object = Object.normalize(data["object"])
218 assert object.data["url"] == "https://prismo.news/posts/83"
221 test "it cleans up incoming notices which are not really DMs" do
223 other_user = insert(:user)
225 to = [user.ap_id, other_user.ap_id]
228 File.read!("test/fixtures/mastodon-post-activity.json")
238 data = Map.put(data, "object", object)
240 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
242 assert data["to"] == []
243 assert data["cc"] == to
245 object_data = Object.normalize(activity).data
247 assert object_data["to"] == []
248 assert object_data["cc"] == to
251 test "it works for incoming follow requests" do
255 File.read!("test/fixtures/mastodon-follow-activity.json")
257 |> Map.put("object", user.ap_id)
259 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
261 assert data["actor"] == "http://mastodon.example.org/users/admin"
262 assert data["type"] == "Follow"
263 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
264 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
267 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
268 Pleroma.Config.put([:user, :deny_follow_blocked], true)
271 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
273 {:ok, user} = User.block(user, target)
276 File.read!("test/fixtures/mastodon-follow-activity.json")
278 |> Map.put("object", user.ap_id)
280 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
282 %Activity{} = activity = Activity.get_by_ap_id(id)
284 assert activity.data["state"] == "reject"
287 test "it works for incoming follow requests from hubzilla" do
291 File.read!("test/fixtures/hubzilla-follow-activity.json")
293 |> Map.put("object", user.ap_id)
294 |> Utils.normalize_params()
296 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
298 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
299 assert data["type"] == "Follow"
300 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
301 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
304 test "it works for incoming likes" do
306 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
309 File.read!("test/fixtures/mastodon-like.json")
311 |> Map.put("object", activity.data["object"])
313 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
315 assert data["actor"] == "http://mastodon.example.org/users/admin"
316 assert data["type"] == "Like"
317 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
318 assert data["object"] == activity.data["object"]
321 test "it returns an error for incoming unlikes wihout a like activity" do
323 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
326 File.read!("test/fixtures/mastodon-undo-like.json")
328 |> Map.put("object", activity.data["object"])
330 assert Transmogrifier.handle_incoming(data) == :error
333 test "it works for incoming unlikes with an existing like activity" do
335 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
338 File.read!("test/fixtures/mastodon-like.json")
340 |> Map.put("object", activity.data["object"])
342 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
345 File.read!("test/fixtures/mastodon-undo-like.json")
347 |> Map.put("object", like_data)
348 |> Map.put("actor", like_data["actor"])
350 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
352 assert data["actor"] == "http://mastodon.example.org/users/admin"
353 assert data["type"] == "Undo"
354 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
355 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
358 test "it works for incoming announces" do
359 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
361 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
363 assert data["actor"] == "http://mastodon.example.org/users/admin"
364 assert data["type"] == "Announce"
367 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
369 assert data["object"] ==
370 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
372 assert Activity.get_create_by_object_ap_id(data["object"])
375 test "it works for incoming announces with an existing activity" do
377 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
380 File.read!("test/fixtures/mastodon-announce.json")
382 |> Map.put("object", activity.data["object"])
384 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
386 assert data["actor"] == "http://mastodon.example.org/users/admin"
387 assert data["type"] == "Announce"
390 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
392 assert data["object"] == activity.data["object"]
394 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
397 test "it does not clobber the addressing on announce activities" do
399 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
402 File.read!("test/fixtures/mastodon-announce.json")
404 |> Map.put("object", Object.normalize(activity).data["id"])
405 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
408 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
410 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
413 test "it ensures that as:Public activities make it to their followers collection" do
417 File.read!("test/fixtures/mastodon-post-activity.json")
419 |> Map.put("actor", user.ap_id)
420 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
425 |> Map.put("attributedTo", user.ap_id)
426 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
429 data = Map.put(data, "object", object)
431 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
433 assert data["cc"] == [User.ap_followers(user)]
436 test "it ensures that address fields become lists" do
440 File.read!("test/fixtures/mastodon-post-activity.json")
442 |> Map.put("actor", user.ap_id)
443 |> Map.put("to", nil)
444 |> Map.put("cc", nil)
448 |> Map.put("attributedTo", user.ap_id)
449 |> Map.put("to", nil)
450 |> Map.put("cc", nil)
452 data = Map.put(data, "object", object)
454 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
456 assert !is_nil(data["to"])
457 assert !is_nil(data["cc"])
460 test "it works for incoming update activities" do
461 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
463 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
464 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
467 update_data["object"]
468 |> Map.put("actor", data["actor"])
469 |> Map.put("id", data["actor"])
473 |> Map.put("actor", data["actor"])
474 |> Map.put("object", object)
476 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
478 user = User.get_cached_by_ap_id(data["actor"])
479 assert user.name == "gargle"
481 assert user.avatar["url"] == [
484 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
488 assert user.info.banner["url"] == [
491 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
495 assert user.bio == "<p>Some bio</p>"
498 test "it works for incoming update activities which lock the account" do
499 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
501 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
502 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
505 update_data["object"]
506 |> Map.put("actor", data["actor"])
507 |> Map.put("id", data["actor"])
508 |> Map.put("manuallyApprovesFollowers", true)
512 |> Map.put("actor", data["actor"])
513 |> Map.put("object", object)
515 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
517 user = User.get_cached_by_ap_id(data["actor"])
518 assert user.info.locked == true
521 test "it works for incoming deletes" do
522 activity = insert(:note_activity)
525 File.read!("test/fixtures/mastodon-delete.json")
530 |> Map.put("id", activity.data["object"])
534 |> Map.put("object", object)
535 |> Map.put("actor", activity.data["actor"])
537 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
539 refute Activity.get_by_id(activity.id)
542 test "it fails for incoming deletes with spoofed origin" do
543 activity = insert(:note_activity)
546 File.read!("test/fixtures/mastodon-delete.json")
551 |> Map.put("id", activity.data["object"])
555 |> Map.put("object", object)
557 :error = Transmogrifier.handle_incoming(data)
559 assert Activity.get_by_id(activity.id)
562 test "it works for incoming unannounces with an existing notice" do
564 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
567 File.read!("test/fixtures/mastodon-announce.json")
569 |> Map.put("object", activity.data["object"])
571 {:ok, %Activity{data: announce_data, local: false}} =
572 Transmogrifier.handle_incoming(announce_data)
575 File.read!("test/fixtures/mastodon-undo-announce.json")
577 |> Map.put("object", announce_data)
578 |> Map.put("actor", announce_data["actor"])
580 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
582 assert data["type"] == "Undo"
583 assert data["object"]["type"] == "Announce"
584 assert data["object"]["object"] == activity.data["object"]
586 assert data["object"]["id"] ==
587 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
590 test "it works for incomming unfollows with an existing follow" do
594 File.read!("test/fixtures/mastodon-follow-activity.json")
596 |> Map.put("object", user.ap_id)
598 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
601 File.read!("test/fixtures/mastodon-unfollow-activity.json")
603 |> Map.put("object", follow_data)
605 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
607 assert data["type"] == "Undo"
608 assert data["object"]["type"] == "Follow"
609 assert data["object"]["object"] == user.ap_id
610 assert data["actor"] == "http://mastodon.example.org/users/admin"
612 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
615 test "it works for incoming blocks" do
619 File.read!("test/fixtures/mastodon-block-activity.json")
621 |> Map.put("object", user.ap_id)
623 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
625 assert data["type"] == "Block"
626 assert data["object"] == user.ap_id
627 assert data["actor"] == "http://mastodon.example.org/users/admin"
629 blocker = User.get_cached_by_ap_id(data["actor"])
631 assert User.blocks?(blocker, user)
634 test "incoming blocks successfully tear down any follow relationship" do
635 blocker = insert(:user)
636 blocked = insert(:user)
639 File.read!("test/fixtures/mastodon-block-activity.json")
641 |> Map.put("object", blocked.ap_id)
642 |> Map.put("actor", blocker.ap_id)
644 {:ok, blocker} = User.follow(blocker, blocked)
645 {:ok, blocked} = User.follow(blocked, blocker)
647 assert User.following?(blocker, blocked)
648 assert User.following?(blocked, blocker)
650 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
652 assert data["type"] == "Block"
653 assert data["object"] == blocked.ap_id
654 assert data["actor"] == blocker.ap_id
656 blocker = User.get_cached_by_ap_id(data["actor"])
657 blocked = User.get_cached_by_ap_id(data["object"])
659 assert User.blocks?(blocker, blocked)
661 refute User.following?(blocker, blocked)
662 refute User.following?(blocked, blocker)
665 test "it works for incoming unblocks with an existing block" do
669 File.read!("test/fixtures/mastodon-block-activity.json")
671 |> Map.put("object", user.ap_id)
673 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
676 File.read!("test/fixtures/mastodon-unblock-activity.json")
678 |> Map.put("object", block_data)
680 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
681 assert data["type"] == "Undo"
682 assert data["object"]["type"] == "Block"
683 assert data["object"]["object"] == user.ap_id
684 assert data["actor"] == "http://mastodon.example.org/users/admin"
686 blocker = User.get_cached_by_ap_id(data["actor"])
688 refute User.blocks?(blocker, user)
691 test "it works for incoming accepts which were pre-accepted" do
692 follower = insert(:user)
693 followed = insert(:user)
695 {:ok, follower} = User.follow(follower, followed)
696 assert User.following?(follower, followed) == true
698 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
701 File.read!("test/fixtures/mastodon-accept-activity.json")
703 |> Map.put("actor", followed.ap_id)
706 accept_data["object"]
707 |> Map.put("actor", follower.ap_id)
708 |> Map.put("id", follow_activity.data["id"])
710 accept_data = Map.put(accept_data, "object", object)
712 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
713 refute activity.local
715 assert activity.data["object"] == follow_activity.data["id"]
717 follower = User.get_cached_by_id(follower.id)
719 assert User.following?(follower, followed) == true
722 test "it works for incoming accepts which were orphaned" do
723 follower = insert(:user)
724 followed = insert(:user, %{info: %User.Info{locked: true}})
726 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
729 File.read!("test/fixtures/mastodon-accept-activity.json")
731 |> Map.put("actor", followed.ap_id)
734 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
736 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
737 assert activity.data["object"] == follow_activity.data["id"]
739 follower = User.get_cached_by_id(follower.id)
741 assert User.following?(follower, followed) == true
744 test "it works for incoming accepts which are referenced by IRI only" do
745 follower = insert(:user)
746 followed = insert(:user, %{info: %User.Info{locked: true}})
748 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
751 File.read!("test/fixtures/mastodon-accept-activity.json")
753 |> Map.put("actor", followed.ap_id)
754 |> Map.put("object", follow_activity.data["id"])
756 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
757 assert activity.data["object"] == follow_activity.data["id"]
759 follower = User.get_cached_by_id(follower.id)
761 assert User.following?(follower, followed) == true
764 test "it fails for incoming accepts which cannot be correlated" do
765 follower = insert(:user)
766 followed = insert(:user, %{info: %User.Info{locked: true}})
769 File.read!("test/fixtures/mastodon-accept-activity.json")
771 |> Map.put("actor", followed.ap_id)
774 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
776 :error = Transmogrifier.handle_incoming(accept_data)
778 follower = User.get_cached_by_id(follower.id)
780 refute User.following?(follower, followed) == true
783 test "it fails for incoming rejects which cannot be correlated" do
784 follower = insert(:user)
785 followed = insert(:user, %{info: %User.Info{locked: true}})
788 File.read!("test/fixtures/mastodon-reject-activity.json")
790 |> Map.put("actor", followed.ap_id)
793 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
795 :error = Transmogrifier.handle_incoming(accept_data)
797 follower = User.get_cached_by_id(follower.id)
799 refute User.following?(follower, followed) == true
802 test "it works for incoming rejects which are orphaned" do
803 follower = insert(:user)
804 followed = insert(:user, %{info: %User.Info{locked: true}})
806 {:ok, follower} = User.follow(follower, followed)
807 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
809 assert User.following?(follower, followed) == true
812 File.read!("test/fixtures/mastodon-reject-activity.json")
814 |> Map.put("actor", followed.ap_id)
817 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
819 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
820 refute activity.local
822 follower = User.get_cached_by_id(follower.id)
824 assert User.following?(follower, followed) == false
827 test "it works for incoming rejects which are referenced by IRI only" do
828 follower = insert(:user)
829 followed = insert(:user, %{info: %User.Info{locked: true}})
831 {:ok, follower} = User.follow(follower, followed)
832 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
834 assert User.following?(follower, followed) == true
837 File.read!("test/fixtures/mastodon-reject-activity.json")
839 |> Map.put("actor", followed.ap_id)
840 |> Map.put("object", follow_activity.data["id"])
842 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
844 follower = User.get_cached_by_id(follower.id)
846 assert User.following?(follower, followed) == false
849 test "it rejects activities without a valid ID" do
853 File.read!("test/fixtures/mastodon-follow-activity.json")
855 |> Map.put("object", user.ap_id)
858 :error = Transmogrifier.handle_incoming(data)
861 test "it remaps video URLs as attachments if necessary" do
863 Fetcher.fetch_object_from_id(
864 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
869 "mediaType" => "video/mp4",
871 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
872 "mimeType" => "video/mp4",
877 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
878 "mediaType" => "video/mp4",
885 assert object.data["url"] ==
886 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
888 assert object.data["attachment"] == [attachment]
891 test "it accepts Flag activities" do
893 other_user = insert(:user)
895 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
896 object = Object.normalize(activity.data["object"])
899 "@context" => "https://www.w3.org/ns/activitystreams",
900 "cc" => [user.ap_id],
901 "object" => [user.ap_id, object.data["id"]],
903 "content" => "blocked AND reported!!!",
904 "actor" => other_user.ap_id
907 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
909 assert activity.data["object"] == [user.ap_id, object.data["id"]]
910 assert activity.data["content"] == "blocked AND reported!!!"
911 assert activity.data["actor"] == other_user.ap_id
912 assert activity.data["cc"] == [user.ap_id]
916 describe "prepare outgoing" do
917 test "it turns mentions into tags" do
919 other_user = insert(:user)
922 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
924 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
925 object = modified["object"]
927 expected_mention = %{
928 "href" => other_user.ap_id,
929 "name" => "@#{other_user.nickname}",
934 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
939 assert Enum.member?(object["tag"], expected_tag)
940 assert Enum.member?(object["tag"], expected_mention)
943 test "it adds the sensitive property" do
946 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
947 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
949 assert modified["object"]["sensitive"]
952 test "it adds the json-ld context and the conversation property" do
955 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
956 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
958 assert modified["@context"] ==
959 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
961 assert modified["object"]["conversation"] == modified["context"]
964 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
967 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
968 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
970 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
973 test "it translates ostatus IDs to external URLs" do
974 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
975 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
979 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
980 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
982 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
985 test "it translates ostatus reply_to IDs to external URLs" do
986 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
987 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
992 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
994 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
996 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
999 test "it strips internal hashtag data" do
1000 user = insert(:user)
1002 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1005 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1006 "type" => "Hashtag",
1010 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1012 assert modified["object"]["tag"] == [expected_tag]
1015 test "it strips internal fields" do
1016 user = insert(:user)
1018 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1020 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1022 assert length(modified["object"]["tag"]) == 2
1024 assert is_nil(modified["object"]["emoji"])
1025 assert is_nil(modified["object"]["like_count"])
1026 assert is_nil(modified["object"]["announcements"])
1027 assert is_nil(modified["object"]["announcement_count"])
1028 assert is_nil(modified["object"]["context_id"])
1031 test "it strips internal fields of article" do
1032 activity = insert(:article_activity)
1034 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1036 assert length(modified["object"]["tag"]) == 2
1038 assert is_nil(modified["object"]["emoji"])
1039 assert is_nil(modified["object"]["like_count"])
1040 assert is_nil(modified["object"]["announcements"])
1041 assert is_nil(modified["object"]["announcement_count"])
1042 assert is_nil(modified["object"]["context_id"])
1045 test "it adds like collection to object" do
1046 activity = insert(:note_activity)
1047 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1049 assert modified["object"]["likes"]["type"] == "OrderedCollection"
1050 assert modified["object"]["likes"]["totalItems"] == 0
1053 test "the directMessage flag is present" do
1054 user = insert(:user)
1055 other_user = insert(:user)
1057 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1059 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1061 assert modified["directMessage"] == false
1064 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1066 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1068 assert modified["directMessage"] == false
1071 CommonAPI.post(user, %{
1072 "status" => "@#{other_user.nickname} :moominmamma:",
1073 "visibility" => "direct"
1076 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1078 assert modified["directMessage"] == true
1082 describe "user upgrade" do
1083 test "it upgrades a user to activitypub" do
1086 nickname: "rye@niu.moe",
1088 ap_id: "https://niu.moe/users/rye",
1089 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1092 user_two = insert(:user, %{following: [user.follower_address]})
1094 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1095 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1096 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1098 user = User.get_cached_by_id(user.id)
1099 assert user.info.note_count == 1
1101 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1102 assert user.info.ap_enabled
1103 assert user.info.note_count == 1
1104 assert user.follower_address == "https://niu.moe/users/rye/followers"
1106 user = User.get_cached_by_id(user.id)
1107 assert user.info.note_count == 1
1109 activity = Activity.get_by_id(activity.id)
1110 assert user.follower_address in activity.recipients
1116 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1125 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1128 } = user.info.banner
1130 refute "..." in activity.recipients
1132 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1133 refute user.follower_address in unrelated_activity.recipients
1135 user_two = User.get_cached_by_id(user_two.id)
1136 assert user.follower_address in user_two.following
1137 refute "..." in user_two.following
1141 describe "maybe_retire_websub" do
1142 test "it deletes all websub client subscripitions with the user as topic" do
1143 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
1144 {:ok, ws} = Repo.insert(subscription)
1146 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
1147 {:ok, ws2} = Repo.insert(subscription)
1149 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
1151 refute Repo.get(WebsubClientSubscription, ws.id)
1152 assert Repo.get(WebsubClientSubscription, ws2.id)
1156 describe "actor rewriting" do
1157 test "it fixes the actor URL property to be a proper URI" do
1159 "url" => %{"href" => "http://example.com"}
1162 rewritten = Transmogrifier.maybe_fix_user_object(data)
1163 assert rewritten["url"] == "http://example.com"
1167 describe "actor origin containment" do
1168 test "it rejects activities which reference objects with bogus origins" do
1170 "@context" => "https://www.w3.org/ns/activitystreams",
1171 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1172 "actor" => "http://mastodon.example.org/users/admin",
1173 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1174 "object" => "https://info.pleroma.site/activity.json",
1175 "type" => "Announce"
1178 :error = Transmogrifier.handle_incoming(data)
1181 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1183 "@context" => "https://www.w3.org/ns/activitystreams",
1184 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1185 "actor" => "http://mastodon.example.org/users/admin",
1186 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1187 "object" => "https://info.pleroma.site/activity2.json",
1188 "type" => "Announce"
1191 :error = Transmogrifier.handle_incoming(data)
1194 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1196 "@context" => "https://www.w3.org/ns/activitystreams",
1197 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1198 "actor" => "http://mastodon.example.org/users/admin",
1199 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1200 "object" => "https://info.pleroma.site/activity3.json",
1201 "type" => "Announce"
1204 :error = Transmogrifier.handle_incoming(data)
1208 describe "reserialization" do
1209 test "successfully reserializes a message with inReplyTo == nil" do
1210 user = insert(:user)
1213 "@context" => "https://www.w3.org/ns/activitystreams",
1214 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1218 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1223 "attributedTo" => user.ap_id
1225 "actor" => user.ap_id
1228 {:ok, activity} = Transmogrifier.handle_incoming(message)
1230 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1233 test "successfully reserializes a message with AS2 objects in IR" do
1234 user = insert(:user)
1237 "@context" => "https://www.w3.org/ns/activitystreams",
1238 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1242 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1247 "attributedTo" => user.ap_id,
1249 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1250 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1253 "actor" => user.ap_id
1256 {:ok, activity} = Transmogrifier.handle_incoming(message)
1258 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1262 test "Rewrites Answers to Notes" do
1263 user = insert(:user)
1265 {:ok, poll_activity} =
1266 CommonAPI.post(user, %{
1267 "status" => "suya...",
1268 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1271 poll_object = Object.normalize(poll_activity)
1272 # TODO: Replace with CommonAPI vote creation when implemented
1274 File.read!("test/fixtures/mastodon-vote.json")
1276 |> Kernel.put_in(["to"], user.ap_id)
1277 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1278 |> Kernel.put_in(["object", "to"], user.ap_id)
1280 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1281 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1283 assert data["object"]["type"] == "Note"