93b29dfaef8631567e4364f85f5b80a35e11b054
[akkoma] / test / web / mastodon_api / mastodon_api_controller_test.exs
1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
2 use Pleroma.Web.ConnCase
3
4 alias Pleroma.Web.TwitterAPI.TwitterAPI
5 alias Pleroma.{Repo, User, Activity, Notification}
6 alias Pleroma.Web.{OStatus, CommonAPI}
7
8 import Pleroma.Factory
9
10 test "the home timeline", %{conn: conn} do
11 user = insert(:user)
12 following = insert(:user)
13
14 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
15
16 conn = conn
17 |> assign(:user, user)
18 |> get("/api/v1/timelines/home")
19
20 assert length(json_response(conn, 200)) == 0
21
22 {:ok, user} = User.follow(user, following)
23
24 conn = build_conn()
25 |> assign(:user, user)
26 |> get("/api/v1/timelines/home")
27
28 assert [%{"content" => "test"}] = json_response(conn, 200)
29 end
30
31 test "the public timeline", %{conn: conn} do
32 following = insert(:user)
33
34 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
35 {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
36
37 conn = conn
38 |> get("/api/v1/timelines/public", %{"local" => "False"})
39
40 assert length(json_response(conn, 200)) == 2
41
42 conn = build_conn()
43 |> get("/api/v1/timelines/public", %{"local" => "True"})
44
45 assert [%{"content" => "test"}] = json_response(conn, 200)
46
47 conn = build_conn()
48 |> get("/api/v1/timelines/public", %{"local" => "1"})
49
50 assert [%{"content" => "test"}] = json_response(conn, 200)
51 end
52
53 test "posting a status", %{conn: conn} do
54 user = insert(:user)
55
56 conn = conn
57 |> assign(:user, user)
58 |> post("/api/v1/statuses", %{"status" => "cofe", "spoiler_text" => "2hu", "sensitive" => "false"})
59
60 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = json_response(conn, 200)
61 assert Repo.get(Activity, id)
62 end
63
64 test "posting a sensitive status", %{conn: conn} do
65 user = insert(:user)
66
67 conn = conn
68 |> assign(:user, user)
69 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
70
71 assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
72 assert Repo.get(Activity, id)
73 end
74
75 test "replying to a status", %{conn: conn} do
76 user = insert(:user)
77
78 {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"})
79
80 conn = conn
81 |> assign(:user, user)
82 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
83
84 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
85
86 activity = Repo.get(Activity, id)
87
88 assert activity.data["context"] == replied_to.data["context"]
89 assert activity.data["object"]["inReplyToStatusId"] == replied_to.id
90 end
91
92 test "verify_credentials", %{conn: conn} do
93 user = insert(:user)
94
95 conn = conn
96 |> assign(:user, user)
97 |> get("/api/v1/accounts/verify_credentials")
98
99 assert %{"id" => id} = json_response(conn, 200)
100 assert id == to_string(user.id)
101 end
102
103 test "get a status", %{conn: conn} do
104 activity = insert(:note_activity)
105
106 conn = conn
107 |> get("/api/v1/statuses/#{activity.id}")
108
109 assert %{"id" => id} = json_response(conn, 200)
110 assert id == to_string(activity.id)
111 end
112
113 describe "deleting a status" do
114 test "when you created it", %{conn: conn} do
115 activity = insert(:note_activity)
116 author = User.get_by_ap_id(activity.data["actor"])
117
118 conn = conn
119 |> assign(:user, author)
120 |> delete("/api/v1/statuses/#{activity.id}")
121
122 assert %{} = json_response(conn, 200)
123
124 assert Repo.get(Activity, activity.id) == nil
125 end
126
127 test "when you didn't create it", %{conn: conn} do
128 activity = insert(:note_activity)
129 user = insert(:user)
130
131 conn = conn
132 |> assign(:user, user)
133 |> delete("/api/v1/statuses/#{activity.id}")
134
135 assert %{"error" => _} = json_response(conn, 403)
136
137 assert Repo.get(Activity, activity.id) == activity
138 end
139 end
140
141 describe "notifications" do
142 test "list of notifications", %{conn: conn} do
143 user = insert(:user)
144 other_user = insert(:user)
145
146 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
147 {:ok, [notification]} = Notification.create_notifications(activity)
148
149 conn = conn
150 |> assign(:user, user)
151 |> get("/api/v1/notifications")
152
153 expected_response = "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
154 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
155 assert response == expected_response
156 end
157
158 test "getting a single notification", %{conn: conn} do
159 user = insert(:user)
160 other_user = insert(:user)
161
162 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
163 {:ok, [notification]} = Notification.create_notifications(activity)
164
165 conn = conn
166 |> assign(:user, user)
167 |> get("/api/v1/notifications/#{notification.id}")
168
169 expected_response = "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
170 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
171 assert response == expected_response
172 end
173
174 test "dismissing a single notification", %{conn: conn} do
175 user = insert(:user)
176 other_user = insert(:user)
177
178 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
179 {:ok, [notification]} = Notification.create_notifications(activity)
180
181 conn = conn
182 |> assign(:user, user)
183 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
184
185 assert %{} = json_response(conn, 200)
186 end
187
188 test "clearing all notifications", %{conn: conn} do
189 user = insert(:user)
190 other_user = insert(:user)
191
192 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
193 {:ok, [notification]} = Notification.create_notifications(activity)
194
195 conn = conn
196 |> assign(:user, user)
197 |> post("/api/v1/notifications/clear")
198
199 assert %{} = json_response(conn, 200)
200
201 conn = build_conn()
202 |> assign(:user, user)
203 |> get("/api/v1/notifications")
204
205 assert all = json_response(conn, 200)
206 assert all == []
207 end
208 end
209
210 describe "reblogging" do
211 test "reblogs and returns the reblogged status", %{conn: conn} do
212 activity = insert(:note_activity)
213 user = insert(:user)
214
215 conn = conn
216 |> assign(:user, user)
217 |> post("/api/v1/statuses/#{activity.id}/reblog")
218
219 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} = json_response(conn, 200)
220 assert to_string(activity.id) == id
221 end
222 end
223
224 describe "favoriting" do
225 test "favs a status and returns it", %{conn: conn} do
226 activity = insert(:note_activity)
227 user = insert(:user)
228
229 conn = conn
230 |> assign(:user, user)
231 |> post("/api/v1/statuses/#{activity.id}/favourite")
232
233 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = json_response(conn, 200)
234 assert to_string(activity.id) == id
235 end
236 end
237
238 describe "unfavoriting" do
239 test "unfavorites a status and returns it", %{conn: conn} do
240 activity = insert(:note_activity)
241 user = insert(:user)
242
243 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
244
245 conn = conn
246 |> assign(:user, user)
247 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
248
249 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} = json_response(conn, 200)
250 assert to_string(activity.id) == id
251 end
252 end
253
254 describe "user timelines" do
255 test "gets a users statuses", %{conn: conn} do
256 _note = insert(:note_activity)
257 note_two = insert(:note_activity)
258
259 user = User.get_by_ap_id(note_two.data["actor"])
260
261 conn = conn
262 |> get("/api/v1/accounts/#{user.id}/statuses")
263
264 assert [%{"id" => id}] = json_response(conn, 200)
265
266 assert id == to_string(note_two.id)
267 end
268
269 test "gets an users media", %{conn: conn} do
270 note = insert(:note_activity)
271 user = User.get_by_ap_id(note.data["actor"])
272
273 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
274 media = TwitterAPI.upload(file, "json")
275 |> Poison.decode!
276
277 {:ok, image_post} = TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]})
278
279 conn = conn
280 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
281
282 assert [%{"id" => id}] = json_response(conn, 200)
283 assert id == to_string(image_post.id)
284
285 conn = build_conn()
286 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
287
288 assert [%{"id" => id}] = json_response(conn, 200)
289 assert id == to_string(image_post.id)
290 end
291 end
292
293 describe "user relationships" do
294 test "returns the relationships for the current user", %{conn: conn} do
295 user = insert(:user)
296 other_user = insert(:user)
297 {:ok, user} = User.follow(user, other_user)
298
299 conn = conn
300 |> assign(:user, user)
301 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
302
303 assert [relationship] = json_response(conn, 200)
304
305 assert to_string(other_user.id) == relationship["id"]
306 end
307 end
308
309 test "account fetching", %{conn: conn} do
310 user = insert(:user)
311
312 conn = conn
313 |> get("/api/v1/accounts/#{user.id}")
314
315 assert %{"id" => id} = json_response(conn, 200)
316 assert id == to_string(user.id)
317
318 conn = build_conn()
319 |> get("/api/v1/accounts/-1")
320
321 assert %{"error" => "Can't find user"} = json_response(conn, 404)
322 end
323
324 test "media upload", %{conn: conn} do
325 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
326
327 user = insert(:user)
328
329 conn = conn
330 |> assign(:user, user)
331 |> post("/api/v1/media", %{"file" => file})
332
333 assert media = json_response(conn, 200)
334
335 assert media["type"] == "image"
336 end
337
338 test "hashtag timeline", %{conn: conn} do
339 following = insert(:user)
340
341 {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
342 {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
343
344 conn = conn
345 |> get("/api/v1/timelines/tag/2hu")
346
347 assert [%{"id" => id}] = json_response(conn, 200)
348
349 assert id == to_string(activity.id)
350 end
351
352 test "getting followers", %{conn: conn} do
353 user = insert(:user)
354 other_user = insert(:user)
355 {:ok, user} = User.follow(user, other_user)
356
357 conn = conn
358 |> get("/api/v1/accounts/#{other_user.id}/followers")
359
360 assert [%{"id" => id}] = json_response(conn, 200)
361 assert id == to_string(user.id)
362 end
363
364 test "getting following", %{conn: conn} do
365 user = insert(:user)
366 other_user = insert(:user)
367 {:ok, user} = User.follow(user, other_user)
368
369 conn = conn
370 |> get("/api/v1/accounts/#{user.id}/following")
371
372 assert [%{"id" => id}] = json_response(conn, 200)
373 assert id == to_string(other_user.id)
374 end
375
376 test "following / unfollowing a user", %{conn: conn} do
377 user = insert(:user)
378 other_user = insert(:user)
379
380 conn = conn
381 |> assign(:user, user)
382 |> post("/api/v1/accounts/#{other_user.id}/follow")
383
384 assert %{"id" => id, "following" => true} = json_response(conn, 200)
385
386 user = Repo.get(User, user.id)
387 conn = build_conn()
388 |> assign(:user, user)
389 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
390
391 assert %{"id" => id, "following" => false} = json_response(conn, 200)
392
393 user = Repo.get(User, user.id)
394 conn = build_conn()
395 |> assign(:user, user)
396 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
397
398 assert %{"id" => id} = json_response(conn, 200)
399 assert id == to_string(other_user.id)
400 end
401
402 test "blocking / unblocking a user", %{conn: conn} do
403 user = insert(:user)
404 other_user = insert(:user)
405
406 conn = conn
407 |> assign(:user, user)
408 |> post("/api/v1/accounts/#{other_user.id}/block")
409
410 assert %{"id" => id, "blocking" => true} = json_response(conn, 200)
411
412 user = Repo.get(User, user.id)
413 conn = build_conn()
414 |> assign(:user, user)
415 |> post("/api/v1/accounts/#{other_user.id}/unblock")
416
417 assert %{"id" => id, "blocking" => false} = json_response(conn, 200)
418 end
419
420 test "getting a list of blocks", %{conn: conn} do
421 user = insert(:user)
422 other_user = insert(:user)
423
424 {:ok, user} = User.block(user, other_user)
425
426 conn = conn
427 |> assign(:user, user)
428 |> get("/api/v1/blocks")
429
430 other_user_id = to_string(other_user.id)
431 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
432 end
433
434 test "unimplemented mute endpoints" do
435 user = insert(:user)
436 other_user = insert(:user)
437
438 ["mute", "unmute"]
439 |> Enum.each(fn(endpoint) ->
440 conn = build_conn()
441 |> assign(:user, user)
442 |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
443
444 assert %{"id" => id} = json_response(conn, 200)
445 assert id == to_string(other_user.id)
446 end)
447 end
448
449 test "unimplemented mutes, follow_requests, blocks, domain blocks" do
450 user = insert(:user)
451
452 ["blocks", "domain_blocks", "mutes", "follow_requests"]
453 |> Enum.each(fn(endpoint) ->
454 conn = build_conn()
455 |> assign(:user, user)
456 |> get("/api/v1/#{endpoint}")
457
458 assert [] = json_response(conn, 200)
459 end)
460 end
461
462 test "account search", %{conn: conn} do
463 user = insert(:user)
464 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
465 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
466
467 conn = conn
468 |> assign(:user, user)
469 |> get("/api/v1/accounts/search", %{"q" => "2hu"})
470
471 assert [account] = json_response(conn, 200)
472 assert account["id"] == to_string(user_three.id)
473 end
474
475 test "search", %{conn: conn} do
476 user = insert(:user)
477 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
478 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
479
480 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
481 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
482
483 conn = conn
484 |> get("/api/v1/search", %{"q" => "2hu"})
485
486 assert results = json_response(conn, 200)
487
488 [account] = results["accounts"]
489 assert account["id"] == to_string(user_three.id)
490
491 assert results["hashtags"] == []
492
493 [status] = results["statuses"]
494 assert status["id"] == to_string(activity.id)
495 end
496
497 test "search fetches remote statuses", %{conn: conn} do
498 conn = conn
499 |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
500 assert results = json_response(conn, 200)
501
502 [status] = results["statuses"]
503 assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
504 end
505
506 test "search fetches remote accounts", %{conn: conn} do
507 conn = conn
508 |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
509
510 assert results = json_response(conn, 200)
511 [account] = results["accounts"]
512 assert account["acct"] == "shp@social.heldscal.la"
513 end
514
515 test "returns the favorites of a user", %{conn: conn} do
516 user = insert(:user)
517 other_user = insert(:user)
518
519 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
520 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
521
522 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
523
524 conn = conn
525 |> assign(:user, user)
526 |> get("/api/v1/favourites")
527
528 assert [status] = json_response(conn, 200)
529 assert status["id"] == to_string(activity.id)
530 end
531
532 describe "updating credentials" do
533 test "updates the user's bio" do
534 user = insert(:user)
535
536 conn = conn
537 |> assign(:user, user)
538 |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"})
539
540 assert user = json_response(conn, 200)
541 assert user["note"] == "I drink #cofe"
542 end
543
544 test "updates the user's name" do
545 user = insert(:user)
546
547 conn = conn
548 |> assign(:user, user)
549 |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
550
551 assert user = json_response(conn, 200)
552 assert user["display_name"] == "markorepairs"
553 end
554
555 test "updates the user's avatar" do
556 user = insert(:user)
557
558 new_avatar = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
559
560 conn = conn
561 |> assign(:user, user)
562 |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
563
564 assert user = json_response(conn, 200)
565 assert user["avatar"] != "https://placehold.it/48x48"
566 end
567
568 test "updates the user's banner" do
569 user = insert(:user)
570
571 new_header = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
572
573 conn = conn
574 |> assign(:user, user)
575 |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
576
577 assert user = json_response(conn, 200)
578 assert user["header"] != "https://placehold.it/700x335"
579 end
580 end
581
582 test "get instance information" do
583 insert(:user, %{local: true})
584 user = insert(:user, %{local: true})
585 insert(:user, %{local: false})
586
587 {:ok, _} = TwitterAPI.create_status(user, %{"status" => "cofe"})
588
589 Pleroma.Stats.update_stats()
590
591 conn = conn
592 |> get("/api/v1/instance")
593
594 assert result = json_response(conn, 200)
595
596 assert result["stats"]["user_count"] == 2
597 assert result["stats"]["status_count"] == 1
598 end
599 end