Merge branch 'issue/media-proxy-whitelist' into 'develop'
Add more tests for using media_proxy and whitelists See merge request pleroma/pleroma!1262
This commit is contained in:
		
						commit
						3d7bb47124
					
				
					 2 changed files with 91 additions and 26 deletions
				
			
		| 
						 | 
				
			
			@ -149,6 +149,21 @@ test "encoding S3 links (must preserve `%2F`)" do
 | 
			
		|||
      encoded = url(url)
 | 
			
		||||
      assert decode_result(encoded) == url
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not change whitelisted urls" do
 | 
			
		||||
      upload_config = Pleroma.Config.get([Pleroma.Upload])
 | 
			
		||||
      media_url = "https://media.pleroma.social"
 | 
			
		||||
      Pleroma.Config.put([Pleroma.Upload, :base_url], media_url)
 | 
			
		||||
      Pleroma.Config.put([:media_proxy, :whitelist], ["media.pleroma.social"])
 | 
			
		||||
      Pleroma.Config.put([:media_proxy, :base_url], "https://cache.pleroma.social")
 | 
			
		||||
 | 
			
		||||
      url = "#{media_url}/static/logo.png"
 | 
			
		||||
      encoded = url(url)
 | 
			
		||||
 | 
			
		||||
      assert String.starts_with?(encoded, media_url)
 | 
			
		||||
 | 
			
		||||
      Pleroma.Config.put([Pleroma.Upload], upload_config)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "when disabled" do
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1421,6 +1421,82 @@ test "returns the relationships for the current user", %{conn: conn} 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 @@ test "account fetching also works nickname", %{conn: conn} 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)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue