First
[anni] / test / pleroma / web / pleroma_api / controllers / account_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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   describe "POST /api/v1/pleroma/accounts/confirmation_resend" do
17     setup do
18       {:ok, user} =
19         insert(:user)
20         |> User.confirmation_changeset(set_confirmation: false)
21         |> User.update_and_set_cache()
22
23       refute user.is_confirmed
24
25       [user: user]
26     end
27
28     setup do: clear_config([:instance, :account_activation_required], true)
29
30     test "resend account confirmation email", %{conn: conn, user: user} do
31       conn
32       |> put_req_header("content-type", "application/json")
33       |> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
34       |> json_response_and_validate_schema(:no_content)
35
36       ObanHelpers.perform_all()
37
38       email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
39       notify_email = Config.get([:instance, :notify_email])
40       instance_name = Config.get([:instance, :name])
41
42       assert_email_sent(
43         from: {instance_name, notify_email},
44         to: {user.name, user.email},
45         html_body: email.html_body
46       )
47     end
48
49     test "resend account confirmation email (with nickname)", %{conn: conn, user: user} do
50       conn
51       |> put_req_header("content-type", "application/json")
52       |> post("/api/v1/pleroma/accounts/confirmation_resend?nickname=#{user.nickname}")
53       |> json_response_and_validate_schema(:no_content)
54
55       ObanHelpers.perform_all()
56
57       email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
58       notify_email = Config.get([:instance, :notify_email])
59       instance_name = Config.get([:instance, :name])
60
61       assert_email_sent(
62         from: {instance_name, notify_email},
63         to: {user.name, user.email},
64         html_body: email.html_body
65       )
66     end
67   end
68
69   describe "getting favorites timeline of specified user" do
70     setup do
71       [current_user, user] = insert_pair(:user, hide_favorites: false)
72       %{user: current_user, conn: conn} = oauth_access(["read:favourites"], user: current_user)
73       [current_user: current_user, user: user, conn: conn]
74     end
75
76     test "returns list of statuses favorited by specified user", %{
77       conn: conn,
78       user: user
79     } do
80       [activity | _] = insert_pair(:note_activity)
81       CommonAPI.favorite(user, activity.id)
82
83       response =
84         conn
85         |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
86         |> json_response_and_validate_schema(:ok)
87
88       [like] = response
89
90       assert length(response) == 1
91       assert like["id"] == activity.id
92     end
93
94     test "returns favorites for specified user_id when requester is not logged in", %{
95       user: user
96     } do
97       activity = insert(:note_activity)
98       CommonAPI.favorite(user, activity.id)
99
100       response =
101         build_conn()
102         |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
103         |> json_response_and_validate_schema(200)
104
105       assert length(response) == 1
106     end
107
108     test "returns favorited DM only when user is logged in and he is one of recipients", %{
109       current_user: current_user,
110       user: user
111     } do
112       {:ok, direct} =
113         CommonAPI.post(current_user, %{
114           status: "Hi @#{user.nickname}!",
115           visibility: "direct"
116         })
117
118       CommonAPI.favorite(user, direct.id)
119
120       for u <- [user, current_user] do
121         response =
122           build_conn()
123           |> assign(:user, u)
124           |> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
125           |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
126           |> json_response_and_validate_schema(:ok)
127
128         assert length(response) == 1
129       end
130
131       response =
132         build_conn()
133         |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
134         |> json_response_and_validate_schema(200)
135
136       assert length(response) == 0
137     end
138
139     test "does not return others' favorited DM when user is not one of recipients", %{
140       conn: conn,
141       user: user
142     } do
143       user_two = insert(:user)
144
145       {:ok, direct} =
146         CommonAPI.post(user_two, %{
147           status: "Hi @#{user.nickname}!",
148           visibility: "direct"
149         })
150
151       CommonAPI.favorite(user, direct.id)
152
153       response =
154         conn
155         |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
156         |> json_response_and_validate_schema(:ok)
157
158       assert Enum.empty?(response)
159     end
160
161     test "paginates favorites using since_id and max_id", %{
162       conn: conn,
163       user: user
164     } do
165       activities = insert_list(10, :note_activity)
166
167       Enum.each(activities, fn activity ->
168         CommonAPI.favorite(user, activity.id)
169       end)
170
171       third_activity = Enum.at(activities, 2)
172       seventh_activity = Enum.at(activities, 6)
173
174       response =
175         conn
176         |> get(
177           "/api/v1/pleroma/accounts/#{user.id}/favourites?since_id=#{third_activity.id}&max_id=#{seventh_activity.id}"
178         )
179         |> json_response_and_validate_schema(:ok)
180
181       assert length(response) == 3
182       refute third_activity in response
183       refute seventh_activity in response
184     end
185
186     test "limits favorites using limit parameter", %{
187       conn: conn,
188       user: user
189     } do
190       7
191       |> insert_list(:note_activity)
192       |> Enum.each(fn activity ->
193         CommonAPI.favorite(user, activity.id)
194       end)
195
196       response =
197         conn
198         |> get("/api/v1/pleroma/accounts/#{user.id}/favourites?limit=3")
199         |> json_response_and_validate_schema(:ok)
200
201       assert length(response) == 3
202     end
203
204     test "returns empty response when user does not have any favorited statuses", %{
205       conn: conn,
206       user: user
207     } do
208       response =
209         conn
210         |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
211         |> json_response_and_validate_schema(:ok)
212
213       assert Enum.empty?(response)
214     end
215
216     test "returns 404 error when specified user is not exist", %{conn: conn} do
217       conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
218
219       assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
220     end
221
222     test "returns 403 error when user has hidden own favorites", %{conn: conn} do
223       user = insert(:user, hide_favorites: true)
224       activity = insert(:note_activity)
225       CommonAPI.favorite(user, activity.id)
226
227       conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
228
229       assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
230     end
231
232     test "hides favorites for new users by default", %{conn: conn} do
233       user = insert(:user)
234       activity = insert(:note_activity)
235       CommonAPI.favorite(user, activity.id)
236
237       assert user.hide_favorites
238       conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
239
240       assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
241     end
242   end
243
244   describe "subscribing / unsubscribing" do
245     test "subscribing / unsubscribing to a user" do
246       %{user: user, conn: conn} = oauth_access(["follow"])
247       subscription_target = insert(:user)
248
249       ret_conn =
250         conn
251         |> assign(:user, user)
252         |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
253
254       assert %{"id" => _id, "subscribing" => true} =
255                json_response_and_validate_schema(ret_conn, 200)
256
257       conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
258
259       assert %{"id" => _id, "subscribing" => false} = json_response_and_validate_schema(conn, 200)
260     end
261   end
262
263   describe "subscribing" do
264     test "returns 404 when subscription_target not found" do
265       %{conn: conn} = oauth_access(["write:follows"])
266
267       conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
268
269       assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
270     end
271   end
272
273   describe "unsubscribing" do
274     test "returns 404 when subscription_target not found" do
275       %{conn: conn} = oauth_access(["follow"])
276
277       conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
278
279       assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
280     end
281   end
282
283   describe "account endorsements" do
284     test "returns a list of pinned accounts", %{conn: conn} do
285       %{id: id1} = user1 = insert(:user)
286       %{id: id2} = user2 = insert(:user)
287       %{id: id3} = user3 = insert(:user)
288
289       CommonAPI.follow(user1, user2)
290       CommonAPI.follow(user1, user3)
291
292       User.endorse(user1, user2)
293       User.endorse(user1, user3)
294
295       [%{"id" => ^id2}, %{"id" => ^id3}] =
296         conn
297         |> get("/api/v1/pleroma/accounts/#{id1}/endorsements")
298         |> json_response_and_validate_schema(200)
299     end
300
301     test "returns 404 error when specified user is not exist", %{conn: conn} do
302       conn = get(conn, "/api/v1/pleroma/accounts/test/endorsements")
303
304       assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
305     end
306   end
307
308   describe "birthday reminders" do
309     test "returns a list of friends having birthday on specified day" do
310       %{user: user, conn: conn} = oauth_access(["read:accounts"])
311
312       %{id: id1} =
313         user1 =
314         insert(:user, %{
315           birthday: "2001-02-12",
316           show_birthday: true
317         })
318
319       user2 =
320         insert(:user, %{
321           birthday: "2001-02-14",
322           show_birthday: true
323         })
324
325       user3 = insert(:user)
326
327       CommonAPI.follow(user, user1)
328       CommonAPI.follow(user, user2)
329       CommonAPI.follow(user, user3)
330
331       [%{"id" => ^id1}] =
332         conn
333         |> get("/api/v1/pleroma/birthdays?day=12&month=2")
334         |> json_response_and_validate_schema(:ok)
335     end
336
337     test "the list doesn't list friends with hidden birth date" do
338       %{user: user, conn: conn} = oauth_access(["read:accounts"])
339
340       user1 =
341         insert(:user, %{
342           birthday: "2001-02-12",
343           show_birthday: false
344         })
345
346       %{id: id2} =
347         user2 =
348         insert(:user, %{
349           birthday: "2001-02-12",
350           show_birthday: true
351         })
352
353       CommonAPI.follow(user, user1)
354       CommonAPI.follow(user, user2)
355
356       [%{"id" => ^id2}] =
357         conn
358         |> get("/api/v1/pleroma/birthdays?day=12&month=2")
359         |> json_response_and_validate_schema(:ok)
360     end
361   end
362 end