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
35 |> with_credentials(user.nickname, "test")
36 |> post("/api/statuses/update.json", %{ status: "Nice meme." })
38 assert json_response(conn, 200) == ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
42 describe "GET /statuses/public_timeline.json" do
43 test "returns statuses", %{conn: conn} do
44 {:ok, user} = UserBuilder.insert
45 activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
46 ActivityBuilder.insert_list(10, %{}, %{user: user})
47 since_id = List.last(activities).id
50 |> get("/api/statuses/public_timeline.json", %{since_id: since_id})
52 response = json_response(conn, 200)
54 assert length(response) == 10
58 describe "GET /statuses/show/:id.json" do
59 test "returns one status", %{conn: conn} do
60 {:ok, user} = UserBuilder.insert
61 {:ok, activity} = ActivityBuilder.insert(%{}, %{user: user})
62 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
65 |> get("/api/statuses/show/#{activity.id}.json")
67 response = json_response(conn, 200)
69 assert response == ActivityRepresenter.to_map(activity, %{user: actor})
73 describe "GET /statusnet/conversation/:id.json" do
74 test "returns the statuses in the conversation", %{conn: conn} do
75 {:ok, _user} = UserBuilder.insert
76 {:ok, _activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"})
77 {:ok, _activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"})
78 {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
81 |> get("/api/statusnet/conversation/1.json")
83 response = json_response(conn, 200)
85 assert length(response) == 2
89 describe "GET /statuses/friends_timeline.json" do
91 test "without valid credentials", %{conn: conn} do
92 conn = get conn, "/api/statuses/friends_timeline.json"
93 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
96 test "with credentials", %{conn: conn, user: current_user} do
98 activities = ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user})
99 returned_activities = ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
100 other_user = insert(:user)
101 ActivityBuilder.insert_list(10, %{}, %{user: other_user})
102 since_id = List.last(activities).id
104 current_user = Ecto.Changeset.change(current_user, following: [User.ap_followers(user)]) |> Repo.update!
107 |> with_credentials(current_user.nickname, "test")
108 |> get("/api/statuses/friends_timeline.json", %{since_id: since_id})
110 response = json_response(conn, 200)
112 assert length(response) == 10
113 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)
117 describe "GET /statuses/user_timeline.json" do
119 test "without any params", %{conn: conn} do
120 conn = get(conn, "/api/statuses/user_timeline.json")
121 assert json_response(conn, 400) == %{"error" => "You need to specify screen_name or user_id"}
124 test "with user_id", %{conn: conn} do
126 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
128 conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
129 response = json_response(conn, 200)
130 assert length(response) == 1
131 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
134 test "with screen_name", %{conn: conn} do
136 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
138 conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
139 response = json_response(conn, 200)
140 assert length(response) == 1
141 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
144 test "with credentials", %{conn: conn, user: current_user} do
145 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user})
147 |> with_credentials(current_user.nickname, "test")
148 |> get("/api/statuses/user_timeline.json")
150 response = json_response(conn, 200)
152 assert length(response) == 1
153 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
156 test "with credentials with user_id", %{conn: conn, user: current_user} do
158 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
160 |> with_credentials(current_user.nickname, "test")
161 |> get("/api/statuses/user_timeline.json", %{"user_id" => user.id})
163 response = json_response(conn, 200)
165 assert length(response) == 1
166 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
169 test "with credentials screen_name", %{conn: conn, user: current_user} do
171 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
173 |> with_credentials(current_user.nickname, "test")
174 |> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
176 response = json_response(conn, 200)
178 assert length(response) == 1
179 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
183 describe "POST /friendships/create.json" do
185 test "without valid credentials", %{conn: conn} do
186 conn = post conn, "/api/friendships/create.json"
187 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
190 test "with credentials", %{conn: conn, user: current_user} do
191 followed = insert(:user)
194 |> with_credentials(current_user.nickname, "test")
195 |> post("/api/friendships/create.json", %{user_id: followed.id})
197 current_user = Repo.get(User, current_user.id)
198 assert current_user.following == [User.ap_followers(followed)]
199 assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user})
203 describe "POST /friendships/destroy.json" do
205 test "without valid credentials", %{conn: conn} do
206 conn = post conn, "/api/friendships/destroy.json"
207 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
210 test "with credentials", %{conn: conn, user: current_user} do
211 followed = insert(:user)
213 {:ok, current_user} = User.follow(current_user, followed)
214 assert current_user.following == [User.ap_followers(followed)]
217 |> with_credentials(current_user.nickname, "test")
218 |> post("/api/friendships/destroy.json", %{user_id: followed.id})
220 current_user = Repo.get(User, current_user.id)
221 assert current_user.following == []
222 assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user})
226 describe "POST /api/qvitter/update_avatar.json" do
228 test "without valid credentials", %{conn: conn} do
229 conn = post conn, "/api/qvitter/update_avatar.json"
230 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
233 test "with credentials", %{conn: conn, user: current_user} do
235 |> with_credentials(current_user.nickname, "test")
236 |> post("/api/qvitter/update_avatar.json", %{img: Pleroma.Web.ActivityPub.ActivityPubTest.data_uri})
238 current_user = Repo.get(User, current_user.id)
239 assert is_map(current_user.avatar)
240 assert json_response(conn, 200) == UserRepresenter.to_map(current_user, %{for: current_user})
244 describe "POST /api/favorites/create/:id" do
246 test "without valid credentials", %{conn: conn} do
247 note_activity = insert(:note_activity)
248 conn = post conn, "/api/favorites/create/#{note_activity.id}.json"
249 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
252 test "with credentials", %{conn: conn, user: current_user} do
253 note_activity = insert(:note_activity)
256 |> with_credentials(current_user.nickname, "test")
257 |> post("/api/favorites/create/#{note_activity.id}.json")
259 assert json_response(conn, 200)
263 describe "POST /api/favorites/destroy/:id" do
265 test "without valid credentials", %{conn: conn} do
266 note_activity = insert(:note_activity)
267 conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json"
268 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
271 test "with credentials", %{conn: conn, user: current_user} do
272 note_activity = insert(:note_activity)
273 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
274 ActivityPub.like(current_user, object)
277 |> with_credentials(current_user.nickname, "test")
278 |> post("/api/favorites/destroy/#{note_activity.id}.json")
280 assert json_response(conn, 200)
284 describe "POST /api/statuses/retweet/:id" do
286 test "without valid credentials", %{conn: conn} do
287 note_activity = insert(:note_activity)
288 conn = post conn, "/api/statuses/retweet/#{note_activity.id}.json"
289 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
292 test "with credentials", %{conn: conn, user: current_user} do
293 note_activity = insert(:note_activity)
296 |> with_credentials(current_user.nickname, "test")
297 |> post("/api/statuses/retweet/#{note_activity.id}.json")
299 assert json_response(conn, 200)
303 describe "POST /api/account/register" do
304 test "it creates a new user", %{conn: conn} do
306 "nickname" => "lain",
307 "email" => "lain@wired.jp",
308 "fullname" => "lain iwakura",
309 "bio" => "close the world.",
310 "password" => "bear",
315 |> post("/api/account/register", data)
317 user = json_response(conn, 200)
319 fetched_user = Repo.get_by(User, nickname: "lain")
320 assert user == UserRepresenter.to_map(fetched_user)
323 test "it returns errors on a problem", %{conn: conn} do
325 "email" => "lain@wired.jp",
326 "fullname" => "lain iwakura",
327 "bio" => "close the world.",
328 "password" => "bear",
333 |> post("/api/account/register", data)
335 errors = json_response(conn, 400)
337 assert is_binary(errors["error"])
341 defp valid_user(_context) do
342 { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
346 defp with_credentials(conn, username, password) do
347 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
348 put_req_header(conn, "authorization", header_content)
352 Supervisor.terminate_child(Pleroma.Supervisor, ConCache)
353 Supervisor.restart_child(Pleroma.Supervisor, ConCache)