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 "unreblogging" do
302 test "unreblogs and returns the unreblogged status", %{conn: conn} do
303 activity = insert(:note_activity)
306 {:ok, _, _} = CommonAPI.repeat(activity.id, user)
310 |> assign(:user, user)
311 |> post("/api/v1/statuses/#{activity.id}/unreblog")
313 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
315 assert to_string(activity.id) == id
319 describe "favoriting" do
320 test "favs a status and returns it", %{conn: conn} do
321 activity = insert(:note_activity)
326 |> assign(:user, user)
327 |> post("/api/v1/statuses/#{activity.id}/favourite")
329 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
330 json_response(conn, 200)
332 assert to_string(activity.id) == id
336 describe "unfavoriting" do
337 test "unfavorites a status and returns it", %{conn: conn} do
338 activity = insert(:note_activity)
341 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
345 |> assign(:user, user)
346 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
348 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
349 json_response(conn, 200)
351 assert to_string(activity.id) == id
355 describe "user timelines" do
356 test "gets a users statuses", %{conn: conn} do
357 _note = insert(:note_activity)
358 note_two = insert(:note_activity)
360 user = User.get_by_ap_id(note_two.data["actor"])
364 |> get("/api/v1/accounts/#{user.id}/statuses")
366 assert [%{"id" => id}] = json_response(conn, 200)
368 assert id == to_string(note_two.id)
371 test "unimplemented pinned statuses feature", %{conn: conn} do
372 note = insert(:note_activity)
373 user = User.get_by_ap_id(note.data["actor"])
377 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
379 assert json_response(conn, 200) == []
382 test "gets an users media", %{conn: conn} do
383 note = insert(:note_activity)
384 user = User.get_by_ap_id(note.data["actor"])
387 content_type: "image/jpg",
388 path: Path.absname("test/fixtures/image.jpg"),
389 filename: "an_image.jpg"
393 TwitterAPI.upload(file, "json")
397 TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]})
401 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
403 assert [%{"id" => id}] = json_response(conn, 200)
404 assert id == to_string(image_post.id)
408 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
410 assert [%{"id" => id}] = json_response(conn, 200)
411 assert id == to_string(image_post.id)
415 describe "user relationships" do
416 test "returns the relationships for the current user", %{conn: conn} do
418 other_user = insert(:user)
419 {:ok, user} = User.follow(user, other_user)
423 |> assign(:user, user)
424 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
426 assert [relationship] = json_response(conn, 200)
428 assert to_string(other_user.id) == relationship["id"]
432 test "account fetching", %{conn: conn} do
437 |> get("/api/v1/accounts/#{user.id}")
439 assert %{"id" => id} = json_response(conn, 200)
440 assert id == to_string(user.id)
444 |> get("/api/v1/accounts/-1")
446 assert %{"error" => "Can't find user"} = json_response(conn, 404)
449 test "media upload", %{conn: conn} do
451 content_type: "image/jpg",
452 path: Path.absname("test/fixtures/image.jpg"),
453 filename: "an_image.jpg"
460 |> assign(:user, user)
461 |> post("/api/v1/media", %{"file" => file})
463 assert media = json_response(conn, 200)
465 assert media["type"] == "image"
468 test "hashtag timeline", %{conn: conn} do
469 following = insert(:user)
472 {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
475 OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
479 |> get("/api/v1/timelines/tag/2hu")
481 assert [%{"id" => id}] = json_response(conn, 200)
483 assert id == to_string(activity.id)
487 test "getting followers", %{conn: conn} do
489 other_user = insert(:user)
490 {:ok, user} = User.follow(user, other_user)
494 |> get("/api/v1/accounts/#{other_user.id}/followers")
496 assert [%{"id" => id}] = json_response(conn, 200)
497 assert id == to_string(user.id)
500 test "getting following", %{conn: conn} do
502 other_user = insert(:user)
503 {:ok, user} = User.follow(user, other_user)
507 |> get("/api/v1/accounts/#{user.id}/following")
509 assert [%{"id" => id}] = json_response(conn, 200)
510 assert id == to_string(other_user.id)
513 test "following / unfollowing a user", %{conn: conn} do
515 other_user = insert(:user)
519 |> assign(:user, user)
520 |> post("/api/v1/accounts/#{other_user.id}/follow")
522 assert %{"id" => _id, "following" => true} = json_response(conn, 200)
524 user = Repo.get(User, user.id)
528 |> assign(:user, user)
529 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
531 assert %{"id" => _id, "following" => false} = json_response(conn, 200)
533 user = Repo.get(User, user.id)
537 |> assign(:user, user)
538 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
540 assert %{"id" => id} = json_response(conn, 200)
541 assert id == to_string(other_user.id)
544 test "blocking / unblocking a user", %{conn: conn} do
546 other_user = insert(:user)
550 |> assign(:user, user)
551 |> post("/api/v1/accounts/#{other_user.id}/block")
553 assert %{"id" => _id, "blocking" => true} = json_response(conn, 200)
555 user = Repo.get(User, user.id)
559 |> assign(:user, user)
560 |> post("/api/v1/accounts/#{other_user.id}/unblock")
562 assert %{"id" => _id, "blocking" => false} = json_response(conn, 200)
565 test "getting a list of blocks", %{conn: conn} do
567 other_user = insert(:user)
569 {:ok, user} = User.block(user, other_user)
573 |> assign(:user, user)
574 |> get("/api/v1/blocks")
576 other_user_id = to_string(other_user.id)
577 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
580 test "unimplemented mute endpoints" do
582 other_user = insert(:user)
585 |> Enum.each(fn endpoint ->
588 |> assign(:user, user)
589 |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
591 assert %{"id" => id} = json_response(conn, 200)
592 assert id == to_string(other_user.id)
596 test "unimplemented mutes, follow_requests, blocks, domain blocks" do
599 ["blocks", "domain_blocks", "mutes", "follow_requests"]
600 |> Enum.each(fn endpoint ->
603 |> assign(:user, user)
604 |> get("/api/v1/#{endpoint}")
606 assert [] = json_response(conn, 200)
610 test "account search", %{conn: conn} do
612 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
613 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
617 |> assign(:user, user)
618 |> get("/api/v1/accounts/search", %{"q" => "shp"})
619 |> json_response(200)
621 result_ids = for result <- results, do: result["acct"]
623 assert user_two.nickname in result_ids
624 assert user_three.nickname in result_ids
628 |> assign(:user, user)
629 |> get("/api/v1/accounts/search", %{"q" => "2hu"})
630 |> json_response(200)
632 result_ids = for result <- results, do: result["acct"]
634 assert user_three.nickname in result_ids
637 test "search", %{conn: conn} do
639 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
640 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
642 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
645 CommonAPI.post(user, %{
646 "status" => "This is about 2hu, but private",
647 "visibility" => "private"
650 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
654 |> get("/api/v1/search", %{"q" => "2hu"})
656 assert results = json_response(conn, 200)
658 [account | _] = results["accounts"]
659 assert account["id"] == to_string(user_three.id)
661 assert results["hashtags"] == []
663 [status] = results["statuses"]
664 assert status["id"] == to_string(activity.id)
667 test "search fetches remote statuses", %{conn: conn} do
671 |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
673 assert results = json_response(conn, 200)
675 [status] = results["statuses"]
676 assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
680 test "search fetches remote accounts", %{conn: conn} do
683 |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
685 assert results = json_response(conn, 200)
686 [account] = results["accounts"]
687 assert account["acct"] == "shp@social.heldscal.la"
690 test "returns the favorites of a user", %{conn: conn} do
692 other_user = insert(:user)
694 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
695 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
697 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
701 |> assign(:user, user)
702 |> get("/api/v1/favourites")
704 assert [status] = json_response(conn, 200)
705 assert status["id"] == to_string(activity.id)
708 describe "updating credentials" do
709 test "updates the user's bio", %{conn: conn} do
714 |> assign(:user, user)
715 |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"})
717 assert user = json_response(conn, 200)
718 assert user["note"] == "I drink #cofe"
721 test "updates the user's name", %{conn: conn} do
726 |> assign(:user, user)
727 |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
729 assert user = json_response(conn, 200)
730 assert user["display_name"] == "markorepairs"
733 test "updates the user's avatar", %{conn: conn} do
736 new_avatar = %Plug.Upload{
737 content_type: "image/jpg",
738 path: Path.absname("test/fixtures/image.jpg"),
739 filename: "an_image.jpg"
744 |> assign(:user, user)
745 |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
747 assert user = json_response(conn, 200)
748 assert user["avatar"] != "https://placehold.it/48x48"
751 test "updates the user's banner", %{conn: conn} do
754 new_header = %Plug.Upload{
755 content_type: "image/jpg",
756 path: Path.absname("test/fixtures/image.jpg"),
757 filename: "an_image.jpg"
762 |> assign(:user, user)
763 |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
765 assert user = json_response(conn, 200)
766 assert user["header"] != "https://placehold.it/700x335"
770 test "get instance information", %{conn: conn} do
771 insert(:user, %{local: true})
772 user = insert(:user, %{local: true})
773 insert(:user, %{local: false})
775 {:ok, _} = TwitterAPI.create_status(user, %{"status" => "cofe"})
777 Pleroma.Stats.update_stats()
781 |> get("/api/v1/instance")
783 assert result = json_response(conn, 200)
785 assert result["stats"]["user_count"] == 2
786 assert result["stats"]["status_count"] == 1