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

  alias Pleroma.Web.TwitterAPI.TwitterAPI
5
  alias Pleroma.{Repo, User, Activity, Notification}
lain's avatar
lain committed
6
  alias Pleroma.Web.{OStatus, CommonAPI}
lain's avatar
lain committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

  import Pleroma.Factory

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

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

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

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

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

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

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

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

    {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
    {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")

    conn = conn
lain's avatar
lain committed
38
    |> get("/api/v1/timelines/public", %{"local" => "False"})
lain's avatar
lain committed
39
40
41
42
43
44
45

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

    conn = build_conn()
    |> get("/api/v1/timelines/public", %{"local" => "True"})

    assert [%{"content" => "test"}] = json_response(conn, 200)
lain's avatar
lain committed
46
47
48
49
50

    conn = build_conn()
    |> get("/api/v1/timelines/public", %{"local" => "1"})

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

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

    conn = conn
    |> assign(:user, user)
lain's avatar
lain committed
58
    |> post("/api/v1/statuses", %{"status" => "cofe", "spoiler_text" => "2hu", "sensitive" => "false"})
lain's avatar
lain committed
59

lain's avatar
lain committed
60
    assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = json_response(conn, 200)
lain's avatar
lain committed
61
62
63
    assert Repo.get(Activity, id)
  end

64
65
66
67
68
69
70
71
72
73
74
  test "posting a sensitive status", %{conn: conn} do
    user = insert(:user)

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

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

lain's avatar
lain committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  test "replying to a status", %{conn: conn} do
    user = insert(:user)

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

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

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

    activity = Repo.get(Activity, id)

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

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

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

    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
100
    assert id == to_string(user.id)
lain's avatar
lain committed
101
102
103
104
105
106
107
108
109
  end

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

    conn = conn
    |> get("/api/v1/statuses/#{activity.id}")

    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
110
    assert id == to_string(activity.id)
lain's avatar
lain committed
111
  end
lain's avatar
lain committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

  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"])

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

      assert %{} = json_response(conn, 200)

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

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

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

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

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

141
142
143
144
145
146
147
148
149
150
151
152
  describe "notifications" do
    test "list of notifications", %{conn: conn} do
      user = insert(:user)
      other_user = insert(:user)

      {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
      {:ok, [notification]} = Notification.create_notifications(activity)

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

eal's avatar
eal committed
153
      expected_response = "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
      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)

      {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
      {:ok, [notification]} = Notification.create_notifications(activity)

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

eal's avatar
eal committed
169
      expected_response = "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
      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)

      {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
      {:ok, [notification]} = Notification.create_notifications(activity)

      conn = conn
      |> assign(:user, user)
      |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})

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

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

      {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
      {:ok, [notification]} = Notification.create_notifications(activity)

      conn = conn
      |> assign(:user, user)
      |> post("/api/v1/notifications/clear")

      assert %{} = json_response(conn, 200)

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

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

lain's avatar
lain committed
210
211
212
213
214
215
216
217
218
  describe "reblogging" do
    test "reblogs and returns the reblogged status", %{conn: conn} do
      activity = insert(:note_activity)
      user = insert(:user)

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

lain's avatar
lain committed
219
      assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} = json_response(conn, 200)
lain's avatar
lain committed
220
      assert to_string(activity.id) == id
lain's avatar
lain committed
221
222
    end
  end
lain's avatar
lain committed
223
224
225
226
227
228
229
230
231
232
233

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

      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
234
      assert to_string(activity.id) == id
lain's avatar
lain committed
235
236
    end
  end
lain's avatar
lain committed
237
238
239
240
241
242
243
244
245
246
247
248
249

  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)

      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
250
      assert to_string(activity.id) == id
lain's avatar
lain committed
251
252
    end
  end
lain's avatar
lain committed
253
254
255
256
257
258
259
260
261
262
263
264
265

  describe "user timelines" do
    test "gets a users statuses", %{conn: conn} do
      _note = insert(:note_activity)
      note_two = insert(:note_activity)

      user = User.get_by_ap_id(note_two.data["actor"])

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

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

lain's avatar
lain committed
266
      assert id == to_string(note_two.id)
lain's avatar
lain committed
267
    end
eal's avatar
eal committed
268
269
270
271
272
273
274
275
276
277
278
279

    test "gets an users media", %{conn: conn} do
      note = insert(:note_activity)
      user = User.get_by_ap_id(note.data["actor"])

      file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
      media = TwitterAPI.upload(file, "json")
      |> Poison.decode!

      {:ok, image_post} = TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]})

      conn = conn
eal's avatar
eal committed
280
281
282
283
284
285
286
      |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})

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

      conn = build_conn()
      |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
eal's avatar
eal committed
287
288
289
290

      assert [%{"id" => id}] = json_response(conn, 200)
      assert id == to_string(image_post.id)
    end
lain's avatar
lain committed
291
  end
Roger Braun's avatar
Roger Braun committed
292
293
294
295
296
297
298
299
300
301
302
303
304

  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)

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

      assert [relationship] = json_response(conn, 200)

lain's avatar
lain committed
305
      assert to_string(other_user.id) == relationship["id"]
Roger Braun's avatar
Roger Braun committed
306
307
    end
  end
Roger Braun's avatar
Roger Braun committed
308
309
310
311
312
313
314
315

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

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

    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
316
    assert id == to_string(user.id)
Roger Braun's avatar
Roger Braun committed
317
318
319
320
321
322

    conn = build_conn()
    |> get("/api/v1/accounts/-1")

    assert %{"error" => "Can't find user"} = json_response(conn, 404)
  end
lain's avatar
lain committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336

  test "media upload", %{conn: conn} do
    file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}

    user = insert(:user)

    conn = conn
    |> assign(:user, user)
    |> post("/api/v1/media", %{"file" => file})

    assert media = json_response(conn, 200)

    assert media["type"] == "image"
  end
Roger Braun's avatar
Roger Braun committed
337
338
339
340
341
342
343
344
345
346
347
348

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

    {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
    {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")

    conn = conn
    |> get("/api/v1/timelines/tag/2hu")

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

lain's avatar
lain committed
349
    assert id == to_string(activity.id)
Roger Braun's avatar
Roger Braun committed
350
  end
351
352
353
354
355
356
357
358
359
360

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

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

    assert [%{"id" => id}] = json_response(conn, 200)
lain's avatar
lain committed
361
    assert id == to_string(user.id)
362
363
364
365
366
367
368
369
370
371
372
  end

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

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

    assert [%{"id" => id}] = json_response(conn, 200)
lain's avatar
lain committed
373
    assert id == to_string(other_user.id)
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
  end

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

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

    assert %{"id" => id, "following" => true} = json_response(conn, 200)

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

    assert %{"id" => id, "following" => false} = json_response(conn, 200)
eal's avatar
eal committed
392
393
394
395
396
397

    user = Repo.get(User, user.id)
    conn = build_conn()
    |> assign(:user, user)
    |> post("/api/v1/follows", %{"uri" => other_user.nickname})

eal's avatar
eal committed
398
    assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
399
    assert id == to_string(other_user.id)
400
401
  end

lain's avatar
lain committed
402
  test "blocking / unblocking a user", %{conn: conn} do
403
404
405
    user = insert(:user)
    other_user = insert(:user)

lain's avatar
lain committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
    conn = conn
    |> assign(:user, user)
    |> post("/api/v1/accounts/#{other_user.id}/block")

    assert %{"id" => id, "blocking" => true} = json_response(conn, 200)

    user = Repo.get(User, user.id)
    conn = build_conn()
    |> assign(:user, user)
    |> post("/api/v1/accounts/#{other_user.id}/unblock")

    assert %{"id" => id, "blocking" => false} = json_response(conn, 200)
  end

lain's avatar
lain committed
420
421
422
423
424
425
426
427
428
429
  test "getting a list of blocks", %{conn: conn} do
    user = insert(:user)
    other_user = insert(:user)

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

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

lain's avatar
lain committed
430
    other_user_id = to_string(other_user.id)
lain's avatar
lain committed
431
432
433
    assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
  end

lain's avatar
lain committed
434
435
436
437
438
  test "unimplemented mute endpoints" do
    user = insert(:user)
    other_user = insert(:user)

    ["mute", "unmute"]
439
440
441
442
443
444
    |> Enum.each(fn(endpoint) ->
      conn = build_conn()
      |> assign(:user, user)
      |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")

      assert %{"id" => id} = json_response(conn, 200)
lain's avatar
lain committed
445
      assert id == to_string(other_user.id)
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
    end)
  end

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

    ["blocks", "domain_blocks", "mutes", "follow_requests"]
    |> Enum.each(fn(endpoint) ->
      conn = build_conn()
      |> assign(:user, user)
      |> get("/api/v1/#{endpoint}")

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

lain's avatar
Typo.    
lain committed
462
  test "account search", %{conn: conn} do
lain's avatar
lain committed
463
464
465
466
467
468
469
470
471
    user = insert(:user)
    user_two = insert(:user, %{nickname: "shp@shitposter.club"})
    user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

    conn = conn
    |> assign(:user, user)
    |> get("/api/v1/accounts/search", %{"q" => "2hu"})

    assert [account] = json_response(conn, 200)
lain's avatar
lain committed
472
    assert account["id"] == to_string(user_three.id)
lain's avatar
lain committed
473
474
  end

lain's avatar
lain committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
  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"})
    {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})

    conn = conn
    |> get("/api/v1/search", %{"q" => "2hu"})

    assert results = json_response(conn, 200)

    [account] = results["accounts"]
lain's avatar
lain committed
489
    assert account["id"] == to_string(user_three.id)
lain's avatar
lain committed
490
491
492
493

    assert results["hashtags"] == []

    [status] = results["statuses"]
lain's avatar
lain committed
494
    assert status["id"] == to_string(activity.id)
lain's avatar
lain committed
495
  end
lain's avatar
lain committed
496

497
498
499
500
501
502
503
504
505
  test "search fetches remote statuses", %{conn: conn} do
    conn = conn
    |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
    assert results = json_response(conn, 200)

    [status] = results["statuses"]
    assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
  end

lain's avatar
lain committed
506
507
508
509
510
511
512
513
  test "search fetches remote accounts", %{conn: conn} do
    conn = conn
    |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})

    assert results = json_response(conn, 200)
    [account] = results["accounts"]
    assert account["acct"] == "shp@social.heldscal.la"
  end
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528

  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)

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

    assert [status] = json_response(conn, 200)
lain's avatar
lain committed
529
    assert status["id"] == to_string(activity.id)
530
  end
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580

  describe "updating credentials" do
    test "updates the user's bio" do
      user = insert(:user)

      conn = conn
      |> assign(:user, user)
      |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"})

      assert user = json_response(conn, 200)
      assert user["note"] == "I drink #cofe"
    end

    test "updates the user's name" do
      user = insert(:user)

      conn = conn
      |> assign(:user, user)
      |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})

      assert user = json_response(conn, 200)
      assert user["display_name"] == "markorepairs"
    end

    test "updates the user's avatar" do
      user = insert(:user)

      new_avatar = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}

      conn = conn
      |> assign(:user, user)
      |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})

      assert user = json_response(conn, 200)
      assert user["avatar"] != "https://placehold.it/48x48"
    end

    test "updates the user's banner" do
      user = insert(:user)

      new_header = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}

      conn = conn
      |> assign(:user, user)
      |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})

      assert user = json_response(conn, 200)
      assert user["header"] != "https://placehold.it/700x335"
    end
  end
lain's avatar
lain committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595

  test "get instance information" do
    insert(:user, %{local: true})
    user = insert(:user, %{local: true})
    insert(:user, %{local: false})

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

    conn = conn
    |> get("/api/v1/instance")

    assert result = json_response(conn, 200)

    assert result["stats"]["user_count"] == 2
  end
lain's avatar
lain committed
596
end