logo

pleroma

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

media_controller_test.exs (6711B)


  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.MastodonAPI.MediaControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import ExUnit.CaptureLog
  7. import Mox
  8. alias Pleroma.Object
  9. alias Pleroma.UnstubbedConfigMock, as: ConfigMock
  10. alias Pleroma.User
  11. alias Pleroma.Web.ActivityPub.ActivityPub
  12. describe "Upload media" do
  13. setup do: oauth_access(["write:media"])
  14. setup do
  15. ConfigMock
  16. |> stub_with(Pleroma.Test.StaticConfig)
  17. image = %Plug.Upload{
  18. content_type: "image/jpeg",
  19. path: Path.absname("test/fixtures/image.jpg"),
  20. filename: "an_image.jpg"
  21. }
  22. [image: image]
  23. end
  24. setup do: clear_config([:media_proxy])
  25. setup do: clear_config([Pleroma.Upload])
  26. test "/api/v1/media", %{conn: conn, image: image} do
  27. desc = "Description of the image"
  28. media =
  29. conn
  30. |> put_req_header("content-type", "multipart/form-data")
  31. |> post("/api/v1/media", %{"file" => image, "description" => desc})
  32. |> json_response_and_validate_schema(:ok)
  33. assert media["type"] == "image"
  34. assert media["description"] == desc
  35. assert media["id"]
  36. object = Object.get_by_id(media["id"])
  37. assert object.data["actor"] == User.ap_id(conn.assigns[:user])
  38. end
  39. test "/api/v2/media", %{conn: conn, user: user, image: image} do
  40. desc = "Description of the image"
  41. response =
  42. conn
  43. |> put_req_header("content-type", "multipart/form-data")
  44. |> post("/api/v2/media", %{"file" => image, "description" => desc})
  45. |> json_response_and_validate_schema(202)
  46. assert media_id = response["id"]
  47. %{conn: conn} = oauth_access(["read:media"], user: user)
  48. media =
  49. conn
  50. |> get("/api/v1/media/#{media_id}")
  51. |> json_response_and_validate_schema(200)
  52. assert media["type"] == "image"
  53. assert media["description"] == desc
  54. assert media["id"]
  55. object = Object.get_by_id(media["id"])
  56. assert object.data["actor"] == user.ap_id
  57. end
  58. test "/api/v2/media, upload_limit", %{conn: conn, user: user} do
  59. desc = "Description of the binary"
  60. upload_limit = Config.get([:instance, :upload_limit]) * 8 + 8
  61. assert :ok ==
  62. File.write(Path.absname("test/tmp/large_binary.data"), <<0::size(upload_limit)>>)
  63. large_binary = %Plug.Upload{
  64. content_type: nil,
  65. path: Path.absname("test/tmp/large_binary.data"),
  66. filename: "large_binary.data"
  67. }
  68. assert capture_log(fn ->
  69. assert %{"error" => "file_too_large"} =
  70. conn
  71. |> put_req_header("content-type", "multipart/form-data")
  72. |> post("/api/v2/media", %{
  73. "file" => large_binary,
  74. "description" => desc
  75. })
  76. |> json_response_and_validate_schema(400)
  77. end) =~
  78. "[error] Elixir.Pleroma.Upload store (using Pleroma.Uploaders.Local) failed: :file_too_large"
  79. clear_config([:instance, :upload_limit], upload_limit)
  80. assert response =
  81. conn
  82. |> put_req_header("content-type", "multipart/form-data")
  83. |> post("/api/v2/media", %{
  84. "file" => large_binary,
  85. "description" => desc
  86. })
  87. |> json_response_and_validate_schema(202)
  88. assert media_id = response["id"]
  89. %{conn: conn} = oauth_access(["read:media"], user: user)
  90. media =
  91. conn
  92. |> get("/api/v1/media/#{media_id}")
  93. |> json_response_and_validate_schema(200)
  94. assert media["type"] == "unknown"
  95. assert media["description"] == desc
  96. assert media["id"]
  97. assert :ok == File.rm(Path.absname("test/tmp/large_binary.data"))
  98. end
  99. test "Do not allow nested filename", %{conn: conn, image: image} do
  100. image = %Plug.Upload{
  101. image
  102. | filename: "../../../../../nested/file.jpg"
  103. }
  104. desc = "Description of the image"
  105. media =
  106. conn
  107. |> put_req_header("content-type", "multipart/form-data")
  108. |> post("/api/v1/media", %{"file" => image, "description" => desc})
  109. |> json_response_and_validate_schema(:ok)
  110. refute Regex.match?(~r"/nested/", media["url"])
  111. end
  112. end
  113. describe "Update media description" do
  114. setup do: oauth_access(["write:media"])
  115. setup %{user: actor} do
  116. ConfigMock
  117. |> stub_with(Pleroma.Test.StaticConfig)
  118. file = %Plug.Upload{
  119. content_type: "image/jpeg",
  120. path: Path.absname("test/fixtures/image.jpg"),
  121. filename: "an_image.jpg"
  122. }
  123. {:ok, %Object{} = object} =
  124. ActivityPub.upload(
  125. file,
  126. actor: User.ap_id(actor),
  127. description: "test-m"
  128. )
  129. [object: object]
  130. end
  131. test "/api/v1/media/:id good request", %{conn: conn, object: object} do
  132. media =
  133. conn
  134. |> put_req_header("content-type", "multipart/form-data")
  135. |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
  136. |> json_response_and_validate_schema(:ok)
  137. assert media["description"] == "test-media"
  138. assert refresh_record(object).data["name"] == "test-media"
  139. end
  140. end
  141. describe "Get media by id (/api/v1/media/:id)" do
  142. setup do: oauth_access(["read:media"])
  143. setup %{user: actor} do
  144. ConfigMock
  145. |> stub_with(Pleroma.Test.StaticConfig)
  146. file = %Plug.Upload{
  147. content_type: "image/jpeg",
  148. path: Path.absname("test/fixtures/image.jpg"),
  149. filename: "an_image.jpg"
  150. }
  151. {:ok, %Object{} = object} =
  152. ActivityPub.upload(
  153. file,
  154. actor: User.ap_id(actor),
  155. description: "test-media"
  156. )
  157. [object: object]
  158. end
  159. test "it returns media object when requested by owner", %{conn: conn, object: object} do
  160. media =
  161. conn
  162. |> get("/api/v1/media/#{object.id}")
  163. |> json_response_and_validate_schema(:ok)
  164. assert media["description"] == "test-media"
  165. assert media["type"] == "image"
  166. assert media["id"]
  167. end
  168. test "it returns 403 if media object requested by non-owner", %{object: object, user: user} do
  169. %{conn: conn, user: other_user} = oauth_access(["read:media"])
  170. assert object.data["actor"] == user.ap_id
  171. refute user.id == other_user.id
  172. conn
  173. |> get("/api/v1/media/#{object.id}")
  174. |> json_response_and_validate_schema(403)
  175. end
  176. end
  177. end