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 %{"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)
254 describe "user relationships" do
255 test "returns the relationships for the current user", %{conn: conn} do
257 other_user = insert(:user)
258 {:ok, user} = User.follow(user, other_user)
261 |> assign(:user, user)
262 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
264 assert [relationship] = json_response(conn, 200)
266 assert to_string(other_user.id) == relationship["id"]
270 test "account fetching", %{conn: conn} do
274 |> get("/api/v1/accounts/#{user.id}")
276 assert %{"id" => id} = json_response(conn, 200)
277 assert id == to_string(user.id)
280 |> get("/api/v1/accounts/-1")
282 assert %{"error" => "Can't find user"} = json_response(conn, 404)
285 test "media upload", %{conn: conn} do
286 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
291 |> assign(:user, user)
292 |> post("/api/v1/media", %{"file" => file})
294 assert media = json_response(conn, 200)
296 assert media["type"] == "image"
299 test "hashtag timeline", %{conn: conn} do
300 following = insert(:user)
302 {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
303 {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
306 |> get("/api/v1/timelines/tag/2hu")
308 assert [%{"id" => id}] = json_response(conn, 200)
310 assert id == to_string(activity.id)
313 test "getting followers", %{conn: conn} do
315 other_user = insert(:user)
316 {:ok, user} = User.follow(user, other_user)
319 |> get("/api/v1/accounts/#{other_user.id}/followers")
321 assert [%{"id" => id}] = json_response(conn, 200)
322 assert id == to_string(user.id)
325 test "getting following", %{conn: conn} do
327 other_user = insert(:user)
328 {:ok, user} = User.follow(user, other_user)
331 |> get("/api/v1/accounts/#{user.id}/following")
333 assert [%{"id" => id}] = json_response(conn, 200)
334 assert id == to_string(other_user.id)
337 test "following / unfollowing a user", %{conn: conn} do
339 other_user = insert(:user)
342 |> assign(:user, user)
343 |> post("/api/v1/accounts/#{other_user.id}/follow")
345 assert %{"id" => id, "following" => true} = json_response(conn, 200)
347 user = Repo.get(User, user.id)
349 |> assign(:user, user)
350 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
352 assert %{"id" => id, "following" => false} = json_response(conn, 200)
354 user = Repo.get(User, user.id)
356 |> assign(:user, user)
357 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
359 assert %{"id" => id} = json_response(conn, 200)
360 assert id == to_string(other_user.id)
363 test "blocking / unblocking a user", %{conn: conn} do
365 other_user = insert(:user)
368 |> assign(:user, user)
369 |> post("/api/v1/accounts/#{other_user.id}/block")
371 assert %{"id" => id, "blocking" => true} = json_response(conn, 200)
373 user = Repo.get(User, user.id)
375 |> assign(:user, user)
376 |> post("/api/v1/accounts/#{other_user.id}/unblock")
378 assert %{"id" => id, "blocking" => false} = json_response(conn, 200)
381 test "getting a list of blocks", %{conn: conn} do
383 other_user = insert(:user)
385 {:ok, user} = User.block(user, other_user)
388 |> assign(:user, user)
389 |> get("/api/v1/blocks")
391 other_user_id = to_string(other_user.id)
392 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
395 test "unimplemented mute endpoints" do
397 other_user = insert(:user)
400 |> Enum.each(fn(endpoint) ->
402 |> assign(:user, user)
403 |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
405 assert %{"id" => id} = json_response(conn, 200)
406 assert id == to_string(other_user.id)
410 test "unimplemented mutes, follow_requests, blocks, domain blocks" do
413 ["blocks", "domain_blocks", "mutes", "follow_requests"]
414 |> Enum.each(fn(endpoint) ->
416 |> assign(:user, user)
417 |> get("/api/v1/#{endpoint}")
419 assert [] = json_response(conn, 200)
423 test "account search", %{conn: conn} do
425 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
426 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
429 |> assign(:user, user)
430 |> get("/api/v1/accounts/search", %{"q" => "2hu"})
432 assert [account] = json_response(conn, 200)
433 assert account["id"] == to_string(user_three.id)
436 test "search", %{conn: conn} do
438 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
439 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
441 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
442 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
445 |> get("/api/v1/search", %{"q" => "2hu"})
447 assert results = json_response(conn, 200)
449 [account] = results["accounts"]
450 assert account["id"] == to_string(user_three.id)
452 assert results["hashtags"] == []
454 [status] = results["statuses"]
455 assert status["id"] == to_string(activity.id)
458 test "search fetches remote statuses", %{conn: conn} do
460 |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
461 assert results = json_response(conn, 200)
463 [status] = results["statuses"]
464 assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
467 test "search fetches remote accounts", %{conn: conn} do
469 |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
471 assert results = json_response(conn, 200)
472 [account] = results["accounts"]
473 assert account["acct"] == "shp@social.heldscal.la"
476 test "returns the favorites of a user", %{conn: conn} do
478 other_user = insert(:user)
480 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
481 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
483 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
486 |> assign(:user, user)
487 |> get("/api/v1/favourites")
489 assert [status] = json_response(conn, 200)
490 assert status["id"] == to_string(activity.id)
493 describe "updating credentials" do
494 test "updates the user's bio" do
498 |> assign(:user, user)
499 |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"})
501 assert user = json_response(conn, 200)
502 assert user["note"] == "I drink #cofe"
505 test "updates the user's name" do
509 |> assign(:user, user)
510 |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
512 assert user = json_response(conn, 200)
513 assert user["display_name"] == "markorepairs"
516 test "updates the user's avatar" do
519 new_avatar = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
522 |> assign(:user, user)
523 |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
525 assert user = json_response(conn, 200)
526 assert user["avatar"] != "https://placehold.it/48x48"
529 test "updates the user's banner" do
532 new_header = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
535 |> assign(:user, user)
536 |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
538 assert user = json_response(conn, 200)
539 assert user["header"] != "https://placehold.it/700x335"