logo

pleroma

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

answer_handling_test.exs (2489B)


  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.ActivityPub.Transmogrifier.AnswerHandlingTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Object
  8. alias Pleroma.Web.ActivityPub.Transmogrifier
  9. alias Pleroma.Web.CommonAPI
  10. import Pleroma.Factory
  11. setup_all do
  12. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  13. :ok
  14. end
  15. test "incoming, rewrites Note to Answer and increments vote counters" do
  16. user = insert(:user)
  17. {:ok, activity} =
  18. CommonAPI.post(user, %{
  19. status: "suya...",
  20. poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
  21. })
  22. object = Object.normalize(activity)
  23. data =
  24. File.read!("test/fixtures/mastodon-vote.json")
  25. |> Poison.decode!()
  26. |> Kernel.put_in(["to"], user.ap_id)
  27. |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
  28. |> Kernel.put_in(["object", "to"], user.ap_id)
  29. {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
  30. answer_object = Object.normalize(activity)
  31. assert answer_object.data["type"] == "Answer"
  32. assert answer_object.data["inReplyTo"] == object.data["id"]
  33. new_object = Object.get_by_ap_id(object.data["id"])
  34. assert new_object.data["replies_count"] == object.data["replies_count"]
  35. assert Enum.any?(
  36. new_object.data["oneOf"],
  37. fn
  38. %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
  39. _ -> false
  40. end
  41. )
  42. end
  43. test "outgoing, rewrites Answer to Note" do
  44. user = insert(:user)
  45. {:ok, poll_activity} =
  46. CommonAPI.post(user, %{
  47. status: "suya...",
  48. poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
  49. })
  50. poll_object = Object.normalize(poll_activity)
  51. # TODO: Replace with CommonAPI vote creation when implemented
  52. data =
  53. File.read!("test/fixtures/mastodon-vote.json")
  54. |> Poison.decode!()
  55. |> Kernel.put_in(["to"], user.ap_id)
  56. |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
  57. |> Kernel.put_in(["object", "to"], user.ap_id)
  58. {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
  59. {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
  60. assert data["object"]["type"] == "Note"
  61. end
  62. end