Add more tests

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-01-12 21:39:14 +01:00
parent 0f90fd5805
commit eedf551eed
7 changed files with 100 additions and 21 deletions

View file

@ -1525,9 +1525,9 @@ def endorse(%User{} = endorser, %User{} = target) do
with max_endorsed_users <- Pleroma.Config.get([:instance, :max_endorsed_users], 0), with max_endorsed_users <- Pleroma.Config.get([:instance, :max_endorsed_users], 0),
endorsed_users <- endorsed_users <-
User.endorsed_users_relation(endorser) User.endorsed_users_relation(endorser)
|> Pleroma.Repo.all() do |> Repo.aggregate(:count, :id) do
cond 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"} {:error, "You have already pinned the maximum number of users"}
not following?(endorser, target) -> not following?(endorser, target) ->

View file

@ -529,10 +529,9 @@ def endorsements(%{assigns: %{user: user}} = conn, params) do
users = users =
user user
|> User.endorsed_users_relation(_restrict_deactivated = true) |> User.endorsed_users_relation(_restrict_deactivated = true)
|> Pleroma.Pagination.fetch_paginated(Map.put(params, :skip_order, true)) |> Pleroma.Repo.all()
conn conn
|> add_link_headers(users)
|> render("index.json", |> render("index.json",
users: users, users: users,
for: user, for: user,

View file

@ -112,7 +112,6 @@ def endorsements(%{assigns: %{user: for_user, account: user}} = conn, params) do
|> Pleroma.Repo.all() |> Pleroma.Repo.all()
conn conn
|> add_link_headers(users)
|> render("index.json", |> render("index.json",
for: for_user, for: for_user,
users: users, users: users,

View file

@ -2498,4 +2498,39 @@ defp object_id_from_created_activity(user) do
%{object: %{data: %{"id" => object_id}}} = Activity.get_by_id_with_object(id) %{object: %{data: %{"id" => object_id}}} = Activity.get_by_id_with_object(id)
object_id object_id
end 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 end

View file

@ -1207,6 +1207,18 @@ test "also unsubscribes a user" do
refute User.subscribed_to?(follower, followed) refute User.subscribed_to?(follower, followed)
end 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 test "cancels a pending follow for a local user" do
follower = insert(:user) follower = insert(:user)
followed = insert(:user, is_locked: true) followed = insert(:user, is_locked: true)

View file

@ -1845,12 +1845,9 @@ test "create a note on a user" do
setup do: clear_config([:instance, :max_endorsed_users], 1) setup do: clear_config([:instance, :max_endorsed_users], 1)
test "pin account", %{user: user, conn: conn} do test "pin account", %{user: user, conn: conn} do
%{id: id1} = insert(:user) %{id: id1} = other_user1 = insert(:user)
conn CommonAPI.follow(user, other_user1)
|> put_req_header("content-type", "application/json")
|> post("/api/v1/accounts/#{id1}/follow")
|> json_response_and_validate_schema(200)
assert %{"id" => ^id1, "endorsed" => true} = assert %{"id" => ^id1, "endorsed" => true} =
conn conn
@ -1865,19 +1862,31 @@ test "pin account", %{user: user, conn: conn} do
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
end 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 test "max pinned accounts", %{user: user, conn: conn} do
%{id: id1} = insert(:user) %{id: id1} = other_user1 = insert(:user)
%{id: id2} = insert(:user) %{id: id2} = other_user2 = insert(:user)
conn CommonAPI.follow(user, other_user1)
|> put_req_header("content-type", "application/json") CommonAPI.follow(user, other_user2)
|> 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)
conn conn
|> put_req_header("content-type", "application/json") |> put_req_header("content-type", "application/json")

View file

@ -279,4 +279,29 @@ test "returns 404 when subscription_target not found" do
assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404) assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
end end
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 end