transmogrifier_test.exs 25.7 KB
Newer Older
1
2
3
defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
  use Pleroma.DataCase
  alias Pleroma.Web.ActivityPub.Transmogrifier
4
  alias Pleroma.Web.ActivityPub.Utils
5
  alias Pleroma.Web.ActivityPub.ActivityPub
6
  alias Pleroma.Web.OStatus
7
  alias Pleroma.Activity
8
  alias Pleroma.User
lain's avatar
lain committed
9
  alias Pleroma.Repo
10
  alias Pleroma.Web.Websub.WebsubClientSubscription
lain's avatar
lain committed
11

lain's avatar
lain committed
12
13
  import Pleroma.Factory
  alias Pleroma.Web.CommonAPI
14
15

  describe "handle_incoming" do
lain's avatar
lain committed
16
17
18
    test "it ignores an incoming notice if we already have it" do
      activity = insert(:note_activity)

lain's avatar
lain committed
19
20
21
22
      data =
        File.read!("test/fixtures/mastodon-post-activity.json")
        |> Poison.decode!()
        |> Map.put("object", activity.data["object"])
lain's avatar
lain committed
23
24
25
26
27
28

      {:ok, returned_activity} = Transmogrifier.handle_incoming(data)

      assert activity == returned_activity
    end

29
    test "it fetches replied-to activities if we don't have them" do
lain's avatar
lain committed
30
31
32
      data =
        File.read!("test/fixtures/mastodon-post-activity.json")
        |> Poison.decode!()
33

lain's avatar
lain committed
34
35
36
      object =
        data["object"]
        |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
37

lain's avatar
lain committed
38
39
40
      data =
        data
        |> Map.put("object", object)
41
42
43

      {:ok, returned_activity} = Transmogrifier.handle_incoming(data)

lain's avatar
lain committed
44
45
46
47
48
49
50
51
      assert activity =
               Activity.get_create_activity_by_object_ap_id(
                 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
               )

      assert returned_activity.data["object"]["inReplyToAtomUri"] ==
               "https://shitposter.club/notice/2827873"

lain's avatar
lain committed
52
53
54
      assert returned_activity.data["object"]["inReplyToStatusId"] == activity.id
    end

55
    test "it works for incoming notices" do
lain's avatar
lain committed
56
      data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
57
58

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
lain's avatar
lain committed
59
60
61
62
63
64
65

      assert data["id"] ==
               "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"

      assert data["context"] ==
               "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"

66
      assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
lain's avatar
lain committed
67

68
      assert data["cc"] == [
lain's avatar
lain committed
69
70
71
72
               "http://mastodon.example.org/users/admin/followers",
               "http://localtesting.pleroma.lol/users/lain"
             ]

73
74
75
76
77
78
      assert data["actor"] == "http://mastodon.example.org/users/admin"

      object = data["object"]
      assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"

      assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
lain's avatar
lain committed
79

80
      assert object["cc"] == [
lain's avatar
lain committed
81
82
83
84
               "http://mastodon.example.org/users/admin/followers",
               "http://localtesting.pleroma.lol/users/lain"
             ]

85
86
      assert object["actor"] == "http://mastodon.example.org/users/admin"
      assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
lain's avatar
lain committed
87
88
89
90

      assert object["context"] ==
               "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"

lain's avatar
lain committed
91
      assert object["sensitive"] == true
lain's avatar
lain committed
92
93
94
95

      user = User.get_by_ap_id(object["actor"])

      assert user.info["note_count"] == 1
96
    end
97

98
    test "it works for incoming notices with hashtags" do
lain's avatar
lain committed
99
      data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!()
100
101
102
103
104

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
      assert Enum.at(data["object"]["tag"], 2) == "moo"
    end

105
106
    test "it works for incoming follow requests" do
      user = insert(:user)
lain's avatar
lain committed
107
108

      data =
lain's avatar
lain committed
109
110
        File.read!("test/fixtures/mastodon-follow-activity.json")
        |> Poison.decode!()
lain's avatar
lain committed
111
        |> Map.put("object", user.ap_id)
112
113
114
115
116
117
118

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["actor"] == "http://mastodon.example.org/users/admin"
      assert data["type"] == "Follow"
      assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
      assert User.following?(User.get_by_ap_id(data["actor"]), user)
lain's avatar
lain committed
119
120
    end

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    test "it works for incoming follow requests from hubzilla" do
      user = insert(:user)

      data =
        File.read!("test/fixtures/hubzilla-follow-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)
        |> Utils.normalize_params()

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
      assert data["type"] == "Follow"
      assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
      assert User.following?(User.get_by_ap_id(data["actor"]), user)
    end

lain's avatar
lain committed
138
139
140
    test "it works for incoming likes" do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
141

lain's avatar
lain committed
142
      data =
lain's avatar
lain committed
143
144
        File.read!("test/fixtures/mastodon-like.json")
        |> Poison.decode!()
lain's avatar
lain committed
145
        |> Map.put("object", activity.data["object"]["id"])
lain's avatar
lain committed
146
147
148
149
150
151
152

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["actor"] == "http://mastodon.example.org/users/admin"
      assert data["type"] == "Like"
      assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
      assert data["object"] == activity.data["object"]["id"]
153
    end
lain's avatar
lain committed
154

Thog's avatar
Thog committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    test "it returns an error for incoming unlikes wihout a like activity" do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})

      data =
        File.read!("test/fixtures/mastodon-undo-like.json")
        |> Poison.decode!()
        |> Map.put("object", activity.data["object"]["id"])

      assert Transmogrifier.handle_incoming(data) == :error
    end

    test "it works for incoming unlikes with an existing like activity" do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => "leave a like pls"})

      like_data =
        File.read!("test/fixtures/mastodon-like.json")
        |> Poison.decode!()
        |> Map.put("object", activity.data["object"]["id"])

      {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)

      data =
        File.read!("test/fixtures/mastodon-undo-like.json")
        |> Poison.decode!()
        |> Map.put("object", like_data)
        |> Map.put("actor", like_data["actor"])

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["actor"] == "http://mastodon.example.org/users/admin"
      assert data["type"] == "Undo"
      assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
      assert data["object"]["id"] == "http://mastodon.example.org/users/admin#likes/2"
    end

lain's avatar
lain committed
192
    test "it works for incoming announces" do
lain's avatar
lain committed
193
      data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!()
lain's avatar
lain committed
194
195
196
197
198

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["actor"] == "http://mastodon.example.org/users/admin"
      assert data["type"] == "Announce"
lain's avatar
lain committed
199
200
201
202
203
204

      assert data["id"] ==
               "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"

      assert data["object"] ==
               "http://mastodon.example.org/users/admin/statuses/99541947525187367"
205
206
207
208
209
210
211
212

      assert Activity.get_create_activity_by_object_ap_id(data["object"])
    end

    test "it works for incoming announces with an existing activity" do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})

lain's avatar
lain committed
213
214
215
216
      data =
        File.read!("test/fixtures/mastodon-announce.json")
        |> Poison.decode!()
        |> Map.put("object", activity.data["object"]["id"])
217
218
219
220
221

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["actor"] == "http://mastodon.example.org/users/admin"
      assert data["type"] == "Announce"
lain's avatar
lain committed
222
223
224
225

      assert data["id"] ==
               "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"

226
227
228
      assert data["object"] == activity.data["object"]["id"]

      assert Activity.get_create_activity_by_object_ap_id(data["object"]).id == activity.id
lain's avatar
lain committed
229
    end
lain's avatar
lain committed
230
231

    test "it works for incoming update activities" do
lain's avatar
lain committed
232
      data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
lain's avatar
lain committed
233
234

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
lain's avatar
lain committed
235
236
237
238
239
240
      update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()

      object =
        update_data["object"]
        |> Map.put("actor", data["actor"])
        |> Map.put("id", data["actor"])
lain's avatar
lain committed
241

lain's avatar
lain committed
242
243
244
245
      update_data =
        update_data
        |> Map.put("actor", data["actor"])
        |> Map.put("object", object)
lain's avatar
lain committed
246
247
248
249
250

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)

      user = User.get_cached_by_ap_id(data["actor"])
      assert user.name == "gargle"
lain's avatar
lain committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

      assert user.avatar["url"] == [
               %{
                 "href" =>
                   "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
               }
             ]

      assert user.info["banner"]["url"] == [
               %{
                 "href" =>
                   "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
               }
             ]

lain's avatar
lain committed
266
267
      assert user.bio == "<p>Some bio</p>"
    end
lain's avatar
lain committed
268

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    test "it works for incoming update activities which lock the account" do
      data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
      update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!()

      object =
        update_data["object"]
        |> Map.put("actor", data["actor"])
        |> Map.put("id", data["actor"])
        |> Map.put("manuallyApprovesFollowers", true)

      update_data =
        update_data
        |> Map.put("actor", data["actor"])
        |> Map.put("object", object)

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)

      user = User.get_cached_by_ap_id(data["actor"])
      assert user.info["locked"] == true
    end

lain's avatar
lain committed
292
293
294
    test "it works for incoming deletes" do
      activity = insert(:note_activity)

lain's avatar
lain committed
295
296
297
298
299
300
301
      data =
        File.read!("test/fixtures/mastodon-delete.json")
        |> Poison.decode!()

      object =
        data["object"]
        |> Map.put("id", activity.data["object"]["id"])
lain's avatar
lain committed
302

lain's avatar
lain committed
303
304
305
306
      data =
        data
        |> Map.put("object", object)
        |> Map.put("actor", activity.data["actor"])
lain's avatar
lain committed
307

308
      {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
lain's avatar
lain committed
309
310
311

      refute Repo.get(Activity, activity.id)
    end
normandy's avatar
normandy committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

    test "it works for incoming unannounces with an existing notice" do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})

      announce_data =
        File.read!("test/fixtures/mastodon-announce.json")
        |> Poison.decode!()
        |> Map.put("object", activity.data["object"]["id"])

      {:ok, %Activity{data: announce_data, local: false}} =
        Transmogrifier.handle_incoming(announce_data)

      data =
        File.read!("test/fixtures/mastodon-undo-announce.json")
        |> Poison.decode!()
        |> Map.put("object", announce_data)
        |> Map.put("actor", announce_data["actor"])

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["type"] == "Undo"
      assert data["object"]["type"] == "Announce"
      assert data["object"]["object"] == activity.data["object"]["id"]

      assert data["object"]["id"] ==
               "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
    end
normandy's avatar
normandy committed
340

normandy's avatar
normandy committed
341
    test "it works for incomming unfollows with an existing follow" do
normandy's avatar
normandy committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
      user = insert(:user)

      follow_data =
        File.read!("test/fixtures/mastodon-follow-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)

      {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)

      data =
        File.read!("test/fixtures/mastodon-unfollow-activity.json")
        |> Poison.decode!()
        |> Map.put("object", follow_data)

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["type"] == "Undo"
      assert data["object"]["type"] == "Follow"
360
      assert data["object"]["object"] == user.ap_id
normandy's avatar
normandy committed
361
362
363
364
      assert data["actor"] == "http://mastodon.example.org/users/admin"

      refute User.following?(User.get_by_ap_id(data["actor"]), user)
    end
365
366
367
368
369
370
371
372
373
374
375
376
377

    test "it works for incoming blocks" do
      user = insert(:user)

      data =
        File.read!("test/fixtures/mastodon-block-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["type"] == "Block"
      assert data["object"] == user.ap_id
normandy's avatar
normandy committed
378
      assert data["actor"] == "http://mastodon.example.org/users/admin"
379
380
381

      blocker = User.get_by_ap_id(data["actor"])

382
      assert User.blocks?(blocker, user)
383
384
    end

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    test "incoming blocks successfully tear down any follow relationship" do
      blocker = insert(:user)
      blocked = insert(:user)

      data =
        File.read!("test/fixtures/mastodon-block-activity.json")
        |> Poison.decode!()
        |> Map.put("object", blocked.ap_id)
        |> Map.put("actor", blocker.ap_id)

      {:ok, blocker} = User.follow(blocker, blocked)
      {:ok, blocked} = User.follow(blocked, blocker)

      assert User.following?(blocker, blocked)
      assert User.following?(blocked, blocker)

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["type"] == "Block"
      assert data["object"] == blocked.ap_id
      assert data["actor"] == blocker.ap_id

      blocker = User.get_by_ap_id(data["actor"])
      blocked = User.get_by_ap_id(data["object"])

      assert User.blocks?(blocker, blocked)

      refute User.following?(blocker, blocked)
      refute User.following?(blocked, blocker)
    end

normandy's avatar
normandy committed
416
    test "it works for incoming unblocks with an existing block" do
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
      user = insert(:user)

      block_data =
        File.read!("test/fixtures/mastodon-block-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)

      {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)

      data =
        File.read!("test/fixtures/mastodon-unblock-activity.json")
        |> Poison.decode!()
        |> Map.put("object", block_data)

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
      assert data["type"] == "Undo"
      assert data["object"]["type"] == "Block"
      assert data["object"]["object"] == user.ap_id
normandy's avatar
normandy committed
435
      assert data["actor"] == "http://mastodon.example.org/users/admin"
436
437
438

      blocker = User.get_by_ap_id(data["actor"])

normandy's avatar
normandy committed
439
      refute User.blocks?(blocker, user)
440
    end
441
442
443
444
445
446
447
448

    test "it works for incoming accepts which were pre-accepted" do
      follower = insert(:user)
      followed = insert(:user)

      {:ok, follower} = User.follow(follower, followed)
      assert User.following?(follower, followed) == true

449
450
      {:ok, follow_activity} = ActivityPub.follow(follower, followed)

451
452
453
454
455
      accept_data =
        File.read!("test/fixtures/mastodon-accept-activity.json")
        |> Poison.decode!()
        |> Map.put("actor", followed.ap_id)

lain's avatar
lain committed
456
457
458
459
      object =
        accept_data["object"]
        |> Map.put("actor", follower.ap_id)
        |> Map.put("id", follow_activity.data["id"])
460

lain's avatar
lain committed
461
      accept_data = Map.put(accept_data, "object", object)
462

463
464
465
466
      {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
      refute activity.local

      assert activity.data["object"] == follow_activity.data["id"]
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

      follower = Repo.get(User, follower.id)

      assert User.following?(follower, followed) == true
    end

    test "it works for incoming accepts which were orphaned" do
      follower = insert(:user)
      followed = insert(:user, %{info: %{"locked" => true}})

      {:ok, follow_activity} = ActivityPub.follow(follower, followed)

      accept_data =
        File.read!("test/fixtures/mastodon-accept-activity.json")
        |> Poison.decode!()
        |> Map.put("actor", followed.ap_id)

      accept_data =
        Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))

487
488
      {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
      assert activity.data["object"] == follow_activity.data["id"]
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506

      follower = Repo.get(User, follower.id)

      assert User.following?(follower, followed) == true
    end

    test "it works for incoming accepts which are referenced by IRI only" do
      follower = insert(:user)
      followed = insert(:user, %{info: %{"locked" => true}})

      {:ok, follow_activity} = ActivityPub.follow(follower, followed)

      accept_data =
        File.read!("test/fixtures/mastodon-accept-activity.json")
        |> Poison.decode!()
        |> Map.put("actor", followed.ap_id)
        |> Map.put("object", follow_activity.data["id"])

507
508
      {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
      assert activity.data["object"] == follow_activity.data["id"]
509
510
511
512
513
514

      follower = Repo.get(User, follower.id)

      assert User.following?(follower, followed) == true
    end

515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
    test "it fails for incoming accepts which cannot be correlated" do
      follower = insert(:user)
      followed = insert(:user, %{info: %{"locked" => true}})

      accept_data =
        File.read!("test/fixtures/mastodon-accept-activity.json")
        |> Poison.decode!()
        |> Map.put("actor", followed.ap_id)

      accept_data =
        Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))

      :error = Transmogrifier.handle_incoming(accept_data)

      follower = Repo.get(User, follower.id)

      refute User.following?(follower, followed) == true
    end

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
    test "it fails for incoming rejects which cannot be correlated" do
      follower = insert(:user)
      followed = insert(:user, %{info: %{"locked" => true}})

      accept_data =
        File.read!("test/fixtures/mastodon-reject-activity.json")
        |> Poison.decode!()
        |> Map.put("actor", followed.ap_id)

      accept_data =
        Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))

      :error = Transmogrifier.handle_incoming(accept_data)

      follower = Repo.get(User, follower.id)

      refute User.following?(follower, followed) == true
    end

553
554
555
556
557
    test "it works for incoming rejects which are orphaned" do
      follower = insert(:user)
      followed = insert(:user, %{info: %{"locked" => true}})

      {:ok, follower} = User.follow(follower, followed)
558
      {:ok, _follow_activity} = ActivityPub.follow(follower, followed)
559
560
561
562
563
564
565
566
567
568
569

      assert User.following?(follower, followed) == true

      reject_data =
        File.read!("test/fixtures/mastodon-reject-activity.json")
        |> Poison.decode!()
        |> Map.put("actor", followed.ap_id)

      reject_data =
        Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))

570
571
      {:ok, activity} = Transmogrifier.handle_incoming(reject_data)
      refute activity.local
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598

      follower = Repo.get(User, follower.id)

      assert User.following?(follower, followed) == false
    end

    test "it works for incoming rejects which are referenced by IRI only" do
      follower = insert(:user)
      followed = insert(:user, %{info: %{"locked" => true}})

      {:ok, follower} = User.follow(follower, followed)
      {:ok, follow_activity} = ActivityPub.follow(follower, followed)

      assert User.following?(follower, followed) == true

      reject_data =
        File.read!("test/fixtures/mastodon-reject-activity.json")
        |> Poison.decode!()
        |> Map.put("actor", followed.ap_id)
        |> Map.put("object", follow_activity.data["id"])

      {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)

      follower = Repo.get(User, follower.id)

      assert User.following?(follower, followed) == false
    end
599
  end
lain's avatar
lain committed
600
601
602
603
604
605

  describe "prepare outgoing" do
    test "it turns mentions into tags" do
      user = insert(:user)
      other_user = insert(:user)

lain's avatar
lain committed
606
607
      {:ok, activity} =
        CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
lain's avatar
lain committed
608
609
610
611

      {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
      object = modified["object"]

lain's avatar
lain committed
612
      expected_mention = %{
lain's avatar
lain committed
613
614
        "href" => other_user.ap_id,
        "name" => "@#{other_user.nickname}",
lain's avatar
lain committed
615
        "type" => "Mention"
lain's avatar
lain committed
616
      }
lain's avatar
lain committed
617

lain's avatar
lain committed
618
      expected_tag = %{
lain's avatar
lain committed
619
        "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu",
lain's avatar
lain committed
620
621
622
        "type" => "Hashtag",
        "name" => "#2hu"
      }
lain's avatar
lain committed
623

lain's avatar
lain committed
624
      assert Enum.member?(object["tag"], expected_tag)
lain's avatar
lain committed
625
      assert Enum.member?(object["tag"], expected_mention)
lain's avatar
lain committed
626
627
    end

lain's avatar
lain committed
628
629
630
631
632
633
634
635
636
    test "it adds the sensitive property" do
      user = insert(:user)

      {:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
      {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)

      assert modified["object"]["sensitive"]
    end

lain's avatar
lain committed
637
    test "it adds the json-ld context and the conversation property" do
lain's avatar
lain committed
638
639
640
641
642
643
      user = insert(:user)

      {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
      {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)

      assert modified["@context"] == "https://www.w3.org/ns/activitystreams"
lain's avatar
lain committed
644
      assert modified["object"]["conversation"] == modified["context"]
lain's avatar
lain committed
645
646
647
648
649
650
651
652
653
654
    end

    test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
      user = insert(:user)

      {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
      {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)

      assert modified["object"]["actor"] == modified["object"]["attributedTo"]
    end
655
656
657
658
659
660
661
662
663
664
665
666

    test "it translates ostatus IDs to external URLs" do
      incoming = File.read!("test/fixtures/incoming_note_activity.xml")
      {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)

      user = insert(:user)

      {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
      {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)

      assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
    end
667
668
669
670
671
672
673

    test "it translates ostatus reply_to IDs to external URLs" do
      incoming = File.read!("test/fixtures/incoming_note_activity.xml")
      {:ok, [referred_activity]} = OStatus.handle_incoming(incoming)

      user = insert(:user)

lain's avatar
lain committed
674
675
676
      {:ok, activity} =
        CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id})

677
678
679
680
      {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)

      assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29"
    end
lain's avatar
lain committed
681
  end
lain's avatar
lain committed
682
683
684

  describe "user upgrade" do
    test "it upgrades a user to activitypub" do
lain's avatar
lain committed
685
686
687
688
689
690
691
692
      user =
        insert(:user, %{
          nickname: "rye@niu.moe",
          local: false,
          ap_id: "https://niu.moe/users/rye",
          follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
        })

lain's avatar
lain committed
693
694
695
696
      user_two = insert(:user, %{following: [user.follower_address]})

      {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
      {:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
lain's avatar
lain committed
697
      assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients
lain's avatar
lain committed
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714

      user = Repo.get(User, user.id)
      assert user.info["note_count"] == 1

      {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
      assert user.info["ap_enabled"]
      assert user.info["note_count"] == 1
      assert user.follower_address == "https://niu.moe/users/rye/followers"

      # Wait for the background task
      :timer.sleep(1000)

      user = Repo.get(User, user.id)
      assert user.info["note_count"] == 1

      activity = Repo.get(Activity, activity.id)
      assert user.follower_address in activity.recipients
lain's avatar
lain committed
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733

      assert %{
               "url" => [
                 %{
                   "href" =>
                     "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
                 }
               ]
             } = user.avatar

      assert %{
               "url" => [
                 %{
                   "href" =>
                     "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
                 }
               ]
             } = user.info["banner"]

lain's avatar
lain committed
734
735
736
737
738
739
740
741
742
743
      refute "..." in activity.recipients

      unrelated_activity = Repo.get(Activity, unrelated_activity.id)
      refute user.follower_address in unrelated_activity.recipients

      user_two = Repo.get(User, user_two.id)
      assert user.follower_address in user_two.following
      refute "..." in user_two.following
    end
  end
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758

  describe "maybe_retire_websub" do
    test "it deletes all websub client subscripitions with the user as topic" do
      subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
      {:ok, ws} = Repo.insert(subscription)

      subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
      {:ok, ws2} = Repo.insert(subscription)

      Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")

      refute Repo.get(WebsubClientSubscription, ws.id)
      assert Repo.get(WebsubClientSubscription, ws2.id)
    end
  end
759
760
761
762
763
764
765
766
767
768
769

  describe "actor rewriting" do
    test "it fixes the actor URL property to be a proper URI" do
      data = %{
        "url" => %{"href" => "http://example.com"}
      }

      rewritten = Transmogrifier.maybe_fix_user_object(data)
      assert rewritten["url"] == "http://example.com"
    end
  end
770
end