logo

pleroma

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

attachment_validator_test.exs (6573B)


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