logo

pleroma

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

question_handling_test.exs (5652B)


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