mastodon_api_controller_test.exs 30.6 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)
csaurus's avatar
csaurus committed
181
182
  end

lain's avatar
lain committed
183
184
185
186
187
  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
188
189
190
191
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
lain's avatar
lain committed
192
193
194
195
196
197
198
199
200
201
202
203

    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

  test "verify_credentials", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
204
205
206
207
    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/accounts/verify_credentials")
lain's avatar
lain committed
208
209

    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
210
    assert id == to_string(user.id)
lain's avatar
lain committed
211
212
213
214
215
  end

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

lain's avatar
lain committed
216
217
218
    conn =
      conn
      |> get("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
219
220

    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
221
    assert id == to_string(activity.id)
lain's avatar
lain committed
222
  end
lain's avatar
lain committed
223
224
225
226
227
228

  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
229
230
231
232
      conn =
        conn
        |> assign(:user, author)
        |> delete("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
233
234
235
236
237
238
239
240
241
242

      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
243
244
245
246
      conn =
        conn
        |> assign(:user, user)
        |> delete("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
247
248
249
250
251
252

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

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

eal's avatar
eal committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  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
357
      {:ok, _activity_one} = TwitterAPI.create_status(user, %{"status" => "Marisa is cute."})
eal's avatar
eal committed
358
359
360
361
362
363
364
365
366
367
368
369
370
      {: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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394

    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
395
396
  end

397
398
399
400
401
  describe "notifications" do
    test "list of notifications", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

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

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

lain's avatar
lain committed
407
408
409
410
411
412
413
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/notifications")

      expected_response =
        "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
414
415
416
417
418
419
420
421
422

      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
423
424
425
      {:ok, activity} =
        TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})

426
427
      {:ok, [notification]} = Notification.create_notifications(activity)

lain's avatar
lain committed
428
429
430
431
432
433
434
      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>"
435
436
437
438
439
440
441
442
443

      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
444
445
446
      {:ok, activity} =
        TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})

447
448
      {:ok, [notification]} = Notification.create_notifications(activity)

lain's avatar
lain committed
449
450
451
452
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
453
454
455
456
457
458
459
460

      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
461
462
463
      {:ok, activity} =
        TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})

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

lain's avatar
lain committed
466
467
468
469
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/notifications/clear")
470
471
472

      assert %{} = json_response(conn, 200)

lain's avatar
lain committed
473
474
475
476
      conn =
        build_conn()
        |> assign(:user, user)
        |> get("/api/v1/notifications")
477
478
479
480
481
482

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

lain's avatar
lain committed
483
484
485
486
487
  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
488
489
490
491
492
493
494
      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
495

lain's avatar
lain committed
496
      assert to_string(activity.id) == id
lain's avatar
lain committed
497
498
    end
  end
lain's avatar
lain committed
499

normandy's avatar
normandy committed
500
501
502
503
504
505
506
507
508
509
510
511
  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
512
      assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
normandy's avatar
normandy committed
513
514
515
516
517

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

lain's avatar
lain committed
518
519
520
521
522
  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
523
524
525
526
527
528
529
      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
530

lain's avatar
lain committed
531
      assert to_string(activity.id) == id
lain's avatar
lain committed
532
    end
533
534
535
536
537
538
539
540
541
542
543
544

    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
545
  end
lain's avatar
lain committed
546
547
548
549
550
551
552
553

  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
554
555
556
557
558
559
560
      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
561

lain's avatar
lain committed
562
      assert to_string(activity.id) == id
lain's avatar
lain committed
563
564
    end
  end
lain's avatar
lain committed
565
566
567

  describe "user timelines" do
    test "gets a users statuses", %{conn: conn} do
568
569
570
      user_one = insert(:user)
      user_two = insert(:user)
      user_three = insert(:user)
lain's avatar
lain committed
571

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

574
575
576
577
578
579
580
581
582
583
584
585
      {: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
586
        conn
587
        |> get("/api/v1/accounts/#{user_one.id}/statuses")
lain's avatar
lain committed
588

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

592
593
594
595
      resp =
        conn
        |> assign(:user, user_two)
        |> get("/api/v1/accounts/#{user_one.id}/statuses")
lain's avatar
lain committed
596

597
598
599
600
601
602
603
604
      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
605

606
607
608
      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
609
    end
eal's avatar
eal committed
610

Gian Sass's avatar
Gian Sass committed
611
612
    test "unimplemented pinned statuses feature", %{conn: conn} do
      note = insert(:note_activity)
Gian Sass's avatar
Oops    
Gian Sass committed
613
      user = User.get_by_ap_id(note.data["actor"])
Gian Sass's avatar
Gian Sass committed
614
615
616
617
618
619
620
621

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

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

eal's avatar
eal committed
622
623
624
625
    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
626
627
628
629
630
      file = %Plug.Upload{
        content_type: "image/jpg",
        path: Path.absname("test/fixtures/image.jpg"),
        filename: "an_image.jpg"
      }
eal's avatar
eal committed
631

lain's avatar
lain committed
632
633
634
      media =
        TwitterAPI.upload(file, "json")
        |> Poison.decode!()
eal's avatar
eal committed
635

lain's avatar
lain committed
636
637
638
639
640
641
      {: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
642
643
644
645

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

lain's avatar
lain committed
646
647
648
      conn =
        build_conn()
        |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
eal's avatar
eal committed
649
650
651
652

      assert [%{"id" => id}] = json_response(conn, 200)
      assert id == to_string(image_post.id)
    end
lain's avatar
lain committed
653
  end
Roger Braun's avatar
Roger Braun committed
654
655
656
657
658
659
660

  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
661
662
663
664
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
Roger Braun's avatar
Roger Braun committed
665
666
667

      assert [relationship] = json_response(conn, 200)

lain's avatar
lain committed
668
      assert to_string(other_user.id) == relationship["id"]
Roger Braun's avatar
Roger Braun committed
669
670
    end
  end
Roger Braun's avatar
Roger Braun committed
671

kaniini's avatar
kaniini committed
672
673
674
675
676
677
678
  describe "locked accounts" do
    test "/api/v1/follow_requests works" do
      user = insert(:user, %{info: %{"locked" => true}})
      other_user = insert(:user)

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

679
680
681
682
683
      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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
      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
      user = insert(:user, %{info: %{"locked" => true}})
      other_user = insert(:user)

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

699
700
701
702
703
      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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
      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

    test "/api/v1/follow_requests/:id/reject works" do
      user = insert(:user, %{info: %{"locked" => true}})
      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
739
740
741
  test "account fetching", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
742
743
744
    conn =
      conn
      |> get("/api/v1/accounts/#{user.id}")
Roger Braun's avatar
Roger Braun committed
745
746

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

lain's avatar
lain committed
749
750
751
    conn =
      build_conn()
      |> get("/api/v1/accounts/-1")
Roger Braun's avatar
Roger Braun committed
752
753
754

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

  test "media upload", %{conn: conn} do
lain's avatar
lain committed
757
758
759
760
761
    file = %Plug.Upload{
      content_type: "image/jpg",
      path: Path.absname("test/fixtures/image.jpg"),
      filename: "an_image.jpg"
    }
lain's avatar
lain committed
762

763
764
    desc = "Description of the image"

lain's avatar
lain committed
765
766
    user = insert(:user)

lain's avatar
lain committed
767
768
769
    conn =
      conn
      |> assign(:user, user)
770
      |> post("/api/v1/media", %{"file" => file, "description" => desc})
lain's avatar
lain committed
771
772
773
774

    assert media = json_response(conn, 200)

    assert media["type"] == "image"
775
    assert media["description"] == desc
lain's avatar
lain committed
776
  end
Roger Braun's avatar
Roger Braun committed
777
778
779
780

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

lain's avatar
lain committed
781
    capture_log(fn ->
lain's avatar
lain committed
782
      {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
lain's avatar
lain committed
783
784
785
786
787
788
789

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

      conn =
        conn
        |> get("/api/v1/timelines/tag/2hu")
Roger Braun's avatar
Roger Braun committed
790

lain's avatar
lain committed
791
      assert [%{"id" => id}] = json_response(conn, 200)
Roger Braun's avatar
Roger Braun committed
792

lain's avatar
lain committed
793
      assert id == to_string(activity.id)
lain's avatar
lain committed
794
    end)
Roger Braun's avatar
Roger Braun committed
795
  end
796
797
798
799
800
801

  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
802
803
804
    conn =
      conn
      |> get("/api/v1/accounts/#{other_user.id}/followers")
805
806

    assert [%{"id" => id}] = json_response(conn, 200)
lain's avatar
lain committed
807
    assert id == to_string(user.id)
808
809
810
811
812
813
814
  end

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

lain's avatar
lain committed
815
816
817
    conn =
      conn
      |> get("/api/v1/accounts/#{user.id}/following")
818
819

    assert [%{"id" => id}] = json_response(conn, 200)
lain's avatar
lain committed
820
    assert id == to_string(other_user.id)
821
822
823
824
825
826
  end

  test "following / unfollowing a user", %{conn: conn} do
    user = insert(:user)
    other_user = insert(:user)

lain's avatar
lain committed
827
828
829
830
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/accounts/#{other_user.id}/follow")
831

lain's avatar
lain committed
832
    assert %{"id" => _id, "following" => true} = json_response(conn, 200)
833
834

    user = Repo.get(User, user.id)
lain's avatar
lain committed
835
836
837
838
839

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

lain's avatar
lain committed
841
    assert %{"id" => _id, "following" => false} = json_response(conn, 200)
eal's avatar
eal committed
842
843

    user = Repo.get(User, user.id)
lain's avatar
lain committed
844
845
846
847
848

    conn =
      build_conn()
      |> assign(:user, user)
      |> post("/api/v1/follows", %{"uri" => other_user.nickname})
eal's avatar
eal committed
849

eal's avatar
eal committed
850
    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
851
    assert id == to_string(other_user.id)
852
853
  end

lain's avatar
lain committed
854
  test "blocking / unblocking a user", %{conn: conn} do
855
856
857
    user = insert(:user)
    other_user = insert(:user)

lain's avatar
lain committed
858
859
860
861
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/accounts/#{other_user.id}/block")
lain's avatar
lain committed
862

lain's avatar
lain committed
863
    assert %{"id" => _id, "blocking" => true} = json_response(conn, 200)
lain's avatar
lain committed
864
865

    user = Repo.get(User, user.id)
lain's avatar
lain committed
866
867
868
869
870

    conn =
      build_conn()
      |> assign(:user, user)
      |> post("/api/v1/accounts/#{other_user.id}/unblock")
lain's avatar
lain committed
871

lain's avatar
lain committed
872
    assert %{"id" => _id, "blocking" => false} = json_response(conn, 200)
lain's avatar
lain committed
873
874
  end

lain's avatar
lain committed
875
876
877
878
879
880
  test "getting a list of blocks", %{conn: conn} do
    user = insert(:user)
    other_user = insert(:user)

    {:ok, user} = User.block(user, other_user)

lain's avatar
lain committed
881
882
883
884
    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/blocks")
lain's avatar
lain committed
885

lain's avatar
lain committed
886
    other_user_id = to_string(other_user.id)
lain's avatar
lain committed
887
888
889
    assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
  end

eal's avatar
eal committed
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
  test "blocking / unblocking a domain", %{conn: conn} do
    user = insert(:user)
    other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"})

    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})

    assert %{} = json_response(conn, 200)
    user = User.get_cached_by_ap_id(user.ap_id)
    assert User.blocks?(user, other_user)

    conn =
      build_conn()
      |> assign(:user, user)
      |> delete("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})

    assert %{} = json_response(conn, 200)
    user = User.get_cached_by_ap_id(user.ap_id)
    refute User.blocks?(user, other_user)
  end

  test "getting a list of domain blocks" do
    user = insert(:user)

    {:ok, user} = User.block_domain(user, "bad.site")
    {:ok, user} = User.block_domain(user, "even.worse.site")

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

    domain_blocks = json_response(conn, 200)

    assert "bad.site" in domain_blocks
    assert "even.worse.site" in domain_blocks
  end

lain's avatar
lain committed
930
931
932
933
934
  test "unimplemented mute endpoints" do
    user = insert(:user)
    other_user = insert(:user)

    ["mute", "unmute"]
lain's avatar
lain committed
935
936
937
938
939
    |> Enum.each(fn endpoint ->
      conn =
        build_conn()
        |> assign(:user, user)
        |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
940
941

      assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
942
      assert id == to_string(other_user.id)
943
944
945
946
947
948
949
    end)
  end

  test "unimplemented mutes, follow_requests, blocks, domain blocks" do
    user = insert(:user)

    ["blocks", "domain_blocks", "mutes", "follow_requests"]
lain's avatar
lain committed
950
951
952
953
954
    |> Enum.each(fn endpoint ->
      conn =
        build_conn()
        |> assign(:user, user)
        |> get("/api/v1/#{endpoint}")
955
956
957
958

      assert [] = json_response(conn, 200)
    end)
  end
lain's avatar
lain committed
959

lain's avatar
Typo.    
lain committed
960
  test "account search", %{conn: conn} do
lain's avatar
lain committed
961
    user = insert(:user)
lain's avatar
lain committed
962
    user_two = insert(:user, %{nickname: "shp@shitposter.club"})
lain's avatar
lain committed
963
964
    user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

lain's avatar
lain committed
965
966
967
968
969
970
971
972
973
974
975
976
    results =
      conn
      |> assign(:user, user)
      |> get("/api/v1/accounts/search", %{"q" => "shp"})
      |> json_response(200)

    result_ids = for result <- results, do: result["acct"]

    assert user_two.nickname in result_ids
    assert user_three.nickname in result_ids

    results =
lain's avatar
lain committed
977
978
979
      conn
      |> assign(:user, user)
      |> get("/api/v1/accounts/search", %{"q" => "2hu"})
lain's avatar
lain committed
980
      |> json_response(200)
lain's avatar
lain committed
981

lain's avatar
lain committed
982
983
984
    result_ids = for result <- results, do: result["acct"]

    assert user_three.nickname in result_ids
lain's avatar
lain committed
985
986
  end

lain's avatar
lain committed
987
988
989
990
991
992
  test "search", %{conn: conn} do
    user = insert(:user)
    user_two = insert(:user, %{nickname: "shp@shitposter.club"})
    user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

    {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
lain's avatar
lain committed
993
994
995
996
997
998
999

    {:ok, _activity} =
      CommonAPI.post(user, %{
        "status" => "This is about 2hu, but private",
        "visibility" => "private"
      })

lain's avatar
lain committed
1000
    {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
For faster browsing, not all history is shown. View entire blame