diff options
Diffstat (limited to 'test/web/mastodon_api')
| -rw-r--r-- | test/web/mastodon_api/mastodon_api_controller_test.exs | 102 | 
1 files changed, 76 insertions, 26 deletions
| diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 33c8e209a..de32084bd 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1421,6 +1421,82 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do      end    end +  describe "media upload" do +    setup do +      upload_config = Pleroma.Config.get([Pleroma.Upload]) +      proxy_config = Pleroma.Config.get([:media_proxy]) + +      on_exit(fn -> +        Pleroma.Config.put([Pleroma.Upload], upload_config) +        Pleroma.Config.put([:media_proxy], proxy_config) +      end) + +      user = insert(:user) + +      conn = +        build_conn() +        |> assign(:user, user) + +      image = %Plug.Upload{ +        content_type: "image/jpg", +        path: Path.absname("test/fixtures/image.jpg"), +        filename: "an_image.jpg" +      } + +      [conn: conn, image: image] +    end + +    test "returns uploaded image", %{conn: conn, image: image} do +      desc = "Description of the image" + +      media = +        conn +        |> post("/api/v1/media", %{"file" => image, "description" => desc}) +        |> json_response(:ok) + +      assert media["type"] == "image" +      assert media["description"] == desc +      assert media["id"] + +      object = Repo.get(Object, media["id"]) +      assert object.data["actor"] == User.ap_id(conn.assigns[:user]) +    end + +    test "returns proxied url when media proxy is enabled", %{conn: conn, image: image} do +      Pleroma.Config.put([Pleroma.Upload, :base_url], "https://media.pleroma.social") + +      proxy_url = "https://cache.pleroma.social" +      Pleroma.Config.put([:media_proxy, :enabled], true) +      Pleroma.Config.put([:media_proxy, :base_url], proxy_url) + +      media = +        conn +        |> post("/api/v1/media", %{"file" => image}) +        |> json_response(:ok) + +      assert String.starts_with?(media["url"], proxy_url) +    end + +    test "returns media url when proxy is enabled but media url is whitelisted", %{ +      conn: conn, +      image: image +    } do +      media_url = "https://media.pleroma.social" +      Pleroma.Config.put([Pleroma.Upload, :base_url], media_url) + +      Pleroma.Config.put([:media_proxy, :enabled], true) +      Pleroma.Config.put([:media_proxy, :base_url], "https://cache.pleroma.social") +      Pleroma.Config.put([:media_proxy, :whitelist], ["media.pleroma.social"]) + +      media = +        conn +        |> post("/api/v1/media", %{"file" => image}) +        |> json_response(:ok) + +      assert String.starts_with?(media["url"], media_url) +    end +  end +    describe "locked accounts" do      test "/api/v1/follow_requests works" do        user = insert(:user, %{info: %User.Info{locked: true}}) @@ -1530,32 +1606,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do      assert id == user.id    end -  test "media upload", %{conn: conn} do -    file = %Plug.Upload{ -      content_type: "image/jpg", -      path: Path.absname("test/fixtures/image.jpg"), -      filename: "an_image.jpg" -    } - -    desc = "Description of the image" - -    user = insert(:user) - -    conn = -      conn -      |> assign(:user, user) -      |> post("/api/v1/media", %{"file" => file, "description" => desc}) - -    assert media = json_response(conn, 200) - -    assert media["type"] == "image" -    assert media["description"] == desc -    assert media["id"] - -    object = Repo.get(Object, media["id"]) -    assert object.data["actor"] == User.ap_id(user) -  end -    test "mascot upload", %{conn: conn} do      user = insert(:user) | 
