logo

pleroma

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

poll_controller_test.exs (5391B)


  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.PollControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Object
  7. alias Pleroma.Web.CommonAPI
  8. import Pleroma.Factory
  9. describe "GET /api/v1/polls/:id" do
  10. setup do: oauth_access(["read:statuses"])
  11. test "returns poll entity for object id", %{user: user, conn: conn} do
  12. {:ok, activity} =
  13. CommonAPI.post(user, %{
  14. status: "Pleroma does",
  15. poll: %{options: ["what Mastodon't", "n't what Mastodoes"], expires_in: 20}
  16. })
  17. object = Object.normalize(activity)
  18. conn = get(conn, "/api/v1/polls/#{object.id}")
  19. response = json_response_and_validate_schema(conn, 200)
  20. id = to_string(object.id)
  21. assert %{"id" => ^id, "expired" => false, "multiple" => false} = response
  22. end
  23. test "does not expose polls for private statuses", %{conn: conn} do
  24. other_user = insert(:user)
  25. {:ok, activity} =
  26. CommonAPI.post(other_user, %{
  27. status: "Pleroma does",
  28. poll: %{options: ["what Mastodon't", "n't what Mastodoes"], expires_in: 20},
  29. visibility: "private"
  30. })
  31. object = Object.normalize(activity)
  32. conn = get(conn, "/api/v1/polls/#{object.id}")
  33. assert json_response_and_validate_schema(conn, 404)
  34. end
  35. end
  36. describe "POST /api/v1/polls/:id/votes" do
  37. setup do: oauth_access(["write:statuses"])
  38. test "votes are added to the poll", %{conn: conn} do
  39. other_user = insert(:user)
  40. {:ok, activity} =
  41. CommonAPI.post(other_user, %{
  42. status: "A very delicious sandwich",
  43. poll: %{
  44. options: ["Lettuce", "Grilled Bacon", "Tomato"],
  45. expires_in: 20,
  46. multiple: true
  47. }
  48. })
  49. object = Object.normalize(activity)
  50. conn =
  51. conn
  52. |> put_req_header("content-type", "application/json")
  53. |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
  54. assert json_response_and_validate_schema(conn, 200)
  55. object = Object.get_by_id(object.id)
  56. assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
  57. total_items == 1
  58. end)
  59. end
  60. test "author can't vote", %{user: user, conn: conn} do
  61. {:ok, activity} =
  62. CommonAPI.post(user, %{
  63. status: "Am I cute?",
  64. poll: %{options: ["Yes", "No"], expires_in: 20}
  65. })
  66. object = Object.normalize(activity)
  67. assert conn
  68. |> put_req_header("content-type", "application/json")
  69. |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [1]})
  70. |> json_response_and_validate_schema(422) == %{"error" => "Poll's author can't vote"}
  71. object = Object.get_by_id(object.id)
  72. refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
  73. end
  74. test "does not allow multiple choices on a single-choice question", %{conn: conn} do
  75. other_user = insert(:user)
  76. {:ok, activity} =
  77. CommonAPI.post(other_user, %{
  78. status: "The glass is",
  79. poll: %{options: ["half empty", "half full"], expires_in: 20}
  80. })
  81. object = Object.normalize(activity)
  82. assert conn
  83. |> put_req_header("content-type", "application/json")
  84. |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1]})
  85. |> json_response_and_validate_schema(422) == %{"error" => "Too many choices"}
  86. object = Object.get_by_id(object.id)
  87. refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
  88. total_items == 1
  89. end)
  90. end
  91. test "does not allow choice index to be greater than options count", %{conn: conn} do
  92. other_user = insert(:user)
  93. {:ok, activity} =
  94. CommonAPI.post(other_user, %{
  95. status: "Am I cute?",
  96. poll: %{options: ["Yes", "No"], expires_in: 20}
  97. })
  98. object = Object.normalize(activity)
  99. conn =
  100. conn
  101. |> put_req_header("content-type", "application/json")
  102. |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [2]})
  103. assert json_response_and_validate_schema(conn, 422) == %{"error" => "Invalid indices"}
  104. end
  105. test "returns 404 error when object is not exist", %{conn: conn} do
  106. conn =
  107. conn
  108. |> put_req_header("content-type", "application/json")
  109. |> post("/api/v1/polls/1/votes", %{"choices" => [0]})
  110. assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
  111. end
  112. test "returns 404 when poll is private and not available for user", %{conn: conn} do
  113. other_user = insert(:user)
  114. {:ok, activity} =
  115. CommonAPI.post(other_user, %{
  116. status: "Am I cute?",
  117. poll: %{options: ["Yes", "No"], expires_in: 20},
  118. visibility: "private"
  119. })
  120. object = Object.normalize(activity)
  121. conn =
  122. conn
  123. |> put_req_header("content-type", "application/json")
  124. |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0]})
  125. assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
  126. end
  127. end
  128. end