Include own_votes in the poll data
[akkoma] / test / pleroma / web / mastodon_api / views / poll_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.PollViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Object
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.MastodonAPI.PollView
11
12 import Pleroma.Factory
13 import Tesla.Mock
14
15 setup do
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 :ok
18 end
19
20 test "renders a poll" do
21 user = insert(:user)
22
23 {:ok, activity} =
24 CommonAPI.post(user, %{
25 status: "Is Tenshi eating a corndog cute?",
26 poll: %{
27 options: ["absolutely!", "sure", "yes", "why are you even asking?"],
28 expires_in: 20
29 }
30 })
31
32 object = Object.normalize(activity, fetch: false)
33
34 expected = %{
35 emojis: [],
36 expired: false,
37 id: to_string(object.id),
38 multiple: false,
39 options: [
40 %{title: "absolutely!", votes_count: 0},
41 %{title: "sure", votes_count: 0},
42 %{title: "yes", votes_count: 0},
43 %{title: "why are you even asking?", votes_count: 0}
44 ],
45 voted: false,
46 votes_count: 0,
47 voters_count: 0,
48 own_votes: []
49 }
50
51 result = PollView.render("show.json", %{object: object})
52 expires_at = result.expires_at
53 result = Map.delete(result, :expires_at)
54
55 assert result == expected
56
57 expires_at = NaiveDateTime.from_iso8601!(expires_at)
58 assert NaiveDateTime.diff(expires_at, NaiveDateTime.utc_now()) in 15..20
59 end
60
61 test "detects if it is multiple choice" do
62 user = insert(:user)
63
64 {:ok, activity} =
65 CommonAPI.post(user, %{
66 status: "Which Mastodon developer is your favourite?",
67 poll: %{
68 options: ["Gargron", "Eugen"],
69 expires_in: 20,
70 multiple: true
71 }
72 })
73
74 voter = insert(:user)
75
76 object = Object.normalize(activity, fetch: false)
77
78 {:ok, _votes, object} = CommonAPI.vote(voter, object, [0, 1])
79
80 assert match?(
81 %{
82 multiple: true,
83 voters_count: 1,
84 votes_count: 2
85 },
86 PollView.render("show.json", %{object: object})
87 )
88 end
89
90 test "detects emoji" do
91 user = insert(:user)
92
93 {:ok, activity} =
94 CommonAPI.post(user, %{
95 status: "What's with the smug face?",
96 poll: %{
97 options: [":blank: sip", ":blank::blank: sip", ":blank::blank::blank: sip"],
98 expires_in: 20
99 }
100 })
101
102 object = Object.normalize(activity, fetch: false)
103
104 assert %{emojis: [%{shortcode: "blank"}]} = PollView.render("show.json", %{object: object})
105 end
106
107 test "detects vote status" do
108 user = insert(:user)
109 other_user = insert(:user)
110
111 {:ok, activity} =
112 CommonAPI.post(user, %{
113 status: "Which input devices do you use?",
114 poll: %{
115 options: ["mouse", "trackball", "trackpoint"],
116 multiple: true,
117 expires_in: 20
118 }
119 })
120
121 object = Object.normalize(activity, fetch: false)
122
123 {:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
124
125 result = PollView.render("show.json", %{object: object, for: other_user})
126
127 _own_votes = result[:own_votes]
128
129 assert result[:voted] == true
130 assert own_votes = [1, 2]
131 assert Enum.at(result[:options], 1)[:votes_count] == 1
132 assert Enum.at(result[:options], 2)[:votes_count] == 1
133 end
134
135 test "does not crash on polls with no end date" do
136 object = Object.normalize("https://skippers-bin.com/notes/7x9tmrp97i", fetch: true)
137 result = PollView.render("show.json", %{object: object})
138
139 assert result[:expires_at] == nil
140 assert result[:expired] == false
141 end
142
143 test "doesn't strips HTML tags" do
144 user = insert(:user)
145
146 {:ok, activity} =
147 CommonAPI.post(user, %{
148 status: "What's with the smug face?",
149 poll: %{
150 options: [
151 "<input type=\"date\">",
152 "<input type=\"date\" >",
153 "<input type=\"date\"/>",
154 "<input type=\"date\"></input>"
155 ],
156 expires_in: 20
157 }
158 })
159
160 object = Object.normalize(activity, fetch: false)
161
162 assert %{
163 options: [
164 %{title: "<input type=\"date\">", votes_count: 0},
165 %{title: "<input type=\"date\" >", votes_count: 0},
166 %{title: "<input type=\"date\"/>", votes_count: 0},
167 %{title: "<input type=\"date\"></input>", votes_count: 0}
168 ]
169 } = PollView.render("show.json", %{object: object})
170 end
171 end