Merge remote-tracking branch 'upstream/develop' into restrict-origin
[akkoma] / test / web / mastodon_api / controllers / poll_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 setup do: oauth_access(["read:statuses"])
15
16 test "returns poll entity for object id", %{user: user, conn: conn} do
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 = get(conn, "/api/v1/polls/#{object.id}")
26
27 response = json_response_and_validate_schema(conn, 200)
28 id = to_string(object.id)
29 assert %{"id" => ^id, "expired" => false, "multiple" => false} = response
30 end
31
32 test "does not expose polls for private statuses", %{conn: conn} do
33 other_user = insert(:user)
34
35 {:ok, activity} =
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"
40 })
41
42 object = Object.normalize(activity)
43
44 conn = get(conn, "/api/v1/polls/#{object.id}")
45
46 assert json_response_and_validate_schema(conn, 404)
47 end
48 end
49
50 describe "POST /api/v1/polls/:id/votes" do
51 setup do: oauth_access(["write:statuses"])
52
53 test "votes are added to the poll", %{conn: conn} do
54 other_user = insert(:user)
55
56 {:ok, activity} =
57 CommonAPI.post(other_user, %{
58 status: "A very delicious sandwich",
59 poll: %{
60 options: ["Lettuce", "Grilled Bacon", "Tomato"],
61 expires_in: 20,
62 multiple: true
63 }
64 })
65
66 object = Object.normalize(activity)
67
68 conn =
69 conn
70 |> put_req_header("content-type", "application/json")
71 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
72
73 assert json_response_and_validate_schema(conn, 200)
74 object = Object.get_by_id(object.id)
75
76 assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
77 total_items == 1
78 end)
79 end
80
81 test "author can't vote", %{user: user, conn: conn} do
82 {:ok, activity} =
83 CommonAPI.post(user, %{
84 status: "Am I cute?",
85 poll: %{options: ["Yes", "No"], expires_in: 20}
86 })
87
88 object = Object.normalize(activity)
89
90 assert conn
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"}
94
95 object = Object.get_by_id(object.id)
96
97 refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
98 end
99
100 test "does not allow multiple choices on a single-choice question", %{conn: conn} do
101 other_user = insert(:user)
102
103 {:ok, activity} =
104 CommonAPI.post(other_user, %{
105 status: "The glass is",
106 poll: %{options: ["half empty", "half full"], expires_in: 20}
107 })
108
109 object = Object.normalize(activity)
110
111 assert conn
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"}
115
116 object = Object.get_by_id(object.id)
117
118 refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
119 total_items == 1
120 end)
121 end
122
123 test "does not allow choice index to be greater than options count", %{conn: conn} do
124 other_user = insert(:user)
125
126 {:ok, activity} =
127 CommonAPI.post(other_user, %{
128 status: "Am I cute?",
129 poll: %{options: ["Yes", "No"], expires_in: 20}
130 })
131
132 object = Object.normalize(activity)
133
134 conn =
135 conn
136 |> put_req_header("content-type", "application/json")
137 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [2]})
138
139 assert json_response_and_validate_schema(conn, 422) == %{"error" => "Invalid indices"}
140 end
141
142 test "returns 404 error when object is not exist", %{conn: conn} do
143 conn =
144 conn
145 |> put_req_header("content-type", "application/json")
146 |> post("/api/v1/polls/1/votes", %{"choices" => [0]})
147
148 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
149 end
150
151 test "returns 404 when poll is private and not available for user", %{conn: conn} do
152 other_user = insert(:user)
153
154 {:ok, activity} =
155 CommonAPI.post(other_user, %{
156 status: "Am I cute?",
157 poll: %{options: ["Yes", "No"], expires_in: 20},
158 visibility: "private"
159 })
160
161 object = Object.normalize(activity)
162
163 conn =
164 conn
165 |> put_req_header("content-type", "application/json")
166 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0]})
167
168 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
169 end
170 end
171 end