logo

pleroma

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

question_handling_test.exs (5565B)


  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.ActivityPub.Transmogrifier.QuestionHandlingTest 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 "Mastodon Question activity" do
  16. data = File.read!("test/fixtures/mastodon-question-activity.json") |> Jason.decode!()
  17. {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
  18. object = Object.normalize(activity, fetch: false)
  19. assert object.data["url"] == "https://mastodon.sdf.org/@rinpatch/102070944809637304"
  20. assert object.data["closed"] == "2019-05-11T09:03:36Z"
  21. assert object.data["context"] == activity.data["context"]
  22. assert object.data["context"] ==
  23. "tag:mastodon.sdf.org,2019-05-10:objectId=15095122:objectType=Conversation"
  24. assert object.data["anyOf"] == []
  25. assert Enum.sort(object.data["oneOf"]) ==
  26. Enum.sort([
  27. %{
  28. "name" => "25 char limit is dumb",
  29. "replies" => %{"totalItems" => 0, "type" => "Collection"},
  30. "type" => "Note"
  31. },
  32. %{
  33. "name" => "Dunno",
  34. "replies" => %{"totalItems" => 0, "type" => "Collection"},
  35. "type" => "Note"
  36. },
  37. %{
  38. "name" => "Everyone knows that!",
  39. "replies" => %{"totalItems" => 1, "type" => "Collection"},
  40. "type" => "Note"
  41. },
  42. %{
  43. "name" => "I can't even fit a funny",
  44. "replies" => %{"totalItems" => 1, "type" => "Collection"},
  45. "type" => "Note"
  46. }
  47. ])
  48. user = insert(:user)
  49. {:ok, reply_activity} = CommonAPI.post(user, %{status: "hewwo", in_reply_to_id: activity.id})
  50. reply_object = Object.normalize(reply_activity, fetch: false)
  51. assert reply_object.data["context"] == object.data["context"]
  52. end
  53. test "Mastodon Question activity with HTML tags in plaintext" do
  54. options = [
  55. %{
  56. "type" => "Note",
  57. "name" => "<input type=\"date\">",
  58. "replies" => %{"totalItems" => 0, "type" => "Collection"}
  59. },
  60. %{
  61. "type" => "Note",
  62. "name" => "<input type=\"date\"/>",
  63. "replies" => %{"totalItems" => 0, "type" => "Collection"}
  64. },
  65. %{
  66. "type" => "Note",
  67. "name" => "<input type=\"date\" />",
  68. "replies" => %{"totalItems" => 1, "type" => "Collection"}
  69. },
  70. %{
  71. "type" => "Note",
  72. "name" => "<input type=\"date\"></input>",
  73. "replies" => %{"totalItems" => 1, "type" => "Collection"}
  74. }
  75. ]
  76. data =
  77. File.read!("test/fixtures/mastodon-question-activity.json")
  78. |> Jason.decode!()
  79. |> Kernel.put_in(["object", "oneOf"], options)
  80. {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
  81. object = Object.normalize(activity, fetch: false)
  82. assert Enum.sort(object.data["oneOf"]) == Enum.sort(options)
  83. end
  84. test "Mastodon Question activity with custom emojis" do
  85. options = [
  86. %{
  87. "type" => "Note",
  88. "name" => ":blobcat:",
  89. "replies" => %{"totalItems" => 0, "type" => "Collection"}
  90. },
  91. %{
  92. "type" => "Note",
  93. "name" => ":blobfox:",
  94. "replies" => %{"totalItems" => 0, "type" => "Collection"}
  95. }
  96. ]
  97. tag = [
  98. %{
  99. "icon" => %{
  100. "type" => "Image",
  101. "url" => "https://blob.cat/emoji/custom/blobcats/blobcat.png"
  102. },
  103. "id" => "https://blob.cat/emoji/custom/blobcats/blobcat.png",
  104. "name" => ":blobcat:",
  105. "type" => "Emoji",
  106. "updated" => "1970-01-01T00:00:00Z"
  107. },
  108. %{
  109. "icon" => %{"type" => "Image", "url" => "https://blob.cat/emoji/blobfox/blobfox.png"},
  110. "id" => "https://blob.cat/emoji/blobfox/blobfox.png",
  111. "name" => ":blobfox:",
  112. "type" => "Emoji",
  113. "updated" => "1970-01-01T00:00:00Z"
  114. }
  115. ]
  116. data =
  117. File.read!("test/fixtures/mastodon-question-activity.json")
  118. |> Jason.decode!()
  119. |> Kernel.put_in(["object", "oneOf"], options)
  120. |> Kernel.put_in(["object", "tag"], tag)
  121. {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
  122. object = Object.normalize(activity, fetch: false)
  123. assert object.data["oneOf"] == options
  124. assert object.data["emoji"] == %{
  125. "blobcat" => "https://blob.cat/emoji/custom/blobcats/blobcat.png",
  126. "blobfox" => "https://blob.cat/emoji/blobfox/blobfox.png"
  127. }
  128. end
  129. test "returns same activity if received a second time" do
  130. data = File.read!("test/fixtures/mastodon-question-activity.json") |> Jason.decode!()
  131. assert {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
  132. assert {:ok, ^activity} = Transmogrifier.handle_incoming(data)
  133. end
  134. test "accepts a Question with no content" do
  135. data =
  136. File.read!("test/fixtures/mastodon-question-activity.json")
  137. |> Jason.decode!()
  138. |> Kernel.put_in(["object", "content"], "")
  139. assert {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
  140. end
  141. end