logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

poll_view_test.exs (4449B)


  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. defmodule Pleroma.Web.MastodonAPI.PollViewTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Object
  7. alias Pleroma.Web.CommonAPI
  8. alias Pleroma.Web.MastodonAPI.PollView
  9. import Pleroma.Factory
  10. import Tesla.Mock
  11. setup do
  12. mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  13. :ok
  14. end
  15. test "renders a poll" do
  16. user = insert(:user)
  17. {:ok, activity} =
  18. CommonAPI.post(user, %{
  19. status: "Is Tenshi eating a corndog cute?",
  20. poll: %{
  21. options: ["absolutely!", "sure", "yes", "why are you even asking?"],
  22. expires_in: 20
  23. }
  24. })
  25. object = Object.normalize(activity)
  26. expected = %{
  27. emojis: [],
  28. expired: false,
  29. id: to_string(object.id),
  30. multiple: false,
  31. options: [
  32. %{title: "absolutely!", votes_count: 0},
  33. %{title: "sure", votes_count: 0},
  34. %{title: "yes", votes_count: 0},
  35. %{title: "why are you even asking?", votes_count: 0}
  36. ],
  37. voted: false,
  38. votes_count: 0,
  39. voters_count: nil
  40. }
  41. result = PollView.render("show.json", %{object: object})
  42. expires_at = result.expires_at
  43. result = Map.delete(result, :expires_at)
  44. assert result == expected
  45. expires_at = NaiveDateTime.from_iso8601!(expires_at)
  46. assert NaiveDateTime.diff(expires_at, NaiveDateTime.utc_now()) in 15..20
  47. end
  48. test "detects if it is multiple choice" do
  49. user = insert(:user)
  50. {:ok, activity} =
  51. CommonAPI.post(user, %{
  52. status: "Which Mastodon developer is your favourite?",
  53. poll: %{
  54. options: ["Gargron", "Eugen"],
  55. expires_in: 20,
  56. multiple: true
  57. }
  58. })
  59. voter = insert(:user)
  60. object = Object.normalize(activity)
  61. {:ok, _votes, object} = CommonAPI.vote(voter, object, [0, 1])
  62. assert match?(
  63. %{
  64. multiple: true,
  65. voters_count: 1,
  66. votes_count: 2
  67. },
  68. PollView.render("show.json", %{object: object})
  69. )
  70. end
  71. test "detects emoji" do
  72. user = insert(:user)
  73. {:ok, activity} =
  74. CommonAPI.post(user, %{
  75. status: "What's with the smug face?",
  76. poll: %{
  77. options: [":blank: sip", ":blank::blank: sip", ":blank::blank::blank: sip"],
  78. expires_in: 20
  79. }
  80. })
  81. object = Object.normalize(activity)
  82. assert %{emojis: [%{shortcode: "blank"}]} = PollView.render("show.json", %{object: object})
  83. end
  84. test "detects vote status" do
  85. user = insert(:user)
  86. other_user = insert(:user)
  87. {:ok, activity} =
  88. CommonAPI.post(user, %{
  89. status: "Which input devices do you use?",
  90. poll: %{
  91. options: ["mouse", "trackball", "trackpoint"],
  92. multiple: true,
  93. expires_in: 20
  94. }
  95. })
  96. object = Object.normalize(activity)
  97. {:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
  98. result = PollView.render("show.json", %{object: object, for: other_user})
  99. assert result[:voted] == true
  100. assert Enum.at(result[:options], 1)[:votes_count] == 1
  101. assert Enum.at(result[:options], 2)[:votes_count] == 1
  102. end
  103. test "does not crash on polls with no end date" do
  104. object = Object.normalize("https://skippers-bin.com/notes/7x9tmrp97i")
  105. result = PollView.render("show.json", %{object: object})
  106. assert result[:expires_at] == nil
  107. assert result[:expired] == false
  108. end
  109. test "doesn't strips HTML tags" do
  110. user = insert(:user)
  111. {:ok, activity} =
  112. CommonAPI.post(user, %{
  113. status: "What's with the smug face?",
  114. poll: %{
  115. options: [
  116. "<input type=\"date\">",
  117. "<input type=\"date\" >",
  118. "<input type=\"date\"/>",
  119. "<input type=\"date\"></input>"
  120. ],
  121. expires_in: 20
  122. }
  123. })
  124. object = Object.normalize(activity)
  125. assert %{
  126. options: [
  127. %{title: "<input type=\"date\">", votes_count: 0},
  128. %{title: "<input type=\"date\" >", votes_count: 0},
  129. %{title: "<input type=\"date\"/>", votes_count: 0},
  130. %{title: "<input type=\"date\"></input>", votes_count: 0}
  131. ]
  132. } = PollView.render("show.json", %{object: object})
  133. end
  134. end