1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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)
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}
49 result = PollView.render("show.json", %{object: object})
50 expires_at = result.expires_at
51 result = Map.delete(result, :expires_at)
53 assert result == expected
55 expires_at = NaiveDateTime.from_iso8601!(expires_at)
56 assert NaiveDateTime.diff(expires_at, NaiveDateTime.utc_now()) in 15..20
59 test "detects if it is multiple choice" do
63 CommonAPI.post(user, %{
64 "status" => "Which Mastodon developer is your favourite?",
66 "options" => ["Gargron", "Eugen"],
72 object = Object.normalize(activity)
74 assert %{multiple: true} = PollView.render("show.json", %{object: object})
77 test "detects emoji" do
81 CommonAPI.post(user, %{
82 "status" => "What's with the smug face?",
84 "options" => [":blank: sip", ":blank::blank: sip", ":blank::blank::blank: sip"],
89 object = Object.normalize(activity)
91 assert %{emojis: [%{shortcode: "blank"}]} = PollView.render("show.json", %{object: object})
94 test "detects vote status" do
96 other_user = insert(:user)
99 CommonAPI.post(user, %{
100 "status" => "Which input devices do you use?",
102 "options" => ["mouse", "trackball", "trackpoint"],
108 object = Object.normalize(activity)
110 {:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
112 result = PollView.render("show.json", %{object: object, for: other_user})
114 assert result[:voted] == true
115 assert Enum.at(result[:options], 1)[:votes_count] == 1
116 assert Enum.at(result[:options], 2)[:votes_count] == 1
119 test "does not crash on polls with no end date" do
120 object = Object.normalize("https://skippers-bin.com/notes/7x9tmrp97i")
121 result = PollView.render("show.json", %{object: object})
123 assert result[:expires_at] == nil
124 assert result[:expired] == false