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
7 alias Pleroma.Web.ActivityPub.Transmogrifier
8 alias Pleroma.Web.ActivityPub.Utils
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.OStatus
11 alias Pleroma.Activity
14 alias Pleroma.Web.Websub.WebsubClientSubscription
16 import Pleroma.Factory
17 alias Pleroma.Web.CommonAPI
20 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 describe "handle_incoming" do
25 test "it ignores an incoming notice if we already have it" do
26 activity = insert(:note_activity)
29 File.read!("test/fixtures/mastodon-post-activity.json")
31 |> Map.put("object", activity.data["object"])
33 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
35 assert activity == returned_activity
38 test "it fetches replied-to activities if we don't have them" do
40 File.read!("test/fixtures/mastodon-post-activity.json")
45 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
49 |> Map.put("object", object)
51 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
54 Activity.get_create_activity_by_object_ap_id(
55 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
58 assert returned_activity.data["object"]["inReplyToAtomUri"] ==
59 "https://shitposter.club/notice/2827873"
61 assert returned_activity.data["object"]["inReplyToStatusId"] == activity.id
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 = data["object"]
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_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 assert Enum.at(data["object"]["tag"], 2) == "moo"
114 test "it works for incoming notices with contentMap" do
116 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
118 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
120 assert data["object"]["content"] ==
121 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
124 test "it works for incoming notices with to/cc not being an array (kroeg)" do
125 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
127 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
129 assert data["object"]["content"] ==
130 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
133 test "it works for incoming announces with actor being inlined (kroeg)" do
134 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
136 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
138 assert data["actor"] == "https://puckipedia.com/"
141 test "it works for incoming notices with tag not being an array (kroeg)" do
142 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
144 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
146 assert data["object"]["emoji"] == %{
147 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
150 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
152 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
154 assert "test" in data["object"]["tag"]
157 test "it works for incoming notices with url not being a string (prismo)" do
158 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
160 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
162 assert data["object"]["url"] == "https://prismo.news/posts/83"
165 test "it works for incoming follow requests" do
169 File.read!("test/fixtures/mastodon-follow-activity.json")
171 |> Map.put("object", user.ap_id)
173 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
175 assert data["actor"] == "http://mastodon.example.org/users/admin"
176 assert data["type"] == "Follow"
177 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
178 assert User.following?(User.get_by_ap_id(data["actor"]), user)
181 test "it works for incoming follow requests from hubzilla" do
185 File.read!("test/fixtures/hubzilla-follow-activity.json")
187 |> Map.put("object", user.ap_id)
188 |> Utils.normalize_params()
190 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
192 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
193 assert data["type"] == "Follow"
194 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
195 assert User.following?(User.get_by_ap_id(data["actor"]), user)
198 test "it works for incoming likes" do
200 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
203 File.read!("test/fixtures/mastodon-like.json")
205 |> Map.put("object", activity.data["object"]["id"])
207 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
209 assert data["actor"] == "http://mastodon.example.org/users/admin"
210 assert data["type"] == "Like"
211 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
212 assert data["object"] == activity.data["object"]["id"]
215 test "it returns an error for incoming unlikes wihout a like activity" do
217 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
220 File.read!("test/fixtures/mastodon-undo-like.json")
222 |> Map.put("object", activity.data["object"]["id"])
224 assert Transmogrifier.handle_incoming(data) == :error
227 test "it works for incoming unlikes with an existing like activity" do
229 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
232 File.read!("test/fixtures/mastodon-like.json")
234 |> Map.put("object", activity.data["object"]["id"])
236 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
239 File.read!("test/fixtures/mastodon-undo-like.json")
241 |> Map.put("object", like_data)
242 |> Map.put("actor", like_data["actor"])
244 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
246 assert data["actor"] == "http://mastodon.example.org/users/admin"
247 assert data["type"] == "Undo"
248 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
249 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
252 test "it works for incoming announces" do
253 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
255 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
257 assert data["actor"] == "http://mastodon.example.org/users/admin"
258 assert data["type"] == "Announce"
261 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
263 assert data["object"] ==
264 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
266 assert Activity.get_create_activity_by_object_ap_id(data["object"])
269 test "it works for incoming announces with an existing activity" do
271 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
274 File.read!("test/fixtures/mastodon-announce.json")
276 |> Map.put("object", activity.data["object"]["id"])
278 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
280 assert data["actor"] == "http://mastodon.example.org/users/admin"
281 assert data["type"] == "Announce"
284 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
286 assert data["object"] == activity.data["object"]["id"]
288 assert Activity.get_create_activity_by_object_ap_id(data["object"]).id == activity.id
291 test "it works for incoming update activities" do
292 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
294 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
295 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
298 update_data["object"]
299 |> Map.put("actor", data["actor"])
300 |> Map.put("id", data["actor"])
304 |> Map.put("actor", data["actor"])
305 |> Map.put("object", object)
307 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
309 user = User.get_cached_by_ap_id(data["actor"])
310 assert user.name == "gargle"
312 assert user.avatar["url"] == [
315 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
319 assert user.info.banner["url"] == [
322 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
326 assert user.bio == "<p>Some bio</p>"
329 test "it works for incoming update activities which lock the account" do
330 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
332 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
333 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
336 update_data["object"]
337 |> Map.put("actor", data["actor"])
338 |> Map.put("id", data["actor"])
339 |> Map.put("manuallyApprovesFollowers", true)
343 |> Map.put("actor", data["actor"])
344 |> Map.put("object", object)
346 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
348 user = User.get_cached_by_ap_id(data["actor"])
349 assert user.info.locked == true
352 test "it works for incoming deletes" do
353 activity = insert(:note_activity)
356 File.read!("test/fixtures/mastodon-delete.json")
361 |> Map.put("id", activity.data["object"]["id"])
365 |> Map.put("object", object)
366 |> Map.put("actor", activity.data["actor"])
368 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
370 refute Repo.get(Activity, activity.id)
373 test "it fails for incoming deletes with spoofed origin" do
374 activity = insert(:note_activity)
377 File.read!("test/fixtures/mastodon-delete.json")
382 |> Map.put("id", activity.data["object"]["id"])
386 |> Map.put("object", object)
388 :error = Transmogrifier.handle_incoming(data)
390 assert Repo.get(Activity, activity.id)
393 test "it works for incoming unannounces with an existing notice" do
395 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
398 File.read!("test/fixtures/mastodon-announce.json")
400 |> Map.put("object", activity.data["object"]["id"])
402 {:ok, %Activity{data: announce_data, local: false}} =
403 Transmogrifier.handle_incoming(announce_data)
406 File.read!("test/fixtures/mastodon-undo-announce.json")
408 |> Map.put("object", announce_data)
409 |> Map.put("actor", announce_data["actor"])
411 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
413 assert data["type"] == "Undo"
414 assert data["object"]["type"] == "Announce"
415 assert data["object"]["object"] == activity.data["object"]["id"]
417 assert data["object"]["id"] ==
418 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
421 test "it works for incomming unfollows with an existing follow" do
425 File.read!("test/fixtures/mastodon-follow-activity.json")
427 |> Map.put("object", user.ap_id)
429 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
432 File.read!("test/fixtures/mastodon-unfollow-activity.json")
434 |> Map.put("object", follow_data)
436 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
438 assert data["type"] == "Undo"
439 assert data["object"]["type"] == "Follow"
440 assert data["object"]["object"] == user.ap_id
441 assert data["actor"] == "http://mastodon.example.org/users/admin"
443 refute User.following?(User.get_by_ap_id(data["actor"]), user)
446 test "it works for incoming blocks" do
450 File.read!("test/fixtures/mastodon-block-activity.json")
452 |> Map.put("object", user.ap_id)
454 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
456 assert data["type"] == "Block"
457 assert data["object"] == user.ap_id
458 assert data["actor"] == "http://mastodon.example.org/users/admin"
460 blocker = User.get_by_ap_id(data["actor"])
462 assert User.blocks?(blocker, user)
465 test "incoming blocks successfully tear down any follow relationship" do
466 blocker = insert(:user)
467 blocked = insert(:user)
470 File.read!("test/fixtures/mastodon-block-activity.json")
472 |> Map.put("object", blocked.ap_id)
473 |> Map.put("actor", blocker.ap_id)
475 {:ok, blocker} = User.follow(blocker, blocked)
476 {:ok, blocked} = User.follow(blocked, blocker)
478 assert User.following?(blocker, blocked)
479 assert User.following?(blocked, blocker)
481 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
483 assert data["type"] == "Block"
484 assert data["object"] == blocked.ap_id
485 assert data["actor"] == blocker.ap_id
487 blocker = User.get_by_ap_id(data["actor"])
488 blocked = User.get_by_ap_id(data["object"])
490 assert User.blocks?(blocker, blocked)
492 refute User.following?(blocker, blocked)
493 refute User.following?(blocked, blocker)
496 test "it works for incoming unblocks with an existing block" do
500 File.read!("test/fixtures/mastodon-block-activity.json")
502 |> Map.put("object", user.ap_id)
504 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
507 File.read!("test/fixtures/mastodon-unblock-activity.json")
509 |> Map.put("object", block_data)
511 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
512 assert data["type"] == "Undo"
513 assert data["object"]["type"] == "Block"
514 assert data["object"]["object"] == user.ap_id
515 assert data["actor"] == "http://mastodon.example.org/users/admin"
517 blocker = User.get_by_ap_id(data["actor"])
519 refute User.blocks?(blocker, user)
522 test "it works for incoming accepts which were pre-accepted" do
523 follower = insert(:user)
524 followed = insert(:user)
526 {:ok, follower} = User.follow(follower, followed)
527 assert User.following?(follower, followed) == true
529 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
532 File.read!("test/fixtures/mastodon-accept-activity.json")
534 |> Map.put("actor", followed.ap_id)
537 accept_data["object"]
538 |> Map.put("actor", follower.ap_id)
539 |> Map.put("id", follow_activity.data["id"])
541 accept_data = Map.put(accept_data, "object", object)
543 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
544 refute activity.local
546 assert activity.data["object"] == follow_activity.data["id"]
548 follower = Repo.get(User, follower.id)
550 assert User.following?(follower, followed) == true
553 test "it works for incoming accepts which were orphaned" do
554 follower = insert(:user)
555 followed = insert(:user, %{info: %User.Info{locked: true}})
557 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
560 File.read!("test/fixtures/mastodon-accept-activity.json")
562 |> Map.put("actor", followed.ap_id)
565 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
567 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
568 assert activity.data["object"] == follow_activity.data["id"]
570 follower = Repo.get(User, follower.id)
572 assert User.following?(follower, followed) == true
575 test "it works for incoming accepts which are referenced by IRI only" do
576 follower = insert(:user)
577 followed = insert(:user, %{info: %User.Info{locked: true}})
579 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
582 File.read!("test/fixtures/mastodon-accept-activity.json")
584 |> Map.put("actor", followed.ap_id)
585 |> Map.put("object", follow_activity.data["id"])
587 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
588 assert activity.data["object"] == follow_activity.data["id"]
590 follower = Repo.get(User, follower.id)
592 assert User.following?(follower, followed) == true
595 test "it fails for incoming accepts which cannot be correlated" do
596 follower = insert(:user)
597 followed = insert(:user, %{info: %User.Info{locked: true}})
600 File.read!("test/fixtures/mastodon-accept-activity.json")
602 |> Map.put("actor", followed.ap_id)
605 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
607 :error = Transmogrifier.handle_incoming(accept_data)
609 follower = Repo.get(User, follower.id)
611 refute User.following?(follower, followed) == true
614 test "it fails for incoming rejects which cannot be correlated" do
615 follower = insert(:user)
616 followed = insert(:user, %{info: %User.Info{locked: true}})
619 File.read!("test/fixtures/mastodon-reject-activity.json")
621 |> Map.put("actor", followed.ap_id)
624 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
626 :error = Transmogrifier.handle_incoming(accept_data)
628 follower = Repo.get(User, follower.id)
630 refute User.following?(follower, followed) == true
633 test "it works for incoming rejects which are orphaned" do
634 follower = insert(:user)
635 followed = insert(:user, %{info: %User.Info{locked: true}})
637 {:ok, follower} = User.follow(follower, followed)
638 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
640 assert User.following?(follower, followed) == true
643 File.read!("test/fixtures/mastodon-reject-activity.json")
645 |> Map.put("actor", followed.ap_id)
648 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
650 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
651 refute activity.local
653 follower = Repo.get(User, follower.id)
655 assert User.following?(follower, followed) == false
658 test "it works for incoming rejects which are referenced by IRI only" do
659 follower = insert(:user)
660 followed = insert(:user, %{info: %User.Info{locked: true}})
662 {:ok, follower} = User.follow(follower, followed)
663 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
665 assert User.following?(follower, followed) == true
668 File.read!("test/fixtures/mastodon-reject-activity.json")
670 |> Map.put("actor", followed.ap_id)
671 |> Map.put("object", follow_activity.data["id"])
673 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
675 follower = Repo.get(User, follower.id)
677 assert User.following?(follower, followed) == false
680 test "it rejects activities without a valid ID" do
684 File.read!("test/fixtures/mastodon-follow-activity.json")
686 |> Map.put("object", user.ap_id)
689 :error = Transmogrifier.handle_incoming(data)
692 test "it remaps video URLs as attachments if necessary" do
694 ActivityPub.fetch_object_from_id(
695 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
700 "mediaType" => "video/mp4",
702 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
703 "mimeType" => "video/mp4",
708 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
709 "mediaType" => "video/mp4",
716 assert object.data["url"] ==
717 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
719 assert object.data["attachment"] == [attachment]
723 describe "prepare outgoing" do
724 test "it turns mentions into tags" do
726 other_user = insert(:user)
729 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
731 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
732 object = modified["object"]
734 expected_mention = %{
735 "href" => other_user.ap_id,
736 "name" => "@#{other_user.nickname}",
741 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
746 assert Enum.member?(object["tag"], expected_tag)
747 assert Enum.member?(object["tag"], expected_mention)
750 test "it adds the sensitive property" do
753 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
754 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
756 assert modified["object"]["sensitive"]
759 test "it adds the json-ld context and the conversation property" do
762 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
763 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
765 assert modified["@context"] ==
766 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
768 assert modified["object"]["conversation"] == modified["context"]
771 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
774 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
775 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
777 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
780 test "it translates ostatus IDs to external URLs" do
781 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
782 {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
786 {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
787 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
789 assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
792 test "it translates ostatus reply_to IDs to external URLs" do
793 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
794 {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)
799 CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})
801 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
803 assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
806 test "it strips internal hashtag data" do
809 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
812 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
817 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
819 assert modified["object"]["tag"] == [expected_tag]
822 test "it strips internal fields" do
825 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :moominmamma:"})
827 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
829 assert length(modified["object"]["tag"]) == 2
831 assert is_nil(modified["object"]["emoji"])
832 assert is_nil(modified["object"]["likes"])
833 assert is_nil(modified["object"]["like_count"])
834 assert is_nil(modified["object"]["announcements"])
835 assert is_nil(modified["object"]["announcement_count"])
836 assert is_nil(modified["object"]["context_id"])
840 describe "user upgrade" do
841 test "it upgrades a user to activitypub" do
844 nickname: "rye@niu.moe",
846 ap_id: "https://niu.moe/users/rye",
847 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
850 user_two = insert(:user, %{following: [user.follower_address]})
852 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
853 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
854 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
856 user = Repo.get(User, user.id)
857 assert user.info.note_count == 1
859 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
860 assert user.info.ap_enabled
861 assert user.info.note_count == 1
862 assert user.follower_address == "https://niu.moe/users/rye/followers"
864 # Wait for the background task
867 user = Repo.get(User, user.id)
868 assert user.info.note_count == 1
870 activity = Repo.get(Activity, activity.id)
871 assert user.follower_address in activity.recipients
877 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
886 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
891 refute "..." in activity.recipients
893 unrelated_activity = Repo.get(Activity, unrelated_activity.id)
894 refute user.follower_address in unrelated_activity.recipients
896 user_two = Repo.get(User, user_two.id)
897 assert user.follower_address in user_two.following
898 refute "..." in user_two.following
902 describe "maybe_retire_websub" do
903 test "it deletes all websub client subscripitions with the user as topic" do
904 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
905 {:ok, ws} = Repo.insert(subscription)
907 subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
908 {:ok, ws2} = Repo.insert(subscription)
910 Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
912 refute Repo.get(WebsubClientSubscription, ws.id)
913 assert Repo.get(WebsubClientSubscription, ws2.id)
917 describe "actor rewriting" do
918 test "it fixes the actor URL property to be a proper URI" do
920 "url" => %{"href" => "http://example.com"}
923 rewritten = Transmogrifier.maybe_fix_user_object(data)
924 assert rewritten["url"] == "http://example.com"
928 describe "actor origin containment" do
929 test "it rejects objects with a bogus origin" do
930 {:error, _} = ActivityPub.fetch_object_from_id("https://info.pleroma.site/activity.json")
933 test "it rejects activities which reference objects with bogus origins" do
935 "@context" => "https://www.w3.org/ns/activitystreams",
936 "id" => "http://mastodon.example.org/users/admin/activities/1234",
937 "actor" => "http://mastodon.example.org/users/admin",
938 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
939 "object" => "https://info.pleroma.site/activity.json",
943 :error = Transmogrifier.handle_incoming(data)
946 test "it rejects objects when attributedTo is wrong (variant 1)" do
947 {:error, _} = ActivityPub.fetch_object_from_id("https://info.pleroma.site/activity2.json")
950 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
952 "@context" => "https://www.w3.org/ns/activitystreams",
953 "id" => "http://mastodon.example.org/users/admin/activities/1234",
954 "actor" => "http://mastodon.example.org/users/admin",
955 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
956 "object" => "https://info.pleroma.site/activity2.json",
960 :error = Transmogrifier.handle_incoming(data)
963 test "it rejects objects when attributedTo is wrong (variant 2)" do
964 {:error, _} = ActivityPub.fetch_object_from_id("https://info.pleroma.site/activity3.json")
967 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
969 "@context" => "https://www.w3.org/ns/activitystreams",
970 "id" => "http://mastodon.example.org/users/admin/activities/1234",
971 "actor" => "http://mastodon.example.org/users/admin",
972 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
973 "object" => "https://info.pleroma.site/activity3.json",
977 :error = Transmogrifier.handle_incoming(data)
981 describe "general origin containment" do
982 test "contain_origin_from_id() catches obvious spoofing attempts" do
984 "id" => "http://example.com/~alyssa/activities/1234.json"
988 Transmogrifier.contain_origin_from_id(
989 "http://example.org/~alyssa/activities/1234.json",
994 test "contain_origin_from_id() allows alternate IDs within the same origin domain" do
996 "id" => "http://example.com/~alyssa/activities/1234.json"
1000 Transmogrifier.contain_origin_from_id(
1001 "http://example.com/~alyssa/activities/1234",
1006 test "contain_origin_from_id() allows matching IDs" do
1008 "id" => "http://example.com/~alyssa/activities/1234.json"
1012 Transmogrifier.contain_origin_from_id(
1013 "http://example.com/~alyssa/activities/1234.json",
1018 test "users cannot be collided through fake direction spoofing attempts" do
1020 nickname: "rye@niu.moe",
1022 ap_id: "https://niu.moe/users/rye",
1023 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1026 {:error, _} = User.get_or_fetch_by_ap_id("https://n1u.moe/users/rye")
1029 test "all objects with fake directions are rejected by the object fetcher" do
1031 ActivityPub.fetch_and_contain_remote_object_from_id(
1032 "https://info.pleroma.site/activity4.json"