Add more tests
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
0f90fd5805
commit
eedf551eed
|
@ -1525,9 +1525,9 @@ def endorse(%User{} = endorser, %User{} = target) 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 @@ def endorsements(%{assigns: %{user: user}} = conn, params) 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 @@ def endorsements(%{assigns: %{user: for_user, account: user}} = conn, params) do
|
|||
|> Pleroma.Repo.all()
|
||||
|
||||
conn
|
||||
|> add_link_headers(users)
|
||||
|> render("index.json",
|
||||
for: for_user,
|
||||
users: users,
|
||||
|
|
|
@ -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_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 @@ test "also unsubscribes a user" 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 @@ test "create a note on a user" 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 @@ test "pin account", %{user: user, conn: conn} 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 @@ test "returns 404 when subscription_target not found" 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 New Issue