diff --git a/api.md b/api.md index 9043224..8a639af 100644 --- a/api.md +++ b/api.md @@ -480,6 +480,15 @@ defmodule AppWeb.APITest do } } + # image with invalid content type + @invalid_content_type_image %{ + "" => %Plug.Upload{ + content_type: "image/xyz", + filename: "phoenix.xyz", + path: [:code.priv_dir(:app), "static", "images", "phoenix.xyz"] |> Path.join() + } + } + test "upload succeeds (happy path)", %{conn: conn} do conn = post(conn, ~p"/api/images", @create_attrs) @@ -538,6 +547,14 @@ defmodule AppWeb.APITest do } end + test "image file with invalid content type should return appropriate error", %{conn: conn} do + conn = post(conn, ~p"/api/images", @invalid_content_type_image) + + assert Map.get(Jason.decode!(response(conn, 400)), "errors") == %{ + "detail" => "Error uploading file. The content type of the uploaded file is not valid." + } + end + test "file with invalid binary data type and extension should return error. ", %{conn: conn} do conn = post(conn, ~p"/api/images", @empty_image) @@ -733,6 +750,12 @@ the possible returning values from `upload/1`. "Error uploading file. The contents of the uploaded file may be empty or invalid." }) + {:error, :invalid_extension} -> + render(conn |> put_status(400), %{ + body: + "Error uploading file. The content type of the uploaded file is not valid." + }) + _ -> render(conn |> put_status(400), %{ body: "There was an error uploading the file. Please try again later." diff --git a/lib/app_web/controllers/api_controller.ex b/lib/app_web/controllers/api_controller.ex index ec5b10e..a5858f5 100644 --- a/lib/app_web/controllers/api_controller.ex +++ b/lib/app_web/controllers/api_controller.ex @@ -12,6 +12,12 @@ defmodule AppWeb.ApiController do {:error, :failure_read} -> render(conn |> put_status(400), %{body: "Error uploading file. Failure reading file."}) + {:error, :invalid_extension} -> + render(conn |> put_status(400), %{ + body: + "Error uploading file. The content type of the uploaded file is not valid." + }) + {:error, :invalid_cid} -> render(conn |> put_status(400), %{ body: diff --git a/test/app_web/api_test.exs b/test/app_web/api_test.exs index d8b272b..ebc0115 100644 --- a/test/app_web/api_test.exs +++ b/test/app_web/api_test.exs @@ -66,6 +66,15 @@ defmodule AppWeb.APITest do } } + # image with invalid content type + @invalid_content_type_image %{ + "" => %Plug.Upload{ + content_type: "image/xyz", + filename: "phoenix.xyz", + path: [:code.priv_dir(:app), "static", "images", "phoenix.xyz"] |> Path.join() + } + } + test "upload succeeds (happy path)", %{conn: conn} do conn = post(conn, ~p"/api/images", @create_attrs) @@ -124,6 +133,14 @@ defmodule AppWeb.APITest do } end + test "image file with invalid content type should return appropriate error", %{conn: conn} do + conn = post(conn, ~p"/api/images", @invalid_content_type_image) + + assert Map.get(Jason.decode!(response(conn, 400)), "errors") == %{ + "detail" => "Error uploading file. The content type of the uploaded file is not valid." + } + end + test "file with invalid binary data type and extension should return error. ", %{conn: conn} do conn = post(conn, ~p"/api/images", @empty_image)