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 "POST /friendships/create.json" do
119 test "without valid credentials", %{conn: conn} do
120 conn = post conn, "/api/friendships/create.json"
121 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
124 test "with credentials", %{conn: conn, user: current_user} do
125 followed = insert(:user)
128 |> with_credentials(current_user.nickname, "test")
129 |> post("/api/friendships/create.json", %{user_id: followed.id})
131 current_user = Repo.get(User, current_user.id)
132 assert current_user.following == [User.ap_followers(followed)]
133 assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user})
137 describe "POST /friendships/destroy.json" do
139 test "without valid credentials", %{conn: conn} do
140 conn = post conn, "/api/friendships/destroy.json"
141 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
144 test "with credentials", %{conn: conn, user: current_user} do
145 followed = insert(:user)
147 {:ok, current_user} = User.follow(current_user, followed)
148 assert current_user.following == [User.ap_followers(followed)]
151 |> with_credentials(current_user.nickname, "test")
152 |> post("/api/friendships/destroy.json", %{user_id: followed.id})
154 current_user = Repo.get(User, current_user.id)
155 assert current_user.following == []
156 assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user})
160 describe "GET /help/test.json" do
161 test "returns \"ok\"", %{conn: conn} do
162 conn = get conn, "/api/help/test.json"
163 assert json_response(conn, 200) == "ok"
166 describe "POST /api/qvitter/update_avatar.json" do
168 test "without valid credentials", %{conn: conn} do
169 conn = post conn, "/api/qvitter/update_avatar.json"
170 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
173 test "with credentials", %{conn: conn, user: current_user} do
175 |> with_credentials(current_user.nickname, "test")
176 |> post("/api/qvitter/update_avatar.json", %{img: Pleroma.Web.ActivityPub.ActivityPubTest.data_uri})
178 current_user = Repo.get(User, current_user.id)
179 assert is_map(current_user.avatar)
180 assert json_response(conn, 200) == UserRepresenter.to_map(current_user, %{for: current_user})
184 describe "POST /api/favorites/create/:id" do
186 test "without valid credentials", %{conn: conn} do
187 note_activity = insert(:note_activity)
188 conn = post conn, "/api/favorites/create/#{note_activity.id}.json"
189 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
192 test "with credentials", %{conn: conn, user: current_user} do
193 note_activity = insert(:note_activity)
196 |> with_credentials(current_user.nickname, "test")
197 |> post("/api/favorites/create/#{note_activity.id}.json")
199 assert json_response(conn, 200)
203 describe "POST /api/favorites/destroy/:id" do
205 test "without valid credentials", %{conn: conn} do
206 note_activity = insert(:note_activity)
207 conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json"
208 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
211 test "with credentials", %{conn: conn, user: current_user} do
212 note_activity = insert(:note_activity)
213 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
214 ActivityPub.like(current_user, object)
217 |> with_credentials(current_user.nickname, "test")
218 |> post("/api/favorites/destroy/#{note_activity.id}.json")
220 assert json_response(conn, 200)
224 describe "POST /api/statuses/retweet/:id" do
226 test "without valid credentials", %{conn: conn} do
227 note_activity = insert(:note_activity)
228 conn = post conn, "/api/statuses/retweet/#{note_activity.id}.json"
229 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
232 test "with credentials", %{conn: conn, user: current_user} do
233 note_activity = insert(:note_activity)
236 |> with_credentials(current_user.nickname, "test")
237 |> post("/api/statuses/retweet/#{note_activity.id}.json")
239 assert json_response(conn, 200)
243 describe "POST /api/account/register" do
244 test "it creates a new user", %{conn: conn} do
246 "nickname" => "lain",
247 "email" => "lain@wired.jp",
248 "fullname" => "lain iwakura",
249 "bio" => "close the world.",
250 "password" => "bear",
255 |> post("/api/account/register", data)
257 user = json_response(conn, 200)
259 fetched_user = Repo.get_by(User, nickname: "lain")
260 assert user == UserRepresenter.to_map(fetched_user)
263 test "it returns errors on a problem", %{conn: conn} do
265 "email" => "lain@wired.jp",
266 "fullname" => "lain iwakura",
267 "bio" => "close the world.",
268 "password" => "bear",
273 |> post("/api/account/register", data)
275 errors = json_response(conn, 400)
277 assert is_binary(errors["error"])
281 defp valid_user(_context) do
282 { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
286 defp with_credentials(conn, username, password) do
287 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
288 put_req_header(conn, "authorization", header_content)
292 Supervisor.terminate_child(Pleroma.Supervisor, ConCache)
293 Supervisor.restart_child(Pleroma.Supervisor, ConCache)