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 "returns favorites for specified user_id when requester is not logged in", %{
157 activity = insert(:note_activity)
158 CommonAPI.favorite(user, activity.id)
162 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
163 |> json_response(200)
165 assert length(response) == 1
168 test "returns favorited DM only when user is logged in and he is one of recipients", %{
169 current_user: current_user,
173 CommonAPI.post(current_user, %{
174 "status" => "Hi @#{user.nickname}!",
175 "visibility" => "direct"
178 CommonAPI.favorite(user, direct.id)
180 for u <- [user, current_user] do
184 |> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
185 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
186 |> json_response(:ok)
188 assert length(response) == 1
193 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
194 |> json_response(200)
196 assert length(response) == 0
199 test "does not return others' favorited DM when user is not one of recipients", %{
203 user_two = insert(:user)
206 CommonAPI.post(user_two, %{
207 "status" => "Hi @#{user.nickname}!",
208 "visibility" => "direct"
211 CommonAPI.favorite(user, direct.id)
215 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
216 |> json_response(:ok)
218 assert Enum.empty?(response)
221 test "paginates favorites using since_id and max_id", %{
225 activities = insert_list(10, :note_activity)
227 Enum.each(activities, fn activity ->
228 CommonAPI.favorite(user, activity.id)
231 third_activity = Enum.at(activities, 2)
232 seventh_activity = Enum.at(activities, 6)
236 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{
237 since_id: third_activity.id,
238 max_id: seventh_activity.id
240 |> json_response(:ok)
242 assert length(response) == 3
243 refute third_activity in response
244 refute seventh_activity in response
247 test "limits favorites using limit parameter", %{
252 |> insert_list(:note_activity)
253 |> Enum.each(fn activity ->
254 CommonAPI.favorite(user, activity.id)
259 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{limit: "3"})
260 |> json_response(:ok)
262 assert length(response) == 3
265 test "returns empty response when user does not have any favorited statuses", %{
271 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
272 |> json_response(:ok)
274 assert Enum.empty?(response)
277 test "returns 404 error when specified user is not exist", %{conn: conn} do
278 conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
280 assert json_response(conn, 404) == %{"error" => "Record not found"}
283 test "returns 403 error when user has hidden own favorites", %{conn: conn} do
284 user = insert(:user, hide_favorites: true)
285 activity = insert(:note_activity)
286 CommonAPI.favorite(user, activity.id)
288 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
290 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
293 test "hides favorites for new users by default", %{conn: conn} do
295 activity = insert(:note_activity)
296 CommonAPI.favorite(user, activity.id)
298 assert user.hide_favorites
299 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
301 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
305 describe "subscribing / unsubscribing" do
306 test "subscribing / unsubscribing to a user" do
307 %{user: user, conn: conn} = oauth_access(["follow"])
308 subscription_target = insert(:user)
312 |> assign(:user, user)
313 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
315 assert %{"id" => _id, "subscribing" => true} = json_response(ret_conn, 200)
317 conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
319 assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
323 describe "subscribing" do
324 test "returns 404 when subscription_target not found" do
325 %{conn: conn} = oauth_access(["write:follows"])
327 conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
329 assert %{"error" => "Record not found"} = json_response(conn, 404)
333 describe "unsubscribing" do
334 test "returns 404 when subscription_target not found" do
335 %{conn: conn} = oauth_access(["follow"])
337 conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
339 assert %{"error" => "Record not found"} = json_response(conn, 404)