1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
6 use Oban.Testing, repo: Pleroma.Repo
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.ActivityPub.Utils
14 alias Pleroma.Web.AdminAPI.AccountView
15 alias Pleroma.Web.CommonAPI
18 import Pleroma.Factory
19 import ExUnit.CaptureLog
22 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 setup do: clear_config([:instance, :max_remote_account_fields])
28 describe "handle_incoming" do
29 test "it works for incoming unfollows with an existing follow" do
33 File.read!("test/fixtures/mastodon-follow-activity.json")
35 |> Map.put("object", user.ap_id)
37 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
40 File.read!("test/fixtures/mastodon-unfollow-activity.json")
42 |> Map.put("object", follow_data)
44 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
46 assert data["type"] == "Undo"
47 assert data["object"]["type"] == "Follow"
48 assert data["object"]["object"] == user.ap_id
49 assert data["actor"] == "http://mastodon.example.org/users/admin"
51 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
54 test "it accepts Flag activities" do
56 other_user = insert(:user)
58 {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
59 object = Object.normalize(activity, fetch: false)
63 "id" => activity.object.data["id"],
64 "content" => "test post",
65 "published" => object.data["published"],
66 "actor" => AccountView.render("show.json", %{user: user, skip_visibility_check: true})
70 "@context" => "https://www.w3.org/ns/activitystreams",
72 "object" => [user.ap_id, activity.data["id"]],
74 "content" => "blocked AND reported!!!",
75 "actor" => other_user.ap_id
78 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
80 assert activity.data["object"] == [user.ap_id, note_obj]
81 assert activity.data["content"] == "blocked AND reported!!!"
82 assert activity.data["actor"] == other_user.ap_id
83 assert activity.data["cc"] == [user.ap_id]
86 test "it accepts Move activities" do
87 old_user = insert(:user)
88 new_user = insert(:user)
91 "@context" => "https://www.w3.org/ns/activitystreams",
93 "actor" => old_user.ap_id,
94 "object" => old_user.ap_id,
95 "target" => new_user.ap_id
98 assert :error = Transmogrifier.handle_incoming(message)
100 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
102 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
103 assert activity.actor == old_user.ap_id
104 assert activity.data["actor"] == old_user.ap_id
105 assert activity.data["object"] == old_user.ap_id
106 assert activity.data["target"] == new_user.ap_id
107 assert activity.data["type"] == "Move"
110 test "it fixes both the Create and object contexts in a reply" do
111 insert(:user, ap_id: "https://mk.absturztau.be/users/8ozbzjs3o8")
112 insert(:user, ap_id: "https://p.helene.moe/users/helene")
115 "test/fixtures/create-pleroma-reply-to-misskey-thread.json"
119 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(create_activity)
121 object = Object.normalize(activity, fetch: false)
123 assert activity.data["context"] == object.data["context"]
126 test "it keeps link tags" do
127 insert(:user, ap_id: "https://example.org/users/alice")
129 message = File.read!("test/fixtures/fep-e232.json") |> Jason.decode!()
131 assert capture_log(fn ->
132 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
133 object = Object.normalize(activity)
134 assert [%{"type" => "Mention"}, %{"type" => "Link"}] = object.data["tag"]
135 end) =~ "Object rejected while fetching"
138 test "it accepts quote posts" do
139 insert(:user, ap_id: "https://misskey.io/users/7rkrarq81i")
141 object = File.read!("test/fixtures/quote_post/misskey_quote_post.json") |> Jason.decode!()
144 "@context" => "https://www.w3.org/ns/activitystreams",
146 "actor" => "https://misskey.io/users/7rkrarq81i",
150 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
152 # Object was created in the database
153 object = Object.normalize(activity)
154 assert object.data["quoteUrl"] == "https://misskey.io/notes/8vs6wxufd0"
156 # It fetched the quoted post
157 assert Object.normalize("https://misskey.io/notes/8vs6wxufd0")
161 describe "prepare outgoing" do
162 test "it inlines private announced objects" do
165 {:ok, activity} = CommonAPI.post(user, %{status: "hey", visibility: "private"})
167 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
169 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
171 assert modified["object"]["content"] == "hey"
172 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
175 test "it turns mentions into tags" do
177 other_user = insert(:user)
180 CommonAPI.post(user, %{status: "hey, @#{other_user.nickname}, how are ya? #2hu"})
182 with_mock Pleroma.Notification,
183 get_notified_from_activity: fn _, _ -> [] end do
184 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
186 object = modified["object"]
188 expected_mention = %{
189 "href" => other_user.ap_id,
190 "name" => "@#{other_user.nickname}",
195 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
200 refute called(Pleroma.Notification.get_notified_from_activity(:_, :_))
201 assert Enum.member?(object["tag"], expected_tag)
202 assert Enum.member?(object["tag"], expected_mention)
206 test "it adds the json-ld context and the conversation property" do
209 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
210 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
212 assert modified["@context"] == Utils.make_json_ld_header()["@context"]
214 assert modified["object"]["conversation"] == modified["context"]
217 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
220 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
221 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
223 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
226 test "it strips internal hashtag data" do
229 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu"})
232 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
237 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
239 assert modified["object"]["tag"] == [expected_tag]
242 test "it strips internal fields" do
246 CommonAPI.post(user, %{
247 status: "#2hu :firefox:",
248 generator: %{type: "Application", name: "TestClient", url: "https://pleroma.social"}
251 # Ensure injected application data made it into the activity
252 # as we don't have a Token to derive it from, otherwise it will
253 # be nil and the test will pass
257 url: "https://pleroma.social"
258 } == activity.object.data["generator"]
260 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
262 assert length(modified["object"]["tag"]) == 2
264 assert is_nil(modified["object"]["emoji"])
265 assert is_nil(modified["object"]["like_count"])
266 assert is_nil(modified["object"]["announcements"])
267 assert is_nil(modified["object"]["announcement_count"])
268 assert is_nil(modified["object"]["generator"])
271 test "it strips internal fields of article" do
272 activity = insert(:article_activity)
274 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
276 assert length(modified["object"]["tag"]) == 2
278 assert is_nil(modified["object"]["emoji"])
279 assert is_nil(modified["object"]["like_count"])
280 assert is_nil(modified["object"]["announcements"])
281 assert is_nil(modified["object"]["announcement_count"])
282 assert is_nil(modified["object"]["likes"])
285 test "the directMessage flag is present" do
287 other_user = insert(:user)
289 {:ok, activity} = CommonAPI.post(user, %{status: "2hu :moominmamma:"})
291 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
293 assert modified["directMessage"] == false
295 {:ok, activity} = CommonAPI.post(user, %{status: "@#{other_user.nickname} :moominmamma:"})
297 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
299 assert modified["directMessage"] == false
302 CommonAPI.post(user, %{
303 status: "@#{other_user.nickname} :moominmamma:",
307 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
309 assert modified["directMessage"] == true
312 test "it strips BCC field" do
314 {:ok, list} = Pleroma.List.create("foo", user)
316 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
318 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
320 assert is_nil(modified["bcc"])
323 test "it can handle Listen activities" do
324 listen_activity = insert(:listen)
326 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
328 assert modified["type"] == "Listen"
332 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
334 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
337 test "custom emoji urls are URI encoded" do
338 # :dinosaur: filename has a space -> dino walking.gif
341 {:ok, activity} = CommonAPI.post(user, %{status: "everybody do the dinosaur :dinosaur:"})
343 {:ok, prepared} = Transmogrifier.prepare_outgoing(activity.data)
345 assert length(prepared["object"]["tag"]) == 1
347 url = prepared["object"]["tag"] |> List.first() |> Map.get("icon") |> Map.get("url")
349 assert url == "http://localhost:4001/emoji/dino%20walking.gif"
352 test "Updates of Notes are handled" do
355 {:ok, activity} = CommonAPI.post(user, %{status: "everybody do the dinosaur :dinosaur:"})
356 {:ok, update} = CommonAPI.update(user, activity, %{status: "mew mew :blank:"})
358 {:ok, prepared} = Transmogrifier.prepare_outgoing(update.data)
361 "content" => "mew mew :blank:",
362 "tag" => [%{"name" => ":blank:", "type" => "Emoji"}],
363 "formerRepresentations" => %{
366 "content" => "everybody do the dinosaur :dinosaur:",
367 "tag" => [%{"name" => ":dinosaur:", "type" => "Emoji"}]
371 } = prepared["object"]
374 test "it prepares a quote post" do
377 {:ok, quoted_post} = CommonAPI.post(user, %{status: "hey"})
378 {:ok, quote_post} = CommonAPI.post(user, %{status: "hey", quote_id: quoted_post.id})
380 {:ok, modified} = Transmogrifier.prepare_outgoing(quote_post.data)
382 %{data: %{"id" => quote_id}} = Object.normalize(quoted_post)
384 assert modified["object"]["quoteUrl"] == quote_id
385 assert modified["object"]["quoteUri"] == quote_id
389 describe "actor rewriting" do
390 test "it fixes the actor URL property to be a proper URI" do
392 "url" => %{"href" => "http://example.com"}
395 rewritten = Transmogrifier.maybe_fix_user_object(data)
396 assert rewritten["url"] == "http://example.com"
400 describe "actor origin containment" do
401 test "it rejects activities which reference objects with bogus origins" do
403 "@context" => "https://www.w3.org/ns/activitystreams",
404 "id" => "http://mastodon.example.org/users/admin/activities/1234",
405 "actor" => "http://mastodon.example.org/users/admin",
406 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
407 "object" => "https://info.pleroma.site/activity.json",
411 assert capture_log(fn ->
412 {:error, _} = Transmogrifier.handle_incoming(data)
413 end) =~ "Object rejected while fetching"
416 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
418 "@context" => "https://www.w3.org/ns/activitystreams",
419 "id" => "http://mastodon.example.org/users/admin/activities/1234",
420 "actor" => "http://mastodon.example.org/users/admin",
421 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
422 "object" => "https://info.pleroma.site/activity2.json",
426 assert capture_log(fn ->
427 {:error, _} = Transmogrifier.handle_incoming(data)
428 end) =~ "Object rejected while fetching"
431 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
433 "@context" => "https://www.w3.org/ns/activitystreams",
434 "id" => "http://mastodon.example.org/users/admin/activities/1234",
435 "actor" => "http://mastodon.example.org/users/admin",
436 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
437 "object" => "https://info.pleroma.site/activity3.json",
441 assert capture_log(fn ->
442 {:error, _} = Transmogrifier.handle_incoming(data)
443 end) =~ "Object rejected while fetching"
447 describe "fix_explicit_addressing" do
453 test "moves non-explicitly mentioned actors to cc", %{user: user} do
454 explicitly_mentioned_actors = [
455 "https://pleroma.gold/users/user1",
456 "https://pleroma.gold/user2"
460 "actor" => user.ap_id,
461 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
464 Enum.map(explicitly_mentioned_actors, fn href ->
465 %{"type" => "Mention", "href" => href}
469 fixed_object = Transmogrifier.fix_explicit_addressing(object, user.follower_address)
470 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
471 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
472 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
475 test "does not move actor's follower collection to cc", %{user: user} do
477 "actor" => user.ap_id,
478 "to" => [user.follower_address],
482 fixed_object = Transmogrifier.fix_explicit_addressing(object, user.follower_address)
483 assert user.follower_address in fixed_object["to"]
484 refute user.follower_address in fixed_object["cc"]
487 test "removes recipient's follower collection from cc", %{user: user} do
488 recipient = insert(:user)
491 "actor" => user.ap_id,
492 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
493 "cc" => [user.follower_address, recipient.follower_address]
496 fixed_object = Transmogrifier.fix_explicit_addressing(object, user.follower_address)
498 assert user.follower_address in fixed_object["cc"]
499 refute recipient.follower_address in fixed_object["cc"]
500 refute recipient.follower_address in fixed_object["to"]
504 describe "fix_summary/1" do
505 test "returns fixed object" do
506 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
507 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
508 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
512 describe "fix_url/1" do
513 test "fixes data for object when url is map" do
517 "mimeType" => "video/mp4",
518 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
522 assert Transmogrifier.fix_url(object) == %{
523 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
527 test "returns non-modified object" do
528 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
532 describe "get_obj_helper/2" do
533 test "returns nil when cannot normalize object" do
534 assert capture_log(fn ->
535 refute Transmogrifier.get_obj_helper("test-obj-id")
536 end) =~ "Unsupported URI scheme"
539 @tag capture_log: true
540 test "returns {:ok, %Object{}} for success case" do
541 assert {:ok, %Object{}} =
542 Transmogrifier.get_obj_helper(
543 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
548 describe "fix_attachments/1" do
549 test "puts dimensions into attachment url field" do
553 "type" => "Document",
554 "name" => "Hello world",
555 "url" => "https://media.example.tld/1.jpg",
558 "mediaType" => "image/jpeg",
559 "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
567 "type" => "Document",
568 "name" => "Hello world",
572 "mediaType" => "image/jpeg",
573 "href" => "https://media.example.tld/1.jpg",
578 "mediaType" => "image/jpeg",
579 "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
584 assert Transmogrifier.fix_attachments(object) == expected
588 describe "prepare_object/1" do
589 test "it processes history" do
591 "formerRepresentations" => %{
595 "emoji" => %{"blobcat" => "http://localhost:4001/emoji/blobcat.png"}
601 processed = Transmogrifier.prepare_object(original)
603 history_item = Enum.at(processed["formerRepresentations"]["orderedItems"], 0)
605 refute Map.has_key?(history_item, "generator")
607 assert [%{"name" => ":blobcat:"}] = history_item["tag"]
610 test "it works when there is no or bad history" do
612 "formerRepresentations" => %{
616 "emoji" => %{"blobcat" => "http://localhost:4001/emoji/blobcat.png"}
622 processed = Transmogrifier.prepare_object(original)
623 assert processed["formerRepresentations"] == original["formerRepresentations"]