mastodon_api_controller_test.exs 37.1 KB
Newer Older
lain's avatar
lain committed
1
2
3
4
defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
  use Pleroma.Web.ConnCase

  alias Pleroma.Web.TwitterAPI.TwitterAPI
5
  alias Pleroma.{Repo, User, Activity, Notification}
lain's avatar
lain committed
6
  alias Pleroma.Web.{OStatus, CommonAPI}
kaniini's avatar
kaniini committed
7
  alias Pleroma.Web.ActivityPub.ActivityPub
lain's avatar
lain committed
8
9

  import Pleroma.Factory
lain's avatar
lain committed
10
  import ExUnit.CaptureLog
lain's avatar
lain committed
11
12
13
14
15
16
17

  test "the home timeline", %{conn: conn} do
    user = insert(:user)
    following = insert(:user)

    {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})

lain's avatar
lain committed
18
19
20
21
    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/timelines/home")
lain's avatar
lain committed
22
23
24
25
26

    assert length(json_response(conn, 200)) == 0

    {:ok, user} = User.follow(user, following)

lain's avatar
lain committed
27
28
29
30
    conn =
      build_conn()
      |> assign(:user, user)
      |> get("/api/v1/timelines/home")
lain's avatar
lain committed
31
32
33
34
35
36
37

    assert [%{"content" => "test"}] = json_response(conn, 200)
  end

  test "the public timeline", %{conn: conn} do
    following = insert(:user)

lain's avatar
lain committed
38
    capture_log(fn ->
lain's avatar
lain committed
39
      {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
lain's avatar
lain committed
40

lain's avatar
lain committed
41
42
43
44
45
46
      {:ok, [_activity]} =
        OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")

      conn =
        conn
        |> get("/api/v1/timelines/public", %{"local" => "False"})
lain's avatar
lain committed
47

lain's avatar
lain committed
48
      assert length(json_response(conn, 200)) == 2
lain's avatar
lain committed
49

lain's avatar
lain committed
50
51
52
      conn =
        build_conn()
        |> get("/api/v1/timelines/public", %{"local" => "True"})
lain's avatar
lain committed
53

lain's avatar
lain committed
54
      assert [%{"content" => "test"}] = json_response(conn, 200)
lain's avatar
lain committed
55

lain's avatar
lain committed
56
57
58
      conn =
        build_conn()
        |> get("/api/v1/timelines/public", %{"local" => "1"})
lain's avatar
lain committed
59

lain's avatar
lain committed
60
      assert [%{"content" => "test"}] = json_response(conn, 200)
lain's avatar
lain committed
61
    end)
lain's avatar
lain committed
62
63
64
65
66
  end

  test "posting a status", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
67
68
69
    idempotency_key = "Pikachu rocks!"

    conn_one =
lain's avatar
lain committed
70
71
      conn
      |> assign(:user, user)
lain's avatar
lain committed
72
      |> put_req_header("idempotency-key", idempotency_key)
lain's avatar
lain committed
73
74
75
76
77
78
      |> post("/api/v1/statuses", %{
        "status" => "cofe",
        "spoiler_text" => "2hu",
        "sensitive" => "false"
      })

79
80
81
    {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
    # Six hours
    assert ttl > :timer.seconds(6 * 60 * 60 - 1)
82

lain's avatar
lain committed
83
    assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
lain's avatar
lain committed
84
             json_response(conn_one, 200)
lain's avatar
lain committed
85
86

    assert Repo.get(Activity, id)
lain's avatar
lain committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100

    conn_two =
      conn
      |> assign(:user, user)
      |> put_req_header("idempotency-key", idempotency_key)
      |> post("/api/v1/statuses", %{
        "status" => "cofe",
        "spoiler_text" => "2hu",
        "sensitive" => "false"
      })

    assert %{"id" => second_id} = json_response(conn_two, 200)

    assert id == second_id
101
102
103
104
105
106
107
108
109
110
111
112
113

    conn_three =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{
        "status" => "cofe",
        "spoiler_text" => "2hu",
        "sensitive" => "false"
      })

    assert %{"id" => third_id} = json_response(conn_three, 200)

    refute id == third_id
lain's avatar
lain committed
114
115
  end

116
117
118
  test "posting a sensitive status", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
119
120
121
122
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
123
124
125
126
127

    assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
    assert Repo.get(Activity, id)
  end

csaurus's avatar
csaurus committed
128
  test "posting a direct status", %{conn: conn} do
129
130
131
132
133
134
135
136
137
138
139
    user1 = insert(:user)
    user2 = insert(:user)
    content = "direct cofe @#{user2.nickname}"

    conn =
      conn
      |> assign(:user, user1)
      |> post("api/v1/statuses", %{"status" => content, "visibility" => "direct"})

    assert %{"id" => id, "visibility" => "direct"} = json_response(conn, 200)
    assert activity = Repo.get(Activity, id)
lain's avatar
lain committed
140
141
142
    assert activity.recipients == [user2.ap_id]
    assert activity.data["to"] == [user2.ap_id]
    assert activity.data["cc"] == []
143
144
145
  end

  test "direct timeline", %{conn: conn} do
lain's avatar
lain committed
146
147
    user_one = insert(:user)
    user_two = insert(:user)
148

lain's avatar
lain committed
149
    {:ok, user_two} = User.follow(user_two, user_one)
150

lain's avatar
lain committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    {:ok, direct} =
      CommonAPI.post(user_one, %{
        "status" => "Hi @#{user_two.nickname}!",
        "visibility" => "direct"
      })

    {:ok, _follower_only} =
      CommonAPI.post(user_one, %{
        "status" => "Hi @#{user_two.nickname}!",
        "visibility" => "private"
      })

    # Only direct should be visible here
    res_conn =
165
      conn
lain's avatar
lain committed
166
      |> assign(:user, user_two)
167
168
      |> get("api/v1/timelines/direct")

lain's avatar
lain committed
169
170
171
172
173
174
175
176
177
178
    [status] = json_response(res_conn, 200)

    assert %{"visibility" => "direct"} = status
    assert status["url"] != direct.data["id"]

    # Both should be visible here
    res_conn =
      conn
      |> assign(:user, user_two)
      |> get("api/v1/timelines/home")
179

lain's avatar
lain committed
180
    [_s1, _s2] = json_response(res_conn, 200)
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

    # Test pagination
    Enum.each(1..20, fn _ ->
      {:ok, _} =
        CommonAPI.post(user_one, %{
          "status" => "Hi @#{user_two.nickname}!",
          "visibility" => "direct"
        })
    end)

    res_conn =
      conn
      |> assign(:user, user_two)
      |> get("api/v1/timelines/direct")

    statuses = json_response(res_conn, 200)
    assert length(statuses) == 20

    res_conn =
      conn
      |> assign(:user, user_two)
      |> get("api/v1/timelines/direct", %{max_id: List.last(statuses)["id"]})

    [status] = json_response(res_conn, 200)

    assert status["url"] != direct.data["id"]
csaurus's avatar
csaurus committed
207
208
  end

lain's avatar
lain committed
209
210
211
212
213
  test "replying to a status", %{conn: conn} do
    user = insert(:user)

    {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"})

lain's avatar
lain committed
214
215
216
217
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
lain's avatar
lain committed
218
219
220
221
222
223
224
225
226

    assert %{"content" => "xD", "id" => id} = json_response(conn, 200)

    activity = Repo.get(Activity, id)

    assert activity.data["context"] == replied_to.data["context"]
    assert activity.data["object"]["inReplyToStatusId"] == replied_to.id
  end

lain's avatar
lain committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
    user = insert(:user)

    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})

    assert %{"content" => "xD", "id" => id} = json_response(conn, 200)

    activity = Repo.get(Activity, id)

    assert activity
  end

lain's avatar
lain committed
242
243
244
  test "verify_credentials", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
245
246
247
248
    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/accounts/verify_credentials")
lain's avatar
lain committed
249

250
251
252
253
254
    assert %{"id" => id, "source" => %{"privacy" => "public"}} = json_response(conn, 200)
    assert id == to_string(user.id)
  end

  test "verify_credentials default scope unlisted", %{conn: conn} do
lain's avatar
lain committed
255
    user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "unlisted"}})
256
257
258
259
260
261
262

    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/accounts/verify_credentials")

    assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} = json_response(conn, 200)
lain's avatar
lain committed
263
    assert id == to_string(user.id)
lain's avatar
lain committed
264
265
266
267
268
  end

  test "get a status", %{conn: conn} do
    activity = insert(:note_activity)

lain's avatar
lain committed
269
270
271
    conn =
      conn
      |> get("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
272
273

    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
274
    assert id == to_string(activity.id)
lain's avatar
lain committed
275
  end
lain's avatar
lain committed
276
277
278
279
280
281

  describe "deleting a status" do
    test "when you created it", %{conn: conn} do
      activity = insert(:note_activity)
      author = User.get_by_ap_id(activity.data["actor"])

lain's avatar
lain committed
282
283
284
285
      conn =
        conn
        |> assign(:user, author)
        |> delete("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
286
287
288
289
290
291
292
293
294
295

      assert %{} = json_response(conn, 200)

      assert Repo.get(Activity, activity.id) == nil
    end

    test "when you didn't create it", %{conn: conn} do
      activity = insert(:note_activity)
      user = insert(:user)

lain's avatar
lain committed
296
297
298
299
      conn =
        conn
        |> assign(:user, user)
        |> delete("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
300
301
302
303
304
305

      assert %{"error" => _} = json_response(conn, 403)

      assert Repo.get(Activity, activity.id) == activity
    end
  end
lain's avatar
lain committed
306

Haelwenn's avatar
Haelwenn committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
  describe "filters" do
    test "creating a filter", %{conn: conn} do
      user = insert(:user)

      filter = %Pleroma.Filter{
        phrase: "knights",
        context: ["home"]
      }

      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context})

      assert response = json_response(conn, 200)
      assert response["phrase"] == filter.phrase
      assert response["context"] == filter.context
Lee Starnes's avatar
Lee Starnes committed
324
325
      assert response["id"] != nil
      assert response["id"] != ""
Haelwenn's avatar
Haelwenn committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
416
417
418
419
420
421
422
423
424
425
426
427
    end

    test "fetching a list of filters", %{conn: conn} do
      user = insert(:user)

      query_one = %Pleroma.Filter{
        user_id: user.id,
        filter_id: 1,
        phrase: "knights",
        context: ["home"]
      }

      query_two = %Pleroma.Filter{
        user_id: user.id,
        filter_id: 2,
        phrase: "who",
        context: ["home"]
      }

      {:ok, filter_one} = Pleroma.Filter.create(query_one)
      {:ok, filter_two} = Pleroma.Filter.create(query_two)

      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/filters")

      assert response = json_response(conn, 200)
    end

    test "get a filter", %{conn: conn} do
      user = insert(:user)

      query = %Pleroma.Filter{
        user_id: user.id,
        filter_id: 2,
        phrase: "knight",
        context: ["home"]
      }

      {:ok, filter} = Pleroma.Filter.create(query)

      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/filters/#{filter.filter_id}")

      assert response = json_response(conn, 200)
    end

    test "update a filter", %{conn: conn} do
      user = insert(:user)

      query = %Pleroma.Filter{
        user_id: user.id,
        filter_id: 2,
        phrase: "knight",
        context: ["home"]
      }

      {:ok, filter} = Pleroma.Filter.create(query)

      new = %Pleroma.Filter{
        phrase: "nii",
        context: ["home"]
      }

      conn =
        conn
        |> assign(:user, user)
        |> put("/api/v1/filters/#{query.filter_id}", %{
          phrase: new.phrase,
          context: new.context
        })

      assert response = json_response(conn, 200)
      assert response["phrase"] == new.phrase
      assert response["context"] == new.context
    end

    test "delete a filter", %{conn: conn} do
      user = insert(:user)

      query = %Pleroma.Filter{
        user_id: user.id,
        filter_id: 2,
        phrase: "knight",
        context: ["home"]
      }

      {:ok, filter} = Pleroma.Filter.create(query)

      conn =
        conn
        |> assign(:user, user)
        |> delete("/api/v1/filters/#{filter.filter_id}")

      assert response = json_response(conn, 200)
      assert response == %{}
    end
  end

eal's avatar
eal committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  describe "lists" do
    test "creating a list", %{conn: conn} do
      user = insert(:user)

      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/lists", %{"title" => "cuties"})

      assert %{"title" => title} = json_response(conn, 200)
      assert title == "cuties"
    end

    test "adding users to a list", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)
      {:ok, list} = Pleroma.List.create("name", user)

      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})

      assert %{} == json_response(conn, 200)
      %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
      assert following == [other_user.follower_address]
    end

    test "removing users from a list", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)
      third_user = insert(:user)
      {:ok, list} = Pleroma.List.create("name", user)
      {:ok, list} = Pleroma.List.follow(list, other_user)
      {:ok, list} = Pleroma.List.follow(list, third_user)

      conn =
        conn
        |> assign(:user, user)
        |> delete("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})

      assert %{} == json_response(conn, 200)
      %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
      assert following == [third_user.follower_address]
    end

    test "listing users in a list", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)
      {:ok, list} = Pleroma.List.create("name", user)
      {:ok, list} = Pleroma.List.follow(list, other_user)

      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})

      assert [%{"id" => id}] = json_response(conn, 200)
      assert id == to_string(other_user.id)
    end

    test "retrieving a list", %{conn: conn} do
      user = insert(:user)
      {:ok, list} = Pleroma.List.create("name", user)

      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/lists/#{list.id}")

      assert %{"id" => id} = json_response(conn, 200)
      assert id == to_string(list.id)
    end

    test "renaming a list", %{conn: conn} do
      user = insert(:user)
      {:ok, list} = Pleroma.List.create("name", user)

      conn =
        conn
        |> assign(:user, user)
        |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})

      assert %{"title" => name} = json_response(conn, 200)
      assert name == "newname"
    end

    test "deleting a list", %{conn: conn} do
      user = insert(:user)
      {:ok, list} = Pleroma.List.create("name", user)

      conn =
        conn
        |> assign(:user, user)
        |> delete("/api/v1/lists/#{list.id}")

      assert %{} = json_response(conn, 200)
      assert is_nil(Repo.get(Pleroma.List, list.id))
    end

    test "list timeline", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)
lain's avatar
lain committed
531
      {:ok, _activity_one} = TwitterAPI.create_status(user, %{"status" => "Marisa is cute."})
eal's avatar
eal committed
532
533
534
535
536
537
538
539
540
541
542
543
544
      {:ok, activity_two} = TwitterAPI.create_status(other_user, %{"status" => "Marisa is cute."})
      {:ok, list} = Pleroma.List.create("name", user)
      {:ok, list} = Pleroma.List.follow(list, other_user)

      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/timelines/list/#{list.id}")

      assert [%{"id" => id}] = json_response(conn, 200)

      assert id == to_string(activity_two.id)
    end
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568

    test "list timeline does not leak non-public statuses for unfollowed users", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)
      {:ok, activity_one} = TwitterAPI.create_status(other_user, %{"status" => "Marisa is cute."})

      {:ok, activity_two} =
        TwitterAPI.create_status(other_user, %{
          "status" => "Marisa is cute.",
          "visibility" => "private"
        })

      {:ok, list} = Pleroma.List.create("name", user)
      {:ok, list} = Pleroma.List.follow(list, other_user)

      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/timelines/list/#{list.id}")

      assert [%{"id" => id}] = json_response(conn, 200)

      assert id == to_string(activity_one.id)
    end
eal's avatar
eal committed
569
570
  end

571
572
573
574
575
  describe "notifications" do
    test "list of notifications", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

lain's avatar
lain committed
576
577
578
      {:ok, activity} =
        TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})

lain's avatar
lain committed
579
      {:ok, [_notification]} = Notification.create_notifications(activity)
580

lain's avatar
lain committed
581
582
583
584
585
586
587
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/notifications")

      expected_response =
        "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
588
589
590
591
592
593
594
595
596

      assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
      assert response == expected_response
    end

    test "getting a single notification", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

lain's avatar
lain committed
597
598
599
      {:ok, activity} =
        TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})

600
601
      {:ok, [notification]} = Notification.create_notifications(activity)

lain's avatar
lain committed
602
603
604
605
606
607
608
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/notifications/#{notification.id}")

      expected_response =
        "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
609
610
611
612
613
614
615
616
617

      assert %{"status" => %{"content" => response}} = json_response(conn, 200)
      assert response == expected_response
    end

    test "dismissing a single notification", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

lain's avatar
lain committed
618
619
620
      {:ok, activity} =
        TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})

621
622
      {:ok, [notification]} = Notification.create_notifications(activity)

lain's avatar
lain committed
623
624
625
626
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
627
628
629
630
631
632
633
634

      assert %{} = json_response(conn, 200)
    end

    test "clearing all notifications", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

lain's avatar
lain committed
635
636
637
      {:ok, activity} =
        TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})

lain's avatar
lain committed
638
      {:ok, [_notification]} = Notification.create_notifications(activity)
639

lain's avatar
lain committed
640
641
642
643
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/notifications/clear")
644
645
646

      assert %{} = json_response(conn, 200)

lain's avatar
lain committed
647
648
649
650
      conn =
        build_conn()
        |> assign(:user, user)
        |> get("/api/v1/notifications")
651
652
653
654
655
656

      assert all = json_response(conn, 200)
      assert all == []
    end
  end

lain's avatar
lain committed
657
658
659
660
661
  describe "reblogging" do
    test "reblogs and returns the reblogged status", %{conn: conn} do
      activity = insert(:note_activity)
      user = insert(:user)

lain's avatar
lain committed
662
663
664
665
666
667
668
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/statuses/#{activity.id}/reblog")

      assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
               json_response(conn, 200)
lain's avatar
lain committed
669

lain's avatar
lain committed
670
      assert to_string(activity.id) == id
lain's avatar
lain committed
671
672
    end
  end
lain's avatar
lain committed
673

normandy's avatar
normandy committed
674
675
676
677
678
679
680
681
682
683
684
685
  describe "unreblogging" do
    test "unreblogs and returns the unreblogged status", %{conn: conn} do
      activity = insert(:note_activity)
      user = insert(:user)

      {:ok, _, _} = CommonAPI.repeat(activity.id, user)

      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/statuses/#{activity.id}/unreblog")

normandy's avatar
normandy committed
686
      assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
normandy's avatar
normandy committed
687
688
689
690
691

      assert to_string(activity.id) == id
    end
  end

lain's avatar
lain committed
692
693
694
695
696
  describe "favoriting" do
    test "favs a status and returns it", %{conn: conn} do
      activity = insert(:note_activity)
      user = insert(:user)

lain's avatar
lain committed
697
698
699
700
701
702
703
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/statuses/#{activity.id}/favourite")

      assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
               json_response(conn, 200)
lain's avatar
lain committed
704

lain's avatar
lain committed
705
      assert to_string(activity.id) == id
lain's avatar
lain committed
706
    end
707
708
709
710
711
712
713
714
715
716
717
718

    test "returns 500 for a wrong id", %{conn: conn} do
      user = insert(:user)

      resp =
        conn
        |> assign(:user, user)
        |> post("/api/v1/statuses/1/favourite")
        |> json_response(500)

      assert resp == "Something went wrong"
    end
lain's avatar
lain committed
719
  end
lain's avatar
lain committed
720
721
722
723
724
725
726
727

  describe "unfavoriting" do
    test "unfavorites a status and returns it", %{conn: conn} do
      activity = insert(:note_activity)
      user = insert(:user)

      {:ok, _, _} = CommonAPI.favorite(activity.id, user)

lain's avatar
lain committed
728
729
730
731
732
733
734
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/statuses/#{activity.id}/unfavourite")

      assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
               json_response(conn, 200)
lain's avatar
lain committed
735

lain's avatar
lain committed
736
      assert to_string(activity.id) == id
lain's avatar
lain committed
737
738
    end
  end
lain's avatar
lain committed
739
740
741

  describe "user timelines" do
    test "gets a users statuses", %{conn: conn} do
742
743
744
      user_one = insert(:user)
      user_two = insert(:user)
      user_three = insert(:user)
lain's avatar
lain committed
745

746
      {:ok, user_three} = User.follow(user_three, user_one)
lain's avatar
lain committed
747

748
749
750
751
752
753
754
755
756
757
758
759
      {:ok, activity} = CommonAPI.post(user_one, %{"status" => "HI!!!"})

      {:ok, direct_activity} =
        CommonAPI.post(user_one, %{
          "status" => "Hi, @#{user_two.nickname}.",
          "visibility" => "direct"
        })

      {:ok, private_activity} =
        CommonAPI.post(user_one, %{"status" => "private", "visibility" => "private"})

      resp =
lain's avatar
lain committed
760
        conn
761
        |> get("/api/v1/accounts/#{user_one.id}/statuses")
lain's avatar
lain committed
762

763
764
      assert [%{"id" => id}] = json_response(resp, 200)
      assert id == to_string(activity.id)
lain's avatar
lain committed
765

766
767
768
769
      resp =
        conn
        |> assign(:user, user_two)
        |> get("/api/v1/accounts/#{user_one.id}/statuses")
lain's avatar
lain committed
770

771
772
773
774
775
776
777
778
      assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
      assert id_one == to_string(direct_activity.id)
      assert id_two == to_string(activity.id)

      resp =
        conn
        |> assign(:user, user_three)
        |> get("/api/v1/accounts/#{user_one.id}/statuses")
lain's avatar
lain committed
779

780
781
782
      assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
      assert id_one == to_string(private_activity.id)
      assert id_two == to_string(activity.id)
lain's avatar
lain committed
783
    end
eal's avatar
eal committed
784

Gian Sass's avatar
Gian Sass committed
785
786
    test "unimplemented pinned statuses feature", %{conn: conn} do
      note = insert(:note_activity)
Gian Sass's avatar
Oops    
Gian Sass committed
787
      user = User.get_by_ap_id(note.data["actor"])
Gian Sass's avatar
Gian Sass committed
788
789
790
791
792
793
794
795

      conn =
        conn
        |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")

      assert json_response(conn, 200) == []
    end

eal's avatar
eal committed
796
797
798
799
    test "gets an users media", %{conn: conn} do
      note = insert(:note_activity)
      user = User.get_by_ap_id(note.data["actor"])

lain's avatar
lain committed
800
801
802
803
804
      file = %Plug.Upload{
        content_type: "image/jpg",
        path: Path.absname("test/fixtures/image.jpg"),
        filename: "an_image.jpg"
      }
eal's avatar
eal committed
805

lain's avatar
lain committed
806
807
808
      media =
        TwitterAPI.upload(file, "json")
        |> Poison.decode!()
eal's avatar
eal committed
809

lain's avatar
lain committed
810
811
812
813
814
815
      {:ok, image_post} =
        TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]})

      conn =
        conn
        |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
eal's avatar
eal committed
816
817
818
819

      assert [%{"id" => id}] = json_response(conn, 200)
      assert id == to_string(image_post.id)

lain's avatar
lain committed
820
821
822
      conn =
        build_conn()
        |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
eal's avatar
eal committed
823
824
825
826

      assert [%{"id" => id}] = json_response(conn, 200)
      assert id == to_string(image_post.id)
    end
lain's avatar
lain committed
827
  end
Roger Braun's avatar
Roger Braun committed
828
829
830
831
832
833
834

  describe "user relationships" do
    test "returns the relationships for the current user", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)
      {:ok, user} = User.follow(user, other_user)

lain's avatar
lain committed
835
836
837
838
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
Roger Braun's avatar
Roger Braun committed
839
840
841

      assert [relationship] = json_response(conn, 200)

lain's avatar
lain committed
842
      assert to_string(other_user.id) == relationship["id"]
Roger Braun's avatar
Roger Braun committed
843
844
    end
  end
Roger Braun's avatar
Roger Braun committed
845

kaniini's avatar
kaniini committed
846
847
  describe "locked accounts" do
    test "/api/v1/follow_requests works" do
lain's avatar
lain committed
848
      user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
kaniini's avatar
kaniini committed
849
850
851
852
      other_user = insert(:user)

      {:ok, activity} = ActivityPub.follow(other_user, user)

853
854
855
856
857
      user = Repo.get(User, user.id)
      other_user = Repo.get(User, other_user.id)

      assert User.following?(other_user, user) == false

kaniini's avatar
kaniini committed
858
859
860
861
862
863
864
865
866
867
      conn =
        build_conn()
        |> assign(:user, user)
        |> get("/api/v1/follow_requests")

      assert [relationship] = json_response(conn, 200)
      assert to_string(other_user.id) == relationship["id"]
    end

    test "/api/v1/follow_requests/:id/authorize works" do
lain's avatar
lain committed
868
      user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
kaniini's avatar
kaniini committed
869
870
871
872
      other_user = insert(:user)

      {:ok, activity} = ActivityPub.follow(other_user, user)

873
874
875
876
877
      user = Repo.get(User, user.id)
      other_user = Repo.get(User, other_user.id)

      assert User.following?(other_user, user) == false

kaniini's avatar
kaniini committed
878
879
880
881
882
883
884
885
886
887
888
889
890
891
      conn =
        build_conn()
        |> assign(:user, user)
        |> post("/api/v1/follow_requests/#{other_user.id}/authorize")

      assert relationship = json_response(conn, 200)
      assert to_string(other_user.id) == relationship["id"]

      user = Repo.get(User, user.id)
      other_user = Repo.get(User, other_user.id)

      assert User.following?(other_user, user) == true
    end

892
    test "verify_credentials", %{conn: conn} do
lain's avatar
lain committed
893
      user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "private"}})
894
895
896
897
898
899
900
901
902
903

      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/accounts/verify_credentials")

      assert %{"id" => id, "source" => %{"privacy" => "private"}} = json_response(conn, 200)
      assert id == to_string(user.id)
    end

kaniini's avatar
kaniini committed
904
    test "/api/v1/follow_requests/:id/reject works" do
lain's avatar
lain committed
905
      user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
kaniini's avatar
kaniini committed
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
      other_user = insert(:user)

      {:ok, activity} = ActivityPub.follow(other_user, user)

      conn =
        build_conn()
        |> assign(:user, user)
        |> post("/api/v1/follow_requests/#{other_user.id}/reject")

      assert relationship = json_response(conn, 200)
      assert to_string(other_user.id) == relationship["id"]

      user = Repo.get(User, user.id)
      other_user = Repo.get(User, other_user.id)

      assert User.following?(other_user, user) == false
    end
  end

Roger Braun's avatar
Roger Braun committed
925
926
927
  test "account fetching", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
928
929
930
    conn =
      conn
      |> get("/api/v1/accounts/#{user.id}")
Roger Braun's avatar
Roger Braun committed
931
932

    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
933
    assert id == to_string(user.id)
Roger Braun's avatar
Roger Braun committed
934

lain's avatar
lain committed
935
936
937
    conn =
      build_conn()
      |> get("/api/v1/accounts/-1")
Roger Braun's avatar
Roger Braun committed
938
939
940

    assert %{"error" => "Can't find user"} = json_response(conn, 404)
  end
lain's avatar
lain committed
941
942

  test "media upload", %{conn: conn} do
lain's avatar
lain committed
943
944
945
946
947
    file = %Plug.Upload{
      content_type: "image/jpg",
      path: Path.absname("test/fixtures/image.jpg"),
      filename: "an_image.jpg"
    }
lain's avatar
lain committed
948

949
950
    desc = "Description of the image"

lain's avatar
lain committed
951
952
    user = insert(:user)

lain's avatar
lain committed
953
954
955
    conn =
      conn
      |> assign(:user, user)
956
      |> post("/api/v1/media", %{"file" => file, "description" => desc})
lain's avatar
lain committed
957
958
959
960

    assert media = json_response(conn, 200)

    assert media["type"] == "image"
961
    assert media["description"] == desc
lain's avatar
lain committed
962
  end
Roger Braun's avatar
Roger Braun committed
963
964
965
966

  test "hashtag timeline", %{conn: conn} do
    following = insert(:user)

lain's avatar
lain committed
967
    capture_log(fn ->
lain's avatar
lain committed
968
      {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
lain's avatar
lain committed
969
970
971
972

      {:ok, [_activity]} =
        OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")

973
      nconn =
lain's avatar
lain committed
974
975
        conn
        |> get("/api/v1/timelines/tag/2hu")
Roger Braun's avatar
Roger Braun committed
976

977
978
979
980
981
982
983
984
985
986
      assert [%{"id" => id}] = json_response(nconn, 200)

      assert id == to_string(activity.id)

      # works for different capitalization too
      nconn =
        conn
        |> get("/api/v1/timelines/tag/2HU")

      assert [%{"id" => id}] = json_response(nconn, 200)
Roger Braun's avatar
Roger Braun committed
987

lain's avatar
lain committed
988
      assert id == to_string(activity.id)
lain's avatar
lain committed
989
    end)
Roger Braun's avatar
Roger Braun committed
990
  end
991
992
993
994
995
996

  test "getting followers", %{conn: conn} do
    user = insert(:user)
    other_user = insert(:user)
    {:ok, user} = User.follow(user, other_user)

lain's avatar
lain committed
997
998
999
    conn =
      conn
      |> get("/api/v1/accounts/#{other_user.id}/followers")
1000

For faster browsing, not all history is shown. View entire blame