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 test "returns poll entity for object id", %{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)
27 |> assign(:user, user)
28 |> get("/api/v1/polls/#{object.id}")
30 response = json_response(conn, 200)
31 id = to_string(object.id)
32 assert %{"id" => ^id, "expired" => false, "multiple" => false} = response
35 test "does not expose polls for private statuses", %{conn: conn} do
37 other_user = insert(:user)
40 CommonAPI.post(user, %{
41 "status" => "Pleroma does",
42 "poll" => %{"options" => ["what Mastodon't", "n't what Mastodoes"], "expires_in" => 20},
43 "visibility" => "private"
46 object = Object.normalize(activity)
50 |> assign(:user, other_user)
51 |> get("/api/v1/polls/#{object.id}")
53 assert json_response(conn, 404)
57 describe "POST /api/v1/polls/:id/votes" do
58 test "votes are added to the poll", %{conn: conn} do
60 other_user = insert(:user)
63 CommonAPI.post(user, %{
64 "status" => "A very delicious sandwich",
66 "options" => ["Lettuce", "Grilled Bacon", "Tomato"],
72 object = Object.normalize(activity)
76 |> assign(:user, other_user)
77 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
79 assert json_response(conn, 200)
80 object = Object.get_by_id(object.id)
82 assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
87 test "author can't vote", %{conn: conn} do
91 CommonAPI.post(user, %{
92 "status" => "Am I cute?",
93 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
96 object = Object.normalize(activity)
99 |> assign(:user, user)
100 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [1]})
101 |> json_response(422) == %{"error" => "Poll's author can't vote"}
103 object = Object.get_by_id(object.id)
105 refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
108 test "does not allow multiple choices on a single-choice question", %{conn: conn} do
110 other_user = insert(:user)
113 CommonAPI.post(user, %{
114 "status" => "The glass is",
115 "poll" => %{"options" => ["half empty", "half full"], "expires_in" => 20}
118 object = Object.normalize(activity)
121 |> assign(:user, other_user)
122 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1]})
123 |> json_response(422) == %{"error" => "Too many choices"}
125 object = Object.get_by_id(object.id)
127 refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
132 test "does not allow choice index to be greater than options count", %{conn: conn} do
134 other_user = insert(:user)
137 CommonAPI.post(user, %{
138 "status" => "Am I cute?",
139 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
142 object = Object.normalize(activity)
146 |> assign(:user, other_user)
147 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [2]})
149 assert json_response(conn, 422) == %{"error" => "Invalid indices"}
152 test "returns 404 error when object is not exist", %{conn: conn} do
157 |> assign(:user, user)
158 |> post("/api/v1/polls/1/votes", %{"choices" => [0]})
160 assert json_response(conn, 404) == %{"error" => "Record not found"}
163 test "returns 404 when poll is private and not available for user", %{conn: conn} do
165 other_user = insert(:user)
168 CommonAPI.post(user, %{
169 "status" => "Am I cute?",
170 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20},
171 "visibility" => "private"
174 object = Object.normalize(activity)
178 |> assign(:user, other_user)
179 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0]})
181 assert json_response(conn, 404) == %{"error" => "Record not found"}