1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.PollControllerTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.Web.CommonAPI
11 import Pleroma.Factory
13 describe "GET /api/v1/polls/:id" do
14 setup do: oauth_access(["read:statuses"])
16 test "returns poll entity for object id", %{user: user, conn: conn} do
18 CommonAPI.post(user, %{
19 status: "Pleroma does",
20 poll: %{options: ["what Mastodon't", "n't what Mastodoes"], expires_in: 20}
23 object = Object.normalize(activity)
25 conn = get(conn, "/api/v1/polls/#{object.id}")
27 response = json_response_and_validate_schema(conn, 200)
28 id = to_string(object.id)
29 assert %{"id" => ^id, "expired" => false, "multiple" => false} = response
32 test "does not expose polls for private statuses", %{conn: conn} do
33 other_user = insert(:user)
36 CommonAPI.post(other_user, %{
37 status: "Pleroma does",
38 poll: %{options: ["what Mastodon't", "n't what Mastodoes"], expires_in: 20},
42 object = Object.normalize(activity)
44 conn = get(conn, "/api/v1/polls/#{object.id}")
46 assert json_response_and_validate_schema(conn, 404)
50 describe "POST /api/v1/polls/:id/votes" do
51 setup do: oauth_access(["write:statuses"])
53 test "votes are added to the poll", %{conn: conn} do
54 other_user = insert(:user)
57 CommonAPI.post(other_user, %{
58 status: "A very delicious sandwich",
60 options: ["Lettuce", "Grilled Bacon", "Tomato"],
66 object = Object.normalize(activity)
70 |> put_req_header("content-type", "application/json")
71 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
73 assert json_response_and_validate_schema(conn, 200)
74 object = Object.get_by_id(object.id)
76 assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
81 test "author can't vote", %{user: user, conn: conn} do
83 CommonAPI.post(user, %{
85 poll: %{options: ["Yes", "No"], expires_in: 20}
88 object = Object.normalize(activity)
91 |> put_req_header("content-type", "application/json")
92 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [1]})
93 |> json_response_and_validate_schema(422) == %{"error" => "Poll's author can't vote"}
95 object = Object.get_by_id(object.id)
97 refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
100 test "does not allow multiple choices on a single-choice question", %{conn: conn} do
101 other_user = insert(:user)
104 CommonAPI.post(other_user, %{
105 status: "The glass is",
106 poll: %{options: ["half empty", "half full"], expires_in: 20}
109 object = Object.normalize(activity)
112 |> put_req_header("content-type", "application/json")
113 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1]})
114 |> json_response_and_validate_schema(422) == %{"error" => "Too many choices"}
116 object = Object.get_by_id(object.id)
118 refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
123 test "does not allow choice index to be greater than options count", %{conn: conn} do
124 other_user = insert(:user)
127 CommonAPI.post(other_user, %{
128 status: "Am I cute?",
129 poll: %{options: ["Yes", "No"], expires_in: 20}
132 object = Object.normalize(activity)
136 |> put_req_header("content-type", "application/json")
137 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [2]})
139 assert json_response_and_validate_schema(conn, 422) == %{"error" => "Invalid indices"}
142 test "returns 404 error when object is not exist", %{conn: conn} do
145 |> put_req_header("content-type", "application/json")
146 |> post("/api/v1/polls/1/votes", %{"choices" => [0]})
148 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
151 test "returns 404 when poll is private and not available for user", %{conn: conn} do
152 other_user = insert(:user)
155 CommonAPI.post(other_user, %{
156 status: "Am I cute?",
157 poll: %{options: ["Yes", "No"], expires_in: 20},
158 visibility: "private"
161 object = Object.normalize(activity)
165 |> put_req_header("content-type", "application/json")
166 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0]})
168 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}