akkoma/test/web/pleroma_api/controllers/account_controller_test.exs

339 lines
10 KiB
Elixir
Raw Normal View History

2019-09-30 07:28:12 +00:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
2019-09-30 07:28:12 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
use Pleroma.Web.ConnCase
alias Pleroma.Config
alias Pleroma.Tests.ObanHelpers
alias Pleroma.User
alias Pleroma.Web.CommonAPI
import Pleroma.Factory
import Swoosh.TestAssertions
@image "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
describe "POST /api/v1/pleroma/accounts/confirmation_resend" do
setup do
{:ok, user} =
insert(:user)
|> User.confirmation_changeset(need_confirmation: true)
|> User.update_and_set_cache()
2019-09-30 07:28:12 +00:00
assert user.confirmation_pending
2019-09-30 07:28:12 +00:00
[user: user]
end
clear_config([:instance, :account_activation_required]) do
Config.put([:instance, :account_activation_required], true)
end
test "resend account confirmation email", %{conn: conn, user: user} do
conn
|> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
|> json_response(:no_content)
ObanHelpers.perform_all()
email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
notify_email = Config.get([:instance, :notify_email])
instance_name = Config.get([:instance, :name])
assert_email_sent(
from: {instance_name, notify_email},
to: {user.name, user.email},
html_body: email.html_body
)
end
end
describe "PATCH /api/v1/pleroma/accounts/update_avatar" do
setup do: oauth_access(["write:accounts"])
test "user avatar can be set", %{user: user, conn: conn} do
2019-09-30 07:28:12 +00:00
avatar_image = File.read!("test/fixtures/avatar_data_uri")
conn = patch(conn, "/api/v1/pleroma/accounts/update_avatar", %{img: avatar_image})
2019-09-30 07:28:12 +00:00
user = refresh_record(user)
assert %{
"name" => _,
"type" => _,
"url" => [
%{
"href" => _,
"mediaType" => _,
"type" => _
}
]
} = user.avatar
assert %{"url" => _} = json_response(conn, 200)
end
test "user avatar can be reset", %{user: user, conn: conn} do
conn = patch(conn, "/api/v1/pleroma/accounts/update_avatar", %{img: ""})
2019-09-30 07:28:12 +00:00
user = User.get_cached_by_id(user.id)
assert user.avatar == nil
assert %{"url" => nil} = json_response(conn, 200)
end
end
describe "PATCH /api/v1/pleroma/accounts/update_banner" do
setup do: oauth_access(["write:accounts"])
2019-09-30 07:28:12 +00:00
test "can set profile banner", %{user: user, conn: conn} do
conn = patch(conn, "/api/v1/pleroma/accounts/update_banner", %{"banner" => @image})
2019-09-30 07:28:12 +00:00
user = refresh_record(user)
assert user.banner["type"] == "Image"
2019-09-30 07:28:12 +00:00
assert %{"url" => _} = json_response(conn, 200)
end
test "can reset profile banner", %{user: user, conn: conn} do
conn = patch(conn, "/api/v1/pleroma/accounts/update_banner", %{"banner" => ""})
2019-09-30 07:28:12 +00:00
user = refresh_record(user)
assert user.banner == %{}
2019-09-30 07:28:12 +00:00
assert %{"url" => nil} = json_response(conn, 200)
end
end
describe "PATCH /api/v1/pleroma/accounts/update_background" do
setup do: oauth_access(["write:accounts"])
2019-09-30 07:28:12 +00:00
test "background image can be set", %{user: user, conn: conn} do
conn = patch(conn, "/api/v1/pleroma/accounts/update_background", %{"img" => @image})
2019-09-30 07:28:12 +00:00
user = refresh_record(user)
assert user.background["type"] == "Image"
2019-09-30 07:28:12 +00:00
assert %{"url" => _} = json_response(conn, 200)
end
test "background image can be reset", %{user: user, conn: conn} do
conn = patch(conn, "/api/v1/pleroma/accounts/update_background", %{"img" => ""})
2019-09-30 07:28:12 +00:00
user = refresh_record(user)
assert user.background == %{}
2019-09-30 07:28:12 +00:00
assert %{"url" => nil} = json_response(conn, 200)
end
end
describe "getting favorites timeline of specified user" do
setup do
[current_user, user] = insert_pair(:user, hide_favorites: false)
%{user: current_user, conn: conn} = oauth_access(["read:favourites"], user: current_user)
[current_user: current_user, user: user, conn: conn]
2019-09-30 07:28:12 +00:00
end
test "returns list of statuses favorited by specified user", %{
conn: conn,
user: user
} do
[activity | _] = insert_pair(:note_activity)
CommonAPI.favorite(user, activity.id)
2019-09-30 07:28:12 +00:00
response =
conn
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|> json_response(:ok)
[like] = response
assert length(response) == 1
assert like["id"] == activity.id
end
test "does not return favorites for specified user_id when user is not logged in", %{
2019-09-30 07:28:12 +00:00
user: user
} do
activity = insert(:note_activity)
CommonAPI.favorite(user, activity.id)
2019-09-30 07:28:12 +00:00
build_conn()
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|> json_response(403)
2019-09-30 07:28:12 +00:00
end
test "returns favorited DM only when user is logged in and he is one of recipients", %{
current_user: current_user,
user: user
} do
{:ok, direct} =
CommonAPI.post(current_user, %{
"status" => "Hi @#{user.nickname}!",
"visibility" => "direct"
})
CommonAPI.favorite(user, direct.id)
2019-09-30 07:28:12 +00:00
for u <- [user, current_user] do
response =
build_conn()
|> assign(:user, u)
|> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|> json_response(:ok)
2019-09-30 07:28:12 +00:00
assert length(response) == 1
end
2019-09-30 07:28:12 +00:00
build_conn()
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|> json_response(403)
2019-09-30 07:28:12 +00:00
end
test "does not return others' favorited DM when user is not one of recipients", %{
conn: conn,
user: user
} do
user_two = insert(:user)
{:ok, direct} =
CommonAPI.post(user_two, %{
"status" => "Hi @#{user.nickname}!",
"visibility" => "direct"
})
CommonAPI.favorite(user, direct.id)
2019-09-30 07:28:12 +00:00
response =
conn
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|> json_response(:ok)
assert Enum.empty?(response)
end
test "paginates favorites using since_id and max_id", %{
conn: conn,
user: user
} do
activities = insert_list(10, :note_activity)
Enum.each(activities, fn activity ->
CommonAPI.favorite(user, activity.id)
2019-09-30 07:28:12 +00:00
end)
third_activity = Enum.at(activities, 2)
seventh_activity = Enum.at(activities, 6)
response =
conn
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{
since_id: third_activity.id,
max_id: seventh_activity.id
})
|> json_response(:ok)
assert length(response) == 3
refute third_activity in response
refute seventh_activity in response
end
test "limits favorites using limit parameter", %{
conn: conn,
user: user
} do
7
|> insert_list(:note_activity)
|> Enum.each(fn activity ->
CommonAPI.favorite(user, activity.id)
2019-09-30 07:28:12 +00:00
end)
response =
conn
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{limit: "3"})
|> json_response(:ok)
assert length(response) == 3
end
test "returns empty response when user does not have any favorited statuses", %{
conn: conn,
user: user
} do
response =
conn
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|> json_response(:ok)
assert Enum.empty?(response)
end
test "returns 404 error when specified user is not exist", %{conn: conn} do
conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
assert json_response(conn, 404) == %{"error" => "Record not found"}
end
test "returns 403 error when user has hidden own favorites", %{conn: conn} do
user = insert(:user, hide_favorites: true)
2019-09-30 07:28:12 +00:00
activity = insert(:note_activity)
CommonAPI.favorite(user, activity.id)
2019-09-30 07:28:12 +00:00
conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
2019-09-30 07:28:12 +00:00
assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
end
test "hides favorites for new users by default", %{conn: conn} do
2019-09-30 07:28:12 +00:00
user = insert(:user)
activity = insert(:note_activity)
CommonAPI.favorite(user, activity.id)
2019-09-30 07:28:12 +00:00
assert user.hide_favorites
conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
2019-09-30 07:28:12 +00:00
assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
end
end
describe "subscribing / unsubscribing" do
test "subscribing / unsubscribing to a user" do
%{user: user, conn: conn} = oauth_access(["follow"])
2019-09-30 07:28:12 +00:00
subscription_target = insert(:user)
ret_conn =
2019-09-30 07:28:12 +00:00
conn
|> assign(:user, user)
|> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
assert %{"id" => _id, "subscribing" => true} = json_response(ret_conn, 200)
2019-09-30 07:28:12 +00:00
conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
2019-09-30 07:28:12 +00:00
assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
end
end
describe "subscribing" do
test "returns 404 when subscription_target not found" do
%{conn: conn} = oauth_access(["write:follows"])
2019-09-30 07:28:12 +00:00
conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
2019-09-30 07:28:12 +00:00
assert %{"error" => "Record not found"} = json_response(conn, 404)
end
end
describe "unsubscribing" do
test "returns 404 when subscription_target not found" do
%{conn: conn} = oauth_access(["follow"])
2019-09-30 07:28:12 +00:00
conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
2019-09-30 07:28:12 +00:00
assert %{"error" => "Record not found"} = json_response(conn, 404)
end
end
end