2019-09-30 22:44:34 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 22:44:34 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.PollControllerTest do
|
2020-12-21 04:21:40 -07:00
|
|
|
use Pleroma.Web.ConnCase, async: true
|
2019-09-30 22:44:34 -06:00
|
|
|
|
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
|
|
|
describe "GET /api/v1/polls/:id" do
|
2019-12-19 07:23:27 -07:00
|
|
|
setup do: oauth_access(["read:statuses"])
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "returns poll entity for object id", %{user: user, conn: conn} do
|
2019-09-30 22:44:34 -06:00
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "Pleroma does",
|
|
|
|
poll: %{options: ["what Mastodon't", "n't what Mastodoes"], expires_in: 20}
|
2019-09-30 22:44:34 -06:00
|
|
|
})
|
|
|
|
|
2021-01-04 05:38:31 -07:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
conn = get(conn, "/api/v1/polls/#{object.id}")
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
response = json_response_and_validate_schema(conn, 200)
|
2019-09-30 22:44:34 -06:00
|
|
|
id = to_string(object.id)
|
|
|
|
assert %{"id" => ^id, "expired" => false, "multiple" => false} = response
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not expose polls for private statuses", %{conn: conn} do
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
2019-12-19 07:23:27 -07:00
|
|
|
CommonAPI.post(other_user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "Pleroma does",
|
|
|
|
poll: %{options: ["what Mastodon't", "n't what Mastodoes"], expires_in: 20},
|
|
|
|
visibility: "private"
|
2019-09-30 22:44:34 -06:00
|
|
|
})
|
|
|
|
|
2021-01-04 05:38:31 -07:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
conn = get(conn, "/api/v1/polls/#{object.id}")
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
assert json_response_and_validate_schema(conn, 404)
|
2019-09-30 22:44:34 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /api/v1/polls/:id/votes" do
|
2019-12-19 07:23:27 -07:00
|
|
|
setup do: oauth_access(["write:statuses"])
|
|
|
|
|
2019-09-30 22:44:34 -06:00
|
|
|
test "votes are added to the poll", %{conn: conn} do
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
2019-12-19 07:23:27 -07:00
|
|
|
CommonAPI.post(other_user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "A very delicious sandwich",
|
|
|
|
poll: %{
|
|
|
|
options: ["Lettuce", "Grilled Bacon", "Tomato"],
|
|
|
|
expires_in: 20,
|
|
|
|
multiple: true
|
2019-09-30 22:44:34 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-01-04 05:38:31 -07:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> put_req_header("content-type", "application/json")
|
|
|
|
|> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
assert json_response_and_validate_schema(conn, 200)
|
2019-09-30 22:44:34 -06:00
|
|
|
object = Object.get_by_id(object.id)
|
|
|
|
|
|
|
|
assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
|
|
|
|
total_items == 1
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "author can't vote", %{user: user, conn: conn} do
|
2019-09-30 22:44:34 -06:00
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "Am I cute?",
|
|
|
|
poll: %{options: ["Yes", "No"], expires_in: 20}
|
2019-09-30 22:44:34 -06:00
|
|
|
})
|
|
|
|
|
2021-01-04 05:38:31 -07:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-09-30 22:44:34 -06:00
|
|
|
|
|
|
|
assert conn
|
2020-05-05 10:14:22 -06:00
|
|
|
|> put_req_header("content-type", "application/json")
|
2019-09-30 22:44:34 -06:00
|
|
|
|> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [1]})
|
2020-05-05 10:14:22 -06:00
|
|
|
|> json_response_and_validate_schema(422) == %{"error" => "Poll's author can't vote"}
|
2019-09-30 22:44:34 -06:00
|
|
|
|
|
|
|
object = Object.get_by_id(object.id)
|
|
|
|
|
|
|
|
refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not allow multiple choices on a single-choice question", %{conn: conn} do
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
2019-12-19 07:23:27 -07:00
|
|
|
CommonAPI.post(other_user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "The glass is",
|
|
|
|
poll: %{options: ["half empty", "half full"], expires_in: 20}
|
2019-09-30 22:44:34 -06:00
|
|
|
})
|
|
|
|
|
2021-01-04 05:38:31 -07:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-09-30 22:44:34 -06:00
|
|
|
|
|
|
|
assert conn
|
2020-05-05 10:14:22 -06:00
|
|
|
|> put_req_header("content-type", "application/json")
|
2019-09-30 22:44:34 -06:00
|
|
|
|> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1]})
|
2020-05-05 10:14:22 -06:00
|
|
|
|> json_response_and_validate_schema(422) == %{"error" => "Too many choices"}
|
2019-09-30 22:44:34 -06:00
|
|
|
|
|
|
|
object = Object.get_by_id(object.id)
|
|
|
|
|
|
|
|
refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
|
|
|
|
total_items == 1
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not allow choice index to be greater than options count", %{conn: conn} do
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
2019-12-19 07:23:27 -07:00
|
|
|
CommonAPI.post(other_user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "Am I cute?",
|
|
|
|
poll: %{options: ["Yes", "No"], expires_in: 20}
|
2019-09-30 22:44:34 -06:00
|
|
|
})
|
|
|
|
|
2021-01-04 05:38:31 -07:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> put_req_header("content-type", "application/json")
|
|
|
|
|> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [2]})
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
assert json_response_and_validate_schema(conn, 422) == %{"error" => "Invalid indices"}
|
2019-09-30 22:44:34 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns 404 error when object is not exist", %{conn: conn} do
|
2020-05-05 10:14:22 -06:00
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> put_req_header("content-type", "application/json")
|
|
|
|
|> post("/api/v1/polls/1/votes", %{"choices" => [0]})
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
|
2019-09-30 22:44:34 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns 404 when poll is private and not available for user", %{conn: conn} do
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
2019-12-19 07:23:27 -07:00
|
|
|
CommonAPI.post(other_user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "Am I cute?",
|
|
|
|
poll: %{options: ["Yes", "No"], expires_in: 20},
|
|
|
|
visibility: "private"
|
2019-09-30 22:44:34 -06:00
|
|
|
})
|
|
|
|
|
2021-01-04 05:38:31 -07:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> put_req_header("content-type", "application/json")
|
|
|
|
|> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0]})
|
2019-09-30 22:44:34 -06:00
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
|
2019-09-30 22:44:34 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|