Skip to content

Commit

Permalink
feat: Adding tests to check for CID and extension pattern matching. #69
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Jun 28, 2023
1 parent 6dc3b4a commit 1490d08
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/app/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ defmodule App.Upload do
# Create `CID` from file contents so filenames are unique
case File.read(image.path) do
{:ok, file_binary} ->
dbg(file_binary)
file_cid = Cid.cid(file_binary)

dbg(file_cid)

file_extension =
image.content_type
|> MIME.extensions()
|> List.first()

dbg(file_extension)

# Check if file `cid` and extension are valid.
case {file_cid, file_extension} do
{"invalid data type", nil} ->
dbg("ay")
{:error, :invalid_extension_and_cid}

{"invalid data type", _extension} ->
{:error, :invalid_cid}

{cid, nil} ->
{_cid, nil} ->
{:error, :invalid_extension}


Expand Down
14 changes: 10 additions & 4 deletions lib/app_web/controllers/api_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ defmodule AppWeb.ApiController do
render(conn |> put_status(400), %{body: "Error uploading file. Failure reading file."})

{:error, :invalid_cid} ->
render(conn |> put_status(400), %{body: "Error uploading file. Failure creating the CID filename."})
render(conn |> put_status(400), %{
body: "Error uploading file. Failure creating the CID filename."
})

{:error, :invalid_extension} ->
render(conn |> put_status(400), %{body: "Error uploading file. Failure parsing the file extension."})
render(conn |> put_status(400), %{
body: "Error uploading file. Failure parsing the file extension."
})

{:error, _reason} ->
render(conn |> put_status(400), %{body: "Error uploading file #26"})
{:error, :invalid_extension_and_cid} ->
render(conn |> put_status(400), %{
body: "Error uploading file. The file extension and contents are invalid."
})
end
rescue
e ->
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ defmodule App.MixProject do

# Mocking tests
{:mox, "~> 1.0", only: :test},
{:mock, "~> 0.3.0", only: :test},

# Useful functions: github.com/dwyl/useful
{:useful, "~> 1.11.1"},
Expand Down
Empty file added priv/static/images/empty
Empty file.
41 changes: 41 additions & 0 deletions test/app_web/api_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule AppWeb.APITest do
use AppWeb.ConnCase, async: true

import Mock

# without image keyword:
@create_attrs %{
"" => %Plug.Upload{
Expand Down Expand Up @@ -37,6 +39,15 @@ defmodule AppWeb.APITest do
}
}

# empty_file
@empty_file %{
"" => %Plug.Upload{
content_type: "image/something",
filename: "empty",
path: [:code.priv_dir(:app), "static", "images", "empty"] |> Path.join()
}
}

test "upload succeeds (happy path)", %{conn: conn} do
conn = post(conn, ~p"/api/images", @create_attrs)

Expand Down Expand Up @@ -79,4 +90,34 @@ defmodule AppWeb.APITest do
"detail" => "Error uploading file. Failure reading file."
}
end

test "empty file should return appropriate error", %{conn: conn} do
conn = post(conn, ~p"/api/images", @empty_file)

assert Map.get(Jason.decode!(response(conn, 400)), "errors") == %{
"detail" => "Error uploading file. Failure parsing the file extension."
}
end

test "file with invalid binary data type and extension should return error.", %{conn: conn} do

with_mock Cid, [cid: fn(_input) -> "invalid data type" end] do
conn = post(conn, ~p"/api/images", @empty_file)

assert Map.get(Jason.decode!(response(conn, 400)), "errors") == %{
"detail" => "Error uploading file. The file extension and contents are invalid."
}
end
end

test "file with invalid binary data (cid) but valid content type should return error", %{conn: conn} do

with_mock Cid, [cid: fn(_input) -> "invalid data type" end] do
conn = post(conn, ~p"/api/images", @valid_image_attrs)

assert Map.get(Jason.decode!(response(conn, 400)), "errors") == %{
"detail" => "Error uploading file. Failure creating the CID filename."
}
end
end
end

0 comments on commit 1490d08

Please sign in to comment.