Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use endpoint-specific ordering validation #1699

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ae_mdw/aexn_tokens.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule AeMdw.AexnTokens do
with {:ok, cursor} <- deserialize_aexn_cursor(cursor),
{:ok, params} <- validate_params(query),
{:ok, filters} <- Util.convert_params(params, &convert_param/1) do
sorting_table = Map.get(@sorting_table, order_by)
sorting_table = Map.fetch!(@sorting_table, order_by)

paginated_aexn_contracts =
filters
Expand Down
17 changes: 5 additions & 12 deletions lib/ae_mdw_web/controllers/aexn_token_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ defmodule AeMdwWeb.AexnTokenController do

@endpoint_timeout Application.compile_env(:ae_mdw, :endpoint_timeout)

plug PaginatedPlug, order_by: ~w(name symbol creation pubkey amount)a
plug PaginatedPlug,
[order_by: ~w(name symbol creation)a] when action in ~w(aex9_contracts aex141_contracts)a

plug PaginatedPlug, [order_by: ~w(pubkey amount)a] when action in ~w(aex9_event_balances)a
plug PaginatedPlug when action in ~w(aex9_account_balances aex9_token_balance_history)a

action_fallback(FallbackController)

Expand Down Expand Up @@ -73,7 +77,6 @@ defmodule AeMdwWeb.AexnTokenController do
} = assigns

with {:ok, contract_pk} <- validate_aex9(contract_id),
{:ok, order_by} <- validate_balances_order_by(order_by),
{:ok, {prev_cursor, balance_keys, next_cursor}} <-
Aex9.fetch_event_balances(state, contract_pk, pagination, cursor, order_by) do
balances = Enum.map(balance_keys, &render_event_balance(state, &1))
Expand Down Expand Up @@ -168,16 +171,6 @@ defmodule AeMdwWeb.AexnTokenController do
end
end

defp validate_balances_order_by(:name), do: {:ok, :pubkey}

defp validate_balances_order_by(order_by) when order_by in [:amount, :pubkey] do
{:ok, order_by}
end

defp validate_balances_order_by(order_by) do
{:error, ErrInput.Query.exception(value: order_by)}
end

defp validate_block_hash(nil), do: {:ok, nil}

defp validate_block_hash(block_id) do
Expand Down
16 changes: 16 additions & 0 deletions test/ae_mdw_web/controllers/aexn_token_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,13 @@ defmodule AeMdwWeb.AexnTokenControllerTest do
assert %{"error" => ^error_msg} =
conn |> get("/v2/aex9", cursor: cursor) |> json_response(400)
end

test "when invalid order by", %{conn: conn} do
assert %{"error" => "invalid query: by=pubkey"} =
conn
|> get("/v2/aex9", by: "pubkey")
|> json_response(400)
end
end

describe "aex141_count" do
Expand Down Expand Up @@ -865,6 +872,15 @@ defmodule AeMdwWeb.AexnTokenControllerTest do

assert %{"data" => ^balances} = conn |> get(prev_balances) |> json_response(200)
end

test "when invalid order by", %{conn: conn, contract_pk: contract_pk} do
contract_id = encode_contract(contract_pk)

assert %{"error" => "invalid query: by=foo"} =
conn
|> get("/v2/aex9/#{contract_id}/balances", by: "foo")
|> json_response(400)
end
end

describe "aex9_token_balance" do
Expand Down