From 5b8b08a255b3ad4d9ded54a0455ef32155f272bd Mon Sep 17 00:00:00 2001 From: Stamates Date: Thu, 6 Sep 2018 13:36:50 -0400 Subject: [PATCH] Refactor address rendering functions and add test coverage --- .../templates/transaction/_tile.html.eex | 4 +- .../transaction/_token_transfer.html.eex | 4 +- .../templates/transaction/overview.html.eex | 4 +- .../lib/block_scout_web/views/address_view.ex | 98 ++++++----- .../block_scout_web/views/transaction_view.ex | 3 +- .../features/viewing_addresses_test.exs | 45 +++-- .../views/address_view_test.exs | 157 +++++++++++++----- 7 files changed, 200 insertions(+), 115 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction/_tile.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction/_tile.html.eex index 6752ea9d77..fd17a86d69 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction/_tile.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction/_tile.html.eex @@ -11,9 +11,9 @@
<%= render "_link.html", transaction_hash: @transaction.hash %> - <%= BlockScoutWeb.AddressView.display_address_hash(assigns[:current_address], @transaction, :from) %> + <%= @transaction |> BlockScoutWeb.AddressView.address_partial_selector(:from, assigns[:current_address]) |> BlockScoutWeb.AddressView.render_partial() %> → - <%= BlockScoutWeb.AddressView.display_address_hash(assigns[:current_address], @transaction, :to) %> + <%= @transaction |> BlockScoutWeb.AddressView.address_partial_selector(:to, assigns[:current_address]) |> BlockScoutWeb.AddressView.render_partial() %> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction/_token_transfer.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction/_token_transfer.html.eex index 11ad54a146..3ee194e882 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction/_token_transfer.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction/_token_transfer.html.eex @@ -11,9 +11,9 @@ <% end %> <% end %> - <%= BlockScoutWeb.AddressView.display_address_hash(@address, @token_transfer, :from, true) %> + <%= @token_transfer |> BlockScoutWeb.AddressView.address_partial_selector(:from, @address, true) |> BlockScoutWeb.AddressView.render_partial() %> → - <%= BlockScoutWeb.AddressView.display_address_hash(@address, @token_transfer, :to, true) %> + <%= @token_transfer |> BlockScoutWeb.AddressView.address_partial_selector(:to, @address, true) |> BlockScoutWeb.AddressView.render_partial() %> <%= token_transfer_amount(@token_transfer) %> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex index 180aa6cb86..63b34d0df2 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex @@ -15,9 +15,9 @@

<%= gettext "Transaction Details" %>

<%= @transaction %>

- <%= BlockScoutWeb.AddressView.display_address_hash(nil, @transaction, :from) %> + <%= @transaction |> BlockScoutWeb.AddressView.address_partial_selector(:from, nil) |> BlockScoutWeb.AddressView.render_partial() %> - <%= BlockScoutWeb.AddressView.display_address_hash(nil, @transaction, :to) %> + <%= @transaction |> BlockScoutWeb.AddressView.address_partial_selector(:to, nil) |> BlockScoutWeb.AddressView.render_partial() %>
<%= BlockScoutWeb.TransactionView.transaction_display_type(@transaction) %> diff --git a/apps/block_scout_web/lib/block_scout_web/views/address_view.ex b/apps/block_scout_web/lib/block_scout_web/views/address_view.ex index 9d88532d95..83e236f7db 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/address_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/address_view.ex @@ -5,6 +5,42 @@ defmodule BlockScoutWeb.AddressView do @dialyzer :no_match + def address_partial_selector(struct_to_render_from, direction, current_address, truncate \\ false) + + def address_partial_selector(%TokenTransfer{to_address: address}, :to, current_address, truncate) do + matching_address_check(current_address, address.hash, contract?(address), truncate) + end + + def address_partial_selector(%TokenTransfer{from_address: address}, :from, current_address, truncate) do + matching_address_check(current_address, address.hash, contract?(address), truncate) + end + + def address_partial_selector( + %Transaction{to_address_hash: nil, created_contract_address_hash: nil}, + :to, + _current_address, + _truncate + ) do + gettext("Contract Address Pending") + end + + def address_partial_selector( + %Transaction{to_address_hash: nil, created_contract_address_hash: hash}, + :to, + current_address, + truncate + ) do + matching_address_check(current_address, hash, true, truncate) + end + + def address_partial_selector(%Transaction{to_address: address}, :to, current_address, truncate) do + matching_address_check(current_address, address.hash, contract?(address), truncate) + end + + def address_partial_selector(%Transaction{from_address: address}, :from, current_address, truncate) do + matching_address_check(current_address, address.hash, contract?(address), truncate) + end + def address_title(%Address{} = address) do if contract?(address) do gettext("Contract Address") @@ -34,32 +70,6 @@ defmodule BlockScoutWeb.AddressView do def contract?(nil), do: true - def display_address_hash(current_address, struct_to_render_from, direction, truncate \\ false) - - def display_address_hash(current_address, %TokenTransfer{to_address: address}, :to, truncate) do - display_hash(current_address, address.hash, contract?(address), truncate) - end - - def display_address_hash(current_address, %TokenTransfer{from_address: address}, :from, truncate) do - display_hash(current_address, address.hash, contract?(address), truncate) - end - - def display_address_hash(_current_address, %Transaction{to_address_hash: nil, created_contract_address_hash: nil}, :to, _truncate) do - gettext("Contract Address Pending") - end - - def display_address_hash(current_address, %Transaction{to_address_hash: nil, created_contract_address_hash: hash}, :to, truncate) do - display_hash(current_address, hash, true, truncate) - end - - def display_address_hash(current_address, %Transaction{to_address: address}, :to, truncate) do - display_hash(current_address, address.hash, contract?(address), truncate) - end - - def display_address_hash(current_address, %Transaction{from_address: address}, :from, truncate) do - display_hash(current_address, address.hash, contract?(address), truncate) - end - def hash(%Address{hash: hash}) do to_string(hash) end @@ -71,6 +81,17 @@ defmodule BlockScoutWeb.AddressView do |> Base.encode64() end + def render_partial(%{partial: partial, address_hash: hash, contract: contract?, truncate: truncate}) do + render( + partial, + address_hash: hash, + contract: contract?, + truncate: truncate + ) + end + + def render_partial(text), do: text + def smart_contract_verified?(%Address{smart_contract: %SmartContract{}}), do: true def smart_contract_verified?(%Address{smart_contract: nil}), do: false @@ -88,30 +109,21 @@ defmodule BlockScoutWeb.AddressView do def trimmed_hash(_), do: "" - defp display_hash(nil, hash, contract?, truncate) do - render( - "_link.html", - address_hash: hash, - contract: contract?, - truncate: truncate - ) - end - - defp display_hash(current_address, hash, contract?, truncate) do - if current_address.hash == hash do - render( - "_responsive_hash.html", + defp matching_address_check(current_address, hash, contract?, truncate) do + if current_address && current_address.hash == hash do + %{ + partial: "_responsive_hash.html", address_hash: hash, contract: contract?, truncate: truncate - ) + } else - render( - "_link.html", + %{ + partial: "_link.html", address_hash: hash, contract: contract?, truncate: truncate - ) + } end end end diff --git a/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex b/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex index 05f79f9f6e..fea9d63815 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex @@ -99,7 +99,8 @@ defmodule BlockScoutWeb.TransactionView do end # This is the address to be shown in the to field - def to_address_hash(%Transaction{to_address_hash: nil, created_contract_address_hash: address_hash}), do: address_hash |> IO.inspect + def to_address_hash(%Transaction{to_address_hash: nil, created_contract_address_hash: address_hash}), + do: address_hash def to_address_hash(%Transaction{to_address: %Address{hash: address_hash}}), do: address_hash diff --git a/apps/block_scout_web/test/block_scout_web/features/viewing_addresses_test.exs b/apps/block_scout_web/test/block_scout_web/features/viewing_addresses_test.exs index bbd3b1a1ec..ad998dbaaf 100644 --- a/apps/block_scout_web/test/block_scout_web/features/viewing_addresses_test.exs +++ b/apps/block_scout_web/test/block_scout_web/features/viewing_addresses_test.exs @@ -2,7 +2,6 @@ defmodule BlockScoutWeb.ViewingAddressesTest do use BlockScoutWeb.FeatureCase, async: true alias Explorer.Chain.Wei - alias Explorer.Factory alias BlockScoutWeb.{AddressPage, AddressView, Notifier} setup do @@ -41,7 +40,7 @@ defmodule BlockScoutWeb.ViewingAddressesTest do describe "viewing contract creator" do test "see the contract creator and transaction links", %{session: session} do address = insert(:address) - contract = insert(:address, contract_code: Factory.data("contract_code")) + contract = insert(:contract_address) transaction = insert(:transaction, from_address: address, created_contract_address: contract) internal_transaction = @@ -63,9 +62,9 @@ defmodule BlockScoutWeb.ViewingAddressesTest do test "see the contract creator and transaction links even when the creator is another contract", %{session: session} do lincoln = insert(:address) - contract = insert(:address, contract_code: Factory.data("contract_code")) + contract = insert(:contract_address) transaction = insert(:transaction) - another_contract = insert(:address, contract_code: Factory.data("contract_code")) + another_contract = insert(:contract_address) insert( :internal_transaction, @@ -285,12 +284,12 @@ defmodule BlockScoutWeb.ViewingAddressesTest do lincoln = addresses.lincoln taft = addresses.taft - contract_token_address = insert(:contract_address) - insert(:token, contract_address: contract_token_address) + contract_address = insert(:contract_address) + insert(:token, contract_address: contract_address) transaction = :transaction - |> insert(from_address: lincoln, to_address: contract_token_address) + |> insert(from_address: lincoln, to_address: contract_address) |> with_block(block) insert( @@ -298,7 +297,7 @@ defmodule BlockScoutWeb.ViewingAddressesTest do from_address: lincoln, to_address: taft, transaction: transaction, - token_contract_address: contract_token_address + token_contract_address: contract_address ) session @@ -318,12 +317,12 @@ defmodule BlockScoutWeb.ViewingAddressesTest do taft = addresses.taft morty = build(:address) - contract_token_address = insert(:contract_address) - insert(:token, contract_address: contract_token_address) + contract_address = insert(:contract_address) + insert(:token, contract_address: contract_address) transaction = :transaction - |> insert(from_address: lincoln, to_address: contract_token_address) + |> insert(from_address: lincoln, to_address: contract_address) |> with_block(block) insert( @@ -331,7 +330,7 @@ defmodule BlockScoutWeb.ViewingAddressesTest do from_address: lincoln, to_address: taft, transaction: transaction, - token_contract_address: contract_token_address + token_contract_address: contract_address ) insert( @@ -339,7 +338,7 @@ defmodule BlockScoutWeb.ViewingAddressesTest do from_address: lincoln, to_address: morty, transaction: transaction, - token_contract_address: contract_token_address + token_contract_address: contract_address ) session @@ -358,17 +357,13 @@ defmodule BlockScoutWeb.ViewingAddressesTest do lincoln = addresses.lincoln taft = addresses.taft - contract_token_address = - insert( - :address, - contract_code: Factory.data("contract_code") - ) + contract_address = insert(:contract_address) - insert(:token, contract_address: contract_token_address) + insert(:token, contract_address: contract_address) transaction = :transaction - |> insert(from_address: lincoln, to_address: contract_token_address) + |> insert(from_address: lincoln, to_address: contract_address) |> with_block(block) insert_list( @@ -377,7 +372,7 @@ defmodule BlockScoutWeb.ViewingAddressesTest do from_address: lincoln, to_address: taft, transaction: transaction, - token_contract_address: contract_token_address + token_contract_address: contract_address ) session @@ -393,12 +388,12 @@ defmodule BlockScoutWeb.ViewingAddressesTest do lincoln = addresses.lincoln taft = addresses.taft - contract_token_address = insert(:contract_address) - insert(:token, contract_address: contract_token_address) + contract_address = insert(:contract_address) + insert(:token, contract_address: contract_address) transaction = :transaction - |> insert(from_address: lincoln, to_address: contract_token_address) + |> insert(from_address: lincoln, to_address: contract_address) |> with_block(block) insert_list( @@ -407,7 +402,7 @@ defmodule BlockScoutWeb.ViewingAddressesTest do from_address: lincoln, to_address: taft, transaction: transaction, - token_contract_address: contract_token_address + token_contract_address: contract_address ) session diff --git a/apps/block_scout_web/test/block_scout_web/views/address_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/address_view_test.exs index 310edc23d3..3bce75c435 100644 --- a/apps/block_scout_web/test/block_scout_web/views/address_view_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/address_view_test.exs @@ -1,9 +1,106 @@ defmodule BlockScoutWeb.AddressViewTest do use BlockScoutWeb.ConnCase, async: true - alias Explorer.Chain.Data + alias Explorer.Chain.{Address, Data, Transaction} alias BlockScoutWeb.AddressView + describe "address_partial_selector/4" do + test "for a pending contract creation to address" do + transaction = insert(:transaction, to_address: nil, created_contract_address_hash: nil) + assert AddressView.address_partial_selector(transaction, :to, nil) == "Contract Address Pending" + end + + test "will truncate address" do + transaction = %Transaction{to_address_hash: hash} = insert(:transaction) + + assert %{ + partial: "_link.html", + address_hash: ^hash, + contract: false, + truncate: true + } = AddressView.address_partial_selector(transaction, :to, nil, true) + end + + test "for a non-contract to address not on address page" do + transaction = %Transaction{to_address_hash: hash} = insert(:transaction) + + assert %{ + partial: "_link.html", + address_hash: ^hash, + contract: false, + truncate: false + } = AddressView.address_partial_selector(transaction, :to, nil) + end + + test "for a non-contract to address non matching address page" do + transaction = %Transaction{to_address_hash: hash} = insert(:transaction) + + assert %{ + partial: "_link.html", + address_hash: ^hash, + contract: false, + truncate: false + } = AddressView.address_partial_selector(transaction, :to, nil) + end + + test "for a non-contract to address matching address page" do + transaction = %Transaction{to_address_hash: hash} = insert(:transaction) + + assert %{ + partial: "_responsive_hash.html", + address_hash: ^hash, + contract: false, + truncate: false + } = AddressView.address_partial_selector(transaction, :to, transaction.to_address) + end + + test "for a contract to address non matching address page" do + contract = %Address{hash: hash} = insert(:contract_address) + transaction = insert(:transaction, to_address: nil, created_contract_address: contract) + + assert %{ + partial: "_link.html", + address_hash: ^hash, + contract: true, + truncate: false + } = AddressView.address_partial_selector(transaction, :to, transaction.to_address) + end + + test "for a contract to address matching address page" do + contract = %Address{hash: hash} = insert(:contract_address) + transaction = insert(:transaction, to_address: nil, created_contract_address: contract) + + assert %{ + partial: "_responsive_hash.html", + address_hash: ^hash, + contract: true, + truncate: false + } = AddressView.address_partial_selector(transaction, :to, contract) + end + + test "for a non-contract from address not on address page" do + transaction = %Transaction{to_address_hash: hash} = insert(:transaction) + + assert %{ + partial: "_link.html", + address_hash: ^hash, + contract: false, + truncate: false + } = AddressView.address_partial_selector(transaction, :to, nil) + end + + test "for a non-contract from address matching address page" do + transaction = %Transaction{from_address_hash: hash} = insert(:transaction) + + assert %{ + partial: "_responsive_hash.html", + address_hash: ^hash, + contract: false, + truncate: false + } = AddressView.address_partial_selector(transaction, :from, transaction.from_address) + end + end + describe "contract?/1" do test "with a smart contract" do {:ok, code} = Data.cast("0x000000000000000000000000862d67cb0773ee3f8ce7ea89b328ffea861ab3ef") @@ -21,51 +118,31 @@ defmodule BlockScoutWeb.AddressViewTest do end end - describe "display_address_hash/3" do - test "for a pending contract creation to address" do - transaction = insert(:transaction, to_address: nil, created_contract_address_hash: nil) - assert AddressView.display_address_hash(nil, transaction, :to) == "Contract Address Pending" + describe "qr_code/1" do + test "it returns an encoded value" do + address = build(:address) + assert {:ok, _} = Base.decode64(AddressView.qr_code(address)) end + end - test "for a non-contract to address not on address page" do - transaction = insert(:transaction) - rendered_string = - nil - |> AddressView.display_address_hash(transaction, :to) - |> Phoenix.HTML.safe_to_string() - assert rendered_string =~ "responsive_hash" - assert rendered_string =~ "address_hash_link" - refute rendered_string =~ "contract-address" - end + describe "render_partial/1" do + test "renders _link partial" do + %Address{hash: hash} = build(:address) - test "for a non-contract to address non matching address page" do - transaction = insert(:transaction) - rendered_string = - :address - |> insert() - |> AddressView.display_address_hash(transaction, :to) - |> Phoenix.HTML.safe_to_string() - assert rendered_string =~ "responsive_hash" - assert rendered_string =~ "address_hash_link" - refute rendered_string =~ "contract-address" + assert {:safe, _} = + AddressView.render_partial(%{partial: "_link.html", address_hash: hash, contract: false, truncate: false}) end - test "for a non-contract to address matching address page" do - transaction = insert(:transaction) - rendered_string = - transaction.to_address - |> AddressView.display_address_hash(transaction, :to) - |> Phoenix.HTML.safe_to_string() - assert rendered_string =~ "responsive_hash" - refute rendered_string =~ "address_hash_link" - refute rendered_string =~ "contract-address" - end - end + test "renders _responsive_hash partial" do + %Address{hash: hash} = build(:address) - describe "qr_code/1" do - test "it returns an encoded value" do - address = build(:address) - assert {:ok, _} = Base.decode64(AddressView.qr_code(address)) + assert {:safe, _} = + AddressView.render_partial(%{ + partial: "_responsive_hash.html", + address_hash: hash, + contract: false, + truncate: false + }) end end