mastodon_api_controller_test.exs 43.4 KB
Newer Older
kaniini's avatar
kaniini committed
1
# Pleroma: A lightweight social networking server
minibikini's avatar
minibikini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
kaniini's avatar
kaniini committed
3
4
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5
6
7
8
defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
  use Pleroma.Web.ConnCase

  alias Pleroma.Web.TwitterAPI.TwitterAPI
9
  alias Pleroma.{Repo, User, Object, Activity, Notification}
lain's avatar
lain committed
10
  alias Pleroma.Web.{OStatus, CommonAPI}
kaniini's avatar
kaniini committed
11
  alias Pleroma.Web.ActivityPub.ActivityPub
Maksim's avatar
Maksim committed
12
  alias Pleroma.Web.MastodonAPI.FilterView
lain's avatar
lain committed
13
  import Pleroma.Factory
lain's avatar
lain committed
14
  import ExUnit.CaptureLog
Maksim's avatar
Maksim committed
15
16
17
18
19
20
  import Tesla.Mock

  setup do
    mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
    :ok
  end
lain's avatar
lain committed
21
22
23
24
25
26
27

  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
28
29
30
31
    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/timelines/home")
lain's avatar
lain committed
32
33
34
35
36

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

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

lain's avatar
lain committed
37
38
39
40
    conn =
      build_conn()
      |> assign(:user, user)
      |> get("/api/v1/timelines/home")
lain's avatar
lain committed
41
42
43
44
45
46
47

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

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

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

lain's avatar
lain committed
51
52
53
54
55
56
      {: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
57

lain's avatar
lain committed
58
      assert length(json_response(conn, 200)) == 2
lain's avatar
lain committed
59

lain's avatar
lain committed
60
61
62
      conn =
        build_conn()
        |> get("/api/v1/timelines/public", %{"local" => "True"})
lain's avatar
lain committed
63

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

lain's avatar
lain committed
66
67
68
      conn =
        build_conn()
        |> get("/api/v1/timelines/public", %{"local" => "1"})
lain's avatar
lain committed
69

lain's avatar
lain committed
70
      assert [%{"content" => "test"}] = json_response(conn, 200)
lain's avatar
lain committed
71
    end)
lain's avatar
lain committed
72
73
74
75
76
  end

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

lain's avatar
lain committed
77
78
79
    idempotency_key = "Pikachu rocks!"

    conn_one =
lain's avatar
lain committed
80
81
      conn
      |> assign(:user, user)
lain's avatar
lain committed
82
      |> put_req_header("idempotency-key", idempotency_key)
lain's avatar
lain committed
83
84
85
86
87
88
      |> post("/api/v1/statuses", %{
        "status" => "cofe",
        "spoiler_text" => "2hu",
        "sensitive" => "false"
      })

89
90
91
    {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
    # Six hours
    assert ttl > :timer.seconds(6 * 60 * 60 - 1)
92

lain's avatar
lain committed
93
    assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
lain's avatar
lain committed
94
             json_response(conn_one, 200)
lain's avatar
lain committed
95
96

    assert Repo.get(Activity, id)
lain's avatar
lain committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110

    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
111
112
113
114
115
116
117
118
119
120
121
122
123

    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
124
125
  end

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

lain's avatar
lain committed
129
130
131
132
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
133
134
135
136
137

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

csaurus's avatar
csaurus committed
138
  test "posting a direct status", %{conn: conn} do
139
140
141
142
143
144
145
146
147
148
149
    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
150
151
152
    assert activity.recipients == [user2.ap_id]
    assert activity.data["to"] == [user2.ap_id]
    assert activity.data["cc"] == []
153
154
155
  end

  test "direct timeline", %{conn: conn} do
lain's avatar
lain committed
156
157
    user_one = insert(:user)
    user_two = insert(:user)
158

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

lain's avatar
lain committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    {: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 =
175
      conn
lain's avatar
lain committed
176
      |> assign(:user, user_two)
177
178
      |> get("api/v1/timelines/direct")

lain's avatar
lain committed
179
180
181
182
183
184
185
186
187
188
    [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")
189

lain's avatar
lain committed
190
    [_s1, _s2] = json_response(res_conn, 200)
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

    # 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
217
218
  end

lain's avatar
lain committed
219
220
221
222
223
  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
224
225
226
227
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
lain's avatar
lain committed
228
229
230
231
232
233
234
235
236

    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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  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
252
253
254
  test "verify_credentials", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
255
256
257
258
    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/accounts/verify_credentials")
lain's avatar
lain committed
259

260
261
262
263
264
    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
265
    user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "unlisted"}})
266
267
268
269
270
271
272

    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
273
    assert id == to_string(user.id)
lain's avatar
lain committed
274
275
276
277
278
  end

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

lain's avatar
lain committed
279
280
281
    conn =
      conn
      |> get("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
282
283

    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
284
    assert id == to_string(activity.id)
lain's avatar
lain committed
285
  end
lain's avatar
lain committed
286
287
288
289
290
291

  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
292
293
294
295
      conn =
        conn
        |> assign(:user, author)
        |> delete("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
296
297
298

      assert %{} = json_response(conn, 200)

299
      refute Repo.get(Activity, activity.id)
lain's avatar
lain committed
300
301
302
303
304
305
    end

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

lain's avatar
lain committed
306
307
308
309
      conn =
        conn
        |> assign(:user, user)
        |> delete("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
310
311
312
313
314
315

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

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

Haelwenn's avatar
Haelwenn committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  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
334
335
      assert response["id"] != nil
      assert response["id"] != ""
Haelwenn's avatar
Haelwenn committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    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)

Maksim's avatar
Maksim committed
358
      response =
Haelwenn's avatar
Haelwenn committed
359
360
361
        conn
        |> assign(:user, user)
        |> get("/api/v1/filters")
Maksim's avatar
Maksim committed
362
363
364
365
366
367
368
369
        |> json_response(200)

      assert response ==
               render_json(
                 FilterView,
                 "filters.json",
                 filters: [filter_two, filter_one]
               )
Haelwenn's avatar
Haelwenn committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    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}")

kaniini's avatar
kaniini committed
389
      assert _response = json_response(conn, 200)
Haelwenn's avatar
Haelwenn committed
390
391
392
393
394
395
396
397
398
399
400
401
    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"]
      }

Maksim's avatar
Maksim committed
402
      {:ok, _filter} = Pleroma.Filter.create(query)
Haelwenn's avatar
Haelwenn committed
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443

      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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
  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
547
      {:ok, _activity_one} = TwitterAPI.create_status(user, %{"status" => "Marisa is cute."})
eal's avatar
eal committed
548
549
550
551
552
553
554
555
556
557
558
559
560
      {: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
561
562
563
564
565
566

    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."})

Maksim's avatar
Maksim committed
567
      {:ok, _activity_two} =
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
        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
585
586
  end

587
588
589
590
591
  describe "notifications" do
    test "list of notifications", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

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

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

lain's avatar
lain committed
597
598
599
600
601
602
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/notifications")

      expected_response =
kaniini's avatar
kaniini committed
603
604
605
        "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
          user.ap_id
        }\">@<span>#{user.nickname}</span></a></span>"
606
607
608
609
610
611
612
613
614

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

618
619
      {:ok, [notification]} = Notification.create_notifications(activity)

lain's avatar
lain committed
620
621
622
623
624
625
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/notifications/#{notification.id}")

      expected_response =
kaniini's avatar
kaniini committed
626
627
628
        "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
          user.ap_id
        }\">@<span>#{user.nickname}</span></a></span>"
629
630
631
632
633
634
635
636
637

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

641
642
      {:ok, [notification]} = Notification.create_notifications(activity)

lain's avatar
lain committed
643
644
645
646
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
647
648
649
650
651
652
653
654

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

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

lain's avatar
lain committed
660
661
662
663
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/notifications/clear")
664
665
666

      assert %{} = json_response(conn, 200)

lain's avatar
lain committed
667
668
669
670
      conn =
        build_conn()
        |> assign(:user, user)
        |> get("/api/v1/notifications")
671
672
673
674
675
676

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

lain's avatar
lain committed
677
678
679
680
681
  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
682
683
684
685
686
687
688
      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
689

lain's avatar
lain committed
690
      assert to_string(activity.id) == id
lain's avatar
lain committed
691
692
    end
  end
lain's avatar
lain committed
693

normandy's avatar
normandy committed
694
695
696
697
698
699
700
701
702
703
704
705
  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
706
      assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
normandy's avatar
normandy committed
707
708
709
710
711

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

lain's avatar
lain committed
712
713
714
715
716
  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
717
718
719
720
721
722
723
      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
724

lain's avatar
lain committed
725
      assert to_string(activity.id) == id
lain's avatar
lain committed
726
    end
727
728
729
730
731
732
733
734
735
736
737
738

    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
739
  end
lain's avatar
lain committed
740
741
742
743
744
745
746
747

  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
748
749
750
751
752
753
754
      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
755

lain's avatar
lain committed
756
      assert to_string(activity.id) == id
lain's avatar
lain committed
757
758
    end
  end
lain's avatar
lain committed
759
760
761

  describe "user timelines" do
    test "gets a users statuses", %{conn: conn} do
762
763
764
      user_one = insert(:user)
      user_two = insert(:user)
      user_three = insert(:user)
lain's avatar
lain committed
765

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

768
769
770
771
772
773
774
775
776
777
778
779
      {: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
780
        conn
781
        |> get("/api/v1/accounts/#{user_one.id}/statuses")
lain's avatar
lain committed
782

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

786
787
788
789
      resp =
        conn
        |> assign(:user, user_two)
        |> get("/api/v1/accounts/#{user_one.id}/statuses")
lain's avatar
lain committed
790

791
792
793
794
795
796
797
798
      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
799

800
801
802
      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
803
    end
eal's avatar
eal committed
804

Gian Sass's avatar
Gian Sass committed
805
806
    test "unimplemented pinned statuses feature", %{conn: conn} do
      note = insert(:note_activity)
Gian Sass's avatar
Oops    
Gian Sass committed
807
      user = User.get_by_ap_id(note.data["actor"])
Gian Sass's avatar
Gian Sass committed
808
809
810
811
812
813
814
815

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

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

eal's avatar
eal committed
816
817
818
819
    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
820
821
822
823
824
      file = %Plug.Upload{
        content_type: "image/jpg",
        path: Path.absname("test/fixtures/image.jpg"),
        filename: "an_image.jpg"
      }
eal's avatar
eal committed
825

lain's avatar
lain committed
826
      media =
827
        TwitterAPI.upload(file, user, "json")
lain's avatar
lain committed
828
        |> Poison.decode!()
eal's avatar
eal committed
829

lain's avatar
lain committed
830
831
832
833
834
835
      {: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
836
837
838
839

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

lain's avatar
lain committed
840
841
842
      conn =
        build_conn()
        |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
eal's avatar
eal committed
843
844
845
846

      assert [%{"id" => id}] = json_response(conn, 200)
      assert id == to_string(image_post.id)
    end
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866

    test "gets a user's statuses without reblogs", %{conn: conn} do
      user = insert(:user)
      {:ok, post} = CommonAPI.post(user, %{"status" => "HI!!!"})
      {:ok, _, _} = CommonAPI.repeat(post.id, user)

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

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

      conn =
        conn
        |> get("/api/v1/accounts/#{user.id}/statuses", %{"exclude_reblogs" => "1"})

      assert [%{"id" => id}] = json_response(conn, 200)
      assert id == to_string(post.id)
    end
lain's avatar
lain committed
867
  end
Roger Braun's avatar
Roger Braun committed
868
869
870
871
872
873
874

  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
875
876
877
878
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
Roger Braun's avatar
Roger Braun committed
879
880
881

      assert [relationship] = json_response(conn, 200)

lain's avatar
lain committed
882
      assert to_string(other_user.id) == relationship["id"]
Roger Braun's avatar
Roger Braun committed
883
884
    end
  end
Roger Braun's avatar
Roger Braun committed
885

kaniini's avatar
kaniini committed
886
887
  describe "locked accounts" do
    test "/api/v1/follow_requests works" do
lain's avatar
lain committed
888
      user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
kaniini's avatar
kaniini committed
889
890
      other_user = insert(:user)

Maksim's avatar
Maksim committed
891
      {:ok, _activity} = ActivityPub.follow(other_user, user)
kaniini's avatar
kaniini committed
892

893
894
895
896
897
      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
898
899
900
901
902
903
904
905
906
907
      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
908
      user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
kaniini's avatar
kaniini committed
909
910
      other_user = insert(:user)

Maksim's avatar
Maksim committed
911
      {:ok, _activity} = ActivityPub.follow(other_user, user)
kaniini's avatar
kaniini committed
912

913
914
915
916
917
      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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
      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

932
    test "verify_credentials", %{conn: conn} do
lain's avatar
lain committed
933
      user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "private"}})
934
935
936
937
938
939
940
941
942
943

      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
944
    test "/api/v1/follow_requests/:id/reject works" do
lain's avatar
lain committed
945
      user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
kaniini's avatar
kaniini committed
946
947
      other_user = insert(:user)

Maksim's avatar
Maksim committed
948
      {:ok, _activity} = ActivityPub.follow(other_user, user)
kaniini's avatar
kaniini committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964

      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
965
966
967
  test "account fetching", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
968
969
970
    conn =
      conn
      |> get("/api/v1/accounts/#{user.id}")
Roger Braun's avatar
Roger Braun committed
971
972

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

lain's avatar
lain committed
975
976
977
    conn =
      build_conn()
      |> get("/api/v1/accounts/-1")
Roger Braun's avatar
Roger Braun committed
978
979
980

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

  test "media upload", %{conn: conn} do
lain's avatar
lain committed
983
984
985
986
987
    file = %Plug.Upload{
      content_type: "image/jpg",
      path: Path.absname("test/fixtures/image.jpg"),
      filename: "an_image.jpg"
    }
lain's avatar
lain committed
988

989
990
    desc = "Description of the image"

lain's avatar
lain committed
991
992
    user = insert(:user)

lain's avatar
lain committed
993
994
995
    conn =
      conn
      |> assign(:user, user)
996
      |> post("/api/v1/media", %{"file" => file, "description" => desc})
lain's avatar
lain committed
997
998
999
1000

    assert media = json_response(conn, 200)

    assert media["type"] == "image"
1001
    assert media["description"] == desc
1002
1003
1004
1005
    assert media["id"]

    object = Repo.get(Object, media["id"])
    assert object.data["actor"] == User.ap_id(user)
lain's avatar
lain committed
1006
  end
Roger Braun's avatar
Roger Braun committed
1007
1008
1009
1010

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

lain's avatar
lain committed
1011
    capture_log(fn ->
lain's avatar
lain committed
1012
      {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
lain's avatar
lain committed
1013
1014
1015
1016

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

1017
      nconn =
lain's avatar
lain committed
1018
1019
        conn
        |> get("/api/v1/timelines/tag/2hu")
Roger Braun's avatar
Roger Braun committed
1020

1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
      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
1031

lain's avatar
lain committed
1032
      assert id == to_string(activity.id)
lain's avatar
lain committed
1033
    end)
Roger Braun's avatar
Roger Braun committed
1034
  end
1035
1036
1037
1038
1039
1040

  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
1041
1042
1043
    conn =
      conn
      |> get("/api/v1/accounts/#{other_user.id}/followers")
1044
1045

    assert [%{"id" => id}] = json_response(conn, 200)
lain's avatar
lain committed
1046
    assert id == to_string(user.id)
1047
1048
  end

lain's avatar
lain committed
1049
1050
1051
  test "getting followers, hide_network", %{conn: conn} do
    user = insert(:user)
    other_user = insert(:user, %{info: %{hide_network: true}})
Maksim's avatar
Maksim committed
1052
    {:ok, _user} = User.follow(user, other_user)
lain's avatar
lain committed
1053
1054
1055
1056
1057
1058
1059
1060

    conn =
      conn
      |> get("/api/v1/accounts/#{other_user.id}/followers")

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

1061
1062
1063
  test "getting followers, hide_network, same user requesting", %{conn: conn} do
    user = insert(:user)
    other_user = insert(:user, %{info: %{hide_network: true}})
Maksim's avatar
Maksim committed
1064
    {:ok, _user} = User.follow(user, other_user)
1065
1066
1067
1068
1069
1070
1071
1072
1073

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

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

1074
1075
1076
1077
1078
  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
1079
1080
1081
    conn =
      conn
      |> get("/api/v1/accounts/#{user.id}/following")
1082
1083

    assert [%{"id" => id}] = json_response(conn, 200)
lain's avatar
lain committed
1084
    assert id == to_string(other_user.id)
1085
1086
  end

lain's avatar
lain committed
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
  test "getting following, hide_network", %{conn: conn} do
    user = insert(:user, %{info: %{hide_network: true}})
    other_user = insert(:user)
    {:ok, user} = User.follow(user, other_user)

    conn =
      conn
      |> get("/api/v1/accounts/#{user.id}/following")

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

1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
  test "getting following, hide_network, same user requesting", %{conn: conn} do
    user = insert(:user, %{info: %{hide_network: true}})
    other_user = insert(:user)
    {:ok, user} = User.follow(user, other_user)

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

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

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

lain's avatar
lain committed
1116
1117
1118
1119
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/accounts/#{other_user.id}/follow")
1120

lain's avatar
lain committed
1121
    assert %{"id" => _id, "following" => true} = json_response(conn, 200)
1122
1123

    user = Repo.get(User, user.id)
lain's avatar
lain committed
1124
1125
1126
1127
1128

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

lain's avatar
lain committed
1130
    assert %{"id" => _id, "following" => false} = json_response(conn, 200)
eal's avatar
eal committed
1131
1132

    user = Repo.get(User, user.id)
lain's avatar
lain committed
1133
1134
1135
1136
1137

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

eal's avatar
eal committed
1139
    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
1140
    assert id == to_string(other_user.id)
1141
1142
  end

lain's avatar
lain committed
1143
  test "blocking / unblocking a user", %{conn: conn} do
1144
1145
1146
    user = insert(:user)
    other_user = insert(:user)

lain's avatar
lain committed
1147
1148
1149
1150
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/accounts/#{other_user.id}/block")
lain's avatar
lain committed
1151

lain's avatar
lain committed
1152
    assert %{"id" => _id, "blocking" => true} = json_response(conn, 200)
lain's avatar
lain committed
1153
1154

    user = Repo.get(User, user.id)
lain's avatar
lain committed
1155
1156
1157
1158
1159

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

lain's avatar
lain committed
1161
    assert %{"id" => _id, "blocking" => false} = json_response(conn, 200)
lain's avatar
lain committed
1162
1163
  end

lain's avatar
lain committed
1164
1165
1166
1167
1168
1169
  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
1170
1171
1172
1173
    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/blocks")
lain's avatar
lain committed
1174

lain's avatar
lain committed
1175
    other_user_id = to_string(other_user.id)
lain's avatar
lain committed
1176
1177
1178
    assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
  end

eal's avatar
eal committed
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
  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

lain's avatar
lain committed
1202
  test "getting a list of domain blocks", %{conn: conn} do
eal's avatar
eal committed
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
    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
1219
1220
1221
1222
1223
  test "unimplemented mute endpoints" do
    user = insert(:user)
    other_user = insert(:user)

    ["mute", "unmute"]
lain's avatar
lain committed
1224
1225
1226
1227
1228
    |> Enum.each(fn endpoint ->
      conn =
        build_conn()
        |> assign(:user, user)
        |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
1229
1230

      assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
1231
      assert id == to_string(other_user.id)
1232
1233
1234
1235
1236
1237
1238
    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
1239
1240
1241
1242
1243
    |> Enum.each(fn endpoint ->
      conn =
        build_conn()
        |> assign(:user, user)
        |> get("/api/v1/#{endpoint}")
1244
1245
1246
1247

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

lain's avatar
Typo.    
lain committed
1249
  test "account search", %{conn: conn} do
lain's avatar
lain committed
1250
    user = insert(:user)
lain's avatar
lain committed
1251
    user_two = insert(:user, %{nickname: "shp@shitposter.club"})
lain's avatar
lain committed
1252
1253
    user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

lain's avatar
lain committed
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
    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
1266
1267
1268
      conn
      |> assign(:user, user)
      |> get("/api/v1/accounts/search", %{"q" => "2hu"})
lain's avatar
lain committed
1269
      |> json_response(200)
lain's avatar
lain committed
1270

lain's avatar
lain committed
1271
1272
1273
    result_ids = for result <- results, do: result["acct"]

    assert user_three.nickname in result_ids
lain's avatar
lain committed
1274
1275
  end

lain's avatar
lain committed
1276
1277
1278
1279
1280
1281
  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
1282
1283
1284
1285
1286
1287
1288

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

lain's avatar
lain committed
1289
1290
    {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})

lain's avatar
lain committed
1291
1292
1293
    conn =
      conn
      |> get("/api/v1/search", %{"q" => "2hu"})
lain's avatar
lain committed
1294
1295
1296

    assert results = json_response(conn, 200)

lain's avatar
lain committed
1297
    [account | _] = results["accounts"]
lain's avatar
lain committed
1298
    assert account["id"] == to_string(user_three.id)
lain's avatar
lain committed
1299
1300
1301
1302

    assert results["hashtags"] == []

    [status] = results["statuses"]
lain's avatar
lain committed
1303
    assert status["id"] == to_string(activity.id)
lain's avatar
lain committed
1304
  end
lain's avatar
lain committed
1305

1306
  test "search fetches remote statuses", %{conn: conn} do
lain's avatar
lain committed
1307
1308
1309
1310
1311
    capture_log(fn ->
      conn =
        conn
        |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})

lain's avatar
lain committed
1312
      assert results = json_response(conn, 200)
1313

lain's avatar
lain committed
1314
1315
      [status] = results["statuses"]
      assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
lain's avatar
lain committed
1316
    end)
1317
1318
  end

lain's avatar
lain committed
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
  test "search doesn't show statuses that it shouldn't", %{conn: conn} do
    {:ok, activity} =
      CommonAPI.post(insert(:user), %{
        "status" => "This is about 2hu, but private",
        "visibility" => "private"
      })

    capture_log(fn ->
      conn =
        conn
        |> get("/api/v1/search", %{"q" => activity.data["object"]["id"]})

      assert results = json_response(conn, 200)

      [] = results["statuses"]
    end)
  end

lain's avatar
lain committed
1337
  test "search fetches remote accounts", %{conn: conn} do
lain's avatar
lain committed
1338
1339
1340
    conn =
      conn
      |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
lain's avatar
lain committed
1341
1342
1343
1344
1345

    assert results = json_response(conn, 200)
    [account] = results["accounts"]
    assert account["acct"] == "shp@social.heldscal.la"
  end
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355

  test "returns the favorites of a user", %{conn: conn} do
    user = insert(:user)
    other_user = insert(:user)

    {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
    {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})

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

1356
    first_conn =
lain's avatar
lain committed
1357
1358
1359
      conn
      |> assign(:user, user)
      |> get("/api/v1/favourites")
1360

1361
    assert [status] = json_response(first_conn, 200)
lain's avatar
lain committed
1362
    assert status["id"] == to_string(activity.id)
1363

kaniini's avatar
kaniini committed
1364
    assert [{"link", _link_header}] =
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
             Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)

    # Honours query params
    {:ok, second_activity} =
      CommonAPI.post(