1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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(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},
39 "visibility" => "private"
42 object = Object.normalize(activity)
44 conn = get(conn, "/api/v1/polls/#{object.id}")
46 assert json_response(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)
68 conn = post(conn, "/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
70 assert json_response(conn, 200)
71 object = Object.get_by_id(object.id)
73 assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
78 test "author can't vote", %{user: user, conn: conn} do
80 CommonAPI.post(user, %{
81 "status" => "Am I cute?",
82 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
85 object = Object.normalize(activity)
88 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [1]})
89 |> json_response(422) == %{"error" => "Poll's author can't vote"}
91 object = Object.get_by_id(object.id)
93 refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
96 test "does not allow multiple choices on a single-choice question", %{conn: conn} do
97 other_user = insert(:user)
100 CommonAPI.post(other_user, %{
101 "status" => "The glass is",
102 "poll" => %{"options" => ["half empty", "half full"], "expires_in" => 20}
105 object = Object.normalize(activity)
108 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1]})
109 |> json_response(422) == %{"error" => "Too many choices"}
111 object = Object.get_by_id(object.id)
113 refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
118 test "does not allow choice index to be greater than options count", %{conn: conn} do
119 other_user = insert(:user)
122 CommonAPI.post(other_user, %{
123 "status" => "Am I cute?",
124 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
127 object = Object.normalize(activity)
129 conn = post(conn, "/api/v1/polls/#{object.id}/votes", %{"choices" => [2]})
131 assert json_response(conn, 422) == %{"error" => "Invalid indices"}
134 test "returns 404 error when object is not exist", %{conn: conn} do
135 conn = post(conn, "/api/v1/polls/1/votes", %{"choices" => [0]})
137 assert json_response(conn, 404) == %{"error" => "Record not found"}
140 test "returns 404 when poll is private and not available for user", %{conn: conn} do
141 other_user = insert(:user)
144 CommonAPI.post(other_user, %{
145 "status" => "Am I cute?",
146 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20},
147 "visibility" => "private"
150 object = Object.normalize(activity)
152 conn = post(conn, "/api/v1/polls/#{object.id}/votes", %{"choices" => [0]})
154 assert json_response(conn, 404) == %{"error" => "Record not found"}