Merge branch 'fix-auto-link-for-profile-fields' into 'develop'
[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 "does not return favorites for specified user_id when user is not logged in", %{
155 user: user
156 } do
157 activity = insert(:note_activity)
158 CommonAPI.favorite(user, activity.id)
159
160 build_conn()
161 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
162 |> json_response(403)
163 end
164
165 test "returns favorited DM only when user is logged in and he is one of recipients", %{
166 current_user: current_user,
167 user: user
168 } do
169 {:ok, direct} =
170 CommonAPI.post(current_user, %{
171 "status" => "Hi @#{user.nickname}!",
172 "visibility" => "direct"
173 })
174
175 CommonAPI.favorite(user, direct.id)
176
177 for u <- [user, current_user] do
178 response =
179 build_conn()
180 |> assign(:user, u)
181 |> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
182 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
183 |> json_response(:ok)
184
185 assert length(response) == 1
186 end
187
188 build_conn()
189 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
190 |> json_response(403)
191 end
192
193 test "does not return others' favorited DM when user is not one of recipients", %{
194 conn: conn,
195 user: user
196 } do
197 user_two = insert(:user)
198
199 {:ok, direct} =
200 CommonAPI.post(user_two, %{
201 "status" => "Hi @#{user.nickname}!",
202 "visibility" => "direct"
203 })
204
205 CommonAPI.favorite(user, direct.id)
206
207 response =
208 conn
209 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
210 |> json_response(:ok)
211
212 assert Enum.empty?(response)
213 end
214
215 test "paginates favorites using since_id and max_id", %{
216 conn: conn,
217 user: user
218 } do
219 activities = insert_list(10, :note_activity)
220
221 Enum.each(activities, fn activity ->
222 CommonAPI.favorite(user, activity.id)
223 end)
224
225 third_activity = Enum.at(activities, 2)
226 seventh_activity = Enum.at(activities, 6)
227
228 response =
229 conn
230 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{
231 since_id: third_activity.id,
232 max_id: seventh_activity.id
233 })
234 |> json_response(:ok)
235
236 assert length(response) == 3
237 refute third_activity in response
238 refute seventh_activity in response
239 end
240
241 test "limits favorites using limit parameter", %{
242 conn: conn,
243 user: user
244 } do
245 7
246 |> insert_list(:note_activity)
247 |> Enum.each(fn activity ->
248 CommonAPI.favorite(user, activity.id)
249 end)
250
251 response =
252 conn
253 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{limit: "3"})
254 |> json_response(:ok)
255
256 assert length(response) == 3
257 end
258
259 test "returns empty response when user does not have any favorited statuses", %{
260 conn: conn,
261 user: user
262 } do
263 response =
264 conn
265 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
266 |> json_response(:ok)
267
268 assert Enum.empty?(response)
269 end
270
271 test "returns 404 error when specified user is not exist", %{conn: conn} do
272 conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
273
274 assert json_response(conn, 404) == %{"error" => "Record not found"}
275 end
276
277 test "returns 403 error when user has hidden own favorites", %{conn: conn} do
278 user = insert(:user, hide_favorites: true)
279 activity = insert(:note_activity)
280 CommonAPI.favorite(user, activity.id)
281
282 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
283
284 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
285 end
286
287 test "hides favorites for new users by default", %{conn: conn} do
288 user = insert(:user)
289 activity = insert(:note_activity)
290 CommonAPI.favorite(user, activity.id)
291
292 assert user.hide_favorites
293 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
294
295 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
296 end
297 end
298
299 describe "subscribing / unsubscribing" do
300 test "subscribing / unsubscribing to a user" do
301 %{user: user, conn: conn} = oauth_access(["follow"])
302 subscription_target = insert(:user)
303
304 ret_conn =
305 conn
306 |> assign(:user, user)
307 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
308
309 assert %{"id" => _id, "subscribing" => true} = json_response(ret_conn, 200)
310
311 conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
312
313 assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
314 end
315 end
316
317 describe "subscribing" do
318 test "returns 404 when subscription_target not found" do
319 %{conn: conn} = oauth_access(["write:follows"])
320
321 conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
322
323 assert %{"error" => "Record not found"} = json_response(conn, 404)
324 end
325 end
326
327 describe "unsubscribing" do
328 test "returns 404 when subscription_target not found" do
329 %{conn: conn} = oauth_access(["follow"])
330
331 conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
332
333 assert %{"error" => "Record not found"} = json_response(conn, 404)
334 end
335 end
336 end