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