branch
[akkoma] / test / web / mastodon_api / controllers / poll_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.PollControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Object
9 alias Pleroma.Web.CommonAPI
10
11 import Pleroma.Factory
12
13 describe "GET /api/v1/polls/:id" do
14 test "returns poll entity for object id", %{conn: conn} do
15 user = insert(:user)
16
17 {:ok, activity} =
18 CommonAPI.post(user, %{
19 "status" => "Pleroma does",
20 "poll" => %{"options" => ["what Mastodon't", "n't what Mastodoes"], "expires_in" => 20}
21 })
22
23 object = Object.normalize(activity)
24
25 conn =
26 conn
27 |> assign(:user, user)
28 |> get("/api/v1/polls/#{object.id}")
29
30 response = json_response(conn, 200)
31 id = to_string(object.id)
32 assert %{"id" => ^id, "expired" => false, "multiple" => false} = response
33 end
34
35 test "does not expose polls for private statuses", %{conn: conn} do
36 user = insert(:user)
37 other_user = insert(:user)
38
39 {:ok, activity} =
40 CommonAPI.post(user, %{
41 "status" => "Pleroma does",
42 "poll" => %{"options" => ["what Mastodon't", "n't what Mastodoes"], "expires_in" => 20},
43 "visibility" => "private"
44 })
45
46 object = Object.normalize(activity)
47
48 conn =
49 conn
50 |> assign(:user, other_user)
51 |> get("/api/v1/polls/#{object.id}")
52
53 assert json_response(conn, 404)
54 end
55 end
56
57 describe "POST /api/v1/polls/:id/votes" do
58 test "votes are added to the poll", %{conn: conn} do
59 user = insert(:user)
60 other_user = insert(:user)
61
62 {:ok, activity} =
63 CommonAPI.post(user, %{
64 "status" => "A very delicious sandwich",
65 "poll" => %{
66 "options" => ["Lettuce", "Grilled Bacon", "Tomato"],
67 "expires_in" => 20,
68 "multiple" => true
69 }
70 })
71
72 object = Object.normalize(activity)
73
74 conn =
75 conn
76 |> assign(:user, other_user)
77 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
78
79 assert json_response(conn, 200)
80 object = Object.get_by_id(object.id)
81
82 assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
83 total_items == 1
84 end)
85 end
86
87 test "author can't vote", %{conn: conn} do
88 user = insert(:user)
89
90 {:ok, activity} =
91 CommonAPI.post(user, %{
92 "status" => "Am I cute?",
93 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
94 })
95
96 object = Object.normalize(activity)
97
98 assert conn
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"}
102
103 object = Object.get_by_id(object.id)
104
105 refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
106 end
107
108 test "does not allow multiple choices on a single-choice question", %{conn: conn} do
109 user = insert(:user)
110 other_user = insert(:user)
111
112 {:ok, activity} =
113 CommonAPI.post(user, %{
114 "status" => "The glass is",
115 "poll" => %{"options" => ["half empty", "half full"], "expires_in" => 20}
116 })
117
118 object = Object.normalize(activity)
119
120 assert conn
121 |> assign(:user, other_user)
122 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1]})
123 |> json_response(422) == %{"error" => "Too many choices"}
124
125 object = Object.get_by_id(object.id)
126
127 refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
128 total_items == 1
129 end)
130 end
131
132 test "does not allow choice index to be greater than options count", %{conn: conn} do
133 user = insert(:user)
134 other_user = insert(:user)
135
136 {:ok, activity} =
137 CommonAPI.post(user, %{
138 "status" => "Am I cute?",
139 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
140 })
141
142 object = Object.normalize(activity)
143
144 conn =
145 conn
146 |> assign(:user, other_user)
147 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [2]})
148
149 assert json_response(conn, 422) == %{"error" => "Invalid indices"}
150 end
151
152 test "returns 404 error when object is not exist", %{conn: conn} do
153 user = insert(:user)
154
155 conn =
156 conn
157 |> assign(:user, user)
158 |> post("/api/v1/polls/1/votes", %{"choices" => [0]})
159
160 assert json_response(conn, 404) == %{"error" => "Record not found"}
161 end
162
163 test "returns 404 when poll is private and not available for user", %{conn: conn} do
164 user = insert(:user)
165 other_user = insert(:user)
166
167 {:ok, activity} =
168 CommonAPI.post(user, %{
169 "status" => "Am I cute?",
170 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20},
171 "visibility" => "private"
172 })
173
174 object = Object.normalize(activity)
175
176 conn =
177 conn
178 |> assign(:user, other_user)
179 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0]})
180
181 assert json_response(conn, 404) == %{"error" => "Record not found"}
182 end
183 end
184 end