mastodon_api_controller_test.exs 91.5 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
defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
  use Pleroma.Web.ConnCase

8
  alias Ecto.Changeset
Haelwenn's avatar
Haelwenn committed
9
10
  alias Pleroma.Activity
  alias Pleroma.Notification
11
12
  alias Pleroma.Object
  alias Pleroma.Repo
Eugenij's avatar
Eugenij committed
13
  alias Pleroma.ScheduledActivity
14
  alias Pleroma.User
kaniini's avatar
kaniini committed
15
  alias Pleroma.Web.ActivityPub.ActivityPub
16
  alias Pleroma.Web.CommonAPI
Maksim's avatar
Maksim committed
17
  alias Pleroma.Web.MastodonAPI.FilterView
18
  alias Pleroma.Web.OAuth.App
19
  alias Pleroma.Web.OStatus
20
  alias Pleroma.Web.Push
21
  alias Pleroma.Web.TwitterAPI.TwitterAPI
lain's avatar
lain committed
22
  import Pleroma.Factory
lain's avatar
lain committed
23
  import ExUnit.CaptureLog
Maksim's avatar
Maksim committed
24
25
26
27
28
29
  import Tesla.Mock

  setup do
    mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
    :ok
  end
lain's avatar
lain committed
30
31
32
33
34
35
36

  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
37
38
39
40
    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/timelines/home")
lain's avatar
lain committed
41

42
    assert Enum.empty?(json_response(conn, 200))
lain's avatar
lain committed
43
44
45

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

lain's avatar
lain committed
46
47
48
49
    conn =
      build_conn()
      |> assign(:user, user)
      |> get("/api/v1/timelines/home")
lain's avatar
lain committed
50
51
52
53
54
55
56

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

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

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

lain's avatar
lain committed
60
61
62
63
64
65
      {: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
66

lain's avatar
lain committed
67
      assert length(json_response(conn, 200)) == 2
lain's avatar
lain committed
68

lain's avatar
lain committed
69
70
71
      conn =
        build_conn()
        |> get("/api/v1/timelines/public", %{"local" => "True"})
lain's avatar
lain committed
72

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

lain's avatar
lain committed
75
76
77
      conn =
        build_conn()
        |> get("/api/v1/timelines/public", %{"local" => "1"})
lain's avatar
lain committed
78

lain's avatar
lain committed
79
      assert [%{"content" => "test"}] = json_response(conn, 200)
lain's avatar
lain committed
80
    end)
lain's avatar
lain committed
81
82
83
84
85
  end

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

lain's avatar
lain committed
86
87
88
    idempotency_key = "Pikachu rocks!"

    conn_one =
lain's avatar
lain committed
89
90
      conn
      |> assign(:user, user)
lain's avatar
lain committed
91
      |> put_req_header("idempotency-key", idempotency_key)
lain's avatar
lain committed
92
93
94
95
96
97
      |> post("/api/v1/statuses", %{
        "status" => "cofe",
        "spoiler_text" => "2hu",
        "sensitive" => "false"
      })

98
99
100
    {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
    # Six hours
    assert ttl > :timer.seconds(6 * 60 * 60 - 1)
101

lain's avatar
lain committed
102
    assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
lain's avatar
lain committed
103
             json_response(conn_one, 200)
lain's avatar
lain committed
104

105
    assert Activity.get_by_id(id)
lain's avatar
lain committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119

    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
120
121
122
123
124
125
126
127
128
129
130
131
132

    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
133
134
  end

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

lain's avatar
lain committed
138
139
140
141
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
142
143

    assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
144
    assert Activity.get_by_id(id)
145
146
  end

rinpatch's avatar
rinpatch committed
147
148
149
150
151
152
153
154
155
156
157
  test "posting a fake status", %{conn: conn} do
    user = insert(:user)

    real_conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{
        "status" =>
          "\"Tenshi Eating a Corndog\" is a much discussed concept on /jp/. The significance of it is disputed, so I will focus on one core concept: the symbolism behind it"
      })

158
159
160
161
162
    real_status = json_response(real_conn, 200)

    assert real_status
    assert Object.get_by_ap_id(real_status["uri"])

rinpatch's avatar
rinpatch committed
163
    real_status =
164
      real_status
rinpatch's avatar
rinpatch committed
165
166
167
168
169
170
171
172
173
174
175
176
      |> Map.put("id", nil)
      |> Map.put("url", nil)
      |> Map.put("uri", nil)
      |> Map.put("created_at", nil)
      |> Kernel.put_in(["pleroma", "conversation_id"], nil)

    fake_conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{
        "status" =>
          "\"Tenshi Eating a Corndog\" is a much discussed concept on /jp/. The significance of it is disputed, so I will focus on one core concept: the symbolism behind it",
177
        "preview" => true
rinpatch's avatar
rinpatch committed
178
179
      })

180
181
182
183
184
    fake_status = json_response(fake_conn, 200)

    assert fake_status
    refute Object.get_by_ap_id(fake_status["uri"])

rinpatch's avatar
rinpatch committed
185
    fake_status =
186
      fake_status
rinpatch's avatar
rinpatch committed
187
188
189
190
191
192
193
194
195
      |> Map.put("id", nil)
      |> Map.put("url", nil)
      |> Map.put("uri", nil)
      |> Map.put("created_at", nil)
      |> Kernel.put_in(["pleroma", "conversation_id"], nil)

    assert real_status == fake_status
  end

196
  test "posting a status with OGP link preview", %{conn: conn} do
lain's avatar
lain committed
197
    Pleroma.Config.put([:rich_media, :enabled], true)
198
199
200
201
202
203
204
205
206
207
    user = insert(:user)

    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{
        "status" => "http://example.com/ogp"
      })

    assert %{"id" => id, "card" => %{"title" => "The Rock"}} = json_response(conn, 200)
208
    assert Activity.get_by_id(id)
lain's avatar
lain committed
209
    Pleroma.Config.put([:rich_media, :enabled], false)
210
211
  end

csaurus's avatar
csaurus committed
212
  test "posting a direct status", %{conn: conn} do
213
214
215
216
217
218
219
220
221
222
    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)
223
    assert activity = Activity.get_by_id(id)
Maxim Filippov's avatar
Maxim Filippov committed
224
    assert activity.recipients == [user2.ap_id, user1.ap_id]
lain's avatar
lain committed
225
226
    assert activity.data["to"] == [user2.ap_id]
    assert activity.data["cc"] == []
227
228
229
  end

  test "direct timeline", %{conn: conn} do
lain's avatar
lain committed
230
231
    user_one = insert(:user)
    user_two = insert(:user)
232

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

lain's avatar
lain committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    {: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 =
249
      conn
lain's avatar
lain committed
250
      |> assign(:user, user_two)
251
252
      |> get("api/v1/timelines/direct")

lain's avatar
lain committed
253
254
255
256
257
    [status] = json_response(res_conn, 200)

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

Maxim Filippov's avatar
Maxim Filippov committed
258
259
260
261
262
263
264
265
266
267
    # User should be able to see his own direct message
    res_conn =
      build_conn()
      |> assign(:user, user_one)
      |> get("api/v1/timelines/direct")

    [status] = json_response(res_conn, 200)

    assert %{"visibility" => "direct"} = status

lain's avatar
lain committed
268
269
270
271
272
    # Both should be visible here
    res_conn =
      conn
      |> assign(:user, user_two)
      |> get("api/v1/timelines/home")
273

lain's avatar
lain committed
274
    [_s1, _s2] = json_response(res_conn, 200)
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

    # 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
301
302
  end

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  test "Conversations", %{conn: conn} do
    user_one = insert(:user)
    user_two = insert(:user)

    {:ok, user_two} = User.follow(user_two, user_one)

    {: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"
      })

    res_conn =
      conn
lain's avatar
lain committed
323
      |> assign(:user, user_one)
324
325
326
327
      |> get("/api/v1/conversations")

    assert response = json_response(res_conn, 200)

lain's avatar
lain committed
328
329
330
331
332
333
334
335
336
    assert [
             %{
               "id" => res_id,
               "accounts" => res_accounts,
               "last_status" => res_last_status,
               "unread" => unread
             }
           ] = response

lain's avatar
lain committed
337
    assert length(res_accounts) == 2
338
    assert is_binary(res_id)
lain's avatar
lain committed
339
    assert unread == true
340
    assert res_last_status["id"] == direct.id
341
342
343
344

    # Apparently undocumented API endpoint
    res_conn =
      conn
lain's avatar
lain committed
345
346
      |> assign(:user, user_one)
      |> post("/api/v1/conversations/#{res_id}/read")
347

lain's avatar
lain committed
348
    assert response = json_response(res_conn, 200)
349
350
    assert length(response["accounts"]) == 2
    assert response["last_status"]["id"] == direct.id
lain's avatar
lain committed
351
    assert response["unread"] == false
352
353
354
355

    # (vanilla) Mastodon frontend behaviour
    res_conn =
      conn
lain's avatar
lain committed
356
      |> assign(:user, user_one)
357
      |> get("/api/v1/statuses/#{res_last_status["id"]}/context")
358

lain's avatar
lain committed
359
    assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
360
361
  end

Eugenij's avatar
Eugenij committed
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
  test "doesn't include DMs from blocked users", %{conn: conn} do
    blocker = insert(:user)
    blocked = insert(:user)
    user = insert(:user)
    {:ok, blocker} = User.block(blocker, blocked)

    {:ok, _blocked_direct} =
      CommonAPI.post(blocked, %{
        "status" => "Hi @#{blocker.nickname}!",
        "visibility" => "direct"
      })

    {:ok, direct} =
      CommonAPI.post(user, %{
        "status" => "Hi @#{blocker.nickname}!",
        "visibility" => "direct"
      })

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

    [status] = json_response(res_conn, 200)
    assert status["id"] == direct.id
  end

lain's avatar
lain committed
389
390
391
392
393
  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
394
395
396
397
    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
lain's avatar
lain committed
398
399
400

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

401
    activity = Activity.get_by_id(id)
lain's avatar
lain committed
402
403

    assert activity.data["context"] == replied_to.data["context"]
rinpatch's avatar
rinpatch committed
404
    assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
lain's avatar
lain committed
405
406
  end

lain's avatar
lain committed
407
408
409
410
411
412
413
414
415
416
  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)

417
    activity = Activity.get_by_id(id)
lain's avatar
lain committed
418
419
420
421

    assert activity
  end

lain's avatar
lain committed
422
423
424
  test "verify_credentials", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
425
426
427
428
    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/accounts/verify_credentials")
lain's avatar
lain committed
429

430
431
432
433
434
    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
435
    user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "unlisted"}})
436
437
438
439
440
441
442

    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
443
    assert id == to_string(user.id)
lain's avatar
lain committed
444
445
  end

446
447
448
449
450
451
452
453
454
455
456
457
458
459
  test "apps/verify_credentials", %{conn: conn} do
    token = insert(:oauth_token)

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

    app = Repo.preload(token, :app).app

    expected = %{
      "name" => app.client_name,
      "website" => app.website,
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
      "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
    }

    assert expected == json_response(conn, 200)
  end

  test "creates an oauth app", %{conn: conn} do
    user = insert(:user)
    app_attrs = build(:oauth_app)

    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/apps", %{
        client_name: app_attrs.client_name,
        redirect_uris: app_attrs.redirect_uris
      })

    [app] = Repo.all(App)

    expected = %{
      "name" => app.client_name,
      "website" => app.website,
      "client_id" => app.client_id,
      "client_secret" => app.client_secret,
      "id" => app.id |> to_string(),
      "redirect_uri" => app.redirect_uris,
      "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
488
489
490
491
492
    }

    assert expected == json_response(conn, 200)
  end

lain's avatar
lain committed
493
494
495
  test "get a status", %{conn: conn} do
    activity = insert(:note_activity)

lain's avatar
lain committed
496
497
498
    conn =
      conn
      |> get("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
499
500

    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
501
    assert id == to_string(activity.id)
lain's avatar
lain committed
502
  end
lain's avatar
lain committed
503
504
505
506

  describe "deleting a status" do
    test "when you created it", %{conn: conn} do
      activity = insert(:note_activity)
minibikini's avatar
minibikini committed
507
      author = User.get_cached_by_ap_id(activity.data["actor"])
lain's avatar
lain committed
508

lain's avatar
lain committed
509
510
511
512
      conn =
        conn
        |> assign(:user, author)
        |> delete("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
513
514
515

      assert %{} = json_response(conn, 200)

516
      refute Activity.get_by_id(activity.id)
lain's avatar
lain committed
517
518
519
520
521
522
    end

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

lain's avatar
lain committed
523
524
525
526
      conn =
        conn
        |> assign(:user, user)
        |> delete("/api/v1/statuses/#{activity.id}")
lain's avatar
lain committed
527
528
529

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

530
      assert Activity.get_by_id(activity.id) == activity
lain's avatar
lain committed
531
    end
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552

    test "when you're an admin or moderator", %{conn: conn} do
      activity1 = insert(:note_activity)
      activity2 = insert(:note_activity)
      admin = insert(:user, info: %{is_admin: true})
      moderator = insert(:user, info: %{is_moderator: true})

      res_conn =
        conn
        |> assign(:user, admin)
        |> delete("/api/v1/statuses/#{activity1.id}")

      assert %{} = json_response(res_conn, 200)

      res_conn =
        conn
        |> assign(:user, moderator)
        |> delete("/api/v1/statuses/#{activity2.id}")

      assert %{} = json_response(res_conn, 200)

553
554
      refute Activity.get_by_id(activity1.id)
      refute Activity.get_by_id(activity2.id)
555
    end
lain's avatar
lain committed
556
  end
lain's avatar
lain committed
557

Haelwenn's avatar
Haelwenn committed
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
  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
575
      assert response["irreversible"] == false
Lee Starnes's avatar
Lee Starnes committed
576
577
      assert response["id"] != nil
      assert response["id"] != ""
Haelwenn's avatar
Haelwenn committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
    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
600
      response =
Haelwenn's avatar
Haelwenn committed
601
602
603
        conn
        |> assign(:user, user)
        |> get("/api/v1/filters")
Maksim's avatar
Maksim committed
604
605
606
607
608
609
610
611
        |> json_response(200)

      assert response ==
               render_json(
                 FilterView,
                 "filters.json",
                 filters: [filter_two, filter_one]
               )
Haelwenn's avatar
Haelwenn committed
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
    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
631
      assert _response = json_response(conn, 200)
Haelwenn's avatar
Haelwenn committed
632
633
634
635
636
637
638
639
640
641
642
643
    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
644
      {:ok, _filter} = Pleroma.Filter.create(query)
Haelwenn's avatar
Haelwenn committed
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685

      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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
  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
789
      {:ok, _activity_one} = TwitterAPI.create_status(user, %{"status" => "Marisa is cute."})
eal's avatar
eal committed
790
791
792
793
794
795
796
797
798
799
800
801
802
      {: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
803
804
805
806
807
808

    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
809
      {:ok, _activity_two} =
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
        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
827
828
  end

829
830
831
832
833
  describe "notifications" do
    test "list of notifications", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

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

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

lain's avatar
lain committed
839
840
841
842
843
844
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/notifications")

      expected_response =
kaniini's avatar
kaniini committed
845
846
847
        "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
          user.ap_id
        }\">@<span>#{user.nickname}</span></a></span>"
848
849
850
851
852
853
854
855
856

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

860
861
      {:ok, [notification]} = Notification.create_notifications(activity)

lain's avatar
lain committed
862
863
864
865
866
867
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/notifications/#{notification.id}")

      expected_response =
kaniini's avatar
kaniini committed
868
869
870
        "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
          user.ap_id
        }\">@<span>#{user.nickname}</span></a></span>"
871
872
873
874
875
876
877
878
879

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

883
884
      {:ok, [notification]} = Notification.create_notifications(activity)

lain's avatar
lain committed
885
886
887
888
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
889
890
891
892
893
894
895
896

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

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

lain's avatar
lain committed
902
903
904
905
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/notifications/clear")
906
907
908

      assert %{} = json_response(conn, 200)

lain's avatar
lain committed
909
910
911
912
      conn =
        build_conn()
        |> assign(:user, user)
        |> get("/api/v1/notifications")
913
914
915
916

      assert all = json_response(conn, 200)
      assert all == []
    end
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006

    test "paginates notifications using min_id, since_id, max_id, and limit", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

      {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
      {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
      {:ok, activity3} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
      {:ok, activity4} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})

      notification1_id = Repo.get_by(Notification, activity_id: activity1.id).id |> to_string()
      notification2_id = Repo.get_by(Notification, activity_id: activity2.id).id |> to_string()
      notification3_id = Repo.get_by(Notification, activity_id: activity3.id).id |> to_string()
      notification4_id = Repo.get_by(Notification, activity_id: activity4.id).id |> to_string()

      conn =
        conn
        |> assign(:user, user)

      # min_id
      conn_res =
        conn
        |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")

      result = json_response(conn_res, 200)
      assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result

      # since_id
      conn_res =
        conn
        |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")

      result = json_response(conn_res, 200)
      assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result

      # max_id
      conn_res =
        conn
        |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")

      result = json_response(conn_res, 200)
      assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
    end

    test "filters notifications using exclude_types", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

      {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
      {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
      {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, other_user)
      {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
      {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)

      mention_notification_id =
        Repo.get_by(Notification, activity_id: mention_activity.id).id |> to_string()

      favorite_notification_id =
        Repo.get_by(Notification, activity_id: favorite_activity.id).id |> to_string()

      reblog_notification_id =
        Repo.get_by(Notification, activity_id: reblog_activity.id).id |> to_string()

      follow_notification_id =
        Repo.get_by(Notification, activity_id: follow_activity.id).id |> to_string()

      conn =
        conn
        |> assign(:user, user)

      conn_res =
        get(conn, "/api/v1/notifications", %{exclude_types: ["mention", "favourite", "reblog"]})

      assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)

      conn_res =
        get(conn, "/api/v1/notifications", %{exclude_types: ["favourite", "reblog", "follow"]})

      assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)

      conn_res =
        get(conn, "/api/v1/notifications", %{exclude_types: ["reblog", "follow", "mention"]})

      assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)

      conn_res =
        get(conn, "/api/v1/notifications", %{exclude_types: ["follow", "mention", "favourite"]})

      assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
    end
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058

    test "destroy multiple", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

      {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
      {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
      {:ok, activity3} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
      {:ok, activity4} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})

      notification1_id = Repo.get_by(Notification, activity_id: activity1.id).id |> to_string()
      notification2_id = Repo.get_by(Notification, activity_id: activity2.id).id |> to_string()
      notification3_id = Repo.get_by(Notification, activity_id: activity3.id).id |> to_string()
      notification4_id = Repo.get_by(Notification, activity_id: activity4.id).id |> to_string()

      conn =
        conn
        |> assign(:user, user)

      conn_res =
        conn
        |> get("/api/v1/notifications")

      result = json_response(conn_res, 200)
      assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result

      conn2 =
        conn
        |> assign(:user, other_user)

      conn_res =
        conn2
        |> get("/api/v1/notifications")

      result = json_response(conn_res, 200)
      assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result

      conn_destroy =
        conn
        |> delete("/api/v1/notifications/destroy_multiple", %{
          "ids" => [notification1_id, notification2_id]
        })

      assert json_response(conn_destroy, 200) == %{}

      conn_res =
        conn2
        |> get("/api/v1/notifications")

      result = json_response(conn_res, 200)
      assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
    end
1059
1060
  end

lain's avatar
lain committed
1061
1062
1063
1064
1065
  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
1066
1067
1068
1069
1070
      conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/statuses/#{activity.id}/reblog")

1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
      assert %{
               "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
               "reblogged" => true
             } = json_response(conn, 200)

      assert to_string(activity.id) == id
    end

    test "reblogged status for another user", %{conn: conn} do
      activity = insert(:note_activity)
      user1 = insert(:user)
      user2 = insert(:user)
      user3 = insert(:user)
1084
      CommonAPI.favorite(activity.id, user2)
Alexander Strizhakov's avatar
Alexander Strizhakov committed
1085
      {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
      {:ok, reblog_activity1, _object} = CommonAPI.repeat(activity.id, user1)
      {:ok, _, _object} = CommonAPI.repeat(activity.id, user2)

      conn_res =
        conn
        |> assign(:user, user3)
        |> get("/api/v1/statuses/#{reblog_activity1.id}")

      assert %{
               "reblog" => %{"id" => id, "reblogged" => false, "reblogs_count" => 2},
1096
1097
1098
               "reblogged" => false,
               "favourited" => false,
               "bookmarked" => false
1099
1100
1101
1102
1103
1104
1105
1106
1107
             } = json_response(conn_res, 200)

      conn_res =
        conn
        |> assign(:user, user2)
        |> get("/api/v1/statuses/#{reblog_activity1.id}")

      assert %{
               "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
1108
1109
1110
               "reblogged" => true,
               "favourited" => true,
               "bookmarked" => true
1111
             } = json_response(conn_res, 200)
lain's avatar
lain committed
1112

lain's avatar
lain committed
1113
      assert to_string(activity.id) == id
lain's avatar
lain committed
1114
1115
    end
  end
lain's avatar
lain committed
1116

normandy's avatar
normandy committed
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
  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
1129
      assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
normandy's avatar
normandy committed
1130
1131
1132
1133
1134

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

lain's avatar
lain committed
1135
1136
1137
1138
1139
  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
1140
1141
1142
1143
1144
1145
1146
      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
1147

lain's avatar
lain committed
1148
      assert to_string(activity.id) == id
lain's avatar
lain committed
1149
    end
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161

    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
1162
  end
lain's avatar
lain committed
1163
1164
1165
1166
1167
1168
1169
1170

  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
1171
1172
1173
1174
1175
1176
1177
      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
1178

lain's avatar
lain committed
1179
      assert to_string(activity.id) == id
lain's avatar
lain committed
1180
1181
    end
  end
lain's avatar
lain committed
1182
1183
1184

  describe "user timelines" do
    test "gets a users statuses", %{conn: conn} do
1185
1186
1187
      user_one = insert(:user)
      user_two = insert(:user)
      user_three = insert(:user)
lain's avatar
lain committed
1188

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

1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
      {: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
1203
        conn
1204
        |> get("/api/v1/accounts/#{user_one.id}/statuses")
lain's avatar
lain committed
1205

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

1209
1210
1211
1212
      resp =
        conn
        |> assign(:user, user_two)
        |> get("/api/v1/accounts/#{user_one.id}/statuses")
lain's avatar
lain committed
1213

1214
1215
1216
1217
1218
1219
1220
1221
      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
1222

1223
1224
1225
      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
1226
    end
eal's avatar
eal committed
1227

Gian Sass's avatar
Gian Sass committed
1228
1229
    test "unimplemented pinned statuses feature", %{conn: conn} do
      note = insert(:note_activity)
minibikini's avatar
minibikini committed
1230
      user = User.get_cached_by_ap_id(note.data["actor"])
Gian Sass's avatar
Gian Sass committed
1231
1232
1233
1234
1235
1236
1237
1238

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

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

eal's avatar
eal committed
1239
1240
    test "gets an users media", %{conn: conn} do
      note = insert(:note_activity)
minibikini's avatar
minibikini committed
1241
      user = User.get_cached_by_ap_id(note.data["actor"])
eal's avatar
eal committed
1242

lain's avatar
lain committed
1243
1244
1245
1246
1247
      file = %Plug.Upload{
        content_type: "image/jpg",
        path: Path.absname("test/fixtures/image.jpg"),
        filename: "an_image.jpg"
      }
eal's avatar
eal committed
1248

lain's avatar
lain committed
1249
      media =
1250
        TwitterAPI.upload(file, user, "json")
lain's avatar
lain committed
1251
        |> Poison.decode!()
eal's avatar
eal committed
1252

lain's avatar
lain committed
1253
1254
1255
1256
1257
1258
      {: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
1259
1260
1261
1262

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

lain's avatar
lain committed
1263
1264
1265
      conn =
        build_conn()
        |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
eal's avatar
eal committed
1266
1267
1268
1269

      assert [%{"id" => id}] = json_response(conn, 200)
      assert id == to_string(image_post.id)
    end
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289

    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
1290
  end
Roger Braun's avatar
Roger Braun committed
1291
1292
1293
1294
1295
1296
1297

  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
1298
1299
1300
1301
      conn =
        conn
        |> assign(:user, user)
        |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
Roger Braun's avatar
Roger Braun committed
1302
1303
1304

      assert [relationship] = json_response(conn, 200)

lain's avatar
lain committed
1305
      assert to_string(other_user.id) == relationship["id"]
Roger Braun's avatar
Roger Braun committed
1306
1307
    end
  end
Roger Braun's avatar
Roger Braun committed
1308

kaniini's avatar
kaniini committed
1309
1310
  describe "locked accounts" do
    test "/api/v1/follow_requests works" do
lain's avatar
lain committed
1311
      user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
kaniini's avatar
kaniini committed
1312
1313
      other_user = insert(:user)

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

minibikini's avatar
minibikini committed
1316
1317
      user = User.get_cached_by_id(user.id)
      other_user = User.get_cached_by_id(other_user.id)
1318
1319
1320

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

kaniini's avatar
kaniini committed
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
      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
1331
      user = insert(:user, %{info: %User.Info{locked: true}})
kaniini's avatar
kaniini committed
1332
1333
      other_user = insert(:user)

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

minibikini's avatar
minibikini committed
1336
1337
      user = User.get_cached_by_id(user.id)
      other_user = User.get_cached_by_id(other_user.id)
1338
1339
1340

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

kaniini's avatar
kaniini committed
1341
1342
1343
1344
1345
1346
1347
1348
      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"]

minibikini's avatar
minibikini committed
1349
1350
      user = User.get_cached_by_id(user.id)
      other_user = User.get_cached_by_id(other_user.id)
kaniini's avatar
kaniini committed
1351
1352
1353
1354

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

1355
    test "verify_credentials", %{conn: conn} do
lain's avatar
lain committed
1356
      user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "private"}})
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366

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

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

minibikini's avatar
minibikini committed
1373
      user = User.get_cached_by_id(user.id)
1374

kaniini's avatar
kaniini committed
1375
1376
1377
1378
1379
1380
1381
1382
      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"]

minibikini's avatar
minibikini committed
1383
1384
      user = User.get_cached_by_id(user.id)
      other_user = User.get_cached_by_id(other_user.id)
kaniini's avatar
kaniini committed
1385
1386
1387
1388
1389

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

Roger Braun's avatar
Roger Braun committed
1390
1391
1392
  test "account fetching", %{conn: conn} do
    user = insert(:user)

lain's avatar
lain committed
1393
1394
1395
    conn =
      conn
      |> get("/api/v1/accounts/#{user.id}")
Roger Braun's avatar
Roger Braun committed
1396
1397

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

lain's avatar
lain committed
1400
1401
1402
    conn =
      build_conn()
      |> get("/api/v1/accounts/-1")
Roger Braun's avatar
Roger Braun committed
1403
1404
1405

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

1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
  test "account fetching also works nickname", %{conn: conn} do
    user = insert(:user)

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

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

lain's avatar
lain committed
1418
  test "media upload", %{conn: conn} do
lain's avatar
lain committed
1419
1420
1421
1422
1423
    file = %Plug.Upload{
      content_type: "image/jpg",
      path: Path.absname("test/fixtures/image.jpg"),
      filename: "an_image.jpg"
    }
lain's avatar
lain committed
1424

1425
1426
    desc = "Description of the image"

lain's avatar
lain committed
1427
1428
    user = insert(:user)

lain's avatar
lain committed
1429
1430
1431
    conn =
      conn
      |> assign(:user, user)