Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/unfollow...
[akkoma] / test / web / twitter_api / twitter_api_controller_test.exs
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
7
8 import Pleroma.Factory
9
10 describe "POST /api/account/verify_credentials" do
11 setup [:valid_user]
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."}
15 end
16
17 test "with credentials", %{conn: conn, user: user} do
18 conn = conn
19 |> with_credentials(user.nickname, "test")
20 |> post("/api/account/verify_credentials.json")
21
22 assert json_response(conn, 200) == UserRepresenter.to_map(user)
23 end
24 end
25
26 describe "POST /statuses/update.json" do
27 setup [:valid_user]
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."}
31 end
32
33 test "with credentials", %{conn: conn, user: user} do
34 conn = conn
35 |> with_credentials(user.nickname, "test")
36 |> post("/api/statuses/update.json", %{ status: "Nice meme." })
37
38 assert json_response(conn, 200) == ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
39 end
40 end
41
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
48
49 conn = conn
50 |> get("/api/statuses/public_timeline.json", %{since_id: since_id})
51
52 response = json_response(conn, 200)
53
54 assert length(response) == 10
55 end
56 end
57
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"])
63
64 conn = conn
65 |> get("/api/statuses/show/#{activity.id}.json")
66
67 response = json_response(conn, 200)
68
69 assert response == ActivityRepresenter.to_map(activity, %{user: actor})
70 end
71 end
72
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"})
79
80 conn = conn
81 |> get("/api/statusnet/conversation/1.json")
82
83 response = json_response(conn, 200)
84
85 assert length(response) == 2
86 end
87 end
88
89 describe "GET /statuses/friends_timeline.json" do
90 setup [:valid_user]
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."}
94 end
95
96 test "with credentials", %{conn: conn, user: current_user} do
97 user = insert(:user)
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
103
104 current_user = Ecto.Changeset.change(current_user, following: [User.ap_followers(user)]) |> Repo.update!
105
106 conn = conn
107 |> with_credentials(current_user.nickname, "test")
108 |> get("/api/statuses/friends_timeline.json", %{since_id: since_id})
109
110 response = json_response(conn, 200)
111
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)
114 end
115 end
116
117 describe "POST /friendships/create.json" do
118 setup [:valid_user]
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."}
122 end
123
124 test "with credentials", %{conn: conn, user: current_user} do
125 followed = insert(:user)
126
127 conn = conn
128 |> with_credentials(current_user.nickname, "test")
129 |> post("/api/friendships/create.json", %{user_id: followed.id})
130
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})
134 end
135 end
136
137 describe "POST /friendships/destroy.json" do
138 setup [:valid_user]
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."}
142 end
143
144 test "with credentials", %{conn: conn, user: current_user} do
145 followed = insert(:user)
146
147 {:ok, current_user} = User.follow(current_user, followed)
148 assert current_user.following == [User.ap_followers(followed)]
149
150 conn = conn
151 |> with_credentials(current_user.nickname, "test")
152 |> post("/api/friendships/destroy.json", %{user_id: followed.id})
153
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})
157 end
158 end
159
160 describe "POST /api/qvitter/update_avatar.json" do
161 setup [:valid_user]
162 test "without valid credentials", %{conn: conn} do
163 conn = post conn, "/api/qvitter/update_avatar.json"
164 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
165 end
166
167 test "with credentials", %{conn: conn, user: current_user} do
168 conn = conn
169 |> with_credentials(current_user.nickname, "test")
170 |> post("/api/qvitter/update_avatar.json", %{img: Pleroma.Web.ActivityPub.ActivityPubTest.data_uri})
171
172 current_user = Repo.get(User, current_user.id)
173 assert is_map(current_user.avatar)
174 assert json_response(conn, 200) == UserRepresenter.to_map(current_user, %{for: current_user})
175 end
176 end
177
178 describe "POST /api/favorites/create/:id" do
179 setup [:valid_user]
180 test "without valid credentials", %{conn: conn} do
181 note_activity = insert(:note_activity)
182 conn = post conn, "/api/favorites/create/#{note_activity.id}.json"
183 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
184 end
185
186 test "with credentials", %{conn: conn, user: current_user} do
187 note_activity = insert(:note_activity)
188
189 conn = conn
190 |> with_credentials(current_user.nickname, "test")
191 |> post("/api/favorites/create/#{note_activity.id}.json")
192
193 assert json_response(conn, 200)
194 end
195 end
196
197 describe "POST /api/favorites/destroy/:id" do
198 setup [:valid_user]
199 test "without valid credentials", %{conn: conn} do
200 note_activity = insert(:note_activity)
201 conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json"
202 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
203 end
204
205 test "with credentials", %{conn: conn, user: current_user} do
206 note_activity = insert(:note_activity)
207 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
208 ActivityPub.like(current_user, object)
209
210 conn = conn
211 |> with_credentials(current_user.nickname, "test")
212 |> post("/api/favorites/destroy/#{note_activity.id}.json")
213
214 assert json_response(conn, 200)
215 end
216 end
217
218 describe "POST /api/statuses/retweet/:id" do
219 setup [:valid_user]
220 test "without valid credentials", %{conn: conn} do
221 note_activity = insert(:note_activity)
222 conn = post conn, "/api/statuses/retweet/#{note_activity.id}.json"
223 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
224 end
225
226 test "with credentials", %{conn: conn, user: current_user} do
227 note_activity = insert(:note_activity)
228
229 conn = conn
230 |> with_credentials(current_user.nickname, "test")
231 |> post("/api/statuses/retweet/#{note_activity.id}.json")
232
233 assert json_response(conn, 200)
234 end
235 end
236
237 describe "POST /api/account/register" do
238 test "it creates a new user", %{conn: conn} do
239 data = %{
240 "nickname" => "lain",
241 "email" => "lain@wired.jp",
242 "fullname" => "lain iwakura",
243 "bio" => "close the world.",
244 "password" => "bear",
245 "confirm" => "bear"
246 }
247
248 conn = conn
249 |> post("/api/account/register", data)
250
251 user = json_response(conn, 200)
252
253 fetched_user = Repo.get_by(User, nickname: "lain")
254 assert user == UserRepresenter.to_map(fetched_user)
255 end
256
257 test "it returns errors on a problem", %{conn: conn} do
258 data = %{
259 "email" => "lain@wired.jp",
260 "fullname" => "lain iwakura",
261 "bio" => "close the world.",
262 "password" => "bear",
263 "confirm" => "bear"
264 }
265
266 conn = conn
267 |> post("/api/account/register", data)
268
269 errors = json_response(conn, 400)
270
271 assert is_binary(errors["error"])
272 end
273 end
274
275 defp valid_user(_context) do
276 { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
277 [user: user]
278 end
279
280 defp with_credentials(conn, username, password) do
281 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
282 put_req_header(conn, "authorization", header_content)
283 end
284
285 setup do
286 Supervisor.terminate_child(Pleroma.Supervisor, ConCache)
287 Supervisor.restart_child(Pleroma.Supervisor, ConCache)
288 :ok
289 end
290 end