1 defmodule Pleroma.Web.TwitterAPI.ControllerTest do
2 use Pleroma.Web.ConnCase
3 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
4 alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
5 alias Pleroma.{Repo, Activity, User, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.TwitterAPI.UserView
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.TwitterAPI.TwitterAPI
11 import Pleroma.Factory
13 describe "POST /api/account/verify_credentials" do
15 test "without valid credentials", %{conn: conn} do
16 conn = post conn, "/api/account/verify_credentials.json"
17 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
20 test "with credentials", %{conn: conn, user: user} do
22 |> with_credentials(user.nickname, "test")
23 |> post("/api/account/verify_credentials.json")
25 assert json_response(conn, 200) == UserView.render("show.json", %{user: user})
29 describe "POST /api/account/most_recent_notification" do
31 test "without valid credentials", %{conn: conn} do
32 conn = post conn, "/api/account/most_recent_notification.json"
33 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
36 test "with credentials", %{conn: conn, user: user} do
38 |> with_credentials(user.nickname, "test")
39 |> post("/api/account/most_recent_notification.json", %{id: "200"})
41 assert json_response(conn, 200)
42 user = User.get_by_nickname(user.nickname)
43 assert user.info["most_recent_notification"] == 200
47 describe "POST /statuses/update.json" do
49 test "without valid credentials", %{conn: conn} do
50 conn = post conn, "/api/statuses/update.json"
51 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
54 test "with credentials", %{conn: conn, user: user} do
55 conn_with_creds = conn |> with_credentials(user.nickname, "test")
56 request_path = "/api/statuses/update.json"
58 error_response = %{"request" => request_path,
59 "error" => "Client must provide a 'status' parameter with a value."}
60 conn = conn_with_creds |> post(request_path)
61 assert json_response(conn, 400) == error_response
63 conn = conn_with_creds |> post(request_path, %{ status: "" })
64 assert json_response(conn, 400) == error_response
66 conn = conn_with_creds |> post(request_path, %{ status: " " })
67 assert json_response(conn, 400) == error_response
69 conn = conn_with_creds |> post(request_path, %{ status: "Nice meme." })
70 assert json_response(conn, 200) == ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
74 describe "GET /statuses/public_timeline.json" do
75 test "returns statuses", %{conn: conn} do
76 {:ok, user} = UserBuilder.insert
77 activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
78 ActivityBuilder.insert_list(10, %{}, %{user: user})
79 since_id = List.last(activities).id
82 |> get("/api/statuses/public_timeline.json", %{since_id: since_id})
84 response = json_response(conn, 200)
86 assert length(response) == 10
90 describe "GET /statuses/show/:id.json" do
91 test "returns one status", %{conn: conn} do
92 {:ok, user} = UserBuilder.insert
93 {:ok, activity} = ActivityBuilder.insert(%{}, %{user: user})
94 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
97 |> get("/api/statuses/show/#{activity.id}.json")
99 response = json_response(conn, 200)
101 assert response == ActivityRepresenter.to_map(activity, %{user: actor})
105 describe "GET /users/show.json" do
106 test "gets user with screen_name", %{conn: conn} do
110 |> get("/api/users/show.json", %{"screen_name" => user.nickname})
112 response = json_response(conn, 200)
114 assert response["id"] == user.id
117 test "gets user with user_id", %{conn: conn} do
121 |> get("/api/users/show.json", %{"user_id" => user.id})
123 response = json_response(conn, 200)
125 assert response["id"] == user.id
128 test "gets a user for a logged in user", %{conn: conn} do
130 logged_in = insert(:user)
132 {:ok, logged_in, user, _activity} = TwitterAPI.follow(logged_in, %{"user_id" => user.id})
135 |> with_credentials(logged_in.nickname, "test")
136 |> get("/api/users/show.json", %{"user_id" => user.id})
138 response = json_response(conn, 200)
140 assert response["following"] == true
144 describe "GET /statusnet/conversation/:id.json" do
145 test "returns the statuses in the conversation", %{conn: conn} do
146 {:ok, _user} = UserBuilder.insert
147 {:ok, _activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
148 {:ok, _activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
149 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
151 {:ok, object} = Object.context_mapping("2hu") |> Repo.insert
153 |> get("/api/statusnet/conversation/#{object.id}.json")
155 response = json_response(conn, 200)
157 assert length(response) == 2
161 describe "GET /statuses/friends_timeline.json" do
163 test "without valid credentials", %{conn: conn} do
164 conn = get conn, "/api/statuses/friends_timeline.json"
165 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
168 test "with credentials", %{conn: conn, user: current_user} do
170 activities = ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user})
171 returned_activities = ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
172 other_user = insert(:user)
173 ActivityBuilder.insert_list(10, %{}, %{user: other_user})
174 since_id = List.last(activities).id
176 current_user = Ecto.Changeset.change(current_user, following: [User.ap_followers(user)]) |> Repo.update!
179 |> with_credentials(current_user.nickname, "test")
180 |> get("/api/statuses/friends_timeline.json", %{since_id: since_id})
182 response = json_response(conn, 200)
184 assert length(response) == 10
185 assert response == Enum.map(returned_activities, fn (activity) -> ActivityRepresenter.to_map(activity, %{user: User.get_cached_by_ap_id(activity.data["actor"]), for: current_user}) end)
189 describe "GET /statuses/mentions.json" do
191 test "without valid credentials", %{conn: conn} do
192 conn = get conn, "/api/statuses/mentions.json"
193 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
196 test "with credentials", %{conn: conn, user: current_user} do
197 {:ok, activity} = ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
200 |> with_credentials(current_user.nickname, "test")
201 |> get("/api/statuses/mentions.json")
203 response = json_response(conn, 200)
205 assert length(response) == 1
206 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user, mentioned: [current_user]})
210 describe "GET /statuses/user_timeline.json" do
212 test "without any params", %{conn: conn} do
213 conn = get(conn, "/api/statuses/user_timeline.json")
214 assert json_response(conn, 400) == %{"error" => "You need to specify screen_name or user_id", "request" => "/api/statuses/user_timeline.json"}
217 test "with user_id", %{conn: conn} do
219 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
221 conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
222 response = json_response(conn, 200)
223 assert length(response) == 1
224 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
227 test "with screen_name", %{conn: conn} do
229 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
231 conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
232 response = json_response(conn, 200)
233 assert length(response) == 1
234 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
237 test "with credentials", %{conn: conn, user: current_user} do
238 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user})
240 |> with_credentials(current_user.nickname, "test")
241 |> get("/api/statuses/user_timeline.json")
243 response = json_response(conn, 200)
245 assert length(response) == 1
246 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
249 test "with credentials with user_id", %{conn: conn, user: current_user} do
251 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
253 |> with_credentials(current_user.nickname, "test")
254 |> get("/api/statuses/user_timeline.json", %{"user_id" => user.id})
256 response = json_response(conn, 200)
258 assert length(response) == 1
259 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
262 test "with credentials screen_name", %{conn: conn, user: current_user} do
264 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
266 |> with_credentials(current_user.nickname, "test")
267 |> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
269 response = json_response(conn, 200)
271 assert length(response) == 1
272 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
276 describe "POST /friendships/create.json" do
278 test "without valid credentials", %{conn: conn} do
279 conn = post conn, "/api/friendships/create.json"
280 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
283 test "with credentials", %{conn: conn, user: current_user} do
284 followed = insert(:user)
287 |> with_credentials(current_user.nickname, "test")
288 |> post("/api/friendships/create.json", %{user_id: followed.id})
290 current_user = Repo.get(User, current_user.id)
291 assert User.ap_followers(followed) in current_user.following
292 assert json_response(conn, 200) == UserView.render("show.json", %{user: followed, for: current_user})
296 describe "POST /friendships/destroy.json" do
298 test "without valid credentials", %{conn: conn} do
299 conn = post conn, "/api/friendships/destroy.json"
300 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
303 test "with credentials", %{conn: conn, user: current_user} do
304 followed = insert(:user)
306 {:ok, current_user} = User.follow(current_user, followed)
307 assert User.ap_followers(followed) in current_user.following
308 ActivityPub.follow(current_user, followed)
311 |> with_credentials(current_user.nickname, "test")
312 |> post("/api/friendships/destroy.json", %{user_id: followed.id})
314 current_user = Repo.get(User, current_user.id)
315 assert current_user.following == [current_user.ap_id]
316 assert json_response(conn, 200) == UserView.render("show.json", %{user: followed, for: current_user})
320 describe "POST /blocks/create.json" do
322 test "without valid credentials", %{conn: conn} do
323 conn = post conn, "/api/blocks/create.json"
324 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
327 test "with credentials", %{conn: conn, user: current_user} do
328 blocked = insert(:user)
331 |> with_credentials(current_user.nickname, "test")
332 |> post("/api/blocks/create.json", %{user_id: blocked.id})
334 current_user = Repo.get(User, current_user.id)
335 assert User.blocks?(current_user, blocked)
336 assert json_response(conn, 200) == UserView.render("show.json", %{user: blocked, for: current_user})
340 describe "POST /blocks/destroy.json" do
342 test "without valid credentials", %{conn: conn} do
343 conn = post conn, "/api/blocks/destroy.json"
344 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
347 test "with credentials", %{conn: conn, user: current_user} do
348 blocked = insert(:user)
350 {:ok, current_user} = User.block(current_user, blocked)
351 assert User.blocks?(current_user, blocked)
354 |> with_credentials(current_user.nickname, "test")
355 |> post("/api/blocks/destroy.json", %{user_id: blocked.id})
357 current_user = Repo.get(User, current_user.id)
358 assert current_user.info["blocks"] == []
359 assert json_response(conn, 200) == UserView.render("show.json", %{user: blocked, for: current_user})
363 describe "GET /help/test.json" do
364 test "returns \"ok\"", %{conn: conn} do
365 conn = get conn, "/api/help/test.json"
366 assert json_response(conn, 200) == "ok"
370 describe "POST /api/qvitter/update_avatar.json" do
372 test "without valid credentials", %{conn: conn} do
373 conn = post conn, "/api/qvitter/update_avatar.json"
374 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
377 test "with credentials", %{conn: conn, user: current_user} do
379 |> with_credentials(current_user.nickname, "test")
380 |> post("/api/qvitter/update_avatar.json", %{img: Pleroma.Web.ActivityPub.ActivityPubTest.data_uri})
382 current_user = Repo.get(User, current_user.id)
383 assert is_map(current_user.avatar)
384 assert json_response(conn, 200) == UserView.render("show.json", %{user: current_user, for: current_user})
388 describe "POST /api/favorites/create/:id" do
390 test "without valid credentials", %{conn: conn} do
391 note_activity = insert(:note_activity)
392 conn = post conn, "/api/favorites/create/#{note_activity.id}.json"
393 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
396 test "with credentials", %{conn: conn, user: current_user} do
397 note_activity = insert(:note_activity)
400 |> with_credentials(current_user.nickname, "test")
401 |> post("/api/favorites/create/#{note_activity.id}.json")
403 assert json_response(conn, 200)
407 describe "POST /api/favorites/destroy/:id" do
409 test "without valid credentials", %{conn: conn} do
410 note_activity = insert(:note_activity)
411 conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json"
412 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
415 test "with credentials", %{conn: conn, user: current_user} do
416 note_activity = insert(:note_activity)
417 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
418 ActivityPub.like(current_user, object)
421 |> with_credentials(current_user.nickname, "test")
422 |> post("/api/favorites/destroy/#{note_activity.id}.json")
424 assert json_response(conn, 200)
428 describe "POST /api/statuses/retweet/:id" do
430 test "without valid credentials", %{conn: conn} do
431 note_activity = insert(:note_activity)
432 conn = post conn, "/api/statuses/retweet/#{note_activity.id}.json"
433 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
436 test "with credentials", %{conn: conn, user: current_user} do
437 note_activity = insert(:note_activity)
439 request_path = "/api/statuses/retweet/#{note_activity.id}.json"
442 |> with_credentials(current_user.nickname, "test")
443 |> post(request_path)
444 activity = Repo.get(Activity, note_activity.id)
445 activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
446 assert json_response(response, 200) == ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
450 describe "POST /api/account/register" do
451 test "it creates a new user", %{conn: conn} do
453 "nickname" => "lain",
454 "email" => "lain@wired.jp",
455 "fullname" => "lain iwakura",
456 "bio" => "close the world.",
457 "password" => "bear",
462 |> post("/api/account/register", data)
464 user = json_response(conn, 200)
466 fetched_user = Repo.get_by(User, nickname: "lain")
467 assert user == UserView.render("show.json", %{user: fetched_user})
470 test "it returns errors on a problem", %{conn: conn} do
472 "email" => "lain@wired.jp",
473 "fullname" => "lain iwakura",
474 "bio" => "close the world.",
475 "password" => "bear",
480 |> post("/api/account/register", data)
482 errors = json_response(conn, 400)
484 assert is_binary(errors["error"])
488 describe "GET /api/externalprofile/show" do
489 test "it returns the user", %{conn: conn} do
491 other_user = insert(:user)
494 |> assign(:user, user)
495 |> get("/api/externalprofile/show", %{profileurl: other_user.ap_id})
497 assert json_response(conn, 200) == UserView.render("show.json", %{user: other_user})
501 describe "GET /api/statuses/followers" do
502 test "it returns a user's followers", %{conn: conn} do
504 follower_one = insert(:user)
505 follower_two = insert(:user)
506 not_follower = insert(:user)
508 {:ok, follower_one} = User.follow(follower_one, user)
509 {:ok, follower_two} = User.follow(follower_two, user)
512 |> assign(:user, user)
513 |> get("/api/statuses/followers")
515 assert json_response(conn, 200) == UserView.render("index.json", %{users: [follower_one, follower_two], for: user})
519 describe "GET /api/statuses/friends" do
520 test "it returns a user's friends", %{conn: conn} do
522 followed_one = insert(:user)
523 followed_two = insert(:user)
524 not_followed = insert(:user)
526 {:ok, user} = User.follow(user, followed_one)
527 {:ok, user} = User.follow(user, followed_two)
530 |> assign(:user, user)
531 |> get("/api/statuses/friends")
533 assert MapSet.equal?(MapSet.new(json_response(conn, 200)), MapSet.new(UserView.render("index.json", %{users: [followed_one, followed_two], for: user})))
537 describe "GET /friends/ids" do
538 test "it returns a user's friends", %{conn: conn} do
540 followed_one = insert(:user)
541 followed_two = insert(:user)
542 not_followed = insert(:user)
544 {:ok, user} = User.follow(user, followed_one)
545 {:ok, user} = User.follow(user, followed_two)
548 |> assign(:user, user)
549 |> get("/api/friends/ids")
551 expected = [followed_one.id, followed_two.id]
552 assert MapSet.equal?(MapSet.new(Poison.decode!(json_response(conn, 200))), MapSet.new(expected))
556 describe "POST /api/account/update_profile.json" do
557 test "it updates a user's profile" do
561 |> assign(:user, user)
562 |> post("/api/account/update_profile.json", %{"name" => "new name", "description" => "new description"})
564 user = Repo.get!(User, user.id)
565 assert user.name == "new name"
566 assert user.bio == "new description"
568 assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
572 defp valid_user(_context) do
577 defp with_credentials(conn, username, password) do
578 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
579 put_req_header(conn, "authorization", header_content)
582 describe "GET /api/search.json" do
583 test "it returns search results", %{conn: conn} do
585 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
587 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
588 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
591 |> get("/api/search.json", %{"q" => "2hu", "page" => "1", "rpp" => "1"})
593 assert [status] = json_response(conn, 200)
594 assert status["id"] == activity.id
598 describe "GET /api/statusnet/tags/timeline/:tag.json" do
599 test "it returns the tags timeline" do
601 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
603 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about #2hu"})
604 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
607 |> get("/api/statusnet/tags/timeline/2hu.json")
609 assert [status] = json_response(conn, 200)
610 assert status["id"] == activity.id