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
11 describe "POST /api/account/verify_credentials" do
13 test "without valid credentials", %{conn: conn} do
14 conn = post conn, "/api/account/verify_credentials.json"
15 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
18 test "with credentials", %{conn: conn, user: user} do
20 |> with_credentials(user.nickname, "test")
21 |> post("/api/account/verify_credentials.json")
23 assert json_response(conn, 200) == UserView.render("show.json", %{user: user})
27 describe "POST /api/account/most_recent_notification" do
29 test "without valid credentials", %{conn: conn} do
30 conn = post conn, "/api/account/most_recent_notification.json"
31 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
34 test "with credentials", %{conn: conn, user: user} do
36 |> with_credentials(user.nickname, "test")
37 |> post("/api/account/most_recent_notification.json", %{id: "200"})
39 assert json_response(conn, 200)
40 user = User.get_by_nickname(user.nickname)
41 assert user.info["most_recent_notification"] == 200
45 describe "POST /statuses/update.json" do
47 test "without valid credentials", %{conn: conn} do
48 conn = post conn, "/api/statuses/update.json"
49 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
52 test "with credentials", %{conn: conn, user: user} do
53 conn_with_creds = conn |> with_credentials(user.nickname, "test")
54 request_path = "/api/statuses/update.json"
56 error_response = %{"request" => request_path,
57 "error" => "Client must provide a 'status' parameter with a value."}
58 conn = conn_with_creds |> post(request_path)
59 assert json_response(conn, 400) == error_response
61 conn = conn_with_creds |> post(request_path, %{ status: "" })
62 assert json_response(conn, 400) == error_response
64 conn = conn_with_creds |> post(request_path, %{ status: " " })
65 assert json_response(conn, 400) == error_response
67 conn = conn_with_creds |> post(request_path, %{ status: "Nice meme." })
68 assert json_response(conn, 200) == ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
72 describe "GET /statuses/public_timeline.json" do
73 test "returns statuses", %{conn: conn} do
74 {:ok, user} = UserBuilder.insert
75 activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
76 ActivityBuilder.insert_list(10, %{}, %{user: user})
77 since_id = List.last(activities).id
80 |> get("/api/statuses/public_timeline.json", %{since_id: since_id})
82 response = json_response(conn, 200)
84 assert length(response) == 10
88 describe "GET /statuses/show/:id.json" do
89 test "returns one status", %{conn: conn} do
90 {:ok, user} = UserBuilder.insert
91 {:ok, activity} = ActivityBuilder.insert(%{}, %{user: user})
92 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
95 |> get("/api/statuses/show/#{activity.id}.json")
97 response = json_response(conn, 200)
99 assert response == ActivityRepresenter.to_map(activity, %{user: actor})
103 describe "GET /statusnet/conversation/:id.json" do
104 test "returns the statuses in the conversation", %{conn: conn} do
105 {:ok, _user} = UserBuilder.insert
106 {:ok, _activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
107 {:ok, _activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
108 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
110 {:ok, object} = Object.context_mapping("2hu") |> Repo.insert
112 |> get("/api/statusnet/conversation/#{object.id}.json")
114 response = json_response(conn, 200)
116 assert length(response) == 2
120 describe "GET /statuses/friends_timeline.json" do
122 test "without valid credentials", %{conn: conn} do
123 conn = get conn, "/api/statuses/friends_timeline.json"
124 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
127 test "with credentials", %{conn: conn, user: current_user} do
129 activities = ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user})
130 returned_activities = ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
131 other_user = insert(:user)
132 ActivityBuilder.insert_list(10, %{}, %{user: other_user})
133 since_id = List.last(activities).id
135 current_user = Ecto.Changeset.change(current_user, following: [User.ap_followers(user)]) |> Repo.update!
138 |> with_credentials(current_user.nickname, "test")
139 |> get("/api/statuses/friends_timeline.json", %{since_id: since_id})
141 response = json_response(conn, 200)
143 assert length(response) == 10
144 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)
148 describe "GET /statuses/mentions.json" do
150 test "without valid credentials", %{conn: conn} do
151 conn = get conn, "/api/statuses/mentions.json"
152 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
155 test "with credentials", %{conn: conn, user: current_user} do
156 {:ok, activity} = ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
159 |> with_credentials(current_user.nickname, "test")
160 |> get("/api/statuses/mentions.json")
162 response = json_response(conn, 200)
164 assert length(response) == 1
165 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user, mentioned: [current_user]})
169 describe "GET /statuses/user_timeline.json" do
171 test "without any params", %{conn: conn} do
172 conn = get(conn, "/api/statuses/user_timeline.json")
173 assert json_response(conn, 400) == %{"error" => "You need to specify screen_name or user_id", "request" => "/api/statuses/user_timeline.json"}
176 test "with user_id", %{conn: conn} do
178 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
180 conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
181 response = json_response(conn, 200)
182 assert length(response) == 1
183 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
186 test "with screen_name", %{conn: conn} do
188 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
190 conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
191 response = json_response(conn, 200)
192 assert length(response) == 1
193 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
196 test "with credentials", %{conn: conn, user: current_user} do
197 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user})
199 |> with_credentials(current_user.nickname, "test")
200 |> get("/api/statuses/user_timeline.json")
202 response = json_response(conn, 200)
204 assert length(response) == 1
205 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
208 test "with credentials with user_id", %{conn: conn, user: current_user} do
210 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
212 |> with_credentials(current_user.nickname, "test")
213 |> get("/api/statuses/user_timeline.json", %{"user_id" => user.id})
215 response = json_response(conn, 200)
217 assert length(response) == 1
218 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
221 test "with credentials screen_name", %{conn: conn, user: current_user} do
223 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
225 |> with_credentials(current_user.nickname, "test")
226 |> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
228 response = json_response(conn, 200)
230 assert length(response) == 1
231 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
235 describe "POST /friendships/create.json" do
237 test "without valid credentials", %{conn: conn} do
238 conn = post conn, "/api/friendships/create.json"
239 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
242 test "with credentials", %{conn: conn, user: current_user} do
243 followed = insert(:user)
246 |> with_credentials(current_user.nickname, "test")
247 |> post("/api/friendships/create.json", %{user_id: followed.id})
249 current_user = Repo.get(User, current_user.id)
250 assert current_user.following == [User.ap_followers(followed)]
251 assert json_response(conn, 200) == UserView.render("show.json", %{user: followed, for: current_user})
255 describe "POST /friendships/destroy.json" do
257 test "without valid credentials", %{conn: conn} do
258 conn = post conn, "/api/friendships/destroy.json"
259 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
262 test "with credentials", %{conn: conn, user: current_user} do
263 followed = insert(:user)
265 {:ok, current_user} = User.follow(current_user, followed)
266 assert current_user.following == [User.ap_followers(followed)]
267 ActivityPub.follow(current_user, followed)
270 |> with_credentials(current_user.nickname, "test")
271 |> post("/api/friendships/destroy.json", %{user_id: followed.id})
273 current_user = Repo.get(User, current_user.id)
274 assert current_user.following == []
275 assert json_response(conn, 200) == UserView.render("show.json", %{user: followed, for: current_user})
279 describe "GET /help/test.json" do
280 test "returns \"ok\"", %{conn: conn} do
281 conn = get conn, "/api/help/test.json"
282 assert json_response(conn, 200) == "ok"
286 describe "POST /api/qvitter/update_avatar.json" do
288 test "without valid credentials", %{conn: conn} do
289 conn = post conn, "/api/qvitter/update_avatar.json"
290 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
293 test "with credentials", %{conn: conn, user: current_user} do
295 |> with_credentials(current_user.nickname, "test")
296 |> post("/api/qvitter/update_avatar.json", %{img: Pleroma.Web.ActivityPub.ActivityPubTest.data_uri})
298 current_user = Repo.get(User, current_user.id)
299 assert is_map(current_user.avatar)
300 assert json_response(conn, 200) == UserView.render("show.json", %{user: current_user, for: current_user})
304 describe "POST /api/favorites/create/:id" do
306 test "without valid credentials", %{conn: conn} do
307 note_activity = insert(:note_activity)
308 conn = post conn, "/api/favorites/create/#{note_activity.id}.json"
309 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
312 test "with credentials", %{conn: conn, user: current_user} do
313 note_activity = insert(:note_activity)
316 |> with_credentials(current_user.nickname, "test")
317 |> post("/api/favorites/create/#{note_activity.id}.json")
319 assert json_response(conn, 200)
323 describe "POST /api/favorites/destroy/:id" do
325 test "without valid credentials", %{conn: conn} do
326 note_activity = insert(:note_activity)
327 conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json"
328 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
331 test "with credentials", %{conn: conn, user: current_user} do
332 note_activity = insert(:note_activity)
333 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
334 ActivityPub.like(current_user, object)
337 |> with_credentials(current_user.nickname, "test")
338 |> post("/api/favorites/destroy/#{note_activity.id}.json")
340 assert json_response(conn, 200)
344 describe "POST /api/statuses/retweet/:id" do
346 test "without valid credentials", %{conn: conn} do
347 note_activity = insert(:note_activity)
348 conn = post conn, "/api/statuses/retweet/#{note_activity.id}.json"
349 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
352 test "with credentials", %{conn: conn, user: current_user} do
353 note_activity = insert(:note_activity)
355 request_path = "/api/statuses/retweet/#{note_activity.id}.json"
357 user = Repo.get_by(User, ap_id: note_activity.data["actor"])
359 |> with_credentials(user.nickname, "test")
360 |> post(request_path)
361 assert json_response(response, 400) == %{"error" => "You cannot repeat your own notice.",
362 "request" => request_path}
365 |> with_credentials(current_user.nickname, "test")
366 |> post(request_path)
367 activity = Repo.get(Activity, note_activity.id)
368 activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
369 assert json_response(response, 200) == ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
373 describe "POST /api/account/register" do
374 test "it creates a new user", %{conn: conn} do
376 "nickname" => "lain",
377 "email" => "lain@wired.jp",
378 "fullname" => "lain iwakura",
379 "bio" => "close the world.",
380 "password" => "bear",
385 |> post("/api/account/register", data)
387 user = json_response(conn, 200)
389 fetched_user = Repo.get_by(User, nickname: "lain")
390 assert user == UserView.render("show.json", %{user: fetched_user})
393 test "it returns errors on a problem", %{conn: conn} do
395 "email" => "lain@wired.jp",
396 "fullname" => "lain iwakura",
397 "bio" => "close the world.",
398 "password" => "bear",
403 |> post("/api/account/register", data)
405 errors = json_response(conn, 400)
407 assert is_binary(errors["error"])
411 describe "GET /api/externalprofile/show" do
412 test "it returns the user", %{conn: conn} do
416 |> get("/api/externalprofile/show", %{profileurl: user.ap_id})
418 assert json_response(conn, 200) == UserView.render("show.json", %{user: user})
422 describe "GET /api/statuses/followers" do
423 test "it returns a user's followers", %{conn: conn} do
425 follower_one = insert(:user)
426 follower_two = insert(:user)
427 not_follower = insert(:user)
429 {:ok, follower_one} = User.follow(follower_one, user)
430 {:ok, follower_two} = User.follow(follower_two, user)
433 |> assign(:user, user)
434 |> get("/api/statuses/followers")
436 assert json_response(conn, 200) == UserView.render("index.json", %{users: [follower_one, follower_two], for: user})
440 describe "GET /api/statuses/friends" do
441 test "it returns a user's friends", %{conn: conn} do
443 followed_one = insert(:user)
444 followed_two = insert(:user)
445 not_followed = insert(:user)
447 {:ok, user} = User.follow(user, followed_one)
448 {:ok, user} = User.follow(user, followed_two)
451 |> assign(:user, user)
452 |> get("/api/statuses/friends")
454 assert json_response(conn, 200) == UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
458 describe "POST /api/account/update_profile.json" do
459 test "it updates a user's profile" do
463 |> assign(:user, user)
464 |> post("/api/account/update_profile.json", %{"name" => "new name", "description" => "new description"})
466 user = Repo.get!(User, user.id)
467 assert user.name == "new name"
468 assert user.bio == "new description"
470 assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
474 defp valid_user(_context) do
479 defp with_credentials(conn, username, password) do
480 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
481 put_req_header(conn, "authorization", header_content)