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 |> put_req_header("content-type", "application/json")
35 |> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
36 |> json_response_and_validate_schema(:no_content)
38 ObanHelpers.perform_all()
40 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
41 notify_email = Config.get([:instance, :notify_email])
42 instance_name = Config.get([:instance, :name])
45 from: {instance_name, notify_email},
46 to: {user.name, user.email},
47 html_body: email.html_body
51 test "resend account confirmation email (with nickname)", %{conn: conn, user: user} do
53 |> put_req_header("content-type", "application/json")
54 |> post("/api/v1/pleroma/accounts/confirmation_resend?nickname=#{user.nickname}")
55 |> json_response_and_validate_schema(:no_content)
57 ObanHelpers.perform_all()
59 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
60 notify_email = Config.get([:instance, :notify_email])
61 instance_name = Config.get([:instance, :name])
64 from: {instance_name, notify_email},
65 to: {user.name, user.email},
66 html_body: email.html_body
71 describe "PATCH /api/v1/pleroma/accounts/update_avatar" do
72 setup do: oauth_access(["write:accounts"])
74 test "user avatar can be set", %{user: user, conn: conn} do
75 avatar_image = File.read!("test/fixtures/avatar_data_uri")
79 |> put_req_header("content-type", "multipart/form-data")
80 |> patch("/api/v1/pleroma/accounts/update_avatar", %{img: avatar_image})
82 user = refresh_record(user)
96 assert %{"url" => _} = json_response_and_validate_schema(conn, 200)
99 test "user avatar can be reset", %{user: user, conn: conn} do
102 |> put_req_header("content-type", "multipart/form-data")
103 |> patch("/api/v1/pleroma/accounts/update_avatar", %{img: ""})
105 user = User.get_cached_by_id(user.id)
107 assert user.avatar == nil
109 assert %{"url" => nil} = json_response_and_validate_schema(conn, 200)
113 describe "PATCH /api/v1/pleroma/accounts/update_banner" do
114 setup do: oauth_access(["write:accounts"])
116 test "can set profile banner", %{user: user, conn: conn} do
119 |> put_req_header("content-type", "multipart/form-data")
120 |> patch("/api/v1/pleroma/accounts/update_banner", %{"banner" => @image})
122 user = refresh_record(user)
123 assert user.banner["type"] == "Image"
125 assert %{"url" => _} = json_response_and_validate_schema(conn, 200)
128 test "can reset profile banner", %{user: user, conn: conn} do
131 |> put_req_header("content-type", "multipart/form-data")
132 |> patch("/api/v1/pleroma/accounts/update_banner", %{"banner" => ""})
134 user = refresh_record(user)
135 assert user.banner == %{}
137 assert %{"url" => nil} = json_response_and_validate_schema(conn, 200)
141 describe "PATCH /api/v1/pleroma/accounts/update_background" do
142 setup do: oauth_access(["write:accounts"])
144 test "background image can be set", %{user: user, conn: conn} do
147 |> put_req_header("content-type", "multipart/form-data")
148 |> patch("/api/v1/pleroma/accounts/update_background", %{"img" => @image})
150 user = refresh_record(user)
151 assert user.background["type"] == "Image"
152 # assert %{"url" => _} = json_response(conn, 200)
153 assert %{"url" => _} = json_response_and_validate_schema(conn, 200)
156 test "background image can be reset", %{user: user, conn: conn} do
159 |> put_req_header("content-type", "multipart/form-data")
160 |> patch("/api/v1/pleroma/accounts/update_background", %{"img" => ""})
162 user = refresh_record(user)
163 assert user.background == %{}
164 assert %{"url" => nil} = json_response_and_validate_schema(conn, 200)
168 describe "getting favorites timeline of specified user" do
170 [current_user, user] = insert_pair(:user, hide_favorites: false)
171 %{user: current_user, conn: conn} = oauth_access(["read:favourites"], user: current_user)
172 [current_user: current_user, user: user, conn: conn]
175 test "returns list of statuses favorited by specified user", %{
179 [activity | _] = insert_pair(:note_activity)
180 CommonAPI.favorite(user, activity.id)
184 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
185 |> json_response_and_validate_schema(:ok)
189 assert length(response) == 1
190 assert like["id"] == activity.id
193 test "returns favorites for specified user_id when requester is not logged in", %{
196 activity = insert(:note_activity)
197 CommonAPI.favorite(user, activity.id)
201 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
202 |> json_response_and_validate_schema(200)
204 assert length(response) == 1
207 test "returns favorited DM only when user is logged in and he is one of recipients", %{
208 current_user: current_user,
212 CommonAPI.post(current_user, %{
213 status: "Hi @#{user.nickname}!",
217 CommonAPI.favorite(user, direct.id)
219 for u <- [user, current_user] do
223 |> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
224 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
225 |> json_response_and_validate_schema(:ok)
227 assert length(response) == 1
232 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
233 |> json_response_and_validate_schema(200)
235 assert length(response) == 0
238 test "does not return others' favorited DM when user is not one of recipients", %{
242 user_two = insert(:user)
245 CommonAPI.post(user_two, %{
246 status: "Hi @#{user.nickname}!",
250 CommonAPI.favorite(user, direct.id)
254 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
255 |> json_response_and_validate_schema(:ok)
257 assert Enum.empty?(response)
260 test "paginates favorites using since_id and max_id", %{
264 activities = insert_list(10, :note_activity)
266 Enum.each(activities, fn activity ->
267 CommonAPI.favorite(user, activity.id)
270 third_activity = Enum.at(activities, 2)
271 seventh_activity = Enum.at(activities, 6)
276 "/api/v1/pleroma/accounts/#{user.id}/favourites?since_id=#{third_activity.id}&max_id=#{
280 |> json_response_and_validate_schema(:ok)
282 assert length(response) == 3
283 refute third_activity in response
284 refute seventh_activity in response
287 test "limits favorites using limit parameter", %{
292 |> insert_list(:note_activity)
293 |> Enum.each(fn activity ->
294 CommonAPI.favorite(user, activity.id)
299 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites?limit=3")
300 |> json_response_and_validate_schema(:ok)
302 assert length(response) == 3
305 test "returns empty response when user does not have any favorited statuses", %{
311 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
312 |> json_response_and_validate_schema(:ok)
314 assert Enum.empty?(response)
317 test "returns 404 error when specified user is not exist", %{conn: conn} do
318 conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
320 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
323 test "returns 403 error when user has hidden own favorites", %{conn: conn} do
324 user = insert(:user, hide_favorites: true)
325 activity = insert(:note_activity)
326 CommonAPI.favorite(user, activity.id)
328 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
330 assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
333 test "hides favorites for new users by default", %{conn: conn} do
335 activity = insert(:note_activity)
336 CommonAPI.favorite(user, activity.id)
338 assert user.hide_favorites
339 conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
341 assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
345 describe "subscribing / unsubscribing" do
346 test "subscribing / unsubscribing to a user" do
347 %{user: user, conn: conn} = oauth_access(["follow"])
348 subscription_target = insert(:user)
352 |> assign(:user, user)
353 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
355 assert %{"id" => _id, "subscribing" => true} =
356 json_response_and_validate_schema(ret_conn, 200)
358 conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
360 assert %{"id" => _id, "subscribing" => false} = json_response_and_validate_schema(conn, 200)
364 describe "subscribing" do
365 test "returns 404 when subscription_target not found" do
366 %{conn: conn} = oauth_access(["write:follows"])
368 conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
370 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
374 describe "unsubscribing" do
375 test "returns 404 when subscription_target not found" do
376 %{conn: conn} = oauth_access(["follow"])
378 conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
380 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)