1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
9 require Pleroma.Constants
11 alias Pleroma.Activity
13 alias Pleroma.Tests.ObanHelpers
15 alias Pleroma.Web.ActivityPub.Transmogrifier
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.CommonAPI
20 import Pleroma.Factory
21 import ExUnit.CaptureLog
24 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 setup do: clear_config([:instance, :max_remote_account_fields])
30 describe "handle_incoming" do
31 test "it works for incoming unfollows with an existing follow" do
35 File.read!("test/fixtures/mastodon-follow-activity.json")
37 |> Map.put("object", user.ap_id)
39 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
42 File.read!("test/fixtures/mastodon-unfollow-activity.json")
44 |> Map.put("object", follow_data)
46 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
48 assert data["type"] == "Undo"
49 assert data["object"]["type"] == "Follow"
50 assert data["object"]["object"] == user.ap_id
51 assert data["actor"] == "http://mastodon.example.org/users/admin"
53 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
56 test "it accepts Flag activities" do
58 other_user = insert(:user)
60 {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
61 object = Object.normalize(activity, fetch: false)
65 "id" => activity.data["id"],
66 "content" => "test post",
67 "published" => object.data["published"],
68 "actor" => AccountView.render("show.json", %{user: user, skip_visibility_check: true})
72 "@context" => "https://www.w3.org/ns/activitystreams",
74 "object" => [user.ap_id, activity.data["id"]],
76 "content" => "blocked AND reported!!!",
77 "actor" => other_user.ap_id
80 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
82 assert activity.data["object"] == [user.ap_id, note_obj]
83 assert activity.data["content"] == "blocked AND reported!!!"
84 assert activity.data["actor"] == other_user.ap_id
85 assert activity.data["cc"] == [user.ap_id]
88 test "it accepts Move activities" do
89 old_user = insert(:user)
90 new_user = insert(:user)
93 "@context" => "https://www.w3.org/ns/activitystreams",
95 "actor" => old_user.ap_id,
96 "object" => old_user.ap_id,
97 "target" => new_user.ap_id
100 assert :error = Transmogrifier.handle_incoming(message)
102 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
104 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
105 assert activity.actor == old_user.ap_id
106 assert activity.data["actor"] == old_user.ap_id
107 assert activity.data["object"] == old_user.ap_id
108 assert activity.data["target"] == new_user.ap_id
109 assert activity.data["type"] == "Move"
112 test "it accepts Add/Remove activities" do
114 "test/fixtures/users_mock/user.json"
116 |> String.replace("{{nickname}}", "lain")
118 object_id = "c61d6733-e256-4fe1-ab13-1e369789423f"
121 "test/fixtures/statuses/note.json"
123 |> String.replace("{{nickname}}", "lain")
124 |> String.replace("{{object_id}}", object_id)
126 object_url = "https://example.com/objects/#{object_id}"
128 actor = "https://example.com/users/lain"
138 headers: [{"content-type", "application/activity+json"}]
148 headers: [{"content-type", "application/activity+json"}]
153 "id" => "https://example.com/objects/d61d6733-e256-4fe1-ab13-1e369789423f",
155 "object" => object_url,
156 "target" => "https://example.com/users/lain/collections/featured",
158 "to" => [Pleroma.Constants.as_public()],
159 "cc" => ["https://example.com/users/lain/followers"]
162 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
163 assert activity.data == message
164 user = User.get_cached_by_ap_id(actor)
165 assert user.pinned_objects[object_url]
168 "id" => "http://localhost:400/objects/d61d6733-e256-4fe1-ab13-1e369789423d",
170 "object" => object_url,
171 "target" => "https://example.com/users/lain/collections/featured",
173 "to" => [Pleroma.Constants.as_public()],
174 "cc" => ["https://example.com/users/lain/followers"]
177 assert {:ok, activity} = Transmogrifier.handle_incoming(remove)
178 assert activity.data == remove
180 user = refresh_record(user)
181 refute user.pinned_objects[object_url]
185 describe "prepare outgoing" do
186 test "it inlines private announced objects" do
189 {:ok, activity} = CommonAPI.post(user, %{status: "hey", visibility: "private"})
191 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
193 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
195 assert modified["object"]["content"] == "hey"
196 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
199 test "it turns mentions into tags" do
201 other_user = insert(:user)
204 CommonAPI.post(user, %{status: "hey, @#{other_user.nickname}, how are ya? #2hu"})
206 with_mock Pleroma.Notification,
207 get_notified_from_activity: fn _, _ -> [] end do
208 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
210 object = modified["object"]
212 expected_mention = %{
213 "href" => other_user.ap_id,
214 "name" => "@#{other_user.nickname}",
219 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
224 refute called(Pleroma.Notification.get_notified_from_activity(:_, :_))
225 assert Enum.member?(object["tag"], expected_tag)
226 assert Enum.member?(object["tag"], expected_mention)
230 test "it adds the json-ld context and the conversation property" do
233 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
234 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
236 assert modified["@context"] ==
237 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
239 assert modified["object"]["conversation"] == modified["context"]
242 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
245 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
246 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
248 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
251 test "it strips internal hashtag data" do
254 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu"})
257 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
262 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
264 assert modified["object"]["tag"] == [expected_tag]
267 test "it strips internal fields" do
271 CommonAPI.post(user, %{
272 status: "#2hu :firefox:",
273 generator: %{type: "Application", name: "TestClient", url: "https://pleroma.social"}
276 # Ensure injected application data made it into the activity
277 # as we don't have a Token to derive it from, otherwise it will
278 # be nil and the test will pass
282 url: "https://pleroma.social"
283 } == activity.object.data["generator"]
285 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
287 assert length(modified["object"]["tag"]) == 2
289 assert is_nil(modified["object"]["emoji"])
290 assert is_nil(modified["object"]["like_count"])
291 assert is_nil(modified["object"]["announcements"])
292 assert is_nil(modified["object"]["announcement_count"])
293 assert is_nil(modified["object"]["context_id"])
294 assert is_nil(modified["object"]["generator"])
297 test "it strips internal fields of article" do
298 activity = insert(:article_activity)
300 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
302 assert length(modified["object"]["tag"]) == 2
304 assert is_nil(modified["object"]["emoji"])
305 assert is_nil(modified["object"]["like_count"])
306 assert is_nil(modified["object"]["announcements"])
307 assert is_nil(modified["object"]["announcement_count"])
308 assert is_nil(modified["object"]["context_id"])
309 assert is_nil(modified["object"]["likes"])
312 test "the directMessage flag is present" do
314 other_user = insert(:user)
316 {:ok, activity} = CommonAPI.post(user, %{status: "2hu :moominmamma:"})
318 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
320 assert modified["directMessage"] == false
322 {:ok, activity} = CommonAPI.post(user, %{status: "@#{other_user.nickname} :moominmamma:"})
324 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
326 assert modified["directMessage"] == false
329 CommonAPI.post(user, %{
330 status: "@#{other_user.nickname} :moominmamma:",
334 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
336 assert modified["directMessage"] == true
339 test "it strips BCC field" do
341 {:ok, list} = Pleroma.List.create("foo", user)
343 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
345 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
347 assert is_nil(modified["bcc"])
350 test "it can handle Listen activities" do
351 listen_activity = insert(:listen)
353 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
355 assert modified["type"] == "Listen"
359 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
361 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
364 test "custom emoji urls are URI encoded" do
365 # :dinosaur: filename has a space -> dino walking.gif
368 {:ok, activity} = CommonAPI.post(user, %{status: "everybody do the dinosaur :dinosaur:"})
370 {:ok, prepared} = Transmogrifier.prepare_outgoing(activity.data)
372 assert length(prepared["object"]["tag"]) == 1
374 url = prepared["object"]["tag"] |> List.first() |> Map.get("icon") |> Map.get("url")
376 assert url == "http://localhost:4001/emoji/dino%20walking.gif"
380 describe "user upgrade" do
381 test "it upgrades a user to activitypub" do
384 nickname: "rye@niu.moe",
386 ap_id: "https://niu.moe/users/rye",
387 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
390 user_two = insert(:user)
391 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
393 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
394 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{status: "test"})
395 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
397 user = User.get_cached_by_id(user.id)
398 assert user.note_count == 1
400 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
401 ObanHelpers.perform_all()
403 assert user.ap_enabled
404 assert user.note_count == 1
405 assert user.follower_address == "https://niu.moe/users/rye/followers"
406 assert user.following_address == "https://niu.moe/users/rye/following"
408 user = User.get_cached_by_id(user.id)
409 assert user.note_count == 1
411 activity = Activity.get_by_id(activity.id)
412 assert user.follower_address in activity.recipients
418 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
427 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
432 refute "..." in activity.recipients
434 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
435 refute user.follower_address in unrelated_activity.recipients
437 user_two = User.get_cached_by_id(user_two.id)
438 assert User.following?(user_two, user)
439 refute "..." in User.following(user_two)
443 describe "actor rewriting" do
444 test "it fixes the actor URL property to be a proper URI" do
446 "url" => %{"href" => "http://example.com"}
449 rewritten = Transmogrifier.maybe_fix_user_object(data)
450 assert rewritten["url"] == "http://example.com"
454 describe "actor origin containment" do
455 test "it rejects activities which reference objects with bogus origins" do
457 "@context" => "https://www.w3.org/ns/activitystreams",
458 "id" => "http://mastodon.example.org/users/admin/activities/1234",
459 "actor" => "http://mastodon.example.org/users/admin",
460 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
461 "object" => "https://info.pleroma.site/activity.json",
465 assert capture_log(fn ->
466 {:error, _} = Transmogrifier.handle_incoming(data)
467 end) =~ "Object containment failed"
470 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
472 "@context" => "https://www.w3.org/ns/activitystreams",
473 "id" => "http://mastodon.example.org/users/admin/activities/1234",
474 "actor" => "http://mastodon.example.org/users/admin",
475 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
476 "object" => "https://info.pleroma.site/activity2.json",
480 assert capture_log(fn ->
481 {:error, _} = Transmogrifier.handle_incoming(data)
482 end) =~ "Object containment failed"
485 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
487 "@context" => "https://www.w3.org/ns/activitystreams",
488 "id" => "http://mastodon.example.org/users/admin/activities/1234",
489 "actor" => "http://mastodon.example.org/users/admin",
490 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
491 "object" => "https://info.pleroma.site/activity3.json",
495 assert capture_log(fn ->
496 {:error, _} = Transmogrifier.handle_incoming(data)
497 end) =~ "Object containment failed"
501 describe "fix_explicit_addressing" do
507 test "moves non-explicitly mentioned actors to cc", %{user: user} do
508 explicitly_mentioned_actors = [
509 "https://pleroma.gold/users/user1",
510 "https://pleroma.gold/user2"
514 "actor" => user.ap_id,
515 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
518 Enum.map(explicitly_mentioned_actors, fn href ->
519 %{"type" => "Mention", "href" => href}
523 fixed_object = Transmogrifier.fix_explicit_addressing(object)
524 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
525 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
526 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
529 test "does not move actor's follower collection to cc", %{user: user} do
531 "actor" => user.ap_id,
532 "to" => [user.follower_address],
536 fixed_object = Transmogrifier.fix_explicit_addressing(object)
537 assert user.follower_address in fixed_object["to"]
538 refute user.follower_address in fixed_object["cc"]
541 test "removes recipient's follower collection from cc", %{user: user} do
542 recipient = insert(:user)
545 "actor" => user.ap_id,
546 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
547 "cc" => [user.follower_address, recipient.follower_address]
550 fixed_object = Transmogrifier.fix_explicit_addressing(object)
552 assert user.follower_address in fixed_object["cc"]
553 refute recipient.follower_address in fixed_object["cc"]
554 refute recipient.follower_address in fixed_object["to"]
558 describe "fix_summary/1" do
559 test "returns fixed object" do
560 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
561 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
562 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
566 describe "fix_url/1" do
567 test "fixes data for object when url is map" do
571 "mimeType" => "video/mp4",
572 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
576 assert Transmogrifier.fix_url(object) == %{
577 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
581 test "returns non-modified object" do
582 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
586 describe "get_obj_helper/2" do
587 test "returns nil when cannot normalize object" do
588 assert capture_log(fn ->
589 refute Transmogrifier.get_obj_helper("test-obj-id")
590 end) =~ "Unsupported URI scheme"
593 @tag capture_log: true
594 test "returns {:ok, %Object{}} for success case" do
595 assert {:ok, %Object{}} =
596 Transmogrifier.get_obj_helper(
597 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"