logo

pleroma

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

chat_validation_test.exs (7054B)


  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.ObjectValidators.ChatValidationTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Object
  7. alias Pleroma.Web.ActivityPub.ActivityPub
  8. alias Pleroma.Web.ActivityPub.Builder
  9. alias Pleroma.Web.ActivityPub.ObjectValidator
  10. alias Pleroma.Web.CommonAPI
  11. import Pleroma.Factory
  12. describe "chat message create activities" do
  13. test "it is invalid if the object already exists" do
  14. user = insert(:user)
  15. recipient = insert(:user)
  16. {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
  17. object = Object.normalize(activity, false)
  18. {:ok, create_data, _} = Builder.create(user, object.data, [recipient.ap_id])
  19. {:error, cng} = ObjectValidator.validate(create_data, [])
  20. assert {:object, {"The object to create already exists", []}} in cng.errors
  21. end
  22. test "it is invalid if the object data has a different `to` or `actor` field" do
  23. user = insert(:user)
  24. recipient = insert(:user)
  25. {:ok, object_data, _} = Builder.chat_message(recipient, user.ap_id, "Hey")
  26. {:ok, create_data, _} = Builder.create(user, object_data, [recipient.ap_id])
  27. {:error, cng} = ObjectValidator.validate(create_data, [])
  28. assert {:to, {"Recipients don't match with object recipients", []}} in cng.errors
  29. assert {:actor, {"Actor doesn't match with object actor", []}} in cng.errors
  30. end
  31. end
  32. describe "chat messages" do
  33. setup do
  34. clear_config([:instance, :remote_limit])
  35. user = insert(:user)
  36. recipient = insert(:user, local: false)
  37. {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey :firefox:")
  38. %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
  39. end
  40. test "let's through some basic html", %{user: user, recipient: recipient} do
  41. {:ok, valid_chat_message, _} =
  42. Builder.chat_message(
  43. user,
  44. recipient.ap_id,
  45. "hey <a href='https://example.org'>example</a> <script>alert('uguu')</script>"
  46. )
  47. assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
  48. assert object["content"] ==
  49. "hey <a href=\"https://example.org\">example</a> alert(&#39;uguu&#39;)"
  50. end
  51. test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
  52. assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
  53. assert Map.put(valid_chat_message, "attachment", nil) == object
  54. assert match?(%{"firefox" => _}, object["emoji"])
  55. end
  56. test "validates for a basic object with an attachment", %{
  57. valid_chat_message: valid_chat_message,
  58. user: user
  59. } do
  60. file = %Plug.Upload{
  61. content_type: "image/jpg",
  62. path: Path.absname("test/fixtures/image.jpg"),
  63. filename: "an_image.jpg"
  64. }
  65. {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
  66. valid_chat_message =
  67. valid_chat_message
  68. |> Map.put("attachment", attachment.data)
  69. assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
  70. assert object["attachment"]
  71. end
  72. test "validates for a basic object with an attachment in an array", %{
  73. valid_chat_message: valid_chat_message,
  74. user: user
  75. } do
  76. file = %Plug.Upload{
  77. content_type: "image/jpg",
  78. path: Path.absname("test/fixtures/image.jpg"),
  79. filename: "an_image.jpg"
  80. }
  81. {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
  82. valid_chat_message =
  83. valid_chat_message
  84. |> Map.put("attachment", [attachment.data])
  85. assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
  86. assert object["attachment"]
  87. end
  88. test "validates for a basic object with an attachment but without content", %{
  89. valid_chat_message: valid_chat_message,
  90. user: user
  91. } do
  92. file = %Plug.Upload{
  93. content_type: "image/jpg",
  94. path: Path.absname("test/fixtures/image.jpg"),
  95. filename: "an_image.jpg"
  96. }
  97. {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
  98. valid_chat_message =
  99. valid_chat_message
  100. |> Map.put("attachment", attachment.data)
  101. |> Map.delete("content")
  102. assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
  103. assert object["attachment"]
  104. end
  105. test "does not validate if the message has no content", %{
  106. valid_chat_message: valid_chat_message
  107. } do
  108. contentless =
  109. valid_chat_message
  110. |> Map.delete("content")
  111. refute match?({:ok, _object, _meta}, ObjectValidator.validate(contentless, []))
  112. end
  113. test "does not validate if the message is longer than the remote_limit", %{
  114. valid_chat_message: valid_chat_message
  115. } do
  116. Pleroma.Config.put([:instance, :remote_limit], 2)
  117. refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
  118. end
  119. test "does not validate if the recipient is blocking the actor", %{
  120. valid_chat_message: valid_chat_message,
  121. user: user,
  122. recipient: recipient
  123. } do
  124. Pleroma.User.block(recipient, user)
  125. refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
  126. end
  127. test "does not validate if the recipient is not accepting chat messages", %{
  128. valid_chat_message: valid_chat_message,
  129. recipient: recipient
  130. } do
  131. recipient
  132. |> Ecto.Changeset.change(%{accepts_chat_messages: false})
  133. |> Pleroma.Repo.update!()
  134. refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
  135. end
  136. test "does not validate if the actor or the recipient is not in our system", %{
  137. valid_chat_message: valid_chat_message
  138. } do
  139. chat_message =
  140. valid_chat_message
  141. |> Map.put("actor", "https://raymoo.com/raymoo")
  142. {:error, _} = ObjectValidator.validate(chat_message, [])
  143. chat_message =
  144. valid_chat_message
  145. |> Map.put("to", ["https://raymoo.com/raymoo"])
  146. {:error, _} = ObjectValidator.validate(chat_message, [])
  147. end
  148. test "does not validate for a message with multiple recipients", %{
  149. valid_chat_message: valid_chat_message,
  150. user: user,
  151. recipient: recipient
  152. } do
  153. chat_message =
  154. valid_chat_message
  155. |> Map.put("to", [user.ap_id, recipient.ap_id])
  156. assert {:error, _} = ObjectValidator.validate(chat_message, [])
  157. end
  158. test "does not validate if it doesn't concern local users" do
  159. user = insert(:user, local: false)
  160. recipient = insert(:user, local: false)
  161. {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
  162. assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
  163. end
  164. end
  165. end