Merge branch '1918-avatar-background-header-reset-2' 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 describe "POST /api/v1/pleroma/accounts/confirmation_resend" do
17 setup do
18 {:ok, user} =
19 insert(:user)
20 |> User.confirmation_changeset(need_confirmation: true)
21 |> User.update_and_set_cache()
22
23 assert user.confirmation_pending
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=#{
178 seventh_activity.id
179 }"
180 )
181 |> json_response_and_validate_schema(:ok)
182
183 assert length(response) == 3
184 refute third_activity in response
185 refute seventh_activity in response
186 end
187
188 test "limits favorites using limit parameter", %{
189 conn: conn,
190 user: user
191 } do
192 7
193 |> insert_list(:note_activity)
194 |> Enum.each(fn activity ->
195 CommonAPI.favorite(user, activity.id)
196 end)
197
198 response =
199 conn
200 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites?limit=3")
201 |> json_response_and_validate_schema(:ok)
202
203 assert length(response) == 3
204 end
205
206 test "returns empty response when user does not have any favorited statuses", %{
207 conn: conn,
208 user: user
209 } do
210 response =
211 conn
212 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
213 |> json_response_and_validate_schema(:ok)
214
215 assert Enum.empty?(response)
216 end
217
218 test "returns 404 error when specified user is not exist", %{conn: conn} do
219 conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
220
221 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
222 end
223
224 test "returns 403 error when user has hidden own favorites", %{conn: conn} do
225 user = insert(:user, hide_favorites: true)
226 activity = insert(:note_activity)
227 CommonAPI.favorite(user, activity.id)
228
229 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
230
231 assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
232 end
233
234 test "hides favorites for new users by default", %{conn: conn} do
235 user = insert(:user)
236 activity = insert(:note_activity)
237 CommonAPI.favorite(user, activity.id)
238
239 assert user.hide_favorites
240 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
241
242 assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
243 end
244 end
245
246 describe "subscribing / unsubscribing" do
247 test "subscribing / unsubscribing to a user" do
248 %{user: user, conn: conn} = oauth_access(["follow"])
249 subscription_target = insert(:user)
250
251 ret_conn =
252 conn
253 |> assign(:user, user)
254 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
255
256 assert %{"id" => _id, "subscribing" => true} =
257 json_response_and_validate_schema(ret_conn, 200)
258
259 conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
260
261 assert %{"id" => _id, "subscribing" => false} = json_response_and_validate_schema(conn, 200)
262 end
263 end
264
265 describe "subscribing" do
266 test "returns 404 when subscription_target not found" do
267 %{conn: conn} = oauth_access(["write:follows"])
268
269 conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
270
271 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
272 end
273 end
274
275 describe "unsubscribing" do
276 test "returns 404 when subscription_target not found" do
277 %{conn: conn} = oauth_access(["follow"])
278
279 conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
280
281 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
282 end
283 end
284 end