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", %{"local" => "False"})
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 |> get("/api/v1/timelines/public", %{"local" => "1"})
50 assert [%{"content" => "test"}] = json_response(conn, 200)
53 test "posting a status", %{conn: conn} do
57 |> assign(:user, user)
58 |> post("/api/v1/statuses", %{"status" => "cofe", "spoiler_text" => "2hu", "sensitive" => "false"})
60 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = json_response(conn, 200)
61 assert Repo.get(Activity, id)
64 test "posting a sensitive status", %{conn: conn} do
68 |> assign(:user, user)
69 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
71 assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
72 assert Repo.get(Activity, id)
75 test "replying to a status", %{conn: conn} do
78 {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"})
81 |> assign(:user, user)
82 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
84 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
86 activity = Repo.get(Activity, id)
88 assert activity.data["context"] == replied_to.data["context"]
89 assert activity.data["object"]["inReplyToStatusId"] == replied_to.id
92 test "verify_credentials", %{conn: conn} do
96 |> assign(:user, user)
97 |> get("/api/v1/accounts/verify_credentials")
99 assert %{"id" => id} = json_response(conn, 200)
100 assert id == to_string(user.id)
103 test "get a status", %{conn: conn} do
104 activity = insert(:note_activity)
107 |> get("/api/v1/statuses/#{activity.id}")
109 assert %{"id" => id} = json_response(conn, 200)
110 assert id == to_string(activity.id)
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"])
119 |> assign(:user, author)
120 |> delete("/api/v1/statuses/#{activity.id}")
122 assert %{} = json_response(conn, 200)
124 assert Repo.get(Activity, activity.id) == nil
127 test "when you didn't create it", %{conn: conn} do
128 activity = insert(:note_activity)
132 |> assign(:user, user)
133 |> delete("/api/v1/statuses/#{activity.id}")
135 assert %{"error" => _} = json_response(conn, 403)
137 assert Repo.get(Activity, activity.id) == activity
141 describe "notifications" do
142 test "list of notifications", %{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")
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
158 test "getting 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 |> get("/api/v1/notifications/#{notification.id}")
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
174 test "dismissing a single notification", %{conn: conn} do
176 other_user = insert(:user)
178 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
179 {:ok, [notification]} = Notification.create_notifications(activity)
182 |> assign(:user, user)
183 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
185 assert %{} = json_response(conn, 200)
188 test "clearing all notifications", %{conn: conn} do
190 other_user = insert(:user)
192 {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
193 {:ok, [notification]} = Notification.create_notifications(activity)
196 |> assign(:user, user)
197 |> post("/api/v1/notifications/clear")
199 assert %{} = json_response(conn, 200)
202 |> assign(:user, user)
203 |> get("/api/v1/notifications")
205 assert all = json_response(conn, 200)
210 describe "reblogging" do
211 test "reblogs and returns the reblogged status", %{conn: conn} do
212 activity = insert(:note_activity)
216 |> assign(:user, user)
217 |> post("/api/v1/statuses/#{activity.id}/reblog")
219 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} = json_response(conn, 200)
220 assert to_string(activity.id) == id
224 describe "favoriting" do
225 test "favs a status and returns it", %{conn: conn} do
226 activity = insert(:note_activity)
230 |> assign(:user, user)
231 |> post("/api/v1/statuses/#{activity.id}/favourite")
233 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = json_response(conn, 200)
234 assert to_string(activity.id) == id
238 describe "unfavoriting" do
239 test "unfavorites a status and returns it", %{conn: conn} do
240 activity = insert(:note_activity)
243 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
246 |> assign(:user, user)
247 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
249 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} = json_response(conn, 200)
250 assert to_string(activity.id) == id
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)
259 user = User.get_by_ap_id(note_two.data["actor"])
262 |> get("/api/v1/accounts/#{user.id}/statuses")
264 assert [%{"id" => id}] = json_response(conn, 200)
266 assert id == to_string(note_two.id)
269 test "gets an users media", %{conn: conn} do
270 note = insert(:note_activity)
271 user = User.get_by_ap_id(note.data["actor"])
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")
277 {:ok, image_post} = TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]})
280 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
282 assert [%{"id" => id}] = json_response(conn, 200)
283 assert id == to_string(image_post.id)
286 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
288 assert [%{"id" => id}] = json_response(conn, 200)
289 assert id == to_string(image_post.id)
293 describe "user relationships" do
294 test "returns the relationships for the current user", %{conn: conn} do
296 other_user = insert(:user)
297 {:ok, user} = User.follow(user, other_user)
300 |> assign(:user, user)
301 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
303 assert [relationship] = json_response(conn, 200)
305 assert to_string(other_user.id) == relationship["id"]
309 test "account fetching", %{conn: conn} do
313 |> get("/api/v1/accounts/#{user.id}")
315 assert %{"id" => id} = json_response(conn, 200)
316 assert id == to_string(user.id)
319 |> get("/api/v1/accounts/-1")
321 assert %{"error" => "Can't find user"} = json_response(conn, 404)
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"}
330 |> assign(:user, user)
331 |> post("/api/v1/media", %{"file" => file})
333 assert media = json_response(conn, 200)
335 assert media["type"] == "image"
338 test "hashtag timeline", %{conn: conn} do
339 following = insert(:user)
341 {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
342 {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
345 |> get("/api/v1/timelines/tag/2hu")
347 assert [%{"id" => id}] = json_response(conn, 200)
349 assert id == to_string(activity.id)
352 test "getting followers", %{conn: conn} do
354 other_user = insert(:user)
355 {:ok, user} = User.follow(user, other_user)
358 |> get("/api/v1/accounts/#{other_user.id}/followers")
360 assert [%{"id" => id}] = json_response(conn, 200)
361 assert id == to_string(user.id)
364 test "getting following", %{conn: conn} do
366 other_user = insert(:user)
367 {:ok, user} = User.follow(user, other_user)
370 |> get("/api/v1/accounts/#{user.id}/following")
372 assert [%{"id" => id}] = json_response(conn, 200)
373 assert id == to_string(other_user.id)
376 test "following / unfollowing a user", %{conn: conn} do
378 other_user = insert(:user)
381 |> assign(:user, user)
382 |> post("/api/v1/accounts/#{other_user.id}/follow")
384 assert %{"id" => id, "following" => true} = json_response(conn, 200)
386 user = Repo.get(User, user.id)
388 |> assign(:user, user)
389 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
391 assert %{"id" => id, "following" => false} = json_response(conn, 200)
393 user = Repo.get(User, user.id)
395 |> assign(:user, user)
396 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
398 assert %{"id" => id} = json_response(conn, 200)
399 assert id == to_string(other_user.id)
402 test "blocking / unblocking a user", %{conn: conn} do
404 other_user = insert(:user)
407 |> assign(:user, user)
408 |> post("/api/v1/accounts/#{other_user.id}/block")
410 assert %{"id" => id, "blocking" => true} = json_response(conn, 200)
412 user = Repo.get(User, user.id)
414 |> assign(:user, user)
415 |> post("/api/v1/accounts/#{other_user.id}/unblock")
417 assert %{"id" => id, "blocking" => false} = json_response(conn, 200)
420 test "getting a list of blocks", %{conn: conn} do
422 other_user = insert(:user)
424 {:ok, user} = User.block(user, other_user)
427 |> assign(:user, user)
428 |> get("/api/v1/blocks")
430 other_user_id = to_string(other_user.id)
431 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
434 test "unimplemented mute endpoints" do
436 other_user = insert(:user)
439 |> Enum.each(fn(endpoint) ->
441 |> assign(:user, user)
442 |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
444 assert %{"id" => id} = json_response(conn, 200)
445 assert id == to_string(other_user.id)
449 test "unimplemented mutes, follow_requests, blocks, domain blocks" do
452 ["blocks", "domain_blocks", "mutes", "follow_requests"]
453 |> Enum.each(fn(endpoint) ->
455 |> assign(:user, user)
456 |> get("/api/v1/#{endpoint}")
458 assert [] = json_response(conn, 200)
462 test "account search", %{conn: conn} do
464 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
465 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
468 |> assign(:user, user)
469 |> get("/api/v1/accounts/search", %{"q" => "2hu"})
471 assert [account] = json_response(conn, 200)
472 assert account["id"] == to_string(user_three.id)
475 test "search", %{conn: conn} do
477 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
478 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
480 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
481 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
484 |> get("/api/v1/search", %{"q" => "2hu"})
486 assert results = json_response(conn, 200)
488 [account] = results["accounts"]
489 assert account["id"] == to_string(user_three.id)
491 assert results["hashtags"] == []
493 [status] = results["statuses"]
494 assert status["id"] == to_string(activity.id)
497 test "search fetches remote statuses", %{conn: conn} do
499 |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
500 assert results = json_response(conn, 200)
502 [status] = results["statuses"]
503 assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
506 test "search fetches remote accounts", %{conn: conn} do
508 |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
510 assert results = json_response(conn, 200)
511 [account] = results["accounts"]
512 assert account["acct"] == "shp@social.heldscal.la"
515 test "returns the favorites of a user", %{conn: conn} do
517 other_user = insert(:user)
519 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
520 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
522 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
525 |> assign(:user, user)
526 |> get("/api/v1/favourites")
528 assert [status] = json_response(conn, 200)
529 assert status["id"] == to_string(activity.id)
532 describe "updating credentials" do
533 test "updates the user's bio" do
537 |> assign(:user, user)
538 |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"})
540 assert user = json_response(conn, 200)
541 assert user["note"] == "I drink #cofe"
544 test "updates the user's name" do
548 |> assign(:user, user)
549 |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
551 assert user = json_response(conn, 200)
552 assert user["display_name"] == "markorepairs"
555 test "updates the user's avatar" do
558 new_avatar = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
561 |> assign(:user, user)
562 |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
564 assert user = json_response(conn, 200)
565 assert user["avatar"] != "https://placehold.it/48x48"
568 test "updates the user's banner" do
571 new_header = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
574 |> assign(:user, user)
575 |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
577 assert user = json_response(conn, 200)
578 assert user["header"] != "https://placehold.it/700x335"
582 test "get instance information" do
583 insert(:user, %{local: true})
584 user = insert(:user, %{local: true})
585 insert(:user, %{local: false})
587 {:ok, _} = TwitterAPI.create_status(user, %{"status" => "cofe"})
589 Pleroma.Stats.update_stats()
592 |> get("/api/v1/instance")
594 assert result = json_response(conn, 200)
596 assert result["stats"]["user_count"] == 2
597 assert result["stats"]["status_count"] == 1