Merge branch 'develop' into issue/1276-2
[akkoma] / test / web / pleroma_api / controllers / account_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Config
9 alias Pleroma.Tests.ObanHelpers
10 alias Pleroma.User
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14 import Swoosh.TestAssertions
15
16 @image "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
17
18 describe "POST /api/v1/pleroma/accounts/confirmation_resend" do
19 setup do
20 {:ok, user} =
21 insert(:user)
22 |> User.confirmation_changeset(need_confirmation: true)
23 |> User.update_and_set_cache()
24
25 assert user.confirmation_pending
26
27 [user: user]
28 end
29
30 setup do: clear_config([:instance, :account_activation_required], true)
31
32 test "resend account confirmation email", %{conn: conn, user: user} do
33 conn
34 |> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
35 |> json_response(:no_content)
36
37 ObanHelpers.perform_all()
38
39 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
40 notify_email = Config.get([:instance, :notify_email])
41 instance_name = Config.get([:instance, :name])
42
43 assert_email_sent(
44 from: {instance_name, notify_email},
45 to: {user.name, user.email},
46 html_body: email.html_body
47 )
48 end
49 end
50
51 describe "PATCH /api/v1/pleroma/accounts/update_avatar" do
52 setup do: oauth_access(["write:accounts"])
53
54 test "user avatar can be set", %{user: user, conn: conn} do
55 avatar_image = File.read!("test/fixtures/avatar_data_uri")
56
57 conn = patch(conn, "/api/v1/pleroma/accounts/update_avatar", %{img: avatar_image})
58
59 user = refresh_record(user)
60
61 assert %{
62 "name" => _,
63 "type" => _,
64 "url" => [
65 %{
66 "href" => _,
67 "mediaType" => _,
68 "type" => _
69 }
70 ]
71 } = user.avatar
72
73 assert %{"url" => _} = json_response(conn, 200)
74 end
75
76 test "user avatar can be reset", %{user: user, conn: conn} do
77 conn = patch(conn, "/api/v1/pleroma/accounts/update_avatar", %{img: ""})
78
79 user = User.get_cached_by_id(user.id)
80
81 assert user.avatar == nil
82
83 assert %{"url" => nil} = json_response(conn, 200)
84 end
85 end
86
87 describe "PATCH /api/v1/pleroma/accounts/update_banner" do
88 setup do: oauth_access(["write:accounts"])
89
90 test "can set profile banner", %{user: user, conn: conn} do
91 conn = patch(conn, "/api/v1/pleroma/accounts/update_banner", %{"banner" => @image})
92
93 user = refresh_record(user)
94 assert user.banner["type"] == "Image"
95
96 assert %{"url" => _} = json_response(conn, 200)
97 end
98
99 test "can reset profile banner", %{user: user, conn: conn} do
100 conn = patch(conn, "/api/v1/pleroma/accounts/update_banner", %{"banner" => ""})
101
102 user = refresh_record(user)
103 assert user.banner == %{}
104
105 assert %{"url" => nil} = json_response(conn, 200)
106 end
107 end
108
109 describe "PATCH /api/v1/pleroma/accounts/update_background" do
110 setup do: oauth_access(["write:accounts"])
111
112 test "background image can be set", %{user: user, conn: conn} do
113 conn = patch(conn, "/api/v1/pleroma/accounts/update_background", %{"img" => @image})
114
115 user = refresh_record(user)
116 assert user.background["type"] == "Image"
117 assert %{"url" => _} = json_response(conn, 200)
118 end
119
120 test "background image can be reset", %{user: user, conn: conn} do
121 conn = patch(conn, "/api/v1/pleroma/accounts/update_background", %{"img" => ""})
122
123 user = refresh_record(user)
124 assert user.background == %{}
125 assert %{"url" => nil} = json_response(conn, 200)
126 end
127 end
128
129 describe "getting favorites timeline of specified user" do
130 setup do
131 [current_user, user] = insert_pair(:user, hide_favorites: false)
132 %{user: current_user, conn: conn} = oauth_access(["read:favourites"], user: current_user)
133 [current_user: current_user, user: user, conn: conn]
134 end
135
136 test "returns list of statuses favorited by specified user", %{
137 conn: conn,
138 user: user
139 } do
140 [activity | _] = insert_pair(:note_activity)
141 CommonAPI.favorite(user, activity.id)
142
143 response =
144 conn
145 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
146 |> json_response(:ok)
147
148 [like] = response
149
150 assert length(response) == 1
151 assert like["id"] == activity.id
152 end
153
154 test "returns favorites for specified user_id when requester is not logged in", %{
155 user: user
156 } do
157 activity = insert(:note_activity)
158 CommonAPI.favorite(user, activity.id)
159
160 response =
161 build_conn()
162 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
163 |> json_response(200)
164
165 assert length(response) == 1
166 end
167
168 test "returns favorited DM only when user is logged in and he is one of recipients", %{
169 current_user: current_user,
170 user: user
171 } do
172 {:ok, direct} =
173 CommonAPI.post(current_user, %{
174 "status" => "Hi @#{user.nickname}!",
175 "visibility" => "direct"
176 })
177
178 CommonAPI.favorite(user, direct.id)
179
180 for u <- [user, current_user] do
181 response =
182 build_conn()
183 |> assign(:user, u)
184 |> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
185 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
186 |> json_response(:ok)
187
188 assert length(response) == 1
189 end
190
191 response =
192 build_conn()
193 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
194 |> json_response(200)
195
196 assert length(response) == 0
197 end
198
199 test "does not return others' favorited DM when user is not one of recipients", %{
200 conn: conn,
201 user: user
202 } do
203 user_two = insert(:user)
204
205 {:ok, direct} =
206 CommonAPI.post(user_two, %{
207 "status" => "Hi @#{user.nickname}!",
208 "visibility" => "direct"
209 })
210
211 CommonAPI.favorite(user, direct.id)
212
213 response =
214 conn
215 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
216 |> json_response(:ok)
217
218 assert Enum.empty?(response)
219 end
220
221 test "paginates favorites using since_id and max_id", %{
222 conn: conn,
223 user: user
224 } do
225 activities = insert_list(10, :note_activity)
226
227 Enum.each(activities, fn activity ->
228 CommonAPI.favorite(user, activity.id)
229 end)
230
231 third_activity = Enum.at(activities, 2)
232 seventh_activity = Enum.at(activities, 6)
233
234 response =
235 conn
236 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{
237 since_id: third_activity.id,
238 max_id: seventh_activity.id
239 })
240 |> json_response(:ok)
241
242 assert length(response) == 3
243 refute third_activity in response
244 refute seventh_activity in response
245 end
246
247 test "limits favorites using limit parameter", %{
248 conn: conn,
249 user: user
250 } do
251 7
252 |> insert_list(:note_activity)
253 |> Enum.each(fn activity ->
254 CommonAPI.favorite(user, activity.id)
255 end)
256
257 response =
258 conn
259 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{limit: "3"})
260 |> json_response(:ok)
261
262 assert length(response) == 3
263 end
264
265 test "returns empty response when user does not have any favorited statuses", %{
266 conn: conn,
267 user: user
268 } do
269 response =
270 conn
271 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
272 |> json_response(:ok)
273
274 assert Enum.empty?(response)
275 end
276
277 test "returns 404 error when specified user is not exist", %{conn: conn} do
278 conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
279
280 assert json_response(conn, 404) == %{"error" => "Record not found"}
281 end
282
283 test "returns 403 error when user has hidden own favorites", %{conn: conn} do
284 user = insert(:user, hide_favorites: true)
285 activity = insert(:note_activity)
286 CommonAPI.favorite(user, activity.id)
287
288 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
289
290 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
291 end
292
293 test "hides favorites for new users by default", %{conn: conn} do
294 user = insert(:user)
295 activity = insert(:note_activity)
296 CommonAPI.favorite(user, activity.id)
297
298 assert user.hide_favorites
299 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
300
301 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
302 end
303 end
304
305 describe "subscribing / unsubscribing" do
306 test "subscribing / unsubscribing to a user" do
307 %{user: user, conn: conn} = oauth_access(["follow"])
308 subscription_target = insert(:user)
309
310 ret_conn =
311 conn
312 |> assign(:user, user)
313 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
314
315 assert %{"id" => _id, "subscribing" => true} = json_response(ret_conn, 200)
316
317 conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
318
319 assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
320 end
321 end
322
323 describe "subscribing" do
324 test "returns 404 when subscription_target not found" do
325 %{conn: conn} = oauth_access(["write:follows"])
326
327 conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
328
329 assert %{"error" => "Record not found"} = json_response(conn, 404)
330 end
331 end
332
333 describe "unsubscribing" do
334 test "returns 404 when subscription_target not found" do
335 %{conn: conn} = oauth_access(["follow"])
336
337 conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
338
339 assert %{"error" => "Record not found"} = json_response(conn, 404)
340 end
341 end
342 end