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 "POST /api/favorites/create/:id" do
162 test "without valid credentials", %{conn: conn} do
163 note_activity = insert(:note_activity)
164 conn = post conn, "/api/favorites/create/#{note_activity.id}.json"
165 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
168 test "with credentials", %{conn: conn, user: current_user} do
169 note_activity = insert(:note_activity)
172 |> with_credentials(current_user.nickname, "test")
173 |> post("/api/favorites/create/#{note_activity.id}.json")
175 assert json_response(conn, 200)
179 describe "POST /api/favorites/destroy/:id" do
181 test "without valid credentials", %{conn: conn} do
182 note_activity = insert(:note_activity)
183 conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json"
184 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
187 test "with credentials", %{conn: conn, user: current_user} do
188 note_activity = insert(:note_activity)
189 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
190 ActivityPub.like(current_user, object)
193 |> with_credentials(current_user.nickname, "test")
194 |> post("/api/favorites/destroy/#{note_activity.id}.json")
196 assert json_response(conn, 200)
200 describe "POST /api/statuses/retweet/:id" do
202 test "without valid credentials", %{conn: conn} do
203 note_activity = insert(:note_activity)
204 conn = post conn, "/api/statuses/retweet/#{note_activity.id}.json"
205 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
208 test "with credentials", %{conn: conn, user: current_user} do
209 note_activity = insert(:note_activity)
212 |> with_credentials(current_user.nickname, "test")
213 |> post("/api/statuses/retweet/#{note_activity.id}.json")
215 assert json_response(conn, 200)
219 describe "POST /api/account/register" do
220 test "it creates a new user", %{conn: conn} do
222 "nickname" => "lain",
223 "email" => "lain@wired.jp",
224 "fullname" => "lain iwakura",
225 "bio" => "close the world.",
226 "password" => "bear",
231 |> post("/api/account/register", data)
233 user = json_response(conn, 200)
235 fetched_user = Repo.get_by(User, nickname: "lain")
236 assert user == UserRepresenter.to_map(fetched_user)
239 test "it returns errors on a problem", %{conn: conn} do
241 "email" => "lain@wired.jp",
242 "fullname" => "lain iwakura",
243 "bio" => "close the world.",
244 "password" => "bear",
249 |> post("/api/account/register", data)
251 errors = json_response(conn, 400)
253 assert is_binary(errors["error"])
257 defp valid_user(_context) do
258 { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
262 defp with_credentials(conn, username, password) do
263 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
264 put_req_header(conn, "authorization", header_content)
268 Supervisor.terminate_child(Pleroma.Supervisor, ConCache)
269 Supervisor.restart_child(Pleroma.Supervisor, ConCache)