First
[anni] / test / pleroma / web / mastodon_api / views / poll_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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       votes_count: 0,
46       voters_count: 0
47     }
48
49     result = PollView.render("show.json", %{object: object})
50     expires_at = result.expires_at
51     result = Map.delete(result, :expires_at)
52
53     assert result == expected
54
55     expires_at = NaiveDateTime.from_iso8601!(expires_at)
56     assert NaiveDateTime.diff(expires_at, NaiveDateTime.utc_now()) in 15..20
57   end
58
59   test "detects if it is multiple choice" do
60     user = insert(:user)
61
62     {:ok, activity} =
63       CommonAPI.post(user, %{
64         status: "Which Mastodon developer is your favourite?",
65         poll: %{
66           options: ["Gargron", "Eugen"],
67           expires_in: 20,
68           multiple: true
69         }
70       })
71
72     voter = insert(:user)
73
74     object = Object.normalize(activity, fetch: false)
75
76     {:ok, _votes, object} = CommonAPI.vote(voter, object, [0, 1])
77
78     assert match?(
79              %{
80                multiple: true,
81                voters_count: 1,
82                votes_count: 2
83              },
84              PollView.render("show.json", %{object: object})
85            )
86   end
87
88   test "detects emoji" do
89     user = insert(:user)
90
91     {:ok, activity} =
92       CommonAPI.post(user, %{
93         status: "What's with the smug face?",
94         poll: %{
95           options: [":blank: sip", ":blank::blank: sip", ":blank::blank::blank: sip"],
96           expires_in: 20
97         }
98       })
99
100     object = Object.normalize(activity, fetch: false)
101
102     assert %{emojis: [%{shortcode: "blank"}]} = PollView.render("show.json", %{object: object})
103   end
104
105   test "detects vote status" do
106     user = insert(:user)
107     other_user = insert(:user)
108
109     {:ok, activity} =
110       CommonAPI.post(user, %{
111         status: "Which input devices do you use?",
112         poll: %{
113           options: ["mouse", "trackball", "trackpoint"],
114           multiple: true,
115           expires_in: 20
116         }
117       })
118
119     object = Object.normalize(activity, fetch: false)
120
121     {:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
122
123     result = PollView.render("show.json", %{object: object, for: other_user})
124
125     assert result[:voted] == true
126     assert 1 in result[:own_votes]
127     assert 2 in result[:own_votes]
128     assert Enum.at(result[:options], 1)[:votes_count] == 1
129     assert Enum.at(result[:options], 2)[:votes_count] == 1
130   end
131
132   test "does not crash on polls with no end date" do
133     object = Object.normalize("https://skippers-bin.com/notes/7x9tmrp97i", fetch: true)
134     result = PollView.render("show.json", %{object: object})
135
136     assert result[:expires_at] == nil
137     assert result[:expired] == false
138   end
139
140   test "doesn't strips HTML tags" do
141     user = insert(:user)
142
143     {:ok, activity} =
144       CommonAPI.post(user, %{
145         status: "What's with the smug face?",
146         poll: %{
147           options: [
148             "<input type=\"date\">",
149             "<input type=\"date\" >",
150             "<input type=\"date\"/>",
151             "<input type=\"date\"></input>"
152           ],
153           expires_in: 20
154         }
155       })
156
157     object = Object.normalize(activity, fetch: false)
158
159     assert %{
160              options: [
161                %{title: "<input type=\"date\">", votes_count: 0},
162                %{title: "<input type=\"date\" >", votes_count: 0},
163                %{title: "<input type=\"date\"/>", votes_count: 0},
164                %{title: "<input type=\"date\"></input>", votes_count: 0}
165              ]
166            } = PollView.render("show.json", %{object: object})
167   end
168 end