Skip to content

Commit

Permalink
fix: API tests, default error response
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Jan 18, 2024
1 parent 54c019d commit 097133d
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 52 deletions.
6 changes: 5 additions & 1 deletion elixir-api/lib/app/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ defmodule Hexerei.Router do
|> error_res(
404,
"Not Found",
"Resource not found or method not allowed: #{conn.method} #{conn.request_path}"
[
%{
message: "Resource not found or method not allowed: #{conn.method} #{conn.request_path}"
}
]
)
end

Expand Down
4 changes: 3 additions & 1 deletion elixir-api/lib/routes/api/v1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ defmodule Router.Api.V1 do

match _ do
conn
|> error_res(404, "Not found", "The requested resource could not be found or does not exist")
|> error_res(404, "Not found", [
%{message: "The requested resource could not be found or does not exist"}
])
|> halt()
end
end
2 changes: 1 addition & 1 deletion elixir-api/lib/routes/api/v1/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Router.Api.V1.Config do
else
_ ->
conn
|> error_res(400, "Invalid request", "Unknown error collecting params")
|> error_res(400, "Invalid request", [%{message: "Unknown error collecting params"}])
end
end
end
18 changes: 10 additions & 8 deletions elixir-api/lib/routes/api/v1/inc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,28 @@ defmodule Router.Api.V1.Inc do
receive do
{:increment_view_count_done, result} ->
case result do
:ok ->
{:ok} ->
conn
|> json_res(200, %{code: 200, data: %{id: id, target: "views"}})

:error ->
{:error, error} ->
conn
|> error_res(500, "Internal Server Error", "Failed to increment view count")
|> error_res(500, "Internal Server Error", [error])
end
after
5000 ->
conn
|> error_res(504, "Gateway Timeout", "Timeout waiting for view count increment")
|> error_res(504, "Gateway Timeout", [
%{message: "Timeout waiting for view count increment"}
])
end
else
conn |> error_res(400, "Bad Request", "Invalid target")
conn |> error_res(400, "Bad Request", [%{message: "Invalid target"}])
end
end

def handle_inc(conn, _id, _target),
do: conn |> error_res(400, "Bad Request", "Invalid body content")
do: conn |> error_res(400, "Bad Request", [%{message: "Invalid body content"}])

post "/:id" do
if is_binary(conn.params["id"]) do
Expand All @@ -60,10 +62,10 @@ defmodule Router.Api.V1.Inc do
rescue
e ->
Logger.error("Failed to increment view count: #{inspect(e)}")
conn |> error_res(400, "Bad Request", "Invalid body content")
conn |> error_res(400, "Bad Request", [%{message: "Invalid body content"}, e])
end
else
conn |> error_res(400, "Bad Request", "Invalid ID")
conn |> error_res(400, "Bad Request", [%{message: "Invalid ID"}])
end
end
end
3 changes: 2 additions & 1 deletion elixir-api/lib/routes/api/v1/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ defmodule Router.Api.V1.Post do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
_ -> conn |> error_res(400, "Invalid request", "Invalid or missing parameters")
_ ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
end
end
end
3 changes: 2 additions & 1 deletion elixir-api/lib/routes/api/v1/posts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ defmodule Router.Api.V1.Posts do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
_ -> conn |> error_res(400, "Invalid request", "Invalid or missing parameters")
_ ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
end
end
end
3 changes: 2 additions & 1 deletion elixir-api/lib/routes/api/v1/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ defmodule Router.Api.V1.Project do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
false -> conn |> error_res(400, "Invalid request", "Invalid or missing parameters")
false ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
end
end
end
3 changes: 2 additions & 1 deletion elixir-api/lib/routes/api/v1/projects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ defmodule Router.Api.V1.Projects do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
_ -> conn |> error_res(400, "Invalid request", "Invalid or missing parameters")
_ ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
end
end
end
2 changes: 1 addition & 1 deletion elixir-api/lib/routes/api/v1/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule Router.Api.V1.Tag do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
_ -> conn |> error_res(400, "Invalid request", "Missing ID or invalid type")
_ -> conn |> error_res(400, "Invalid request", [%{message: "Missing ID or invalid type"}])
end
end
end
3 changes: 2 additions & 1 deletion elixir-api/lib/routes/api/v1/tags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ defmodule Router.Api.V1.Tags do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
false -> conn |> error_res(400, "Invalid request", "Invalid or missing parameters")
false ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
end
end
end
12 changes: 8 additions & 4 deletions elixir-api/lib/routes/cdn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,20 @@ defmodule Router.Cdn do
error_res(conn, 500, "Something went wrong")

{:error, %HTTPoison.Error{reason: reason}} ->
error_res(conn, 500, "Something went wrong: #{reason}")
error_res(conn, 500, "Something went wrong", [reason])
end
else
{:error, reason} -> conn |> error_res(500, reason)
_ -> conn |> error_res(400, "Invalid request", "No filetype specified or invalid URL")
{:error, reason} ->
conn |> error_res(500, reason)

_ ->
conn
|> error_res(400, "Invalid request", [%{message: "No filetype specified or invalid URL"}])
end
end

match _ do
conn |> error_res(404, %{code: 404, message: "Not found"})
conn |> error_res(404, "Not found")
end

@impl Plug.ErrorHandler
Expand Down
10 changes: 2 additions & 8 deletions elixir-api/lib/utils/res.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@ defmodule Hexerei.Response do
end

# Standard json res for expected errors
def error_res(conn, status, message, detail \\ nil) do
content =
case detail != nil do
true -> %{code: status, message: message, detail: detail}
_ -> %{code: status, message: message}
end

conn |> json_res(status, content)
def error_res(conn, status, message, errors \\ []) do
conn |> json_res(status, %{code: status, message: message, errors: errors})
end

# Standard json res for unexpected errors
Expand Down
22 changes: 11 additions & 11 deletions elixir-api/lib/utils/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ defmodule Hexerei.Utils do
type == :post or type == :project ->
SanityUtils.try_increment_view_count(query)

errors =
receive do
{:increment_view_count_done, result} ->
case result do
{:ok} ->
[]

{:error, error} ->
[error]
end
end
errors = []
# receive do
# {:increment_view_count_done, result} ->
# case result do
# :ok ->
# []

# {:error, error} ->
# [error]
# end
# end

{
%{
Expand Down
6 changes: 3 additions & 3 deletions elixir-api/test/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ defmodule ApiTest do

assert body["data"] == nil
assert body["message"] == "Invalid request"
assert body["detail"] == "Invalid or missing parameters"
assert body["errors"] |> List.first() == %{"message" => "Invalid or missing parameters"}
end

test "POST '/inc' endpoint should increment views" do
Expand Down Expand Up @@ -231,7 +231,7 @@ defmodule ApiTest do
assert conn.state == :sent
assert conn.status == 200

assert_receive {:increment_view_count_done, :ok}, 5000
assert_receive {:increment_view_count_done, {:ok}}, 5000

body = Poison.decode!(conn.resp_body)

Expand Down Expand Up @@ -278,7 +278,7 @@ defmodule ApiTest do
assert conn.state == :sent
assert conn.status == 200

assert_receive {:increment_view_count_done, :ok}, 5000
assert_receive {:increment_view_count_done, {:ok}}, 5000

body = Poison.decode!(conn.resp_body)

Expand Down
4 changes: 2 additions & 2 deletions elixir-api/test/pt_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule PtTest do
alias Hexerei.PT

test "Constructs heading tree given mixed content" do
result = PT.build_summary(TestFixtures.mixed_headings())
{:ok, result} = PT.build_summary(TestFixtures.mixed_headings())

assert [
%{
Expand All @@ -28,7 +28,7 @@ defmodule PtTest do
end

test "Constructs heading tree with proper nesting" do
result = PT.build_summary(TestFixtures.headings())
{:ok, result} = PT.build_summary(TestFixtures.headings())

assert [
%{
Expand Down
19 changes: 12 additions & 7 deletions elixir-api/test/translate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ defmodule TranslateTest do
end
)

translated_post = Translate.handle_translate(:post, stub_post, "en")
{:ok, translated_post} = Translate.handle_translate(:post, stub_post, "en")

assert translated_post == stub_post

log_output =
capture_log(fn ->
invalid_translated_post = Translate.handle_translate(:post, stub_post, "invalid")
{:error, invalid_translated_post, errors} =
Translate.handle_translate(:post, stub_post, "invalid")

assert invalid_translated_post == stub_post
assert not Enum.empty?(errors)
end)

assert String.contains?(log_output, "Invalid language")
Expand Down Expand Up @@ -82,18 +84,21 @@ defmodule TranslateTest do
end
)

translated_config = Translate.handle_translate(:config, stub_config, "fr")
translated_post = Translate.handle_translate(:post, stub_post, "fr")
translated_posts = Translate.handle_translate(:posts, stub_posts, "fr")
{:ok, translated_config} = Translate.handle_translate(:config, stub_config, "fr")
{:ok, translated_post} = Translate.handle_translate(:post, stub_post, "fr")
{:ok, translated_posts} = Translate.handle_translate(:posts, stub_posts, "fr")

log_output =
capture_log(fn ->
translated_invalid = Translate.handle_translate(:invalid, stub_invalid, "fr")
{:error, translated_invalid, errors} =
Translate.handle_translate(:invalid, stub_invalid, "fr")

assert translated_invalid == stub_invalid
assert not Enum.empty?(errors)
end)

assert String.contains?(log_output, "Error translating invalid: \"Invalid type\"")
assert String.contains?(log_output, "Error translating invalid")
assert String.contains?(log_output, "Invalid type")

assert is_map(translated_config)

Expand Down

0 comments on commit 097133d

Please sign in to comment.