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}
10 test "the home timeline", %{conn: conn} do
12 following = insert(:user)
14 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
17 |> assign(:user, user)
18 |> get("/api/v1/timelines/home")
20 assert length(json_response(conn, 200)) == 0
22 {:ok, user} = User.follow(user, following)
25 |> assign(:user, user)
26 |> get("/api/v1/timelines/home")
28 assert [%{"content" => "test"}] = json_response(conn, 200)
31 test "the public timeline", %{conn: conn} do
32 following = insert(:user)
34 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
35 {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
38 |> get("/api/v1/timelines/public")
40 assert length(json_response(conn, 200)) == 2
43 |> get("/api/v1/timelines/public", %{"local" => "True"})
45 assert [%{"content" => "test"}] = json_response(conn, 200)
48 test "posting a status", %{conn: conn} do
52 |> assign(:user, user)
53 |> post("/api/v1/statuses", %{"status" => "cofe", "spoiler_text" => "2hu"})
55 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu"} = json_response(conn, 200)
56 assert Repo.get(Activity, id)
59 test "replying to a status", %{conn: conn} do
62 {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"})
65 |> assign(:user, user)
66 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
68 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
70 activity = Repo.get(Activity, id)
72 assert activity.data["context"] == replied_to.data["context"]
73 assert activity.data["object"]["inReplyToStatusId"] == replied_to.id
76 test "verify_credentials", %{conn: conn} do
80 |> assign(:user, user)
81 |> get("/api/v1/accounts/verify_credentials")
83 assert %{"id" => id} = json_response(conn, 200)
84 assert id == to_string(user.id)
87 test "get a status", %{conn: conn} do
88 activity = insert(:note_activity)
91 |> get("/api/v1/statuses/#{activity.id}")
93 assert %{"id" => id} = json_response(conn, 200)
94 assert id == to_string(activity.id)
97 describe "deleting a status" do
98 test "when you created it", %{conn: conn} do
99 activity = insert(:note_activity)
100 author = User.get_by_ap_id(activity.data["actor"])
103 |> assign(:user, author)
104 |> delete("/api/v1/statuses/#{activity.id}")
106 assert %{} = json_response(conn, 200)
108 assert Repo.get(Activity, activity.id) == nil
111 test "when you didn't create it", %{conn: conn} do
112 activity = insert(:note_activity)
116 |> assign(:user, user)
117 |> delete("/api/v1/statuses/#{activity.id}")
119 assert %{"error" => _} = json_response(conn, 403)
121 assert Repo.get(Activity, activity.id) == activity
125 describe "notifications" do
126 test "list of notifications", %{conn: conn} do
128 other_user = insert(:user)
130 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
131 {:ok, [notification]} = Notification.create_notifications(activity)
134 |> assign(:user, user)
135 |> get("/api/v1/notifications")
137 expected_response = "hi <a href=\"#{user.ap_id}\">@#{user.nickname}</a>"
138 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
139 assert response == expected_response
142 test "getting a single notification", %{conn: conn} do
144 other_user = insert(:user)
146 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
147 {:ok, [notification]} = Notification.create_notifications(activity)
150 |> assign(:user, user)
151 |> get("/api/v1/notifications/#{notification.id}")
153 expected_response = "hi <a href=\"#{user.ap_id}\">@#{user.nickname}</a>"
154 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
155 assert response == expected_response
158 test "dismissing a single notification", %{conn: conn} do
160 other_user = insert(:user)
162 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
163 {:ok, [notification]} = Notification.create_notifications(activity)
166 |> assign(:user, user)
167 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
169 assert %{} = json_response(conn, 200)
172 test "clearing all notifications", %{conn: conn} do
174 other_user = insert(:user)
176 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
177 {:ok, [notification]} = Notification.create_notifications(activity)
180 |> assign(:user, user)
181 |> post("/api/v1/notifications/clear")
183 assert %{} = json_response(conn, 200)
186 |> assign(:user, user)
187 |> get("/api/v1/notifications")
189 assert all = json_response(conn, 200)
194 describe "reblogging" do
195 test "reblogs and returns the reblogged status", %{conn: conn} do
196 activity = insert(:note_activity)
200 |> assign(:user, user)
201 |> post("/api/v1/statuses/#{activity.id}/reblog")
203 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} = json_response(conn, 200)
204 assert to_string(activity.id) == id
208 describe "favoriting" do
209 test "favs a status and returns it", %{conn: conn} do
210 activity = insert(:note_activity)
214 |> assign(:user, user)
215 |> post("/api/v1/statuses/#{activity.id}/favourite")
217 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = json_response(conn, 200)
218 assert to_string(activity.id) == id
222 describe "unfavoriting" do
223 test "unfavorites a status and returns it", %{conn: conn} do
224 activity = insert(:note_activity)
227 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
230 |> assign(:user, user)
231 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
233 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} = json_response(conn, 200)
234 assert to_string(activity.id) == id
238 describe "user timelines" do
239 test "gets a users statuses", %{conn: conn} do
240 _note = insert(:note_activity)
241 note_two = insert(:note_activity)
243 user = User.get_by_ap_id(note_two.data["actor"])
246 |> get("/api/v1/accounts/#{user.id}/statuses")
248 assert [%{"id" => id}] = json_response(conn, 200)
250 assert id == to_string(note_two.id)
253 test "gets an users media", %{conn: conn} do
254 note = insert(:note_activity)
255 user = User.get_by_ap_id(note.data["actor"])
257 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
258 media = TwitterAPI.upload(file, "json")
261 {:ok, image_post} = TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]})
264 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
266 assert [%{"id" => id}] = json_response(conn, 200)
267 assert id == to_string(image_post.id)
270 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
272 assert [%{"id" => id}] = json_response(conn, 200)
273 assert id == to_string(image_post.id)
277 describe "user relationships" do
278 test "returns the relationships for the current user", %{conn: conn} do
280 other_user = insert(:user)
281 {:ok, user} = User.follow(user, other_user)
284 |> assign(:user, user)
285 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
287 assert [relationship] = json_response(conn, 200)
289 assert to_string(other_user.id) == relationship["id"]
293 test "account fetching", %{conn: conn} do
297 |> get("/api/v1/accounts/#{user.id}")
299 assert %{"id" => id} = json_response(conn, 200)
300 assert id == to_string(user.id)
303 |> get("/api/v1/accounts/-1")
305 assert %{"error" => "Can't find user"} = json_response(conn, 404)
308 test "media upload", %{conn: conn} do
309 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
314 |> assign(:user, user)
315 |> post("/api/v1/media", %{"file" => file})
317 assert media = json_response(conn, 200)
319 assert media["type"] == "image"
322 test "hashtag timeline", %{conn: conn} do
323 following = insert(:user)
325 {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
326 {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
329 |> get("/api/v1/timelines/tag/2hu")
331 assert [%{"id" => id}] = json_response(conn, 200)
333 assert id == to_string(activity.id)
336 test "getting followers", %{conn: conn} do
338 other_user = insert(:user)
339 {:ok, user} = User.follow(user, other_user)
342 |> get("/api/v1/accounts/#{other_user.id}/followers")
344 assert [%{"id" => id}] = json_response(conn, 200)
345 assert id == to_string(user.id)
348 test "getting following", %{conn: conn} do
350 other_user = insert(:user)
351 {:ok, user} = User.follow(user, other_user)
354 |> get("/api/v1/accounts/#{user.id}/following")
356 assert [%{"id" => id}] = json_response(conn, 200)
357 assert id == to_string(other_user.id)
360 test "following / unfollowing a user", %{conn: conn} do
362 other_user = insert(:user)
365 |> assign(:user, user)
366 |> post("/api/v1/accounts/#{other_user.id}/follow")
368 assert %{"id" => id, "following" => true} = json_response(conn, 200)
370 user = Repo.get(User, user.id)
372 |> assign(:user, user)
373 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
375 assert %{"id" => id, "following" => false} = json_response(conn, 200)
377 user = Repo.get(User, user.id)
379 |> assign(:user, user)
380 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
382 assert %{"id" => id} = json_response(conn, 200)
383 assert id == to_string(other_user.id)
386 test "blocking / unblocking a user", %{conn: conn} do
388 other_user = insert(:user)
391 |> assign(:user, user)
392 |> post("/api/v1/accounts/#{other_user.id}/block")
394 assert %{"id" => id, "blocking" => true} = json_response(conn, 200)
396 user = Repo.get(User, user.id)
398 |> assign(:user, user)
399 |> post("/api/v1/accounts/#{other_user.id}/unblock")
401 assert %{"id" => id, "blocking" => false} = json_response(conn, 200)
404 test "getting a list of blocks", %{conn: conn} do
406 other_user = insert(:user)
408 {:ok, user} = User.block(user, other_user)
411 |> assign(:user, user)
412 |> get("/api/v1/blocks")
414 other_user_id = to_string(other_user.id)
415 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
418 test "unimplemented mute endpoints" do
420 other_user = insert(:user)
423 |> Enum.each(fn(endpoint) ->
425 |> assign(:user, user)
426 |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
428 assert %{"id" => id} = json_response(conn, 200)
429 assert id == to_string(other_user.id)
433 test "unimplemented mutes, follow_requests, blocks, domain blocks" do
436 ["blocks", "domain_blocks", "mutes", "follow_requests"]
437 |> Enum.each(fn(endpoint) ->
439 |> assign(:user, user)
440 |> get("/api/v1/#{endpoint}")
442 assert [] = json_response(conn, 200)
446 test "account search", %{conn: conn} do
448 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
449 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
452 |> assign(:user, user)
453 |> get("/api/v1/accounts/search", %{"q" => "2hu"})
455 assert [account] = json_response(conn, 200)
456 assert account["id"] == to_string(user_three.id)
459 test "search", %{conn: conn} do
461 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
462 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
464 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
465 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
468 |> get("/api/v1/search", %{"q" => "2hu"})
470 assert results = json_response(conn, 200)
472 [account] = results["accounts"]
473 assert account["id"] == to_string(user_three.id)
475 assert results["hashtags"] == []
477 [status] = results["statuses"]
478 assert status["id"] == to_string(activity.id)
481 test "search fetches remote statuses", %{conn: conn} do
483 |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
484 assert results = json_response(conn, 200)
486 [status] = results["statuses"]
487 assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
490 test "search fetches remote accounts", %{conn: conn} do
492 |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
494 assert results = json_response(conn, 200)
495 [account] = results["accounts"]
496 assert account["acct"] == "shp@social.heldscal.la"
499 test "returns the favorites of a user", %{conn: conn} do
501 other_user = insert(:user)
503 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
504 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
506 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
509 |> assign(:user, user)
510 |> get("/api/v1/favourites")
512 assert [status] = json_response(conn, 200)
513 assert status["id"] == to_string(activity.id)
516 describe "updating credentials" do
517 test "updates the user's bio" do
521 |> assign(:user, user)
522 |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"})
524 assert user = json_response(conn, 200)
525 assert user["note"] == "I drink #cofe"
528 test "updates the user's name" do
532 |> assign(:user, user)
533 |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
535 assert user = json_response(conn, 200)
536 assert user["display_name"] == "markorepairs"
539 test "updates the user's avatar" do
542 new_avatar = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
545 |> assign(:user, user)
546 |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
548 assert user = json_response(conn, 200)
549 assert user["avatar"] != "https://placehold.it/48x48"
552 test "updates the user's banner" do
555 new_header = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
558 |> assign(:user, user)
559 |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
561 assert user = json_response(conn, 200)
562 assert user["header"] != "https://placehold.it/700x335"