logo

pleroma

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

chat_message_test.exs (5379B)


  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.ChatMessageTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.Activity
  8. alias Pleroma.Chat
  9. alias Pleroma.Object
  10. alias Pleroma.Web.ActivityPub.Transmogrifier
  11. describe "handle_incoming" do
  12. test "handles chonks with attachment" do
  13. data = %{
  14. "@context" => "https://www.w3.org/ns/activitystreams",
  15. "actor" => "https://honk.tedunangst.com/u/tedu",
  16. "id" => "https://honk.tedunangst.com/u/tedu/honk/x6gt8X8PcyGkQcXxzg1T",
  17. "object" => %{
  18. "attachment" => [
  19. %{
  20. "mediaType" => "image/jpeg",
  21. "name" => "298p3RG7j27tfsZ9RQ.jpg",
  22. "summary" => "298p3RG7j27tfsZ9RQ.jpg",
  23. "type" => "Document",
  24. "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
  25. }
  26. ],
  27. "attributedTo" => "https://honk.tedunangst.com/u/tedu",
  28. "content" => "",
  29. "id" => "https://honk.tedunangst.com/u/tedu/chonk/26L4wl5yCbn4dr4y1b",
  30. "published" => "2020-05-18T01:13:03Z",
  31. "to" => [
  32. "https://dontbulling.me/users/lain"
  33. ],
  34. "type" => "ChatMessage"
  35. },
  36. "published" => "2020-05-18T01:13:03Z",
  37. "to" => [
  38. "https://dontbulling.me/users/lain"
  39. ],
  40. "type" => "Create"
  41. }
  42. _user = insert(:user, ap_id: data["actor"])
  43. _user = insert(:user, ap_id: hd(data["to"]))
  44. assert {:ok, _activity} = Transmogrifier.handle_incoming(data)
  45. end
  46. test "it rejects messages that don't contain content" do
  47. data =
  48. File.read!("test/fixtures/create-chat-message.json")
  49. |> Poison.decode!()
  50. object =
  51. data["object"]
  52. |> Map.delete("content")
  53. data =
  54. data
  55. |> Map.put("object", object)
  56. _author =
  57. insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
  58. _recipient =
  59. insert(:user,
  60. ap_id: List.first(data["to"]),
  61. local: true,
  62. last_refreshed_at: DateTime.utc_now()
  63. )
  64. {:error, _} = Transmogrifier.handle_incoming(data)
  65. end
  66. test "it rejects messages that don't concern local users" do
  67. data =
  68. File.read!("test/fixtures/create-chat-message.json")
  69. |> Poison.decode!()
  70. _author =
  71. insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
  72. _recipient =
  73. insert(:user,
  74. ap_id: List.first(data["to"]),
  75. local: false,
  76. last_refreshed_at: DateTime.utc_now()
  77. )
  78. {:error, _} = Transmogrifier.handle_incoming(data)
  79. end
  80. test "it rejects messages where the `to` field of activity and object don't match" do
  81. data =
  82. File.read!("test/fixtures/create-chat-message.json")
  83. |> Poison.decode!()
  84. author = insert(:user, ap_id: data["actor"])
  85. _recipient = insert(:user, ap_id: List.first(data["to"]))
  86. data =
  87. data
  88. |> Map.put("to", author.ap_id)
  89. assert match?({:error, _}, Transmogrifier.handle_incoming(data))
  90. refute Object.get_by_ap_id(data["object"]["id"])
  91. end
  92. test "it fetches the actor if they aren't in our system" do
  93. Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  94. data =
  95. File.read!("test/fixtures/create-chat-message.json")
  96. |> Poison.decode!()
  97. |> Map.put("actor", "http://mastodon.example.org/users/admin")
  98. |> put_in(["object", "actor"], "http://mastodon.example.org/users/admin")
  99. _recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
  100. {:ok, %Activity{} = _activity} = Transmogrifier.handle_incoming(data)
  101. end
  102. test "it doesn't work for deactivated users" do
  103. data =
  104. File.read!("test/fixtures/create-chat-message.json")
  105. |> Poison.decode!()
  106. _author =
  107. insert(:user,
  108. ap_id: data["actor"],
  109. local: false,
  110. last_refreshed_at: DateTime.utc_now(),
  111. deactivated: true
  112. )
  113. _recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
  114. assert {:error, _} = Transmogrifier.handle_incoming(data)
  115. end
  116. test "it inserts it and creates a chat" do
  117. data =
  118. File.read!("test/fixtures/create-chat-message.json")
  119. |> Poison.decode!()
  120. author =
  121. insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
  122. recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
  123. {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
  124. assert activity.local == false
  125. assert activity.actor == author.ap_id
  126. assert activity.recipients == [recipient.ap_id, author.ap_id]
  127. %Object{} = object = Object.get_by_ap_id(activity.data["object"])
  128. assert object
  129. assert object.data["content"] == "You expected a cute girl? Too bad. alert(&#39;XSS&#39;)"
  130. assert match?(%{"firefox" => _}, object.data["emoji"])
  131. refute Chat.get(author.id, recipient.ap_id)
  132. assert Chat.get(recipient.id, author.ap_id)
  133. end
  134. end
  135. end