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
11 alias Pleroma.Tests.ObanHelpers
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.AdminAPI.AccountView
16 alias Pleroma.Web.CommonAPI
19 import Pleroma.Factory
20 import ExUnit.CaptureLog
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 setup do: clear_config([:instance, :max_remote_account_fields])
29 describe "handle_incoming" do
30 test "it works for incoming unfollows with an existing follow" do
34 File.read!("test/fixtures/mastodon-follow-activity.json")
36 |> Map.put("object", user.ap_id)
38 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
41 File.read!("test/fixtures/mastodon-unfollow-activity.json")
43 |> Map.put("object", follow_data)
45 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
47 assert data["type"] == "Undo"
48 assert data["object"]["type"] == "Follow"
49 assert data["object"]["object"] == user.ap_id
50 assert data["actor"] == "http://mastodon.example.org/users/admin"
52 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
55 test "it accepts Flag activities" do
57 other_user = insert(:user)
59 {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
60 object = Object.normalize(activity, fetch: false)
64 "id" => activity.data["id"],
65 "content" => "test post",
66 "published" => object.data["published"],
67 "actor" => AccountView.render("show.json", %{user: user, skip_visibility_check: true})
71 "@context" => "https://www.w3.org/ns/activitystreams",
73 "object" => [user.ap_id, activity.data["id"]],
75 "content" => "blocked AND reported!!!",
76 "actor" => other_user.ap_id
79 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
81 assert activity.data["object"] == [user.ap_id, note_obj]
82 assert activity.data["content"] == "blocked AND reported!!!"
83 assert activity.data["actor"] == other_user.ap_id
84 assert activity.data["cc"] == [user.ap_id]
87 test "it accepts Move activities" do
88 old_user = insert(:user)
89 new_user = insert(:user)
92 "@context" => "https://www.w3.org/ns/activitystreams",
94 "actor" => old_user.ap_id,
95 "object" => old_user.ap_id,
96 "target" => new_user.ap_id
99 assert :error = Transmogrifier.handle_incoming(message)
101 {:ok, _new_user} = User.update_and_set_cache(new_user, %{also_known_as: [old_user.ap_id]})
103 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(message)
104 assert activity.actor == old_user.ap_id
105 assert activity.data["actor"] == old_user.ap_id
106 assert activity.data["object"] == old_user.ap_id
107 assert activity.data["target"] == new_user.ap_id
108 assert activity.data["type"] == "Move"
111 test "it fixes both the Create and object contexts in a reply" do
112 insert(:user, ap_id: "https://mk.absturztau.be/users/8ozbzjs3o8")
113 insert(:user, ap_id: "https://p.helene.moe/users/helene")
116 "test/fixtures/create-pleroma-reply-to-misskey-thread.json"
120 assert {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(create_activity)
122 object = Object.normalize(activity, fetch: false)
124 assert activity.data["context"] == object.data["context"]
128 describe "prepare outgoing" do
129 test "it inlines private announced objects" do
132 {:ok, activity} = CommonAPI.post(user, %{status: "hey", visibility: "private"})
134 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
136 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
138 assert modified["object"]["content"] == "hey"
139 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
142 test "it turns mentions into tags" do
144 other_user = insert(:user)
147 CommonAPI.post(user, %{status: "hey, @#{other_user.nickname}, how are ya? #2hu"})
149 with_mock Pleroma.Notification,
150 get_notified_from_activity: fn _, _ -> [] end do
151 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
153 object = modified["object"]
155 expected_mention = %{
156 "href" => other_user.ap_id,
157 "name" => "@#{other_user.nickname}",
162 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
167 refute called(Pleroma.Notification.get_notified_from_activity(:_, :_))
168 assert Enum.member?(object["tag"], expected_tag)
169 assert Enum.member?(object["tag"], expected_mention)
173 test "it adds the json-ld context and the conversation property" do
176 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
177 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
179 assert modified["@context"] == Utils.make_json_ld_header()["@context"]
181 assert modified["object"]["conversation"] == modified["context"]
184 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
187 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
188 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
190 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
193 test "it strips internal hashtag data" do
196 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu"})
199 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
204 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
206 assert modified["object"]["tag"] == [expected_tag]
209 test "it strips internal fields" do
213 CommonAPI.post(user, %{
214 status: "#2hu :firefox:",
215 generator: %{type: "Application", name: "TestClient", url: "https://pleroma.social"}
218 # Ensure injected application data made it into the activity
219 # as we don't have a Token to derive it from, otherwise it will
220 # be nil and the test will pass
224 url: "https://pleroma.social"
225 } == activity.object.data["generator"]
227 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
229 assert length(modified["object"]["tag"]) == 2
231 assert is_nil(modified["object"]["emoji"])
232 assert is_nil(modified["object"]["like_count"])
233 assert is_nil(modified["object"]["announcements"])
234 assert is_nil(modified["object"]["announcement_count"])
235 assert is_nil(modified["object"]["context_id"])
236 assert is_nil(modified["object"]["generator"])
239 test "it strips internal fields of article" do
240 activity = insert(:article_activity)
242 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
244 assert length(modified["object"]["tag"]) == 2
246 assert is_nil(modified["object"]["emoji"])
247 assert is_nil(modified["object"]["like_count"])
248 assert is_nil(modified["object"]["announcements"])
249 assert is_nil(modified["object"]["announcement_count"])
250 assert is_nil(modified["object"]["context_id"])
251 assert is_nil(modified["object"]["likes"])
254 test "the directMessage flag is present" do
256 other_user = insert(:user)
258 {:ok, activity} = CommonAPI.post(user, %{status: "2hu :moominmamma:"})
260 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
262 assert modified["directMessage"] == false
264 {:ok, activity} = CommonAPI.post(user, %{status: "@#{other_user.nickname} :moominmamma:"})
266 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
268 assert modified["directMessage"] == false
271 CommonAPI.post(user, %{
272 status: "@#{other_user.nickname} :moominmamma:",
276 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
278 assert modified["directMessage"] == true
281 test "it strips BCC field" do
283 {:ok, list} = Pleroma.List.create("foo", user)
285 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
287 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
289 assert is_nil(modified["bcc"])
292 test "custom emoji urls are URI encoded" do
293 # :dinosaur: filename has a space -> dino walking.gif
296 {:ok, activity} = CommonAPI.post(user, %{status: "everybody do the dinosaur :dinosaur:"})
298 {:ok, prepared} = Transmogrifier.prepare_outgoing(activity.data)
300 assert length(prepared["object"]["tag"]) == 1
302 url = prepared["object"]["tag"] |> List.first() |> Map.get("icon") |> Map.get("url")
304 assert url == "http://localhost:4001/emoji/dino%20walking.gif"
308 describe "user upgrade" do
309 test "it upgrades a user to activitypub" do
312 nickname: "rye@niu.moe",
314 ap_id: "https://niu.moe/users/rye",
315 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
318 user_two = insert(:user)
319 Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept)
321 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
322 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{status: "test"})
323 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
325 user = User.get_cached_by_id(user.id)
326 assert user.note_count == 1
328 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
329 ObanHelpers.perform_all()
331 assert user.ap_enabled
332 assert user.note_count == 1
333 assert user.follower_address == "https://niu.moe/users/rye/followers"
334 assert user.following_address == "https://niu.moe/users/rye/following"
336 user = User.get_cached_by_id(user.id)
337 assert user.note_count == 1
339 activity = Activity.get_by_id(activity.id)
340 assert user.follower_address in activity.recipients
346 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
355 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
360 refute "..." in activity.recipients
362 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
363 refute user.follower_address in unrelated_activity.recipients
365 user_two = User.get_cached_by_id(user_two.id)
366 assert User.following?(user_two, user)
367 refute "..." in User.following(user_two)
371 describe "actor rewriting" do
372 test "it fixes the actor URL property to be a proper URI" do
374 "url" => %{"href" => "http://example.com"}
377 rewritten = Transmogrifier.maybe_fix_user_object(data)
378 assert rewritten["url"] == "http://example.com"
382 describe "actor origin containment" do
383 test "it rejects activities which reference objects with bogus origins" do
385 "@context" => "https://www.w3.org/ns/activitystreams",
386 "id" => "http://mastodon.example.org/users/admin/activities/1234",
387 "actor" => "http://mastodon.example.org/users/admin",
388 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
389 "object" => "https://info.pleroma.site/activity.json",
393 assert capture_log(fn ->
394 {:error, _} = Transmogrifier.handle_incoming(data)
395 end) =~ "Object containment failed"
398 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
400 "@context" => "https://www.w3.org/ns/activitystreams",
401 "id" => "http://mastodon.example.org/users/admin/activities/1234",
402 "actor" => "http://mastodon.example.org/users/admin",
403 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
404 "object" => "https://info.pleroma.site/activity2.json",
408 assert capture_log(fn ->
409 {:error, _} = Transmogrifier.handle_incoming(data)
410 end) =~ "Object containment failed"
413 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
415 "@context" => "https://www.w3.org/ns/activitystreams",
416 "id" => "http://mastodon.example.org/users/admin/activities/1234",
417 "actor" => "http://mastodon.example.org/users/admin",
418 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
419 "object" => "https://info.pleroma.site/activity3.json",
423 assert capture_log(fn ->
424 {:error, _} = Transmogrifier.handle_incoming(data)
425 end) =~ "Object containment failed"
429 describe "fix_explicit_addressing" do
435 test "moves non-explicitly mentioned actors to cc", %{user: user} do
436 explicitly_mentioned_actors = [
437 "https://pleroma.gold/users/user1",
438 "https://pleroma.gold/user2"
442 "actor" => user.ap_id,
443 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
446 Enum.map(explicitly_mentioned_actors, fn href ->
447 %{"type" => "Mention", "href" => href}
451 fixed_object = Transmogrifier.fix_explicit_addressing(object, user.follower_address)
452 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
453 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
454 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
457 test "does not move actor's follower collection to cc", %{user: user} do
459 "actor" => user.ap_id,
460 "to" => [user.follower_address],
464 fixed_object = Transmogrifier.fix_explicit_addressing(object, user.follower_address)
465 assert user.follower_address in fixed_object["to"]
466 refute user.follower_address in fixed_object["cc"]
469 test "removes recipient's follower collection from cc", %{user: user} do
470 recipient = insert(:user)
473 "actor" => user.ap_id,
474 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
475 "cc" => [user.follower_address, recipient.follower_address]
478 fixed_object = Transmogrifier.fix_explicit_addressing(object, user.follower_address)
480 assert user.follower_address in fixed_object["cc"]
481 refute recipient.follower_address in fixed_object["cc"]
482 refute recipient.follower_address in fixed_object["to"]
486 describe "fix_summary/1" do
487 test "returns fixed object" do
488 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
489 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
490 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
494 describe "fix_url/1" do
495 test "fixes data for object when url is map" do
499 "mimeType" => "video/mp4",
500 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
504 assert Transmogrifier.fix_url(object) == %{
505 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
509 test "returns non-modified object" do
510 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
514 describe "get_obj_helper/2" do
515 test "returns nil when cannot normalize object" do
516 assert capture_log(fn ->
517 refute Transmogrifier.get_obj_helper("test-obj-id")
518 end) =~ "Unsupported URI scheme"
521 @tag capture_log: true
522 test "returns {:ok, %Object{}} for success case" do
523 assert {:ok, %Object{}} =
524 Transmogrifier.get_obj_helper(
525 "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
530 describe "fix_attachments/1" do
531 test "puts dimensions into attachment url field" do
535 "type" => "Document",
536 "name" => "Hello world",
537 "url" => "https://media.example.tld/1.jpg",
540 "mediaType" => "image/jpeg",
541 "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
549 "type" => "Document",
550 "name" => "Hello world",
554 "mediaType" => "image/jpeg",
555 "href" => "https://media.example.tld/1.jpg",
560 "mediaType" => "image/jpeg",
561 "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
566 assert Transmogrifier.fix_attachments(object) == expected