logo

pleroma

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

media_controller_test.exs (4077B)


  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.MastodonAPI.MediaControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Object
  7. alias Pleroma.User
  8. alias Pleroma.Web.ActivityPub.ActivityPub
  9. describe "Upload media" do
  10. setup do: oauth_access(["write:media"])
  11. setup do
  12. image = %Plug.Upload{
  13. content_type: "image/jpg",
  14. path: Path.absname("test/fixtures/image.jpg"),
  15. filename: "an_image.jpg"
  16. }
  17. [image: image]
  18. end
  19. setup do: clear_config([:media_proxy])
  20. setup do: clear_config([Pleroma.Upload])
  21. test "/api/v1/media", %{conn: conn, image: image} do
  22. desc = "Description of the image"
  23. media =
  24. conn
  25. |> put_req_header("content-type", "multipart/form-data")
  26. |> post("/api/v1/media", %{"file" => image, "description" => desc})
  27. |> json_response_and_validate_schema(:ok)
  28. assert media["type"] == "image"
  29. assert media["description"] == desc
  30. assert media["id"]
  31. object = Object.get_by_id(media["id"])
  32. assert object.data["actor"] == User.ap_id(conn.assigns[:user])
  33. end
  34. test "/api/v2/media", %{conn: conn, user: user, image: image} do
  35. desc = "Description of the image"
  36. response =
  37. conn
  38. |> put_req_header("content-type", "multipart/form-data")
  39. |> post("/api/v2/media", %{"file" => image, "description" => desc})
  40. |> json_response_and_validate_schema(202)
  41. assert media_id = response["id"]
  42. %{conn: conn} = oauth_access(["read:media"], user: user)
  43. media =
  44. conn
  45. |> get("/api/v1/media/#{media_id}")
  46. |> json_response_and_validate_schema(200)
  47. assert media["type"] == "image"
  48. assert media["description"] == desc
  49. assert media["id"]
  50. object = Object.get_by_id(media["id"])
  51. assert object.data["actor"] == user.ap_id
  52. end
  53. end
  54. describe "Update media description" do
  55. setup do: oauth_access(["write:media"])
  56. setup %{user: actor} do
  57. file = %Plug.Upload{
  58. content_type: "image/jpg",
  59. path: Path.absname("test/fixtures/image.jpg"),
  60. filename: "an_image.jpg"
  61. }
  62. {:ok, %Object{} = object} =
  63. ActivityPub.upload(
  64. file,
  65. actor: User.ap_id(actor),
  66. description: "test-m"
  67. )
  68. [object: object]
  69. end
  70. test "/api/v1/media/:id good request", %{conn: conn, object: object} do
  71. media =
  72. conn
  73. |> put_req_header("content-type", "multipart/form-data")
  74. |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
  75. |> json_response_and_validate_schema(:ok)
  76. assert media["description"] == "test-media"
  77. assert refresh_record(object).data["name"] == "test-media"
  78. end
  79. end
  80. describe "Get media by id (/api/v1/media/:id)" do
  81. setup do: oauth_access(["read:media"])
  82. setup %{user: actor} do
  83. file = %Plug.Upload{
  84. content_type: "image/jpg",
  85. path: Path.absname("test/fixtures/image.jpg"),
  86. filename: "an_image.jpg"
  87. }
  88. {:ok, %Object{} = object} =
  89. ActivityPub.upload(
  90. file,
  91. actor: User.ap_id(actor),
  92. description: "test-media"
  93. )
  94. [object: object]
  95. end
  96. test "it returns media object when requested by owner", %{conn: conn, object: object} do
  97. media =
  98. conn
  99. |> get("/api/v1/media/#{object.id}")
  100. |> json_response_and_validate_schema(:ok)
  101. assert media["description"] == "test-media"
  102. assert media["type"] == "image"
  103. assert media["id"]
  104. end
  105. test "it returns 403 if media object requested by non-owner", %{object: object, user: user} do
  106. %{conn: conn, user: other_user} = oauth_access(["read:media"])
  107. assert object.data["actor"] == user.ap_id
  108. refute user.id == other_user.id
  109. conn
  110. |> get("/api/v1/media/#{object.id}")
  111. |> json_response(403)
  112. end
  113. end
  114. end