Merge branch 'fix/pleroma-ctl-rpc' into 'develop'
[akkoma] / test / web / activity_pub / transmogrifier_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Object
9 alias Pleroma.Object.Fetcher
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.CommonAPI
15
16 import Mock
17 import Pleroma.Factory
18 import ExUnit.CaptureLog
19
20 setup_all do
21 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 :ok
23 end
24
25 clear_config([:instance, :max_remote_account_fields])
26
27 describe "handle_incoming" do
28 test "it ignores an incoming notice if we already have it" do
29 activity = insert(:note_activity)
30
31 data =
32 File.read!("test/fixtures/mastodon-post-activity.json")
33 |> Poison.decode!()
34 |> Map.put("object", Object.normalize(activity).data)
35
36 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
37
38 assert activity == returned_activity
39 end
40
41 test "it fetches replied-to activities if we don't have them" do
42 data =
43 File.read!("test/fixtures/mastodon-post-activity.json")
44 |> Poison.decode!()
45
46 object =
47 data["object"]
48 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
49
50 data = Map.put(data, "object", object)
51 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
52 returned_object = Object.normalize(returned_activity, false)
53
54 assert activity =
55 Activity.get_create_by_object_ap_id(
56 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
57 )
58
59 assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
60 end
61
62 test "it does not fetch replied-to activities beyond max_replies_depth" do
63 data =
64 File.read!("test/fixtures/mastodon-post-activity.json")
65 |> Poison.decode!()
66
67 object =
68 data["object"]
69 |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
70
71 data = Map.put(data, "object", object)
72
73 with_mock Pleroma.Web.Federator,
74 allowed_incoming_reply_depth?: fn _ -> false end do
75 {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
76
77 returned_object = Object.normalize(returned_activity, false)
78
79 refute Activity.get_create_by_object_ap_id(
80 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
81 )
82
83 assert returned_object.data["inReplyToAtomUri"] ==
84 "https://shitposter.club/notice/2827873"
85 end
86 end
87
88 test "it does not crash if the object in inReplyTo can't be fetched" do
89 data =
90 File.read!("test/fixtures/mastodon-post-activity.json")
91 |> Poison.decode!()
92
93 object =
94 data["object"]
95 |> Map.put("inReplyTo", "https://404.site/whatever")
96
97 data =
98 data
99 |> Map.put("object", object)
100
101 assert capture_log(fn ->
102 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
103 end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil"
104 end
105
106 test "it works for incoming notices" do
107 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
108
109 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
110
111 assert data["id"] ==
112 "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
113
114 assert data["context"] ==
115 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
116
117 assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
118
119 assert data["cc"] == [
120 "http://mastodon.example.org/users/admin/followers",
121 "http://localtesting.pleroma.lol/users/lain"
122 ]
123
124 assert data["actor"] == "http://mastodon.example.org/users/admin"
125
126 object_data = Object.normalize(data["object"]).data
127
128 assert object_data["id"] ==
129 "http://mastodon.example.org/users/admin/statuses/99512778738411822"
130
131 assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
132
133 assert object_data["cc"] == [
134 "http://mastodon.example.org/users/admin/followers",
135 "http://localtesting.pleroma.lol/users/lain"
136 ]
137
138 assert object_data["actor"] == "http://mastodon.example.org/users/admin"
139 assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
140
141 assert object_data["context"] ==
142 "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
143
144 assert object_data["sensitive"] == true
145
146 user = User.get_cached_by_ap_id(object_data["actor"])
147
148 assert user.info.note_count == 1
149 end
150
151 test "it works for incoming notices with hashtags" do
152 data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
153
154 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
155 object = Object.normalize(data["object"])
156
157 assert Enum.at(object.data["tag"], 2) == "moo"
158 end
159
160 test "it works for incoming questions" do
161 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
162
163 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
164
165 object = Object.normalize(activity)
166
167 assert Enum.all?(object.data["oneOf"], fn choice ->
168 choice["name"] in [
169 "Dunno",
170 "Everyone knows that!",
171 "25 char limit is dumb",
172 "I can't even fit a funny"
173 ]
174 end)
175 end
176
177 test "it works for incoming listens" do
178 data = %{
179 "@context" => "https://www.w3.org/ns/activitystreams",
180 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
181 "cc" => [],
182 "type" => "Listen",
183 "id" => "http://mastodon.example.org/users/admin/listens/1234/activity",
184 "actor" => "http://mastodon.example.org/users/admin",
185 "object" => %{
186 "type" => "Audio",
187 "id" => "http://mastodon.example.org/users/admin/listens/1234",
188 "attributedTo" => "http://mastodon.example.org/users/admin",
189 "title" => "lain radio episode 1",
190 "artist" => "lain",
191 "album" => "lain radio",
192 "length" => 180_000
193 }
194 }
195
196 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
197
198 object = Object.normalize(activity)
199
200 assert object.data["title"] == "lain radio episode 1"
201 assert object.data["artist"] == "lain"
202 assert object.data["album"] == "lain radio"
203 assert object.data["length"] == 180_000
204 end
205
206 test "it rewrites Note votes to Answers and increments vote counters on question activities" do
207 user = insert(:user)
208
209 {:ok, activity} =
210 CommonAPI.post(user, %{
211 "status" => "suya...",
212 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
213 })
214
215 object = Object.normalize(activity)
216
217 data =
218 File.read!("test/fixtures/mastodon-vote.json")
219 |> Poison.decode!()
220 |> Kernel.put_in(["to"], user.ap_id)
221 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
222 |> Kernel.put_in(["object", "to"], user.ap_id)
223
224 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
225 answer_object = Object.normalize(activity)
226 assert answer_object.data["type"] == "Answer"
227 object = Object.get_by_ap_id(object.data["id"])
228
229 assert Enum.any?(
230 object.data["oneOf"],
231 fn
232 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
233 _ -> false
234 end
235 )
236 end
237
238 test "it works for incoming notices with contentMap" do
239 data =
240 File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
241
242 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
243 object = Object.normalize(data["object"])
244
245 assert object.data["content"] ==
246 "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
247 end
248
249 test "it works for incoming notices with to/cc not being an array (kroeg)" do
250 data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!()
251
252 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
253 object = Object.normalize(data["object"])
254
255 assert object.data["content"] ==
256 "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
257 end
258
259 test "it works for incoming announces with actor being inlined (kroeg)" do
260 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
261
262 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
263
264 assert data["actor"] == "https://puckipedia.com/"
265 end
266
267 test "it works for incoming notices with tag not being an array (kroeg)" do
268 data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Poison.decode!()
269
270 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
271 object = Object.normalize(data["object"])
272
273 assert object.data["emoji"] == %{
274 "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
275 }
276
277 data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Poison.decode!()
278
279 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
280 object = Object.normalize(data["object"])
281
282 assert "test" in object.data["tag"]
283 end
284
285 test "it works for incoming notices with url not being a string (prismo)" do
286 data = File.read!("test/fixtures/prismo-url-map.json") |> Poison.decode!()
287
288 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
289 object = Object.normalize(data["object"])
290
291 assert object.data["url"] == "https://prismo.news/posts/83"
292 end
293
294 test "it cleans up incoming notices which are not really DMs" do
295 user = insert(:user)
296 other_user = insert(:user)
297
298 to = [user.ap_id, other_user.ap_id]
299
300 data =
301 File.read!("test/fixtures/mastodon-post-activity.json")
302 |> Poison.decode!()
303 |> Map.put("to", to)
304 |> Map.put("cc", [])
305
306 object =
307 data["object"]
308 |> Map.put("to", to)
309 |> Map.put("cc", [])
310
311 data = Map.put(data, "object", object)
312
313 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
314
315 assert data["to"] == []
316 assert data["cc"] == to
317
318 object_data = Object.normalize(activity).data
319
320 assert object_data["to"] == []
321 assert object_data["cc"] == to
322 end
323
324 test "it works for incoming likes" do
325 user = insert(:user)
326 {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
327
328 data =
329 File.read!("test/fixtures/mastodon-like.json")
330 |> Poison.decode!()
331 |> Map.put("object", activity.data["object"])
332
333 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
334
335 assert data["actor"] == "http://mastodon.example.org/users/admin"
336 assert data["type"] == "Like"
337 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
338 assert data["object"] == activity.data["object"]
339 end
340
341 test "it returns an error for incoming unlikes wihout a like activity" do
342 user = insert(:user)
343 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
344
345 data =
346 File.read!("test/fixtures/mastodon-undo-like.json")
347 |> Poison.decode!()
348 |> Map.put("object", activity.data["object"])
349
350 assert Transmogrifier.handle_incoming(data) == :error
351 end
352
353 test "it works for incoming unlikes with an existing like activity" do
354 user = insert(:user)
355 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
356
357 like_data =
358 File.read!("test/fixtures/mastodon-like.json")
359 |> Poison.decode!()
360 |> Map.put("object", activity.data["object"])
361
362 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
363
364 data =
365 File.read!("test/fixtures/mastodon-undo-like.json")
366 |> Poison.decode!()
367 |> Map.put("object", like_data)
368 |> Map.put("actor", like_data["actor"])
369
370 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
371
372 assert data["actor"] == "http://mastodon.example.org/users/admin"
373 assert data["type"] == "Undo"
374 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
375 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
376 end
377
378 test "it works for incoming unlikes with an existing like activity and a compact object" do
379 user = insert(:user)
380 {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})
381
382 like_data =
383 File.read!("test/fixtures/mastodon-like.json")
384 |> Poison.decode!()
385 |> Map.put("object", activity.data["object"])
386
387 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
388
389 data =
390 File.read!("test/fixtures/mastodon-undo-like.json")
391 |> Poison.decode!()
392 |> Map.put("object", like_data["id"])
393 |> Map.put("actor", like_data["actor"])
394
395 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
396
397 assert data["actor"] == "http://mastodon.example.org/users/admin"
398 assert data["type"] == "Undo"
399 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
400 assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
401 end
402
403 test "it works for incoming announces" do
404 data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
405
406 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
407
408 assert data["actor"] == "http://mastodon.example.org/users/admin"
409 assert data["type"] == "Announce"
410
411 assert data["id"] ==
412 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
413
414 assert data["object"] ==
415 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
416
417 assert Activity.get_create_by_object_ap_id(data["object"])
418 end
419
420 test "it works for incoming announces with an existing activity" do
421 user = insert(:user)
422 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
423
424 data =
425 File.read!("test/fixtures/mastodon-announce.json")
426 |> Poison.decode!()
427 |> Map.put("object", activity.data["object"])
428
429 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
430
431 assert data["actor"] == "http://mastodon.example.org/users/admin"
432 assert data["type"] == "Announce"
433
434 assert data["id"] ==
435 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
436
437 assert data["object"] == activity.data["object"]
438
439 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
440 end
441
442 test "it works for incoming announces with an inlined activity" do
443 data =
444 File.read!("test/fixtures/mastodon-announce-private.json")
445 |> Poison.decode!()
446
447 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
448
449 assert data["actor"] == "http://mastodon.example.org/users/admin"
450 assert data["type"] == "Announce"
451
452 assert data["id"] ==
453 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
454
455 object = Object.normalize(data["object"])
456
457 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
458 assert object.data["content"] == "this is a private toot"
459 end
460
461 test "it rejects incoming announces with an inlined activity from another origin" do
462 data =
463 File.read!("test/fixtures/bogus-mastodon-announce.json")
464 |> Poison.decode!()
465
466 assert :error = Transmogrifier.handle_incoming(data)
467 end
468
469 test "it does not clobber the addressing on announce activities" do
470 user = insert(:user)
471 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
472
473 data =
474 File.read!("test/fixtures/mastodon-announce.json")
475 |> Poison.decode!()
476 |> Map.put("object", Object.normalize(activity).data["id"])
477 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
478 |> Map.put("cc", [])
479
480 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
481
482 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
483 end
484
485 test "it ensures that as:Public activities make it to their followers collection" do
486 user = insert(:user)
487
488 data =
489 File.read!("test/fixtures/mastodon-post-activity.json")
490 |> Poison.decode!()
491 |> Map.put("actor", user.ap_id)
492 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
493 |> Map.put("cc", [])
494
495 object =
496 data["object"]
497 |> Map.put("attributedTo", user.ap_id)
498 |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
499 |> Map.put("cc", [])
500 |> Map.put("id", user.ap_id <> "/activities/12345678")
501
502 data = Map.put(data, "object", object)
503
504 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
505
506 assert data["cc"] == [User.ap_followers(user)]
507 end
508
509 test "it ensures that address fields become lists" do
510 user = insert(:user)
511
512 data =
513 File.read!("test/fixtures/mastodon-post-activity.json")
514 |> Poison.decode!()
515 |> Map.put("actor", user.ap_id)
516 |> Map.put("to", nil)
517 |> Map.put("cc", nil)
518
519 object =
520 data["object"]
521 |> Map.put("attributedTo", user.ap_id)
522 |> Map.put("to", nil)
523 |> Map.put("cc", nil)
524 |> Map.put("id", user.ap_id <> "/activities/12345678")
525
526 data = Map.put(data, "object", object)
527
528 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
529
530 assert !is_nil(data["to"])
531 assert !is_nil(data["cc"])
532 end
533
534 test "it strips internal likes" do
535 data =
536 File.read!("test/fixtures/mastodon-post-activity.json")
537 |> Poison.decode!()
538
539 likes = %{
540 "first" =>
541 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
542 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
543 "totalItems" => 3,
544 "type" => "OrderedCollection"
545 }
546
547 object = Map.put(data["object"], "likes", likes)
548 data = Map.put(data, "object", object)
549
550 {:ok, %Activity{object: object}} = Transmogrifier.handle_incoming(data)
551
552 refute Map.has_key?(object.data, "likes")
553 end
554
555 test "it works for incoming update activities" do
556 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
557
558 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
559 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
560
561 object =
562 update_data["object"]
563 |> Map.put("actor", data["actor"])
564 |> Map.put("id", data["actor"])
565
566 update_data =
567 update_data
568 |> Map.put("actor", data["actor"])
569 |> Map.put("object", object)
570
571 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
572
573 assert data["id"] == update_data["id"]
574
575 user = User.get_cached_by_ap_id(data["actor"])
576 assert user.name == "gargle"
577
578 assert user.avatar["url"] == [
579 %{
580 "href" =>
581 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
582 }
583 ]
584
585 assert user.info.banner["url"] == [
586 %{
587 "href" =>
588 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
589 }
590 ]
591
592 assert user.bio == "<p>Some bio</p>"
593 end
594
595 test "it works with custom profile fields" do
596 {:ok, activity} =
597 "test/fixtures/mastodon-post-activity.json"
598 |> File.read!()
599 |> Poison.decode!()
600 |> Transmogrifier.handle_incoming()
601
602 user = User.get_cached_by_ap_id(activity.actor)
603
604 assert User.Info.fields(user.info) == [
605 %{"name" => "foo", "value" => "bar"},
606 %{"name" => "foo1", "value" => "bar1"}
607 ]
608
609 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
610
611 object =
612 update_data["object"]
613 |> Map.put("actor", user.ap_id)
614 |> Map.put("id", user.ap_id)
615
616 update_data =
617 update_data
618 |> Map.put("actor", user.ap_id)
619 |> Map.put("object", object)
620
621 {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
622
623 user = User.get_cached_by_ap_id(user.ap_id)
624
625 assert User.Info.fields(user.info) == [
626 %{"name" => "foo", "value" => "updated"},
627 %{"name" => "foo1", "value" => "updated"}
628 ]
629
630 Pleroma.Config.put([:instance, :max_remote_account_fields], 2)
631
632 update_data =
633 put_in(update_data, ["object", "attachment"], [
634 %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
635 %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
636 %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
637 ])
638
639 {:ok, _} = Transmogrifier.handle_incoming(update_data)
640
641 user = User.get_cached_by_ap_id(user.ap_id)
642
643 assert User.Info.fields(user.info) == [
644 %{"name" => "foo", "value" => "updated"},
645 %{"name" => "foo1", "value" => "updated"}
646 ]
647
648 update_data = put_in(update_data, ["object", "attachment"], [])
649
650 {:ok, _} = Transmogrifier.handle_incoming(update_data)
651
652 user = User.get_cached_by_ap_id(user.ap_id)
653
654 assert User.Info.fields(user.info) == []
655 end
656
657 test "it works for incoming update activities which lock the account" do
658 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
659
660 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
661 update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()
662
663 object =
664 update_data["object"]
665 |> Map.put("actor", data["actor"])
666 |> Map.put("id", data["actor"])
667 |> Map.put("manuallyApprovesFollowers", true)
668
669 update_data =
670 update_data
671 |> Map.put("actor", data["actor"])
672 |> Map.put("object", object)
673
674 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
675
676 user = User.get_cached_by_ap_id(data["actor"])
677 assert user.info.locked == true
678 end
679
680 test "it works for incoming deletes" do
681 activity = insert(:note_activity)
682 deleting_user = insert(:user)
683
684 data =
685 File.read!("test/fixtures/mastodon-delete.json")
686 |> Poison.decode!()
687
688 object =
689 data["object"]
690 |> Map.put("id", activity.data["object"])
691
692 data =
693 data
694 |> Map.put("object", object)
695 |> Map.put("actor", deleting_user.ap_id)
696
697 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
698 Transmogrifier.handle_incoming(data)
699
700 assert id == data["id"]
701 refute Activity.get_by_id(activity.id)
702 assert actor == deleting_user.ap_id
703 end
704
705 test "it fails for incoming deletes with spoofed origin" do
706 activity = insert(:note_activity)
707
708 data =
709 File.read!("test/fixtures/mastodon-delete.json")
710 |> Poison.decode!()
711
712 object =
713 data["object"]
714 |> Map.put("id", activity.data["object"])
715
716 data =
717 data
718 |> Map.put("object", object)
719
720 assert capture_log(fn ->
721 :error = Transmogrifier.handle_incoming(data)
722 end) =~
723 "[error] Could not decode user at fetch http://mastodon.example.org/users/gargron, {:error, {:error, :nxdomain}}"
724
725 assert Activity.get_by_id(activity.id)
726 end
727
728 test "it works for incoming user deletes" do
729 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
730
731 data =
732 File.read!("test/fixtures/mastodon-delete-user.json")
733 |> Poison.decode!()
734
735 {:ok, _} = Transmogrifier.handle_incoming(data)
736 ObanHelpers.perform_all()
737
738 refute User.get_cached_by_ap_id(ap_id)
739 end
740
741 test "it fails for incoming user deletes with spoofed origin" do
742 %{ap_id: ap_id} = insert(:user)
743
744 data =
745 File.read!("test/fixtures/mastodon-delete-user.json")
746 |> Poison.decode!()
747 |> Map.put("actor", ap_id)
748
749 assert :error == Transmogrifier.handle_incoming(data)
750 assert User.get_cached_by_ap_id(ap_id)
751 end
752
753 test "it works for incoming unannounces with an existing notice" do
754 user = insert(:user)
755 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
756
757 announce_data =
758 File.read!("test/fixtures/mastodon-announce.json")
759 |> Poison.decode!()
760 |> Map.put("object", activity.data["object"])
761
762 {:ok, %Activity{data: announce_data, local: false}} =
763 Transmogrifier.handle_incoming(announce_data)
764
765 data =
766 File.read!("test/fixtures/mastodon-undo-announce.json")
767 |> Poison.decode!()
768 |> Map.put("object", announce_data)
769 |> Map.put("actor", announce_data["actor"])
770
771 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
772
773 assert data["type"] == "Undo"
774 assert object_data = data["object"]
775 assert object_data["type"] == "Announce"
776 assert object_data["object"] == activity.data["object"]
777
778 assert object_data["id"] ==
779 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
780 end
781
782 test "it works for incomming unfollows with an existing follow" do
783 user = insert(:user)
784
785 follow_data =
786 File.read!("test/fixtures/mastodon-follow-activity.json")
787 |> Poison.decode!()
788 |> Map.put("object", user.ap_id)
789
790 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
791
792 data =
793 File.read!("test/fixtures/mastodon-unfollow-activity.json")
794 |> Poison.decode!()
795 |> Map.put("object", follow_data)
796
797 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
798
799 assert data["type"] == "Undo"
800 assert data["object"]["type"] == "Follow"
801 assert data["object"]["object"] == user.ap_id
802 assert data["actor"] == "http://mastodon.example.org/users/admin"
803
804 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
805 end
806
807 test "it works for incoming blocks" do
808 user = insert(:user)
809
810 data =
811 File.read!("test/fixtures/mastodon-block-activity.json")
812 |> Poison.decode!()
813 |> Map.put("object", user.ap_id)
814
815 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
816
817 assert data["type"] == "Block"
818 assert data["object"] == user.ap_id
819 assert data["actor"] == "http://mastodon.example.org/users/admin"
820
821 blocker = User.get_cached_by_ap_id(data["actor"])
822
823 assert User.blocks?(blocker, user)
824 end
825
826 test "incoming blocks successfully tear down any follow relationship" do
827 blocker = insert(:user)
828 blocked = insert(:user)
829
830 data =
831 File.read!("test/fixtures/mastodon-block-activity.json")
832 |> Poison.decode!()
833 |> Map.put("object", blocked.ap_id)
834 |> Map.put("actor", blocker.ap_id)
835
836 {:ok, blocker} = User.follow(blocker, blocked)
837 {:ok, blocked} = User.follow(blocked, blocker)
838
839 assert User.following?(blocker, blocked)
840 assert User.following?(blocked, blocker)
841
842 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
843
844 assert data["type"] == "Block"
845 assert data["object"] == blocked.ap_id
846 assert data["actor"] == blocker.ap_id
847
848 blocker = User.get_cached_by_ap_id(data["actor"])
849 blocked = User.get_cached_by_ap_id(data["object"])
850
851 assert User.blocks?(blocker, blocked)
852
853 refute User.following?(blocker, blocked)
854 refute User.following?(blocked, blocker)
855 end
856
857 test "it works for incoming unblocks with an existing block" do
858 user = insert(:user)
859
860 block_data =
861 File.read!("test/fixtures/mastodon-block-activity.json")
862 |> Poison.decode!()
863 |> Map.put("object", user.ap_id)
864
865 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
866
867 data =
868 File.read!("test/fixtures/mastodon-unblock-activity.json")
869 |> Poison.decode!()
870 |> Map.put("object", block_data)
871
872 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
873 assert data["type"] == "Undo"
874 assert data["object"]["type"] == "Block"
875 assert data["object"]["object"] == user.ap_id
876 assert data["actor"] == "http://mastodon.example.org/users/admin"
877
878 blocker = User.get_cached_by_ap_id(data["actor"])
879
880 refute User.blocks?(blocker, user)
881 end
882
883 test "it works for incoming accepts which were pre-accepted" do
884 follower = insert(:user)
885 followed = insert(:user)
886
887 {:ok, follower} = User.follow(follower, followed)
888 assert User.following?(follower, followed) == true
889
890 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
891
892 accept_data =
893 File.read!("test/fixtures/mastodon-accept-activity.json")
894 |> Poison.decode!()
895 |> Map.put("actor", followed.ap_id)
896
897 object =
898 accept_data["object"]
899 |> Map.put("actor", follower.ap_id)
900 |> Map.put("id", follow_activity.data["id"])
901
902 accept_data = Map.put(accept_data, "object", object)
903
904 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
905 refute activity.local
906
907 assert activity.data["object"] == follow_activity.data["id"]
908
909 assert activity.data["id"] == accept_data["id"]
910
911 follower = User.get_cached_by_id(follower.id)
912
913 assert User.following?(follower, followed) == true
914 end
915
916 test "it works for incoming accepts which were orphaned" do
917 follower = insert(:user)
918 followed = insert(:user, %{info: %User.Info{locked: true}})
919
920 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
921
922 accept_data =
923 File.read!("test/fixtures/mastodon-accept-activity.json")
924 |> Poison.decode!()
925 |> Map.put("actor", followed.ap_id)
926
927 accept_data =
928 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
929
930 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
931 assert activity.data["object"] == follow_activity.data["id"]
932
933 follower = User.get_cached_by_id(follower.id)
934
935 assert User.following?(follower, followed) == true
936 end
937
938 test "it works for incoming accepts which are referenced by IRI only" do
939 follower = insert(:user)
940 followed = insert(:user, %{info: %User.Info{locked: true}})
941
942 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
943
944 accept_data =
945 File.read!("test/fixtures/mastodon-accept-activity.json")
946 |> Poison.decode!()
947 |> Map.put("actor", followed.ap_id)
948 |> Map.put("object", follow_activity.data["id"])
949
950 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
951 assert activity.data["object"] == follow_activity.data["id"]
952
953 follower = User.get_cached_by_id(follower.id)
954
955 assert User.following?(follower, followed) == true
956 end
957
958 test "it fails for incoming accepts which cannot be correlated" do
959 follower = insert(:user)
960 followed = insert(:user, %{info: %User.Info{locked: true}})
961
962 accept_data =
963 File.read!("test/fixtures/mastodon-accept-activity.json")
964 |> Poison.decode!()
965 |> Map.put("actor", followed.ap_id)
966
967 accept_data =
968 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
969
970 :error = Transmogrifier.handle_incoming(accept_data)
971
972 follower = User.get_cached_by_id(follower.id)
973
974 refute User.following?(follower, followed) == true
975 end
976
977 test "it fails for incoming rejects which cannot be correlated" do
978 follower = insert(:user)
979 followed = insert(:user, %{info: %User.Info{locked: true}})
980
981 accept_data =
982 File.read!("test/fixtures/mastodon-reject-activity.json")
983 |> Poison.decode!()
984 |> Map.put("actor", followed.ap_id)
985
986 accept_data =
987 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
988
989 :error = Transmogrifier.handle_incoming(accept_data)
990
991 follower = User.get_cached_by_id(follower.id)
992
993 refute User.following?(follower, followed) == true
994 end
995
996 test "it works for incoming rejects which are orphaned" do
997 follower = insert(:user)
998 followed = insert(:user, %{info: %User.Info{locked: true}})
999
1000 {:ok, follower} = User.follow(follower, followed)
1001 {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
1002
1003 assert User.following?(follower, followed) == true
1004
1005 reject_data =
1006 File.read!("test/fixtures/mastodon-reject-activity.json")
1007 |> Poison.decode!()
1008 |> Map.put("actor", followed.ap_id)
1009
1010 reject_data =
1011 Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
1012
1013 {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
1014 refute activity.local
1015 assert activity.data["id"] == reject_data["id"]
1016
1017 follower = User.get_cached_by_id(follower.id)
1018
1019 assert User.following?(follower, followed) == false
1020 end
1021
1022 test "it works for incoming rejects which are referenced by IRI only" do
1023 follower = insert(:user)
1024 followed = insert(:user, %{info: %User.Info{locked: true}})
1025
1026 {:ok, follower} = User.follow(follower, followed)
1027 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1028
1029 assert User.following?(follower, followed) == true
1030
1031 reject_data =
1032 File.read!("test/fixtures/mastodon-reject-activity.json")
1033 |> Poison.decode!()
1034 |> Map.put("actor", followed.ap_id)
1035 |> Map.put("object", follow_activity.data["id"])
1036
1037 {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
1038
1039 follower = User.get_cached_by_id(follower.id)
1040
1041 assert User.following?(follower, followed) == false
1042 end
1043
1044 test "it rejects activities without a valid ID" do
1045 user = insert(:user)
1046
1047 data =
1048 File.read!("test/fixtures/mastodon-follow-activity.json")
1049 |> Poison.decode!()
1050 |> Map.put("object", user.ap_id)
1051 |> Map.put("id", "")
1052
1053 :error = Transmogrifier.handle_incoming(data)
1054 end
1055
1056 test "it remaps video URLs as attachments if necessary" do
1057 {:ok, object} =
1058 Fetcher.fetch_object_from_id(
1059 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1060 )
1061
1062 attachment = %{
1063 "type" => "Link",
1064 "mediaType" => "video/mp4",
1065 "href" =>
1066 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1067 "mimeType" => "video/mp4",
1068 "size" => 5_015_880,
1069 "url" => [
1070 %{
1071 "href" =>
1072 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1073 "mediaType" => "video/mp4",
1074 "type" => "Link"
1075 }
1076 ],
1077 "width" => 480
1078 }
1079
1080 assert object.data["url"] ==
1081 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
1082
1083 assert object.data["attachment"] == [attachment]
1084 end
1085
1086 test "it accepts Flag activities" do
1087 user = insert(:user)
1088 other_user = insert(:user)
1089
1090 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
1091 object = Object.normalize(activity)
1092
1093 message = %{
1094 "@context" => "https://www.w3.org/ns/activitystreams",
1095 "cc" => [user.ap_id],
1096 "object" => [user.ap_id, object.data["id"]],
1097 "type" => "Flag",
1098 "content" => "blocked AND reported!!!",
1099 "actor" => other_user.ap_id
1100 }
1101
1102 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
1103
1104 assert activity.data["object"] == [user.ap_id, object.data["id"]]
1105 assert activity.data["content"] == "blocked AND reported!!!"
1106 assert activity.data["actor"] == other_user.ap_id
1107 assert activity.data["cc"] == [user.ap_id]
1108 end
1109 end
1110
1111 describe "prepare outgoing" do
1112 test "it inlines private announced objects" do
1113 user = insert(:user)
1114
1115 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey", "visibility" => "private"})
1116
1117 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
1118
1119 {:ok, modified} = Transmogrifier.prepare_outgoing(announce_activity.data)
1120
1121 assert modified["object"]["content"] == "hey"
1122 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1123 end
1124
1125 test "it turns mentions into tags" do
1126 user = insert(:user)
1127 other_user = insert(:user)
1128
1129 {:ok, activity} =
1130 CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
1131
1132 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1133 object = modified["object"]
1134
1135 expected_mention = %{
1136 "href" => other_user.ap_id,
1137 "name" => "@#{other_user.nickname}",
1138 "type" => "Mention"
1139 }
1140
1141 expected_tag = %{
1142 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1143 "type" => "Hashtag",
1144 "name" => "#2hu"
1145 }
1146
1147 assert Enum.member?(object["tag"], expected_tag)
1148 assert Enum.member?(object["tag"], expected_mention)
1149 end
1150
1151 test "it adds the sensitive property" do
1152 user = insert(:user)
1153
1154 {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
1155 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1156
1157 assert modified["object"]["sensitive"]
1158 end
1159
1160 test "it adds the json-ld context and the conversation property" do
1161 user = insert(:user)
1162
1163 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1164 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1165
1166 assert modified["@context"] ==
1167 Pleroma.Web.ActivityPub.Utils.make_json_ld_header()["@context"]
1168
1169 assert modified["object"]["conversation"] == modified["context"]
1170 end
1171
1172 test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
1173 user = insert(:user)
1174
1175 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
1176 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1177
1178 assert modified["object"]["actor"] == modified["object"]["attributedTo"]
1179 end
1180
1181 test "it strips internal hashtag data" do
1182 user = insert(:user)
1183
1184 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu"})
1185
1186 expected_tag = %{
1187 "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
1188 "type" => "Hashtag",
1189 "name" => "#2hu"
1190 }
1191
1192 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1193
1194 assert modified["object"]["tag"] == [expected_tag]
1195 end
1196
1197 test "it strips internal fields" do
1198 user = insert(:user)
1199
1200 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu :firefox:"})
1201
1202 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1203
1204 assert length(modified["object"]["tag"]) == 2
1205
1206 assert is_nil(modified["object"]["emoji"])
1207 assert is_nil(modified["object"]["like_count"])
1208 assert is_nil(modified["object"]["announcements"])
1209 assert is_nil(modified["object"]["announcement_count"])
1210 assert is_nil(modified["object"]["context_id"])
1211 end
1212
1213 test "it strips internal fields of article" do
1214 activity = insert(:article_activity)
1215
1216 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1217
1218 assert length(modified["object"]["tag"]) == 2
1219
1220 assert is_nil(modified["object"]["emoji"])
1221 assert is_nil(modified["object"]["like_count"])
1222 assert is_nil(modified["object"]["announcements"])
1223 assert is_nil(modified["object"]["announcement_count"])
1224 assert is_nil(modified["object"]["context_id"])
1225 assert is_nil(modified["object"]["likes"])
1226 end
1227
1228 test "the directMessage flag is present" do
1229 user = insert(:user)
1230 other_user = insert(:user)
1231
1232 {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu :moominmamma:"})
1233
1234 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1235
1236 assert modified["directMessage"] == false
1237
1238 {:ok, activity} =
1239 CommonAPI.post(user, %{"status" => "@#{other_user.nickname} :moominmamma:"})
1240
1241 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1242
1243 assert modified["directMessage"] == false
1244
1245 {:ok, activity} =
1246 CommonAPI.post(user, %{
1247 "status" => "@#{other_user.nickname} :moominmamma:",
1248 "visibility" => "direct"
1249 })
1250
1251 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1252
1253 assert modified["directMessage"] == true
1254 end
1255
1256 test "it strips BCC field" do
1257 user = insert(:user)
1258 {:ok, list} = Pleroma.List.create("foo", user)
1259
1260 {:ok, activity} =
1261 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1262
1263 {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
1264
1265 assert is_nil(modified["bcc"])
1266 end
1267
1268 test "it can handle Listen activities" do
1269 listen_activity = insert(:listen)
1270
1271 {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data)
1272
1273 assert modified["type"] == "Listen"
1274
1275 user = insert(:user)
1276
1277 {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"})
1278
1279 {:ok, _modified} = Transmogrifier.prepare_outgoing(activity.data)
1280 end
1281 end
1282
1283 describe "user upgrade" do
1284 test "it upgrades a user to activitypub" do
1285 user =
1286 insert(:user, %{
1287 nickname: "rye@niu.moe",
1288 local: false,
1289 ap_id: "https://niu.moe/users/rye",
1290 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
1291 })
1292
1293 user_two = insert(:user, %{following: [user.follower_address]})
1294
1295 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1296 {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
1297 assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
1298
1299 user = User.get_cached_by_id(user.id)
1300 assert user.info.note_count == 1
1301
1302 {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
1303 ObanHelpers.perform_all()
1304
1305 assert user.info.ap_enabled
1306 assert user.info.note_count == 1
1307 assert user.follower_address == "https://niu.moe/users/rye/followers"
1308 assert user.following_address == "https://niu.moe/users/rye/following"
1309
1310 user = User.get_cached_by_id(user.id)
1311 assert user.info.note_count == 1
1312
1313 activity = Activity.get_by_id(activity.id)
1314 assert user.follower_address in activity.recipients
1315
1316 assert %{
1317 "url" => [
1318 %{
1319 "href" =>
1320 "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
1321 }
1322 ]
1323 } = user.avatar
1324
1325 assert %{
1326 "url" => [
1327 %{
1328 "href" =>
1329 "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
1330 }
1331 ]
1332 } = user.info.banner
1333
1334 refute "..." in activity.recipients
1335
1336 unrelated_activity = Activity.get_by_id(unrelated_activity.id)
1337 refute user.follower_address in unrelated_activity.recipients
1338
1339 user_two = User.get_cached_by_id(user_two.id)
1340 assert user.follower_address in user_two.following
1341 refute "..." in user_two.following
1342 end
1343 end
1344
1345 describe "actor rewriting" do
1346 test "it fixes the actor URL property to be a proper URI" do
1347 data = %{
1348 "url" => %{"href" => "http://example.com"}
1349 }
1350
1351 rewritten = Transmogrifier.maybe_fix_user_object(data)
1352 assert rewritten["url"] == "http://example.com"
1353 end
1354 end
1355
1356 describe "actor origin containment" do
1357 test "it rejects activities which reference objects with bogus origins" do
1358 data = %{
1359 "@context" => "https://www.w3.org/ns/activitystreams",
1360 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1361 "actor" => "http://mastodon.example.org/users/admin",
1362 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1363 "object" => "https://info.pleroma.site/activity.json",
1364 "type" => "Announce"
1365 }
1366
1367 :error = Transmogrifier.handle_incoming(data)
1368 end
1369
1370 test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
1371 data = %{
1372 "@context" => "https://www.w3.org/ns/activitystreams",
1373 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1374 "actor" => "http://mastodon.example.org/users/admin",
1375 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1376 "object" => "https://info.pleroma.site/activity2.json",
1377 "type" => "Announce"
1378 }
1379
1380 :error = Transmogrifier.handle_incoming(data)
1381 end
1382
1383 test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
1384 data = %{
1385 "@context" => "https://www.w3.org/ns/activitystreams",
1386 "id" => "http://mastodon.example.org/users/admin/activities/1234",
1387 "actor" => "http://mastodon.example.org/users/admin",
1388 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1389 "object" => "https://info.pleroma.site/activity3.json",
1390 "type" => "Announce"
1391 }
1392
1393 :error = Transmogrifier.handle_incoming(data)
1394 end
1395 end
1396
1397 describe "reserialization" do
1398 test "successfully reserializes a message with inReplyTo == nil" do
1399 user = insert(:user)
1400
1401 message = %{
1402 "@context" => "https://www.w3.org/ns/activitystreams",
1403 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1404 "cc" => [],
1405 "type" => "Create",
1406 "object" => %{
1407 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1408 "cc" => [],
1409 "type" => "Note",
1410 "content" => "Hi",
1411 "inReplyTo" => nil,
1412 "attributedTo" => user.ap_id
1413 },
1414 "actor" => user.ap_id
1415 }
1416
1417 {:ok, activity} = Transmogrifier.handle_incoming(message)
1418
1419 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1420 end
1421
1422 test "successfully reserializes a message with AS2 objects in IR" do
1423 user = insert(:user)
1424
1425 message = %{
1426 "@context" => "https://www.w3.org/ns/activitystreams",
1427 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1428 "cc" => [],
1429 "type" => "Create",
1430 "object" => %{
1431 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
1432 "cc" => [],
1433 "type" => "Note",
1434 "content" => "Hi",
1435 "inReplyTo" => nil,
1436 "attributedTo" => user.ap_id,
1437 "tag" => [
1438 %{"name" => "#2hu", "href" => "http://example.com/2hu", "type" => "Hashtag"},
1439 %{"name" => "Bob", "href" => "http://example.com/bob", "type" => "Mention"}
1440 ]
1441 },
1442 "actor" => user.ap_id
1443 }
1444
1445 {:ok, activity} = Transmogrifier.handle_incoming(message)
1446
1447 {:ok, _} = Transmogrifier.prepare_outgoing(activity.data)
1448 end
1449 end
1450
1451 test "Rewrites Answers to Notes" do
1452 user = insert(:user)
1453
1454 {:ok, poll_activity} =
1455 CommonAPI.post(user, %{
1456 "status" => "suya...",
1457 "poll" => %{"options" => ["suya", "suya.", "suya.."], "expires_in" => 10}
1458 })
1459
1460 poll_object = Object.normalize(poll_activity)
1461 # TODO: Replace with CommonAPI vote creation when implemented
1462 data =
1463 File.read!("test/fixtures/mastodon-vote.json")
1464 |> Poison.decode!()
1465 |> Kernel.put_in(["to"], user.ap_id)
1466 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
1467 |> Kernel.put_in(["object", "to"], user.ap_id)
1468
1469 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
1470 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
1471
1472 assert data["object"]["type"] == "Note"
1473 end
1474
1475 describe "fix_explicit_addressing" do
1476 setup do
1477 user = insert(:user)
1478 [user: user]
1479 end
1480
1481 test "moves non-explicitly mentioned actors to cc", %{user: user} do
1482 explicitly_mentioned_actors = [
1483 "https://pleroma.gold/users/user1",
1484 "https://pleroma.gold/user2"
1485 ]
1486
1487 object = %{
1488 "actor" => user.ap_id,
1489 "to" => explicitly_mentioned_actors ++ ["https://social.beepboop.ga/users/dirb"],
1490 "cc" => [],
1491 "tag" =>
1492 Enum.map(explicitly_mentioned_actors, fn href ->
1493 %{"type" => "Mention", "href" => href}
1494 end)
1495 }
1496
1497 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1498 assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
1499 refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
1500 assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
1501 end
1502
1503 test "does not move actor's follower collection to cc", %{user: user} do
1504 object = %{
1505 "actor" => user.ap_id,
1506 "to" => [user.follower_address],
1507 "cc" => []
1508 }
1509
1510 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1511 assert user.follower_address in fixed_object["to"]
1512 refute user.follower_address in fixed_object["cc"]
1513 end
1514
1515 test "removes recipient's follower collection from cc", %{user: user} do
1516 recipient = insert(:user)
1517
1518 object = %{
1519 "actor" => user.ap_id,
1520 "to" => [recipient.ap_id, "https://www.w3.org/ns/activitystreams#Public"],
1521 "cc" => [user.follower_address, recipient.follower_address]
1522 }
1523
1524 fixed_object = Transmogrifier.fix_explicit_addressing(object)
1525
1526 assert user.follower_address in fixed_object["cc"]
1527 refute recipient.follower_address in fixed_object["cc"]
1528 refute recipient.follower_address in fixed_object["to"]
1529 end
1530 end
1531
1532 describe "fix_summary/1" do
1533 test "returns fixed object" do
1534 assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
1535 assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
1536 assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
1537 end
1538 end
1539
1540 describe "fix_in_reply_to/2" do
1541 clear_config([:instance, :federation_incoming_replies_max_depth])
1542
1543 setup do
1544 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1545 [data: data]
1546 end
1547
1548 test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
1549 assert Transmogrifier.fix_in_reply_to(data) == data
1550 end
1551
1552 test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
1553 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
1554
1555 object_with_reply =
1556 Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
1557
1558 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1559 assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
1560 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1561
1562 object_with_reply =
1563 Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
1564
1565 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1566 assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
1567 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1568
1569 object_with_reply =
1570 Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
1571
1572 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1573 assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
1574 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1575
1576 object_with_reply = Map.put(data["object"], "inReplyTo", [])
1577 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1578 assert modified_object["inReplyTo"] == []
1579 assert modified_object["inReplyToAtomUri"] == ""
1580 end
1581
1582 test "returns modified object when allowed incoming reply", %{data: data} do
1583 object_with_reply =
1584 Map.put(
1585 data["object"],
1586 "inReplyTo",
1587 "https://shitposter.club/notice/2827873"
1588 )
1589
1590 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
1591 modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
1592
1593 assert modified_object["inReplyTo"] ==
1594 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1595
1596 assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
1597
1598 assert modified_object["conversation"] ==
1599 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1600
1601 assert modified_object["context"] ==
1602 "tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
1603 end
1604 end
1605
1606 describe "fix_url/1" do
1607 test "fixes data for object when url is map" do
1608 object = %{
1609 "url" => %{
1610 "type" => "Link",
1611 "mimeType" => "video/mp4",
1612 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1613 }
1614 }
1615
1616 assert Transmogrifier.fix_url(object) == %{
1617 "url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1618 }
1619 end
1620
1621 test "fixes data for video object" do
1622 object = %{
1623 "type" => "Video",
1624 "url" => [
1625 %{
1626 "type" => "Link",
1627 "mimeType" => "video/mp4",
1628 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
1629 },
1630 %{
1631 "type" => "Link",
1632 "mimeType" => "video/mp4",
1633 "href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
1634 },
1635 %{
1636 "type" => "Link",
1637 "mimeType" => "text/html",
1638 "href" => "https://peertube.-2d4c2d1630e3"
1639 },
1640 %{
1641 "type" => "Link",
1642 "mimeType" => "text/html",
1643 "href" => "https://peertube.-2d4c2d16377-42"
1644 }
1645 ]
1646 }
1647
1648 assert Transmogrifier.fix_url(object) == %{
1649 "attachment" => [
1650 %{
1651 "href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
1652 "mimeType" => "video/mp4",
1653 "type" => "Link"
1654 }
1655 ],
1656 "type" => "Video",
1657 "url" => "https://peertube.-2d4c2d1630e3"
1658 }
1659 end
1660
1661 test "fixes url for not Video object" do
1662 object = %{
1663 "type" => "Text",
1664 "url" => [
1665 %{
1666 "type" => "Link",
1667 "mimeType" => "text/html",
1668 "href" => "https://peertube.-2d4c2d1630e3"
1669 },
1670 %{
1671 "type" => "Link",
1672 "mimeType" => "text/html",
1673 "href" => "https://peertube.-2d4c2d16377-42"
1674 }
1675 ]
1676 }
1677
1678 assert Transmogrifier.fix_url(object) == %{
1679 "type" => "Text",
1680 "url" => "https://peertube.-2d4c2d1630e3"
1681 }
1682
1683 assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
1684 "type" => "Text",
1685 "url" => ""
1686 }
1687 end
1688
1689 test "retunrs not modified object" do
1690 assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
1691 end
1692 end
1693
1694 describe "get_obj_helper/2" do
1695 test "returns nil when cannot normalize object" do
1696 refute Transmogrifier.get_obj_helper("test-obj-id")
1697 end
1698
1699 test "returns {:ok, %Object{}} for success case" do
1700 assert {:ok, %Object{}} =
1701 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
1702 end
1703 end
1704
1705 describe "fix_attachments/1" do
1706 test "returns not modified object" do
1707 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1708 assert Transmogrifier.fix_attachments(data) == data
1709 end
1710
1711 test "returns modified object when attachment is map" do
1712 assert Transmogrifier.fix_attachments(%{
1713 "attachment" => %{
1714 "mediaType" => "video/mp4",
1715 "url" => "https://peertube.moe/stat-480.mp4"
1716 }
1717 }) == %{
1718 "attachment" => [
1719 %{
1720 "mediaType" => "video/mp4",
1721 "url" => [
1722 %{
1723 "href" => "https://peertube.moe/stat-480.mp4",
1724 "mediaType" => "video/mp4",
1725 "type" => "Link"
1726 }
1727 ]
1728 }
1729 ]
1730 }
1731 end
1732
1733 test "returns modified object when attachment is list" do
1734 assert Transmogrifier.fix_attachments(%{
1735 "attachment" => [
1736 %{"mediaType" => "video/mp4", "url" => "https://pe.er/stat-480.mp4"},
1737 %{"mimeType" => "video/mp4", "href" => "https://pe.er/stat-480.mp4"}
1738 ]
1739 }) == %{
1740 "attachment" => [
1741 %{
1742 "mediaType" => "video/mp4",
1743 "url" => [
1744 %{
1745 "href" => "https://pe.er/stat-480.mp4",
1746 "mediaType" => "video/mp4",
1747 "type" => "Link"
1748 }
1749 ]
1750 },
1751 %{
1752 "href" => "https://pe.er/stat-480.mp4",
1753 "mediaType" => "video/mp4",
1754 "mimeType" => "video/mp4",
1755 "url" => [
1756 %{
1757 "href" => "https://pe.er/stat-480.mp4",
1758 "mediaType" => "video/mp4",
1759 "type" => "Link"
1760 }
1761 ]
1762 }
1763 ]
1764 }
1765 end
1766 end
1767
1768 describe "fix_emoji/1" do
1769 test "returns not modified object when object not contains tags" do
1770 data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
1771 assert Transmogrifier.fix_emoji(data) == data
1772 end
1773
1774 test "returns object with emoji when object contains list tags" do
1775 assert Transmogrifier.fix_emoji(%{
1776 "tag" => [
1777 %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}},
1778 %{"type" => "Hashtag"}
1779 ]
1780 }) == %{
1781 "emoji" => %{"bib" => "/test"},
1782 "tag" => [
1783 %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"},
1784 %{"type" => "Hashtag"}
1785 ]
1786 }
1787 end
1788
1789 test "returns object with emoji when object contains map tag" do
1790 assert Transmogrifier.fix_emoji(%{
1791 "tag" => %{"type" => "Emoji", "name" => ":bib:", "icon" => %{"url" => "/test"}}
1792 }) == %{
1793 "emoji" => %{"bib" => "/test"},
1794 "tag" => %{"icon" => %{"url" => "/test"}, "name" => ":bib:", "type" => "Emoji"}
1795 }
1796 end
1797 end
1798 end