From 1c60e1eee846c48ccdb1a2c6838bd65d25141d39 Mon Sep 17 00:00:00 2001 From: AndriusA Date: Tue, 16 Jun 2020 11:16:43 +0100 Subject: [PATCH] fixes balance being an empty string --- .../browser/test/rewards_browsertest.cc | 45 +++++++++++++++++-- .../src/bat/ledger/internal/wallet/balance.cc | 1 + .../src/bat/ledger/internal/wallet/wallet.cc | 7 ++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/components/brave_rewards/browser/test/rewards_browsertest.cc b/components/brave_rewards/browser/test/rewards_browsertest.cc index f50dc521f4c1..de9700be0bba 100644 --- a/components/brave_rewards/browser/test/rewards_browsertest.cc +++ b/components/brave_rewards/browser/test/rewards_browsertest.cc @@ -125,6 +125,14 @@ bool URLMatches(const std::string& url, enum class ContributionType { OneTimeTip, MonthlyTip }; +class MadeRequest { + public: + MadeRequest(const std::string& url, int32_t method) + : url(url), method(method) {} + std::string url; + int32_t method; +}; + } // namespace namespace brave_test_resp { @@ -354,7 +362,7 @@ class RewardsBrowserTest int* response_status_code, std::string* response, std::map* headers) { - request_made_ = true; + requests_made_.emplace_back(url, method); std::vector tmp = base::SplitString(url, "/", base::TRIM_WHITESPACE, @@ -1442,7 +1450,7 @@ class RewardsBrowserTest bool last_publisher_added_ = false; bool alter_publisher_list_ = false; bool show_defaults_in_properties_ = false; - bool request_made_ = false; + std::vector requests_made_; double balance_ = 0; double reconciled_tip_total_ = 0; double pending_balance_ = 0; @@ -2764,7 +2772,7 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, PanelDontDoRequests) { ASSERT_TRUE(popup_contents); // Make sure that no request was made - ASSERT_FALSE(request_made_); + ASSERT_TRUE(requests_made_.empty()); } IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, ShowMonthlyIfACOff) { @@ -2973,3 +2981,34 @@ IN_PROC_BROWSER_TEST_F( "td:nth-of-type(3)", "30.000BAT42.90 USD"); } + +IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, ZeroBalanceWalletClaimNotCalled) { + SetUpUpholdWallet(50.0); + EnableRewardsViaCode(); + + requests_made_.clear(); // only consider requests made after initialization + + base::RunLoop run_loop; + auto test_callback = + [&](int32_t result, + std::unique_ptr wallet) { + EXPECT_EQ(result, static_cast(ledger::Result::LEDGER_OK)); + EXPECT_FALSE(requests_made_.empty()); + + // Should not attempt to call /v2/wallet/UUID/claim endpoint + // since by default the wallet should contain 0 `user_funds` + auto wallet_claim_call = std::find_if( + requests_made_.begin(), requests_made_.end(), + [](const MadeRequest& req) { + return req.url.find("/v2/wallet") != std::string::npos && + req.url.find("/claim") != std::string::npos; + }); + + EXPECT_TRUE(wallet_claim_call == requests_made_.end()); + run_loop.Quit(); + }; + + rewards_service_->GetExternalWallet( + "uphold", base::BindLambdaForTesting(test_callback)); + run_loop.Run(); +} diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/balance.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/balance.cc index cf458b172ec8..6bf330099901 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/balance.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/balance.cc @@ -39,6 +39,7 @@ void Balance::Fetch(ledger::FetchBalanceCallback callback) { // we can skip balance server ping if (!braveledger_state::GetFetchOldBalanceEnabled(ledger_)) { auto balance = ledger::Balance::New(); + balance->user_funds = "0"; GetUnblindedTokens(std::move(balance), callback); return; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc index 4f45a5b16fff..54df7f0c3e68 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc @@ -265,6 +265,9 @@ std::string Wallet::GetClaimPayload( const std::string anon_address) { ledger::UnsignedTxProperties unsigned_tx; unsigned_tx.amount = user_funds; + if (unsigned_tx.amount.empty()) { + unsigned_tx.amount = "0"; + } unsigned_tx.currency = "BAT"; unsigned_tx.destination = new_address; const ledger::UnsignedTxState unsigned_tx_state; @@ -303,8 +306,8 @@ std::string Wallet::GetClaimPayload( signed_tx.SetStringKey("octets", octets); base::Value denomination(base::Value::Type::DICTIONARY); - denomination.SetStringKey("amount", user_funds); - denomination.SetStringKey("currency", "BAT"); + denomination.SetStringKey("amount", unsigned_tx.amount); + denomination.SetStringKey("currency", unsigned_tx.currency); base::Value body(base::Value::Type::DICTIONARY); body.SetStringKey("destination", new_address);