1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
14 import Swoosh.TestAssertions
16 describe "POST /api/v1/pleroma/accounts/confirmation_resend" do
20 |> User.confirmation_changeset(set_confirmation: false)
21 |> User.update_and_set_cache()
23 refute user.is_confirmed
28 setup do: clear_config([:instance, :account_activation_required], true)
30 test "resend account confirmation email", %{conn: conn, user: user} do
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)
36 ObanHelpers.perform_all()
38 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
39 notify_email = Config.get([:instance, :notify_email])
40 instance_name = Config.get([:instance, :name])
43 from: {instance_name, notify_email},
44 to: {user.name, user.email},
45 html_body: email.html_body
49 test "resend account confirmation email (with nickname)", %{conn: conn, user: user} do
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)
55 ObanHelpers.perform_all()
57 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
58 notify_email = Config.get([:instance, :notify_email])
59 instance_name = Config.get([:instance, :name])
62 from: {instance_name, notify_email},
63 to: {user.name, user.email},
64 html_body: email.html_body
69 describe "getting favorites timeline of specified user" 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]
76 test "returns list of statuses favorited by specified user", %{
80 [activity | _] = insert_pair(:note_activity)
81 CommonAPI.favorite(user, activity.id)
85 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
86 |> json_response_and_validate_schema(:ok)
90 assert length(response) == 1
91 assert like["id"] == activity.id
94 test "returns favorites for specified user_id when requester is not logged in", %{
97 activity = insert(:note_activity)
98 CommonAPI.favorite(user, activity.id)
102 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
103 |> json_response_and_validate_schema(200)
105 assert length(response) == 1
108 test "returns favorited DM only when user is logged in and he is one of recipients", %{
109 current_user: current_user,
113 CommonAPI.post(current_user, %{
114 status: "Hi @#{user.nickname}!",
118 CommonAPI.favorite(user, direct.id)
120 for u <- [user, current_user] do
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)
128 assert length(response) == 1
133 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
134 |> json_response_and_validate_schema(200)
136 assert length(response) == 0
139 test "does not return others' favorited DM when user is not one of recipients", %{
143 user_two = insert(:user)
146 CommonAPI.post(user_two, %{
147 status: "Hi @#{user.nickname}!",
151 CommonAPI.favorite(user, direct.id)
155 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
156 |> json_response_and_validate_schema(:ok)
158 assert Enum.empty?(response)
161 test "paginates favorites using since_id and max_id", %{
165 activities = insert_list(10, :note_activity)
167 Enum.each(activities, fn activity ->
168 CommonAPI.favorite(user, activity.id)
171 third_activity = Enum.at(activities, 2)
172 seventh_activity = Enum.at(activities, 6)
177 "/api/v1/pleroma/accounts/#{user.id}/favourites?since_id=#{third_activity.id}&max_id=#{seventh_activity.id}"
179 |> json_response_and_validate_schema(:ok)
181 assert length(response) == 3
182 refute third_activity in response
183 refute seventh_activity in response
186 test "limits favorites using limit parameter", %{
191 |> insert_list(:note_activity)
192 |> Enum.each(fn activity ->
193 CommonAPI.favorite(user, activity.id)
198 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites?limit=3")
199 |> json_response_and_validate_schema(:ok)
201 assert length(response) == 3
204 test "returns empty response when user does not have any favorited statuses", %{
210 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
211 |> json_response_and_validate_schema(:ok)
213 assert Enum.empty?(response)
216 test "returns 404 error when specified user is not exist", %{conn: conn} do
217 conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
219 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
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)
227 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
229 assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
232 test "hides favorites for new users by default", %{conn: conn} do
234 activity = insert(:note_activity)
235 CommonAPI.favorite(user, activity.id)
237 assert user.hide_favorites
238 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
240 assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
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)
251 |> assign(:user, user)
252 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
254 assert %{"id" => _id, "subscribing" => true} =
255 json_response_and_validate_schema(ret_conn, 200)
257 conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
259 assert %{"id" => _id, "subscribing" => false} = json_response_and_validate_schema(conn, 200)
263 describe "subscribing" do
264 test "returns 404 when subscription_target not found" do
265 %{conn: conn} = oauth_access(["write:follows"])
267 conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
269 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
273 describe "unsubscribing" do
274 test "returns 404 when subscription_target not found" do
275 %{conn: conn} = oauth_access(["follow"])
277 conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
279 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
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)
289 CommonAPI.follow(user1, user2)
290 CommonAPI.follow(user1, user3)
292 User.endorse(user1, user2)
293 User.endorse(user1, user3)
295 [%{"id" => ^id2}, %{"id" => ^id3}] =
297 |> get("/api/v1/pleroma/accounts/#{id1}/endorsements")
298 |> json_response_and_validate_schema(200)
301 test "returns 404 error when specified user is not exist", %{conn: conn} do
302 conn = get(conn, "/api/v1/pleroma/accounts/test/endorsements")
304 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
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"])
315 birthday: "2001-02-12",
321 birthday: "2001-02-14",
325 user3 = insert(:user)
327 CommonAPI.follow(user, user1)
328 CommonAPI.follow(user, user2)
329 CommonAPI.follow(user, user3)
333 |> get("/api/v1/pleroma/birthdays?day=12&month=2")
334 |> json_response_and_validate_schema(:ok)
337 test "the list doesn't list friends with hidden birth date" do
338 %{user: user, conn: conn} = oauth_access(["read:accounts"])
342 birthday: "2001-02-12",
349 birthday: "2001-02-12",
353 CommonAPI.follow(user, user1)
354 CommonAPI.follow(user, user2)
358 |> get("/api/v1/pleroma/birthdays?day=12&month=2")
359 |> json_response_and_validate_schema(:ok)