Remove vapidPublicKey from Nodeinfo
[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(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(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 = post(conn, "/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
69
70 assert json_response(conn, 200)
71 object = Object.get_by_id(object.id)
72
73 assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
74 total_items == 1
75 end)
76 end
77
78 test "author can't vote", %{user: user, conn: conn} do
79 {:ok, activity} =
80 CommonAPI.post(user, %{
81 "status" => "Am I cute?",
82 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
83 })
84
85 object = Object.normalize(activity)
86
87 assert conn
88 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [1]})
89 |> json_response(422) == %{"error" => "Poll's author can't vote"}
90
91 object = Object.get_by_id(object.id)
92
93 refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
94 end
95
96 test "does not allow multiple choices on a single-choice question", %{conn: conn} do
97 other_user = insert(:user)
98
99 {:ok, activity} =
100 CommonAPI.post(other_user, %{
101 "status" => "The glass is",
102 "poll" => %{"options" => ["half empty", "half full"], "expires_in" => 20}
103 })
104
105 object = Object.normalize(activity)
106
107 assert conn
108 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1]})
109 |> json_response(422) == %{"error" => "Too many choices"}
110
111 object = Object.get_by_id(object.id)
112
113 refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
114 total_items == 1
115 end)
116 end
117
118 test "does not allow choice index to be greater than options count", %{conn: conn} do
119 other_user = insert(:user)
120
121 {:ok, activity} =
122 CommonAPI.post(other_user, %{
123 "status" => "Am I cute?",
124 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
125 })
126
127 object = Object.normalize(activity)
128
129 conn = post(conn, "/api/v1/polls/#{object.id}/votes", %{"choices" => [2]})
130
131 assert json_response(conn, 422) == %{"error" => "Invalid indices"}
132 end
133
134 test "returns 404 error when object is not exist", %{conn: conn} do
135 conn = post(conn, "/api/v1/polls/1/votes", %{"choices" => [0]})
136
137 assert json_response(conn, 404) == %{"error" => "Record not found"}
138 end
139
140 test "returns 404 when poll is private and not available for user", %{conn: conn} do
141 other_user = insert(:user)
142
143 {:ok, activity} =
144 CommonAPI.post(other_user, %{
145 "status" => "Am I cute?",
146 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20},
147 "visibility" => "private"
148 })
149
150 object = Object.normalize(activity)
151
152 conn = post(conn, "/api/v1/polls/#{object.id}/votes", %{"choices" => [0]})
153
154 assert json_response(conn, 404) == %{"error" => "Record not found"}
155 end
156 end
157 end