From b55a965b26428b2611389afd878db60f8da29002 Mon Sep 17 00:00:00 2001 From: Oleg Sovetnik Date: Mon, 23 May 2022 15:19:07 +0300 Subject: [PATCH] Notifier specs --- .../explorer/accounts/notify/notify_test.exs | 91 +++++++++++++++++++ apps/explorer/test/support/factory.ex | 46 +++++++++- 2 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 apps/explorer/test/explorer/accounts/notify/notify_test.exs diff --git a/apps/explorer/test/explorer/accounts/notify/notify_test.exs b/apps/explorer/test/explorer/accounts/notify/notify_test.exs new file mode 100644 index 0000000000..63d7aa2764 --- /dev/null +++ b/apps/explorer/test/explorer/accounts/notify/notify_test.exs @@ -0,0 +1,91 @@ +defmodule Explorer.Accounts.Notify.NotifyTest do + # use ExUnit.Case + use Explorer.DataCase + + import Explorer.Factory + + alias Explorer.Accounts.Notifier.Notify + alias Explorer.Accounts.{WatchlistAddress, WatchlistNotification} + alias Explorer.Chain + alias Explorer.Chain.{Address, Token, TokenTransfer, Transaction, Wei} + alias Explorer.Repo + + setup do + Application.put_env(:explorer, Explorer.Accounts, + sendgrid: [ + sender: "noreply@blockscout.com", + template: "d-666" + ] + ) + + Application.put_env(:explorer, Explorer.Mailer, + adapter: Bamboo.SendGridAdapter, + api_key: "SENDGRID_API_KEY" + ) + + Application.put_env( + :ueberauth, + Ueberauth, + providers: [ + auth0: { + Ueberauth.Strategy.Auth0, + [callback_url: "callback.url"] + } + ], + logout_url: "logout.url", + logout_return_to_url: "return.url" + ) + end + + describe "notify" do + test "when address not in any watchlist" do + tx = with_block(insert(:transaction)) + + notify = Notify.call([tx]) + + wn = + WatchlistNotification + |> first + |> Repo.one() + + assert notify == [[:ok]] + + assert wn == nil + end + + test "when address apears in watchlist" do + wa = + %WatchlistAddress{ + address: address + } = insert(:account_watchlist_address) + + watchlist_address = Repo.preload(wa, :address, watchlist: :identity) + + tx = + %Transaction{ + from_address: from_address, + to_address: to_address, + block_number: block_number, + hash: tx_hash + } = with_block(insert(:transaction, to_address: address)) + + {_, fee} = Chain.fee(tx, :gwei) + amount = Wei.to(tx.value, :ether) + notify = Notify.call([tx]) + + wn = + WatchlistNotification + |> first + |> Repo.one() + + assert notify == [[:ok]] + + assert wn.amount == amount + assert wn.direction == "incoming" + assert wn.method == "transfer" + assert wn.subject == "Coin transaction" + assert wn.tx_fee == fee + assert wn.type == "COIN" + end + end +end diff --git a/apps/explorer/test/support/factory.ex b/apps/explorer/test/support/factory.ex index 34f4692240..4ee1b045ab 100644 --- a/apps/explorer/test/support/factory.ex +++ b/apps/explorer/test/support/factory.ex @@ -6,8 +6,18 @@ defmodule Explorer.Factory do import Ecto.Query import Kernel, except: [+: 2] - alias Bcrypt - alias Explorer.Accounts.{User, UserContact} + alias Comeonin.Bcrypt + + alias Explorer.Accounts.{ + User, + UserContact, + Identity, + TagAddress, + Watchlist, + WatchlistAddress, + WatchlistNotification + } + alias Explorer.Admin.Administrator alias Explorer.Chain.Block.{EmissionReward, Range, Reward} @@ -37,6 +47,38 @@ defmodule Explorer.Factory do alias Explorer.Market.MarketHistory alias Explorer.Repo + def account_identity_factory do + %Identity{ + uid: sequence("github|"), + email: sequence(:email, &"me-#{&1}@blockscout.com"), + name: sequence("John") + } + end + + def account_watchlist_factory do + %Watchlist{ + name: "default", + identity: build(:account_identity) + } + end + + def account_watchlist_address_factory do + %WatchlistAddress{ + name: "wallet", + watchlist: build(:account_watchlist), + address: build(:address), + watch_coin_input: true, + watch_coin_output: true, + watch_erc_20_input: true, + watch_erc_20_output: true, + watch_erc_721_input: true, + watch_erc_721_output: true, + watch_erc_1155_input: true, + watch_erc_1155_output: true, + notify_email: true + } + end + def address_factory do %Address{ hash: address_hash()