logo

pleroma

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

attachment_validator_test.exs (5929B)


  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.ObjectValidators.AttachmentValidatorTest do
  5. use Pleroma.DataCase, async: true
  6. alias Pleroma.UnstubbedConfigMock, as: ConfigMock
  7. alias Pleroma.Web.ActivityPub.ActivityPub
  8. alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
  9. import Mox
  10. import Pleroma.Factory
  11. describe "attachments" do
  12. test "fails without url" do
  13. attachment = %{
  14. "mediaType" => "",
  15. "name" => "",
  16. "summary" => "298p3RG7j27tfsZ9RQ.jpg",
  17. "type" => "Document"
  18. }
  19. assert {:error, _cng} =
  20. AttachmentValidator.cast_and_validate(attachment)
  21. |> Ecto.Changeset.apply_action(:insert)
  22. end
  23. test "works with honkerific attachments" do
  24. attachment = %{
  25. "mediaType" => "",
  26. "name" => "",
  27. "summary" => "298p3RG7j27tfsZ9RQ.jpg",
  28. "type" => "Document",
  29. "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
  30. }
  31. assert {:ok, attachment} =
  32. AttachmentValidator.cast_and_validate(attachment)
  33. |> Ecto.Changeset.apply_action(:insert)
  34. assert attachment.mediaType == "application/octet-stream"
  35. end
  36. test "works with an unknown but valid mime type" do
  37. attachment = %{
  38. "mediaType" => "x-custom/x-type",
  39. "type" => "Document",
  40. "url" => "https://example.org"
  41. }
  42. assert {:ok, attachment} =
  43. AttachmentValidator.cast_and_validate(attachment)
  44. |> Ecto.Changeset.apply_action(:insert)
  45. assert attachment.mediaType == "x-custom/x-type"
  46. end
  47. test "works with invalid mime types" do
  48. attachment = %{
  49. "mediaType" => "x-customx-type",
  50. "type" => "Document",
  51. "url" => "https://example.org"
  52. }
  53. assert {:ok, attachment} =
  54. AttachmentValidator.cast_and_validate(attachment)
  55. |> Ecto.Changeset.apply_action(:insert)
  56. assert attachment.mediaType == "application/octet-stream"
  57. attachment = %{
  58. "mediaType" => "https://example.org",
  59. "type" => "Document",
  60. "url" => "https://example.org"
  61. }
  62. assert {:ok, attachment} =
  63. AttachmentValidator.cast_and_validate(attachment)
  64. |> Ecto.Changeset.apply_action(:insert)
  65. assert attachment.mediaType == "application/octet-stream"
  66. end
  67. test "it turns mastodon attachments into our attachments" do
  68. attachment = %{
  69. "url" =>
  70. "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
  71. "type" => "Document",
  72. "name" => nil,
  73. "mediaType" => "image/jpeg",
  74. "blurhash" => "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
  75. }
  76. {:ok, attachment} =
  77. AttachmentValidator.cast_and_validate(attachment)
  78. |> Ecto.Changeset.apply_action(:insert)
  79. assert [
  80. %{
  81. href:
  82. "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
  83. type: "Link",
  84. mediaType: "image/jpeg"
  85. }
  86. ] = attachment.url
  87. assert attachment.mediaType == "image/jpeg"
  88. assert attachment.blurhash == "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
  89. end
  90. test "it handles our own uploads" do
  91. user = insert(:user)
  92. file = %Plug.Upload{
  93. content_type: "image/jpeg",
  94. path: Path.absname("test/fixtures/image.jpg"),
  95. filename: "an_image.jpg"
  96. }
  97. ConfigMock
  98. |> stub_with(Pleroma.Test.StaticConfig)
  99. {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
  100. {:ok, attachment} =
  101. attachment.data
  102. |> AttachmentValidator.cast_and_validate()
  103. |> Ecto.Changeset.apply_action(:insert)
  104. assert attachment.mediaType == "image/jpeg"
  105. end
  106. test "it handles image dimensions" do
  107. attachment = %{
  108. "url" => [
  109. %{
  110. "type" => "Link",
  111. "mediaType" => "image/jpeg",
  112. "href" => "https://example.com/images/1.jpg",
  113. "width" => 200,
  114. "height" => 100
  115. }
  116. ],
  117. "type" => "Document",
  118. "name" => nil,
  119. "mediaType" => "image/jpeg"
  120. }
  121. {:ok, attachment} =
  122. AttachmentValidator.cast_and_validate(attachment)
  123. |> Ecto.Changeset.apply_action(:insert)
  124. assert [
  125. %{
  126. href: "https://example.com/images/1.jpg",
  127. type: "Link",
  128. mediaType: "image/jpeg",
  129. width: 200,
  130. height: 100
  131. }
  132. ] = attachment.url
  133. assert attachment.mediaType == "image/jpeg"
  134. end
  135. test "it transforms image dimensions to our internal format" do
  136. attachment = %{
  137. "type" => "Document",
  138. "name" => "Hello world",
  139. "url" => "https://media.example.tld/1.jpg",
  140. "width" => 880,
  141. "height" => 960,
  142. "mediaType" => "image/jpeg",
  143. "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
  144. }
  145. expected = %AttachmentValidator{
  146. type: "Document",
  147. name: "Hello world",
  148. mediaType: "image/jpeg",
  149. blurhash: "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of",
  150. url: [
  151. %AttachmentValidator.UrlObjectValidator{
  152. type: "Link",
  153. mediaType: "image/jpeg",
  154. href: "https://media.example.tld/1.jpg",
  155. width: 880,
  156. height: 960
  157. }
  158. ]
  159. }
  160. {:ok, ^expected} =
  161. AttachmentValidator.cast_and_validate(attachment)
  162. |> Ecto.Changeset.apply_action(:insert)
  163. end
  164. end
  165. end