1 defmodule Pleroma.Web.TwitterAPI.ControllerTest do
2 use Pleroma.Web.ConnCase
3 alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
4 alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
5 alias Pleroma.{Repo, Activity, User, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
10 describe "POST /api/account/verify_credentials" do
12 test "without valid credentials", %{conn: conn} do
13 conn = post conn, "/api/account/verify_credentials.json"
14 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
17 test "with credentials", %{conn: conn, user: user} do
19 |> with_credentials(user.nickname, "test")
20 |> post("/api/account/verify_credentials.json")
22 assert json_response(conn, 200) == UserRepresenter.to_map(user)
26 describe "POST /statuses/update.json" do
28 test "without valid credentials", %{conn: conn} do
29 conn = post conn, "/api/statuses/update.json"
30 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
33 test "with credentials", %{conn: conn, user: user} do
34 conn_with_creds = conn |> with_credentials(user.nickname, "test")
35 request_path = "/api/statuses/update.json"
37 error_response = %{"request" => request_path,
38 "error" => "Client must provide a 'status' parameter with a value."}
39 conn = conn_with_creds |> post(request_path)
40 assert json_response(conn, 400) == error_response
42 conn = conn_with_creds |> post(request_path, %{ status: "" })
43 assert json_response(conn, 400) == error_response
45 conn = conn_with_creds |> post(request_path, %{ status: "Nice meme." })
46 assert json_response(conn, 200) == ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
50 describe "GET /statuses/public_timeline.json" do
51 test "returns statuses", %{conn: conn} do
52 {:ok, user} = UserBuilder.insert
53 activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
54 ActivityBuilder.insert_list(10, %{}, %{user: user})
55 since_id = List.last(activities).id
58 |> get("/api/statuses/public_timeline.json", %{since_id: since_id})
60 response = json_response(conn, 200)
62 assert length(response) == 10
66 describe "GET /statuses/show/:id.json" do
67 test "returns one status", %{conn: conn} do
68 {:ok, user} = UserBuilder.insert
69 {:ok, activity} = ActivityBuilder.insert(%{}, %{user: user})
70 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
73 |> get("/api/statuses/show/#{activity.id}.json")
75 response = json_response(conn, 200)
77 assert response == ActivityRepresenter.to_map(activity, %{user: actor})
81 describe "GET /statusnet/conversation/:id.json" do
82 test "returns the statuses in the conversation", %{conn: conn} do
83 {:ok, _user} = UserBuilder.insert
84 {:ok, _activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"})
85 {:ok, _activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"})
86 {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
89 |> get("/api/statusnet/conversation/1.json")
91 response = json_response(conn, 200)
93 assert length(response) == 2
97 describe "GET /statuses/friends_timeline.json" do
99 test "without valid credentials", %{conn: conn} do
100 conn = get conn, "/api/statuses/friends_timeline.json"
101 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
104 test "with credentials", %{conn: conn, user: current_user} do
106 activities = ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user})
107 returned_activities = ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
108 other_user = insert(:user)
109 ActivityBuilder.insert_list(10, %{}, %{user: other_user})
110 since_id = List.last(activities).id
112 current_user = Ecto.Changeset.change(current_user, following: [User.ap_followers(user)]) |> Repo.update!
115 |> with_credentials(current_user.nickname, "test")
116 |> get("/api/statuses/friends_timeline.json", %{since_id: since_id})
118 response = json_response(conn, 200)
120 assert length(response) == 10
121 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)
125 describe "GET /statuses/mentions.json" do
127 test "without valid credentials", %{conn: conn} do
128 conn = get conn, "/api/statuses/mentions.json"
129 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
132 test "with credentials", %{conn: conn, user: current_user} do
133 {:ok, activity} = ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
136 |> with_credentials(current_user.nickname, "test")
137 |> get("/api/statuses/mentions.json")
139 response = json_response(conn, 200)
141 assert length(response) == 1
142 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user, mentioned: [current_user]})
146 describe "GET /statuses/user_timeline.json" do
148 test "without any params", %{conn: conn} do
149 conn = get(conn, "/api/statuses/user_timeline.json")
150 assert json_response(conn, 400) == %{"error" => "You need to specify screen_name or user_id", "request" => "/api/statuses/user_timeline.json"}
153 test "with user_id", %{conn: conn} do
155 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
157 conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
158 response = json_response(conn, 200)
159 assert length(response) == 1
160 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
163 test "with screen_name", %{conn: conn} do
165 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
167 conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
168 response = json_response(conn, 200)
169 assert length(response) == 1
170 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
173 test "with credentials", %{conn: conn, user: current_user} do
174 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user})
176 |> with_credentials(current_user.nickname, "test")
177 |> get("/api/statuses/user_timeline.json")
179 response = json_response(conn, 200)
181 assert length(response) == 1
182 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
185 test "with credentials with user_id", %{conn: conn, user: current_user} do
187 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
189 |> with_credentials(current_user.nickname, "test")
190 |> get("/api/statuses/user_timeline.json", %{"user_id" => user.id})
192 response = json_response(conn, 200)
194 assert length(response) == 1
195 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
198 test "with credentials screen_name", %{conn: conn, user: current_user} do
200 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
202 |> with_credentials(current_user.nickname, "test")
203 |> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
205 response = json_response(conn, 200)
207 assert length(response) == 1
208 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
212 describe "POST /friendships/create.json" do
214 test "without valid credentials", %{conn: conn} do
215 conn = post conn, "/api/friendships/create.json"
216 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
219 test "with credentials", %{conn: conn, user: current_user} do
220 followed = insert(:user)
223 |> with_credentials(current_user.nickname, "test")
224 |> post("/api/friendships/create.json", %{user_id: followed.id})
226 current_user = Repo.get(User, current_user.id)
227 assert current_user.following == [User.ap_followers(followed)]
228 assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user})
232 describe "POST /friendships/destroy.json" do
234 test "without valid credentials", %{conn: conn} do
235 conn = post conn, "/api/friendships/destroy.json"
236 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
239 test "with credentials", %{conn: conn, user: current_user} do
240 followed = insert(:user)
242 {:ok, current_user} = User.follow(current_user, followed)
243 assert current_user.following == [User.ap_followers(followed)]
246 |> with_credentials(current_user.nickname, "test")
247 |> post("/api/friendships/destroy.json", %{user_id: followed.id})
249 current_user = Repo.get(User, current_user.id)
250 assert current_user.following == []
251 assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user})
255 describe "GET /help/test.json" do
256 test "returns \"ok\"", %{conn: conn} do
257 conn = get conn, "/api/help/test.json"
258 assert json_response(conn, 200) == "ok"
262 describe "POST /api/qvitter/update_avatar.json" do
264 test "without valid credentials", %{conn: conn} do
265 conn = post conn, "/api/qvitter/update_avatar.json"
266 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
269 test "with credentials", %{conn: conn, user: current_user} do
271 |> with_credentials(current_user.nickname, "test")
272 |> post("/api/qvitter/update_avatar.json", %{img: Pleroma.Web.ActivityPub.ActivityPubTest.data_uri})
274 current_user = Repo.get(User, current_user.id)
275 assert is_map(current_user.avatar)
276 assert json_response(conn, 200) == UserRepresenter.to_map(current_user, %{for: current_user})
280 describe "POST /api/favorites/create/:id" do
282 test "without valid credentials", %{conn: conn} do
283 note_activity = insert(:note_activity)
284 conn = post conn, "/api/favorites/create/#{note_activity.id}.json"
285 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
288 test "with credentials", %{conn: conn, user: current_user} do
289 note_activity = insert(:note_activity)
292 |> with_credentials(current_user.nickname, "test")
293 |> post("/api/favorites/create/#{note_activity.id}.json")
295 assert json_response(conn, 200)
299 describe "POST /api/favorites/destroy/:id" do
301 test "without valid credentials", %{conn: conn} do
302 note_activity = insert(:note_activity)
303 conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json"
304 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
307 test "with credentials", %{conn: conn, user: current_user} do
308 note_activity = insert(:note_activity)
309 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
310 ActivityPub.like(current_user, object)
313 |> with_credentials(current_user.nickname, "test")
314 |> post("/api/favorites/destroy/#{note_activity.id}.json")
316 assert json_response(conn, 200)
320 describe "POST /api/statuses/retweet/:id" do
322 test "without valid credentials", %{conn: conn} do
323 note_activity = insert(:note_activity)
324 conn = post conn, "/api/statuses/retweet/#{note_activity.id}.json"
325 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
328 test "with credentials", %{conn: conn, user: current_user} do
329 note_activity = insert(:note_activity)
332 |> with_credentials(current_user.nickname, "test")
333 |> post("/api/statuses/retweet/#{note_activity.id}.json")
335 assert json_response(conn, 200)
339 describe "POST /api/account/register" do
340 test "it creates a new user", %{conn: conn} do
342 "nickname" => "lain",
343 "email" => "lain@wired.jp",
344 "fullname" => "lain iwakura",
345 "bio" => "close the world.",
346 "password" => "bear",
351 |> post("/api/account/register", data)
353 user = json_response(conn, 200)
355 fetched_user = Repo.get_by(User, nickname: "lain")
356 assert user == UserRepresenter.to_map(fetched_user)
359 test "it returns errors on a problem", %{conn: conn} do
361 "email" => "lain@wired.jp",
362 "fullname" => "lain iwakura",
363 "bio" => "close the world.",
364 "password" => "bear",
369 |> post("/api/account/register", data)
371 errors = json_response(conn, 400)
373 assert is_binary(errors["error"])
377 defp valid_user(_context) do
382 defp with_credentials(conn, username, password) do
383 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
384 put_req_header(conn, "authorization", header_content)
388 Supervisor.terminate_child(Pleroma.Supervisor, ConCache)
389 Supervisor.restart_child(Pleroma.Supervisor, ConCache)