Refactor address rendering functions and add test coverage

pull/688/head
Stamates 6 years ago
parent a514f3f5b9
commit 5b8b08a255
  1. 4
      apps/block_scout_web/lib/block_scout_web/templates/transaction/_tile.html.eex
  2. 4
      apps/block_scout_web/lib/block_scout_web/templates/transaction/_token_transfer.html.eex
  3. 4
      apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex
  4. 98
      apps/block_scout_web/lib/block_scout_web/views/address_view.ex
  5. 3
      apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex
  6. 45
      apps/block_scout_web/test/block_scout_web/features/viewing_addresses_test.exs
  7. 157
      apps/block_scout_web/test/block_scout_web/views/address_view_test.exs

@ -11,9 +11,9 @@
<div class="col-md-7 col-lg-8 d-flex flex-column pr-2 pr-sm-2 pr-md-0">
<%= render "_link.html", transaction_hash: @transaction.hash %>
<span class="text-nowrap">
<%= BlockScoutWeb.AddressView.display_address_hash(assigns[:current_address], @transaction, :from) %>
<%= @transaction |> BlockScoutWeb.AddressView.address_partial_selector(:from, assigns[:current_address]) |> BlockScoutWeb.AddressView.render_partial() %>
&rarr;
<%= BlockScoutWeb.AddressView.display_address_hash(assigns[:current_address], @transaction, :to) %>
<%= @transaction |> BlockScoutWeb.AddressView.address_partial_selector(:to, assigns[:current_address]) |> BlockScoutWeb.AddressView.render_partial() %>
</span>
<span class="d-flex flex-md-row flex-column mt-3 mt-md-0">
<span class="tile-title">

@ -11,9 +11,9 @@
</span>
<% 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() %>
&rarr;
<%= BlockScoutWeb.AddressView.display_address_hash(@address, @token_transfer, :to, true) %>
<%= @token_transfer |> BlockScoutWeb.AddressView.address_partial_selector(:to, @address, true) |> BlockScoutWeb.AddressView.render_partial() %>
</span>
<span class="col-12 col-md-7 ml-3 ml-sm-0">
<%= token_transfer_amount(@token_transfer) %>

@ -15,9 +15,9 @@
<h1 class="card-title"><%= gettext "Transaction Details" %> </h1>
<h3 data-test="transaction_detail_hash"><%= @transaction %> </h3>
<span class="d-block mb-2 text-muted">
<%= BlockScoutWeb.AddressView.display_address_hash(nil, @transaction, :from) %>
<%= @transaction |> BlockScoutWeb.AddressView.address_partial_selector(:from, nil) |> BlockScoutWeb.AddressView.render_partial() %>
<span class="text-muted"> &rarr; </span>
<%= BlockScoutWeb.AddressView.display_address_hash(nil, @transaction, :to) %>
<%= @transaction |> BlockScoutWeb.AddressView.address_partial_selector(:to, nil) |> BlockScoutWeb.AddressView.render_partial() %>
</span>
<div class="d-flex flex-row justify-content-start text-muted">
<span class="mr-4 text-<%= BlockScoutWeb.TransactionView.type_suffix(@transaction) %>"><%= BlockScoutWeb.TransactionView.transaction_display_type(@transaction) %></span>

@ -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

@ -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

@ -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

@ -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

Loading…
Cancel
Save