1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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(need_confirmation: true)
21 |> User.update_and_set_cache()
23 assert user.confirmation_pending
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=#{
181 |> json_response_and_validate_schema(:ok)
183 assert length(response) == 3
184 refute third_activity in response
185 refute seventh_activity in response
188 test "limits favorites using limit parameter", %{
193 |> insert_list(:note_activity)
194 |> Enum.each(fn activity ->
195 CommonAPI.favorite(user, activity.id)
200 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites?limit=3")
201 |> json_response_and_validate_schema(:ok)
203 assert length(response) == 3
206 test "returns empty response when user does not have any favorited statuses", %{
212 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
213 |> json_response_and_validate_schema(:ok)
215 assert Enum.empty?(response)
218 test "returns 404 error when specified user is not exist", %{conn: conn} do
219 conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
221 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
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)
229 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
231 assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
234 test "hides favorites for new users by default", %{conn: conn} do
236 activity = insert(:note_activity)
237 CommonAPI.favorite(user, activity.id)
239 assert user.hide_favorites
240 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
242 assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
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)
253 |> assign(:user, user)
254 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
256 assert %{"id" => _id, "subscribing" => true} =
257 json_response_and_validate_schema(ret_conn, 200)
259 conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
261 assert %{"id" => _id, "subscribing" => false} = json_response_and_validate_schema(conn, 200)
265 describe "subscribing" do
266 test "returns 404 when subscription_target not found" do
267 %{conn: conn} = oauth_access(["write:follows"])
269 conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
271 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
275 describe "unsubscribing" do
276 test "returns 404 when subscription_target not found" do
277 %{conn: conn} = oauth_access(["follow"])
279 conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
281 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)