logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

poll_view_test.exs (4580B)


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