1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
2 use Pleroma.Web.ConnCase
4 alias Pleroma.Web.TwitterAPI.TwitterAPI
5 alias Pleroma.{Repo, User, Activity, Notification}
6 alias Pleroma.Web.{OStatus, CommonAPI}
9 import ExUnit.CaptureLog
11 test "the home timeline", %{conn: conn} do
13 following = insert(:user)
15 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
19 |> assign(:user, user)
20 |> get("/api/v1/timelines/home")
22 assert length(json_response(conn, 200)) == 0
24 {:ok, user} = User.follow(user, following)
28 |> assign(:user, user)
29 |> get("/api/v1/timelines/home")
31 assert [%{"content" => "test"}] = json_response(conn, 200)
34 test "the public timeline", %{conn: conn} do
35 following = insert(:user)
38 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
41 OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
45 |> get("/api/v1/timelines/public", %{"local" => "False"})
47 assert length(json_response(conn, 200)) == 2
51 |> get("/api/v1/timelines/public", %{"local" => "True"})
53 assert [%{"content" => "test"}] = json_response(conn, 200)
57 |> get("/api/v1/timelines/public", %{"local" => "1"})
59 assert [%{"content" => "test"}] = json_response(conn, 200)
63 test "posting a status", %{conn: conn} do
66 idempotency_key = "Pikachu rocks!"
70 |> assign(:user, user)
71 |> put_req_header("idempotency-key", idempotency_key)
72 |> post("/api/v1/statuses", %{
74 "spoiler_text" => "2hu",
75 "sensitive" => "false"
78 {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
80 assert ttl > :timer.seconds(6 * 60 * 60 - 1)
82 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
83 json_response(conn_one, 200)
85 assert Repo.get(Activity, id)
89 |> assign(:user, user)
90 |> put_req_header("idempotency-key", idempotency_key)
91 |> post("/api/v1/statuses", %{
93 "spoiler_text" => "2hu",
94 "sensitive" => "false"
97 assert %{"id" => second_id} = json_response(conn_two, 200)
99 assert id == second_id
103 |> assign(:user, user)
104 |> post("/api/v1/statuses", %{
106 "spoiler_text" => "2hu",
107 "sensitive" => "false"
110 assert %{"id" => third_id} = json_response(conn_three, 200)
112 refute id == third_id
115 test "posting a sensitive status", %{conn: conn} do
120 |> assign(:user, user)
121 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
123 assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
124 assert Repo.get(Activity, id)
127 test "replying to a status", %{conn: conn} do
130 {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"})
134 |> assign(:user, user)
135 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
137 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
139 activity = Repo.get(Activity, id)
141 assert activity.data["context"] == replied_to.data["context"]
142 assert activity.data["object"]["inReplyToStatusId"] == replied_to.id
145 test "verify_credentials", %{conn: conn} do
150 |> assign(:user, user)
151 |> get("/api/v1/accounts/verify_credentials")
153 assert %{"id" => id} = json_response(conn, 200)
154 assert id == to_string(user.id)
157 test "get a status", %{conn: conn} do
158 activity = insert(:note_activity)
162 |> get("/api/v1/statuses/#{activity.id}")
164 assert %{"id" => id} = json_response(conn, 200)
165 assert id == to_string(activity.id)
168 describe "deleting a status" do
169 test "when you created it", %{conn: conn} do
170 activity = insert(:note_activity)
171 author = User.get_by_ap_id(activity.data["actor"])
175 |> assign(:user, author)
176 |> delete("/api/v1/statuses/#{activity.id}")
178 assert %{} = json_response(conn, 200)
180 assert Repo.get(Activity, activity.id) == nil
183 test "when you didn't create it", %{conn: conn} do
184 activity = insert(:note_activity)
189 |> assign(:user, user)
190 |> delete("/api/v1/statuses/#{activity.id}")
192 assert %{"error" => _} = json_response(conn, 403)
194 assert Repo.get(Activity, activity.id) == activity
198 describe "notifications" do
199 test "list of notifications", %{conn: conn} do
201 other_user = insert(:user)
204 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
206 {:ok, [_notification]} = Notification.create_notifications(activity)
210 |> assign(:user, user)
211 |> get("/api/v1/notifications")
214 "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
216 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
217 assert response == expected_response
220 test "getting a single notification", %{conn: conn} do
222 other_user = insert(:user)
225 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
227 {:ok, [notification]} = Notification.create_notifications(activity)
231 |> assign(:user, user)
232 |> get("/api/v1/notifications/#{notification.id}")
235 "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
237 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
238 assert response == expected_response
241 test "dismissing a single notification", %{conn: conn} do
243 other_user = insert(:user)
246 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
248 {:ok, [notification]} = Notification.create_notifications(activity)
252 |> assign(:user, user)
253 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
255 assert %{} = json_response(conn, 200)
258 test "clearing all notifications", %{conn: conn} do
260 other_user = insert(:user)
263 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
265 {:ok, [_notification]} = Notification.create_notifications(activity)
269 |> assign(:user, user)
270 |> post("/api/v1/notifications/clear")
272 assert %{} = json_response(conn, 200)
276 |> assign(:user, user)
277 |> get("/api/v1/notifications")
279 assert all = json_response(conn, 200)
284 describe "reblogging" do
285 test "reblogs and returns the reblogged status", %{conn: conn} do
286 activity = insert(:note_activity)
291 |> assign(:user, user)
292 |> post("/api/v1/statuses/#{activity.id}/reblog")
294 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
295 json_response(conn, 200)
297 assert to_string(activity.id) == id
301 describe "favoriting" do
302 test "favs a status and returns it", %{conn: conn} do
303 activity = insert(:note_activity)
308 |> assign(:user, user)
309 |> post("/api/v1/statuses/#{activity.id}/favourite")
311 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
312 json_response(conn, 200)
314 assert to_string(activity.id) == id
318 describe "unfavoriting" do
319 test "unfavorites a status and returns it", %{conn: conn} do
320 activity = insert(:note_activity)
323 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
327 |> assign(:user, user)
328 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
330 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
331 json_response(conn, 200)
333 assert to_string(activity.id) == id
337 describe "user timelines" do
338 test "gets a users statuses", %{conn: conn} do
339 _note = insert(:note_activity)
340 note_two = insert(:note_activity)
342 user = User.get_by_ap_id(note_two.data["actor"])
346 |> get("/api/v1/accounts/#{user.id}/statuses")
348 assert [%{"id" => id}] = json_response(conn, 200)
350 assert id == to_string(note_two.id)
353 test "unimplemented pinned statuses feature", %{conn: conn} do
354 note = insert(:note_activity)
355 user = User.get_by_ap_id(note.data["actor"])
359 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
361 assert json_response(conn, 200) == []
364 test "gets an users media", %{conn: conn} do
365 note = insert(:note_activity)
366 user = User.get_by_ap_id(note.data["actor"])
369 content_type: "image/jpg",
370 path: Path.absname("test/fixtures/image.jpg"),
371 filename: "an_image.jpg"
375 TwitterAPI.upload(file, "json")
379 TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]})
383 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
385 assert [%{"id" => id}] = json_response(conn, 200)
386 assert id == to_string(image_post.id)
390 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
392 assert [%{"id" => id}] = json_response(conn, 200)
393 assert id == to_string(image_post.id)
397 describe "user relationships" do
398 test "returns the relationships for the current user", %{conn: conn} do
400 other_user = insert(:user)
401 {:ok, user} = User.follow(user, other_user)
405 |> assign(:user, user)
406 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
408 assert [relationship] = json_response(conn, 200)
410 assert to_string(other_user.id) == relationship["id"]
414 test "account fetching", %{conn: conn} do
419 |> get("/api/v1/accounts/#{user.id}")
421 assert %{"id" => id} = json_response(conn, 200)
422 assert id == to_string(user.id)
426 |> get("/api/v1/accounts/-1")
428 assert %{"error" => "Can't find user"} = json_response(conn, 404)
431 test "media upload", %{conn: conn} do
433 content_type: "image/jpg",
434 path: Path.absname("test/fixtures/image.jpg"),
435 filename: "an_image.jpg"
442 |> assign(:user, user)
443 |> post("/api/v1/media", %{"file" => file})
445 assert media = json_response(conn, 200)
447 assert media["type"] == "image"
450 test "hashtag timeline", %{conn: conn} do
451 following = insert(:user)
454 {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
457 OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
461 |> get("/api/v1/timelines/tag/2hu")
463 assert [%{"id" => id}] = json_response(conn, 200)
465 assert id == to_string(activity.id)
469 test "getting followers", %{conn: conn} do
471 other_user = insert(:user)
472 {:ok, user} = User.follow(user, other_user)
476 |> get("/api/v1/accounts/#{other_user.id}/followers")
478 assert [%{"id" => id}] = json_response(conn, 200)
479 assert id == to_string(user.id)
482 test "getting following", %{conn: conn} do
484 other_user = insert(:user)
485 {:ok, user} = User.follow(user, other_user)
489 |> get("/api/v1/accounts/#{user.id}/following")
491 assert [%{"id" => id}] = json_response(conn, 200)
492 assert id == to_string(other_user.id)
495 test "following / unfollowing a user", %{conn: conn} do
497 other_user = insert(:user)
501 |> assign(:user, user)
502 |> post("/api/v1/accounts/#{other_user.id}/follow")
504 assert %{"id" => _id, "following" => true} = json_response(conn, 200)
506 user = Repo.get(User, user.id)
510 |> assign(:user, user)
511 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
513 assert %{"id" => _id, "following" => false} = json_response(conn, 200)
515 user = Repo.get(User, user.id)
519 |> assign(:user, user)
520 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
522 assert %{"id" => id} = json_response(conn, 200)
523 assert id == to_string(other_user.id)
526 test "blocking / unblocking a user", %{conn: conn} do
528 other_user = insert(:user)
532 |> assign(:user, user)
533 |> post("/api/v1/accounts/#{other_user.id}/block")
535 assert %{"id" => _id, "blocking" => true} = json_response(conn, 200)
537 user = Repo.get(User, user.id)
541 |> assign(:user, user)
542 |> post("/api/v1/accounts/#{other_user.id}/unblock")
544 assert %{"id" => _id, "blocking" => false} = json_response(conn, 200)
547 test "getting a list of blocks", %{conn: conn} do
549 other_user = insert(:user)
551 {:ok, user} = User.block(user, other_user)
555 |> assign(:user, user)
556 |> get("/api/v1/blocks")
558 other_user_id = to_string(other_user.id)
559 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
562 test "unimplemented mute endpoints" do
564 other_user = insert(:user)
567 |> Enum.each(fn endpoint ->
570 |> assign(:user, user)
571 |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
573 assert %{"id" => id} = json_response(conn, 200)
574 assert id == to_string(other_user.id)
578 test "unimplemented mutes, follow_requests, blocks, domain blocks" do
581 ["blocks", "domain_blocks", "mutes", "follow_requests"]
582 |> Enum.each(fn endpoint ->
585 |> assign(:user, user)
586 |> get("/api/v1/#{endpoint}")
588 assert [] = json_response(conn, 200)
592 test "account search", %{conn: conn} do
594 _user_two = insert(:user, %{nickname: "shp@shitposter.club"})
595 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
599 |> assign(:user, user)
600 |> get("/api/v1/accounts/search", %{"q" => "2hu"})
602 assert [account] = json_response(conn, 200)
603 assert account["id"] == to_string(user_three.id)
606 test "search", %{conn: conn} do
608 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
609 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
611 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
614 CommonAPI.post(user, %{
615 "status" => "This is about 2hu, but private",
616 "visibility" => "private"
619 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
623 |> get("/api/v1/search", %{"q" => "2hu"})
625 assert results = json_response(conn, 200)
627 [account] = results["accounts"]
628 assert account["id"] == to_string(user_three.id)
630 assert results["hashtags"] == []
632 [status] = results["statuses"]
633 assert status["id"] == to_string(activity.id)
636 test "search fetches remote statuses", %{conn: conn} do
640 |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
642 assert results = json_response(conn, 200)
644 [status] = results["statuses"]
645 assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
649 test "search fetches remote accounts", %{conn: conn} do
652 |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
654 assert results = json_response(conn, 200)
655 [account] = results["accounts"]
656 assert account["acct"] == "shp@social.heldscal.la"
659 test "returns the favorites of a user", %{conn: conn} do
661 other_user = insert(:user)
663 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
664 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
666 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
670 |> assign(:user, user)
671 |> get("/api/v1/favourites")
673 assert [status] = json_response(conn, 200)
674 assert status["id"] == to_string(activity.id)
677 describe "updating credentials" do
678 test "updates the user's bio", %{conn: conn} do
683 |> assign(:user, user)
684 |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"})
686 assert user = json_response(conn, 200)
687 assert user["note"] == "I drink #cofe"
690 test "updates the user's name", %{conn: conn} do
695 |> assign(:user, user)
696 |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
698 assert user = json_response(conn, 200)
699 assert user["display_name"] == "markorepairs"
702 test "updates the user's avatar", %{conn: conn} do
705 new_avatar = %Plug.Upload{
706 content_type: "image/jpg",
707 path: Path.absname("test/fixtures/image.jpg"),
708 filename: "an_image.jpg"
713 |> assign(:user, user)
714 |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
716 assert user = json_response(conn, 200)
717 assert user["avatar"] != "https://placehold.it/48x48"
720 test "updates the user's banner", %{conn: conn} do
723 new_header = %Plug.Upload{
724 content_type: "image/jpg",
725 path: Path.absname("test/fixtures/image.jpg"),
726 filename: "an_image.jpg"
731 |> assign(:user, user)
732 |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
734 assert user = json_response(conn, 200)
735 assert user["header"] != "https://placehold.it/700x335"
739 test "get instance information", %{conn: conn} do
740 insert(:user, %{local: true})
741 user = insert(:user, %{local: true})
742 insert(:user, %{local: false})
744 {:ok, _} = TwitterAPI.create_status(user, %{"status" => "cofe"})
746 Pleroma.Stats.update_stats()
750 |> get("/api/v1/instance")
752 assert result = json_response(conn, 200)
754 assert result["stats"]["user_count"] == 2
755 assert result["stats"]["status_count"] == 1