Add more tests
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
0f90fd5805
commit
eedf551eed
7 changed files with 100 additions and 21 deletions
|
@ -1525,9 +1525,9 @@ defmodule Pleroma.User do
|
|||
with max_endorsed_users <- Pleroma.Config.get([:instance, :max_endorsed_users], 0),
|
||||
endorsed_users <-
|
||||
User.endorsed_users_relation(endorser)
|
||||
|> Pleroma.Repo.all() do
|
||||
|> Repo.aggregate(:count, :id) do
|
||||
cond do
|
||||
Enum.count(endorsed_users) >= max_endorsed_users ->
|
||||
endorsed_users >= max_endorsed_users ->
|
||||
{:error, "You have already pinned the maximum number of users"}
|
||||
|
||||
not following?(endorser, target) ->
|
||||
|
|
|
@ -529,10 +529,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
|
|||
users =
|
||||
user
|
||||
|> User.endorsed_users_relation(_restrict_deactivated = true)
|
||||
|> Pleroma.Pagination.fetch_paginated(Map.put(params, :skip_order, true))
|
||||
|> Pleroma.Repo.all()
|
||||
|
||||
conn
|
||||
|> add_link_headers(users)
|
||||
|> render("index.json",
|
||||
users: users,
|
||||
for: user,
|
||||
|
|
|
@ -112,7 +112,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
|
|||
|> Pleroma.Repo.all()
|
||||
|
||||
conn
|
||||
|> add_link_headers(users)
|
||||
|> render("index.json",
|
||||
for: for_user,
|
||||
users: users,
|
||||
|
|
|
@ -2498,4 +2498,39 @@ defmodule Pleroma.UserTest do
|
|||
%{object: %{data: %{"id" => object_id}}} = Activity.get_by_id_with_object(id)
|
||||
object_id
|
||||
end
|
||||
|
||||
describe "account endorsements" do
|
||||
test "it pins people" do
|
||||
user = insert(:user)
|
||||
pinned_user = insert(:user)
|
||||
|
||||
{:ok, _pinned_user, _user} = User.follow(user, pinned_user)
|
||||
|
||||
refute User.endorses?(user, pinned_user)
|
||||
|
||||
{:ok, _user_relationship} = User.endorse(user, pinned_user)
|
||||
|
||||
assert User.endorses?(user, pinned_user)
|
||||
end
|
||||
|
||||
test "it unpins users" do
|
||||
user = insert(:user)
|
||||
pinned_user = insert(:user)
|
||||
|
||||
{:ok, _pinned_user, _user} = User.follow(user, pinned_user)
|
||||
{:ok, _user_relationship} = User.endorse(user, pinned_user)
|
||||
{:ok, _user_pin} = User.unendorse(user, pinned_user)
|
||||
|
||||
refute User.endorses?(user, pinned_user)
|
||||
end
|
||||
|
||||
test "it doesn't pin users you do not follow" do
|
||||
user = insert(:user)
|
||||
pinned_user = insert(:user)
|
||||
|
||||
assert {:error, _message} = User.endorse(user, pinned_user)
|
||||
|
||||
refute User.endorses?(user, pinned_user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1207,6 +1207,18 @@ defmodule Pleroma.Web.CommonAPITest do
|
|||
refute User.subscribed_to?(follower, followed)
|
||||
end
|
||||
|
||||
test "also unpins a user" do
|
||||
[follower, followed] = insert_pair(:user)
|
||||
{:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
|
||||
{:ok, _endorsement} = User.endorse(follower, followed)
|
||||
|
||||
assert User.endorses?(follower, followed)
|
||||
|
||||
{:ok, follower} = CommonAPI.unfollow(follower, followed)
|
||||
|
||||
refute User.endorses?(follower, followed)
|
||||
end
|
||||
|
||||
test "cancels a pending follow for a local user" do
|
||||
follower = insert(:user)
|
||||
followed = insert(:user, is_locked: true)
|
||||
|
|
|
@ -1845,12 +1845,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|
|||
setup do: clear_config([:instance, :max_endorsed_users], 1)
|
||||
|
||||
test "pin account", %{user: user, conn: conn} do
|
||||
%{id: id1} = insert(:user)
|
||||
%{id: id1} = other_user1 = insert(:user)
|
||||
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> post("/api/v1/accounts/#{id1}/follow")
|
||||
|> json_response_and_validate_schema(200)
|
||||
CommonAPI.follow(user, other_user1)
|
||||
|
||||
assert %{"id" => ^id1, "endorsed" => true} =
|
||||
conn
|
||||
|
@ -1865,19 +1862,31 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|
|||
|> json_response_and_validate_schema(200)
|
||||
end
|
||||
|
||||
test "unpin account", %{user: user, conn: conn} do
|
||||
%{id: id1} = other_user1 = insert(:user)
|
||||
|
||||
CommonAPI.follow(user, other_user1)
|
||||
User.endorse(user, other_user1)
|
||||
|
||||
assert %{"id" => ^id1, "endorsed" => false} =
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> post("/api/v1/accounts/#{id1}/unpin")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
assert [] =
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> get("/api/v1/endorsements")
|
||||
|> json_response_and_validate_schema(200)
|
||||
end
|
||||
|
||||
test "max pinned accounts", %{user: user, conn: conn} do
|
||||
%{id: id1} = insert(:user)
|
||||
%{id: id2} = insert(:user)
|
||||
%{id: id1} = other_user1 = insert(:user)
|
||||
%{id: id2} = other_user2 = insert(:user)
|
||||
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> post("/api/v1/accounts/#{id1}/follow")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> post("/api/v1/accounts/#{id2}/follow")
|
||||
|> json_response_and_validate_schema(200)
|
||||
CommonAPI.follow(user, other_user1)
|
||||
CommonAPI.follow(user, other_user2)
|
||||
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|
|
|
@ -279,4 +279,29 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
|
|||
assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
|
||||
end
|
||||
end
|
||||
|
||||
describe "account endorsements" do
|
||||
test "returns a list of pinned accounts", %{conn: conn} do
|
||||
%{id: id1} = user1 = insert(:user)
|
||||
%{id: id2} = user2 = insert(:user)
|
||||
%{id: id3} = user3 = insert(:user)
|
||||
|
||||
CommonAPI.follow(user1, user2)
|
||||
CommonAPI.follow(user1, user3)
|
||||
|
||||
User.endorse(user1, user2)
|
||||
User.endorse(user1, user3)
|
||||
|
||||
[%{"id" => ^id2}, %{"id" => ^id3}] =
|
||||
conn
|
||||
|> get("/api/v1/pleroma/accounts/#{id1}/endorsements")
|
||||
|> json_response_and_validate_schema(200)
|
||||
end
|
||||
|
||||
test "returns 404 error when specified user is not exist", %{conn: conn} do
|
||||
conn = get(conn, "/api/v1/pleroma/accounts/test/endorsements")
|
||||
|
||||
assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue