little refactor and tests
for voted & own_votes fields in polls
This commit is contained in:
parent
6bfd497f4a
commit
3f3d64acbf
3 changed files with 101 additions and 24 deletions
|
|
@ -47,6 +47,78 @@ defmodule Pleroma.Web.MastodonAPI.PollControllerTest do
|
|||
end
|
||||
end
|
||||
|
||||
test "own_votes" do
|
||||
%{conn: conn} = oauth_access(["write:statuses", "read:statuses"])
|
||||
|
||||
other_user = insert(:user)
|
||||
|
||||
{:ok, activity} =
|
||||
CommonAPI.post(other_user, %{
|
||||
status: "A very delicious sandwich",
|
||||
poll: %{
|
||||
options: ["Lettuce", "Grilled Bacon", "Tomato"],
|
||||
expires_in: 20,
|
||||
multiple: true
|
||||
}
|
||||
})
|
||||
|
||||
object = Object.normalize(activity, fetch: false)
|
||||
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 2]})
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
object = Object.get_by_id(object.id)
|
||||
|
||||
assert [
|
||||
%{
|
||||
"name" => "Lettuce",
|
||||
"replies" => %{"totalItems" => 1, "type" => "Collection"},
|
||||
"type" => "Note"
|
||||
},
|
||||
%{
|
||||
"name" => "Grilled Bacon",
|
||||
"replies" => %{"totalItems" => 0, "type" => "Collection"},
|
||||
"type" => "Note"
|
||||
},
|
||||
%{
|
||||
"name" => "Tomato",
|
||||
"replies" => %{"totalItems" => 1, "type" => "Collection"},
|
||||
"type" => "Note"
|
||||
}
|
||||
] == object.data["anyOf"]
|
||||
|
||||
assert %{"replies" => %{"totalItems" => 0}} =
|
||||
Enum.find(object.data["anyOf"], fn %{"name" => name} -> name == "Grilled Bacon" end)
|
||||
|
||||
Enum.each(["Lettuce", "Tomato"], fn title ->
|
||||
%{"replies" => %{"totalItems" => total_items}} =
|
||||
Enum.find(object.data["anyOf"], fn %{"name" => name} -> name == title end)
|
||||
|
||||
assert total_items == 1
|
||||
end)
|
||||
|
||||
assert %{
|
||||
"own_votes" => own_votes,
|
||||
"voted" => true
|
||||
} =
|
||||
conn
|
||||
|> get("/api/v1/polls/#{object.id}")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
assert 0 in own_votes
|
||||
assert 2 in own_votes
|
||||
# for non authenticated user
|
||||
response =
|
||||
build_conn()
|
||||
|> get("/api/v1/polls/#{object.id}")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
refute Map.has_key?(response, "own_votes")
|
||||
refute Map.has_key?(response, "voted")
|
||||
end
|
||||
|
||||
describe "POST /api/v1/polls/:id/votes" do
|
||||
setup do: oauth_access(["write:statuses"])
|
||||
|
||||
|
|
@ -65,12 +137,11 @@ defmodule Pleroma.Web.MastodonAPI.PollControllerTest do
|
|||
|
||||
object = Object.normalize(activity, fetch: false)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
assert json_response_and_validate_schema(conn, 200)
|
||||
object = Object.get_by_id(object.id)
|
||||
|
||||
assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
|
||||
|
|
|
|||
|
|
@ -42,10 +42,8 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do
|
|||
%{title: "yes", votes_count: 0},
|
||||
%{title: "why are you even asking?", votes_count: 0}
|
||||
],
|
||||
voted: false,
|
||||
votes_count: 0,
|
||||
voters_count: 0,
|
||||
own_votes: []
|
||||
voters_count: 0
|
||||
}
|
||||
|
||||
result = PollView.render("show.json", %{object: object})
|
||||
|
|
@ -124,10 +122,9 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do
|
|||
|
||||
result = PollView.render("show.json", %{object: object, for: other_user})
|
||||
|
||||
_own_votes = result[:own_votes]
|
||||
|
||||
assert result[:voted] == true
|
||||
assert own_votes = [1, 2]
|
||||
assert 1 in result[:own_votes]
|
||||
assert 2 in result[:own_votes]
|
||||
assert Enum.at(result[:options], 1)[:votes_count] == 1
|
||||
assert Enum.at(result[:options], 2)[:votes_count] == 1
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue