From 8551b6412063a59e1828c56a6c05cbda7fa83b18 Mon Sep 17 00:00:00 2001 From: Sebastian Abondano Date: Mon, 6 Aug 2018 16:33:01 -0400 Subject: [PATCH] Improves API test coverage Why: * For Etherscan compatibility, the existing API calls support GET and POST requests. This commit adds test coverage to ensure we support both GET and POST requests. * Issue link: related to https://github.com/poanetwork/poa-explorer/issues/138 This change addresses the need by: * Adding GET/POST requests tests to `API.RPC.AddressControllerTest` --- .../api/rpc/address_controller_test.exs | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/apps/explorer_web/test/explorer_web/controllers/api/rpc/address_controller_test.exs b/apps/explorer_web/test/explorer_web/controllers/api/rpc/address_controller_test.exs index 37ce1baff0..ae49e44ec4 100644 --- a/apps/explorer_web/test/explorer_web/controllers/api/rpc/address_controller_test.exs +++ b/apps/explorer_web/test/explorer_web/controllers/api/rpc/address_controller_test.exs @@ -117,6 +117,28 @@ defmodule ExplorerWeb.API.RPC.AddressControllerTest do assert response["status"] == "1" assert response["message"] == "OK" end + + test "supports GET and POST requests", %{conn: conn} do + address = insert(:address, fetched_balance: 100) + + params = %{ + "module" => "account", + "action" => "balance", + "address" => "#{address.hash}" + } + + assert get_response = + conn + |> get("/api", params) + |> json_response(200) + + assert post_response = + conn + |> post("/api", params) + |> json_response(200) + + assert get_response == post_response + end end describe "balancemulti" do @@ -284,6 +306,36 @@ defmodule ExplorerWeb.API.RPC.AddressControllerTest do assert response["status"] == "1" assert response["message"] == "OK" end + + test "supports GET and POST requests", %{conn: conn} do + addresses = + for _ <- 1..4 do + insert(:address, fetched_balance: Enum.random(1..1_000)) + end + + address_param = + addresses + |> Enum.map(&"#{&1.hash}") + |> Enum.join(",") + + params = %{ + "module" => "account", + "action" => "balancemulti", + "address" => address_param + } + + assert get_response = + conn + |> get("/api", params) + |> json_response(200) + + assert post_response = + conn + |> post("/api", params) + |> json_response(200) + + assert get_response == post_response + end end describe "txlist" do @@ -956,5 +1008,31 @@ defmodule ExplorerWeb.API.RPC.AddressControllerTest do assert response["status"] == "1" assert response["message"] == "OK" end + + test "supports GET and POST requests", %{conn: conn} do + address = insert(:address) + + :transaction + |> insert(from_address: address) + |> with_block() + + params = %{ + "module" => "account", + "action" => "txlist", + "address" => "#{address.hash}" + } + + assert get_response = + conn + |> get("/api", params) + |> json_response(200) + + assert post_response = + conn + |> post("/api", params) + |> json_response(200) + + assert get_response == post_response + end end end