1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.PollViewTest do
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.MastodonAPI.PollView
12 import Pleroma.Factory
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 test "renders a poll" do
24 CommonAPI.post(user, %{
25 status: "Is Tenshi eating a corndog cute?",
27 options: ["absolutely!", "sure", "yes", "why are you even asking?"],
32 object = Object.normalize(activity, fetch: false)
37 id: to_string(object.id),
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}
51 result = PollView.render("show.json", %{object: object})
52 expires_at = result.expires_at
53 result = Map.delete(result, :expires_at)
55 assert result == expected
57 expires_at = NaiveDateTime.from_iso8601!(expires_at)
58 assert NaiveDateTime.diff(expires_at, NaiveDateTime.utc_now()) in 15..20
61 test "detects if it is multiple choice" do
65 CommonAPI.post(user, %{
66 status: "Which Mastodon developer is your favourite?",
68 options: ["Gargron", "Eugen"],
76 object = Object.normalize(activity, fetch: false)
78 {:ok, _votes, object} = CommonAPI.vote(voter, object, [0, 1])
86 PollView.render("show.json", %{object: object})
90 test "detects emoji" do
94 CommonAPI.post(user, %{
95 status: "What's with the smug face?",
97 options: [":blank: sip", ":blank::blank: sip", ":blank::blank::blank: sip"],
102 object = Object.normalize(activity, fetch: false)
104 assert %{emojis: [%{shortcode: "blank"}]} = PollView.render("show.json", %{object: object})
107 test "detects vote status" do
109 other_user = insert(:user)
112 CommonAPI.post(user, %{
113 status: "Which input devices do you use?",
115 options: ["mouse", "trackball", "trackpoint"],
121 object = Object.normalize(activity, fetch: false)
123 {:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
125 result = PollView.render("show.json", %{object: object, for: other_user})
127 _own_votes = result[:own_votes]
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
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})
139 assert result[:expires_at] == nil
140 assert result[:expired] == false
143 test "doesn't strips HTML tags" do
147 CommonAPI.post(user, %{
148 status: "What's with the smug face?",
151 "<input type=\"date\">",
152 "<input type=\"date\" >",
153 "<input type=\"date\"/>",
154 "<input type=\"date\"></input>"
160 object = Object.normalize(activity, fetch: false)
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}
169 } = PollView.render("show.json", %{object: object})