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 @image "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
18 describe "POST /api/v1/pleroma/accounts/confirmation_resend" do
22 |> User.confirmation_changeset(need_confirmation: true)
23 |> User.update_and_set_cache()
25 assert user.confirmation_pending
30 setup do: clear_config([:instance, :account_activation_required], true)
32 test "resend account confirmation email", %{conn: conn, user: user} do
34 |> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
35 |> json_response(:no_content)
37 ObanHelpers.perform_all()
39 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
40 notify_email = Config.get([:instance, :notify_email])
41 instance_name = Config.get([:instance, :name])
44 from: {instance_name, notify_email},
45 to: {user.name, user.email},
46 html_body: email.html_body
51 describe "PATCH /api/v1/pleroma/accounts/update_avatar" do
52 setup do: oauth_access(["write:accounts"])
54 test "user avatar can be set", %{user: user, conn: conn} do
55 avatar_image = File.read!("test/fixtures/avatar_data_uri")
57 conn = patch(conn, "/api/v1/pleroma/accounts/update_avatar", %{img: avatar_image})
59 user = refresh_record(user)
73 assert %{"url" => _} = json_response(conn, 200)
76 test "user avatar can be reset", %{user: user, conn: conn} do
77 conn = patch(conn, "/api/v1/pleroma/accounts/update_avatar", %{img: ""})
79 user = User.get_cached_by_id(user.id)
81 assert user.avatar == nil
83 assert %{"url" => nil} = json_response(conn, 200)
87 describe "PATCH /api/v1/pleroma/accounts/update_banner" do
88 setup do: oauth_access(["write:accounts"])
90 test "can set profile banner", %{user: user, conn: conn} do
91 conn = patch(conn, "/api/v1/pleroma/accounts/update_banner", %{"banner" => @image})
93 user = refresh_record(user)
94 assert user.banner["type"] == "Image"
96 assert %{"url" => _} = json_response(conn, 200)
99 test "can reset profile banner", %{user: user, conn: conn} do
100 conn = patch(conn, "/api/v1/pleroma/accounts/update_banner", %{"banner" => ""})
102 user = refresh_record(user)
103 assert user.banner == %{}
105 assert %{"url" => nil} = json_response(conn, 200)
109 describe "PATCH /api/v1/pleroma/accounts/update_background" do
110 setup do: oauth_access(["write:accounts"])
112 test "background image can be set", %{user: user, conn: conn} do
113 conn = patch(conn, "/api/v1/pleroma/accounts/update_background", %{"img" => @image})
115 user = refresh_record(user)
116 assert user.background["type"] == "Image"
117 assert %{"url" => _} = json_response(conn, 200)
120 test "background image can be reset", %{user: user, conn: conn} do
121 conn = patch(conn, "/api/v1/pleroma/accounts/update_background", %{"img" => ""})
123 user = refresh_record(user)
124 assert user.background == %{}
125 assert %{"url" => nil} = json_response(conn, 200)
129 describe "getting favorites timeline of specified user" 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]
136 test "returns list of statuses favorited by specified user", %{
140 [activity | _] = insert_pair(:note_activity)
141 CommonAPI.favorite(user, activity.id)
145 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
146 |> json_response(:ok)
150 assert length(response) == 1
151 assert like["id"] == activity.id
154 test "does not return favorites for specified user_id when user is not logged in", %{
157 activity = insert(:note_activity)
158 CommonAPI.favorite(user, activity.id)
161 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
162 |> json_response(403)
165 test "returns favorited DM only when user is logged in and he is one of recipients", %{
166 current_user: current_user,
170 CommonAPI.post(current_user, %{
171 "status" => "Hi @#{user.nickname}!",
172 "visibility" => "direct"
175 CommonAPI.favorite(user, direct.id)
177 for u <- [user, current_user] do
181 |> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
182 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
183 |> json_response(:ok)
185 assert length(response) == 1
189 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
190 |> json_response(403)
193 test "does not return others' favorited DM when user is not one of recipients", %{
197 user_two = insert(:user)
200 CommonAPI.post(user_two, %{
201 "status" => "Hi @#{user.nickname}!",
202 "visibility" => "direct"
205 CommonAPI.favorite(user, direct.id)
209 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
210 |> json_response(:ok)
212 assert Enum.empty?(response)
215 test "paginates favorites using since_id and max_id", %{
219 activities = insert_list(10, :note_activity)
221 Enum.each(activities, fn activity ->
222 CommonAPI.favorite(user, activity.id)
225 third_activity = Enum.at(activities, 2)
226 seventh_activity = Enum.at(activities, 6)
230 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{
231 since_id: third_activity.id,
232 max_id: seventh_activity.id
234 |> json_response(:ok)
236 assert length(response) == 3
237 refute third_activity in response
238 refute seventh_activity in response
241 test "limits favorites using limit parameter", %{
246 |> insert_list(:note_activity)
247 |> Enum.each(fn activity ->
248 CommonAPI.favorite(user, activity.id)
253 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{limit: "3"})
254 |> json_response(:ok)
256 assert length(response) == 3
259 test "returns empty response when user does not have any favorited statuses", %{
265 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
266 |> json_response(:ok)
268 assert Enum.empty?(response)
271 test "returns 404 error when specified user is not exist", %{conn: conn} do
272 conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
274 assert json_response(conn, 404) == %{"error" => "Record not found"}
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)
282 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
284 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
287 test "hides favorites for new users by default", %{conn: conn} do
289 activity = insert(:note_activity)
290 CommonAPI.favorite(user, activity.id)
292 assert user.hide_favorites
293 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
295 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
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)
306 |> assign(:user, user)
307 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
309 assert %{"id" => _id, "subscribing" => true} = json_response(ret_conn, 200)
311 conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
313 assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
317 describe "subscribing" do
318 test "returns 404 when subscription_target not found" do
319 %{conn: conn} = oauth_access(["write:follows"])
321 conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
323 assert %{"error" => "Record not found"} = json_response(conn, 404)
327 describe "unsubscribing" do
328 test "returns 404 when subscription_target not found" do
329 %{conn: conn} = oauth_access(["follow"])
331 conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
333 assert %{"error" => "Record not found"} = json_response(conn, 404)