From 80d490054f80fd844f5d68a7317b1bbc5d6ad628 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Mon, 31 May 2021 14:46:39 +0200 Subject: [PATCH 01/15] Performing wallet linking during Uphold authorization. Adding GetAnonFunds(), OnGetAnonFunds(), TransferAnonFunds() and OnTransferAnonFunds() to UpholdAuthorization. --- .../internal/uphold/uphold_authorization.cc | 95 ++++++++++++++++++- .../internal/uphold/uphold_authorization.h | 18 ++++ 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 93dbc63fb43a..68c35b4c71b5 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -14,6 +14,7 @@ #include "bat/ledger/internal/ledger_impl.h" #include "bat/ledger/internal/logging/event_log_keys.h" #include "bat/ledger/internal/uphold/uphold_util.h" +#include "bat/ledger/option_keys.h" using std::placeholders::_1; using std::placeholders::_2; @@ -24,6 +25,7 @@ namespace uphold { UpholdAuthorization::UpholdAuthorization(LedgerImpl* ledger) : ledger_(ledger), + promotion_server_(std::make_unique(ledger)), uphold_server_(std::make_unique(ledger)) { } @@ -229,12 +231,101 @@ void UpholdAuthorization::OnCardCreate( address.substr(0, 5)); } - base::flat_map args; if (wallet_ptr->status != type::WalletStatus::VERIFIED) { + base::flat_map args{}; args["redirect_url"] = GetSecondStepVerify(); + callback(type::Result::LEDGER_OK, args); } - callback(type::Result::LEDGER_OK, args); + GetAnonFunds(std::bind(&UpholdAuthorization::OnGetAnonFunds, this, _1, _2, callback)); +} + +void UpholdAuthorization::GetAnonFunds( + endpoint::promotion::GetWalletBalanceCallback callback) { + if (ledger_->ledger_client()->GetBooleanOption( + option::kContributionsDisabledForBAPMigration)) { + BLOG(1, "Fetch balance disabled for BAP migration."); + callback(type::Result::LEDGER_OK, type::Balance::New()); + return; + } + + // if we don't have user funds in anon card anymore + // we can skip balance server ping + if (!ledger_->state()->GetFetchOldBalanceEnabled()) { + callback(type::Result::LEDGER_OK, type::Balance::New()); + return; + } + + const auto wallet = ledger_->wallet()->GetWallet(); + if (!wallet) { + BLOG(1, "Wallet is not created!"); + ledger_->state()->SetFetchOldBalanceEnabled(false); + callback(type::Result::LEDGER_OK, type::Balance::New()); + return; + } + + if (wallet->payment_id.empty()) { + BLOG(0, "Payment ID is empty!"); + callback(type::Result::LEDGER_ERROR, type::Balance::New()); + return; + } + + promotion_server_->get_wallet_balance()->Request(callback); +} + +void UpholdAuthorization::OnGetAnonFunds( + const type::Result result, + type::BalancePtr balance, + ledger::ExternalWalletAuthorizationCallback callback) { + if (result != type::Result::LEDGER_OK) { + BLOG(0, "Couldn't get anonymous funds!"); + callback(type::Result::LEDGER_ERROR, {}); + return; + } + + if (balance->user_funds == 0.0) { // TODO: floating-point comparison + ledger_->state()->SetFetchOldBalanceEnabled(false); + } + + TransferAnonFunds(balance->user_funds, std::bind(&UpholdAuthorization::OnTransferAnonFunds, this, _1, callback)); +} + +void UpholdAuthorization::TransferAnonFunds( + const double user_funds, + ledger::endpoint::promotion::PostClaimUpholdCallback callback) { + auto wallet_ptr = uphold::GetWallet(ledger_); + + if (!wallet_ptr) { + BLOG(0, "Wallet is null!"); + callback(type::Result::LEDGER_ERROR); + return; + } + + promotion_server_->post_claim_uphold()->Request(user_funds, callback); +} + +void UpholdAuthorization::OnTransferAnonFunds( + const type::Result result, + ledger::ExternalWalletAuthorizationCallback callback) { + if (result == type::Result::LEDGER_OK) { + callback(type::Result::LEDGER_OK, {}); + return; + } + + if (result == type::Result::ALREADY_EXISTS) { + ledger_->ledger_client()->ShowNotification("wallet_device_limit_reached", {}, [](type::Result) {}); + + std::string event_text = "uphold"; + if (auto wallet_ptr = uphold::GetWallet(ledger_)) + event_text += "/" + wallet_ptr->address.substr(0, 5); + + ledger_->database()->SaveEventLog(log::kDeviceLimitReached, event_text); + + callback(type::Result::ALREADY_EXISTS, {}); + return; + } + + callback(type::Result::LEDGER_ERROR, {}); } } // namespace uphold diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h index e02b9783f630..b60aea159a95 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h @@ -10,6 +10,7 @@ #include #include "base/containers/flat_map.h" +#include "bat/ledger/internal/endpoint/promotion/promotion_server.h" #include "bat/ledger/internal/endpoint/uphold/uphold_server.h" #include "bat/ledger/internal/uphold/uphold_user.h" #include "bat/ledger/ledger.h" @@ -45,7 +46,24 @@ class UpholdAuthorization { const std::string& address, ledger::ExternalWalletAuthorizationCallback callback); + void GetAnonFunds( + endpoint::promotion::GetWalletBalanceCallback callback); + + void OnGetAnonFunds( + const type::Result result, + type::BalancePtr balance, + ledger::ExternalWalletAuthorizationCallback callback); + + void TransferAnonFunds( + const double user_funds, + endpoint::promotion::PostClaimUpholdCallback callback); + + void OnTransferAnonFunds( + const type::Result result, + ledger::ExternalWalletAuthorizationCallback callback); + LedgerImpl* ledger_; // NOT OWNED + std::unique_ptr promotion_server_; std::unique_ptr uphold_server_; }; From 9fc9b7e879054753739629bfb288e4f7e6252fb5 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Wed, 2 Jun 2021 00:12:30 +0200 Subject: [PATCH 02/15] Removing the WalletClaim class (wallet_claim.h and wallet_claim.cc). By removing the call to WalletClaim::Start() in Wallet::ClaimFunds(), we avoid fetching the balances for all the wallet types (anonymous, blinded, uphold and bitflyer) and avoid linking the Uphold account to the Brave wallet in the "generate wallet flow". Fetching the balance of the anonymous wallet and the linking are already implemented in UpholdAuthorization::GetAnonFunds() and UpholdAuthorization::TransferAnonFunds(), respectively, in the "authorization flow". --- vendor/bat-native-ledger/BUILD.gn | 2 - .../internal/uphold/uphold_authorization.cc | 4 +- .../src/bat/ledger/internal/wallet/wallet.cc | 32 ++---- .../src/bat/ledger/internal/wallet/wallet.h | 2 - .../ledger/internal/wallet/wallet_claim.cc | 108 ------------------ .../bat/ledger/internal/wallet/wallet_claim.h | 43 ------- 6 files changed, 12 insertions(+), 179 deletions(-) delete mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_claim.cc delete mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_claim.h diff --git a/vendor/bat-native-ledger/BUILD.gn b/vendor/bat-native-ledger/BUILD.gn index f16aa82bd25a..9b63baa0f783 100644 --- a/vendor/bat-native-ledger/BUILD.gn +++ b/vendor/bat-native-ledger/BUILD.gn @@ -515,8 +515,6 @@ source_set("ledger") { "src/bat/ledger/internal/wallet/wallet.h", "src/bat/ledger/internal/wallet/wallet_balance.cc", "src/bat/ledger/internal/wallet/wallet_balance.h", - "src/bat/ledger/internal/wallet/wallet_claim.cc", - "src/bat/ledger/internal/wallet/wallet_claim.h", "src/bat/ledger/internal/wallet/wallet_create.cc", "src/bat/ledger/internal/wallet/wallet_create.h", "src/bat/ledger/internal/wallet/wallet_recover.cc", diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 68c35b4c71b5..90a659ece9cd 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -293,9 +293,7 @@ void UpholdAuthorization::OnGetAnonFunds( void UpholdAuthorization::TransferAnonFunds( const double user_funds, ledger::endpoint::promotion::PostClaimUpholdCallback callback) { - auto wallet_ptr = uphold::GetWallet(ledger_); - - if (!wallet_ptr) { + if (!uphold::GetWallet(ledger_)) { BLOG(0, "Wallet is null!"); callback(type::Result::LEDGER_ERROR); 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 0b768ce51d22..291e03578c90 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 @@ -32,7 +32,6 @@ Wallet::Wallet(LedgerImpl* ledger) : create_(std::make_unique(ledger)), recover_(std::make_unique(ledger)), balance_(std::make_unique(ledger)), - claim_(std::make_unique(ledger)), promotion_server_(std::make_unique(ledger)) { } @@ -162,27 +161,18 @@ void Wallet::DisconnectWallet( } void Wallet::ClaimFunds(ledger::ResultCallback callback) { - // Anonymous funds claim - claim_->Start([this, callback](const type::Result result) { - if (result != type::Result::LEDGER_OK && - result != type::Result::ALREADY_EXISTS) { - BLOG(0, "Claiming anon funds failed"); - callback(type::Result::CONTINUE); - return; + // tokens claim + ledger_->promotion()->TransferTokens( + [callback](const type::Result result, std::string drain_id) { + if (result != type::Result::LEDGER_OK) { + BLOG(0, "Claiming tokens failed"); + callback(type::Result::CONTINUE); + return; + } + + callback(type::Result::LEDGER_OK); } - - // tokens claim - ledger_->promotion()->TransferTokens( - [callback](const type::Result result, std::string drain_id) { - if (result != type::Result::LEDGER_OK) { - BLOG(0, "Claiming tokens failed"); - callback(type::Result::CONTINUE); - return; - } - - callback(type::Result::LEDGER_OK); - }); - }); + ); } void Wallet::GetAnonWalletStatus(ledger::ResultCallback callback) { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h index 16aaca70f03f..e4b660c608a5 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h @@ -14,7 +14,6 @@ #include "base/containers/flat_map.h" #include "bat/ledger/internal/endpoint/promotion/promotion_server.h" #include "bat/ledger/internal/wallet/wallet_balance.h" -#include "bat/ledger/internal/wallet/wallet_claim.h" #include "bat/ledger/internal/wallet/wallet_create.h" #include "bat/ledger/internal/wallet/wallet_recover.h" #include "bat/ledger/ledger.h" @@ -71,7 +70,6 @@ class Wallet { std::unique_ptr create_; std::unique_ptr recover_; std::unique_ptr balance_; - std::unique_ptr claim_; std::unique_ptr promotion_server_; }; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_claim.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_claim.cc deleted file mode 100644 index ad6ce0de465e..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_claim.cc +++ /dev/null @@ -1,108 +0,0 @@ -/* Copyright (c) 2020 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "bat/ledger/internal/wallet/wallet_claim.h" - -#include -#include -#include - -#include "base/base64.h" -#include "base/json/json_writer.h" -#include "base/strings/stringprintf.h" -#include "base/values.h" -#include "bat/ledger/internal/ledger_impl.h" -#include "bat/ledger/internal/logging/event_log_keys.h" -#include "bat/ledger/internal/uphold/uphold_util.h" - -using std::placeholders::_1; -using std::placeholders::_2; - -namespace { - -} // namespace - -namespace ledger { -namespace wallet { - -WalletClaim::WalletClaim(LedgerImpl* ledger) : - ledger_(ledger), - promotion_server_(std::make_unique(ledger)) { -} - -WalletClaim::~WalletClaim() = default; - -void WalletClaim::Start(ledger::ResultCallback callback) { - ledger_->wallet()->FetchBalance(std::bind(&WalletClaim::OnBalance, - this, - _1, - _2, - callback)); -} - -void WalletClaim::OnBalance( - const type::Result result, - type::BalancePtr balance, - ledger::ResultCallback callback) { - if (result != type::Result::LEDGER_OK || !balance) { - BLOG(0, "Anon funds transfer failed"); - callback(type::Result::LEDGER_ERROR); - return; - } - - if (ledger_->state()->GetAnonTransferChecked() && - balance->user_funds == 0) { - BLOG(1, "Second ping with zero balance"); - callback(type::Result::LEDGER_OK); - return; - } - - auto wallet_ptr = uphold::GetWallet(ledger_); - - if (!wallet_ptr) { - BLOG(0, "Wallet is null"); - callback(type::Result::LEDGER_ERROR); - return; - } - - auto url_callback = std::bind(&WalletClaim::OnTransferFunds, - this, - _1, - callback); - - promotion_server_->post_claim_uphold()->Request( - balance->user_funds, - url_callback); -} - -void WalletClaim::OnTransferFunds( - const type::Result result, - ledger::ResultCallback callback) { - if (result == type::Result::LEDGER_OK) { - ledger_->state()->SetAnonTransferChecked(true); - callback(type::Result::LEDGER_OK); - return; - } - - if (result == type::Result::ALREADY_EXISTS) { - ledger_->state()->SetAnonTransferChecked(true); - ledger_->ledger_client()->ShowNotification("wallet_device_limit_reached", - {}, [](type::Result) {}); - - std::string event_text = "uphold"; - if (auto wallet_ptr = uphold::GetWallet(ledger_)) - event_text += "/" + wallet_ptr->address.substr(0, 5); - - ledger_->database()->SaveEventLog(log::kDeviceLimitReached, event_text); - - callback(type::Result::ALREADY_EXISTS); - return; - } - - callback(type::Result::LEDGER_ERROR); -} - -} // namespace wallet -} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_claim.h b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_claim.h deleted file mode 100644 index 2693c795190f..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_claim.h +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright (c) 2020 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVELEDGER_WALLET_WALLET_CLAIM_H_ -#define BRAVELEDGER_WALLET_WALLET_CLAIM_H_ - -#include -#include - -#include "bat/ledger/internal/endpoint/promotion/promotion_server.h" -#include "bat/ledger/ledger.h" - -namespace ledger { -class LedgerImpl; - -namespace wallet { - -class WalletClaim { - public: - explicit WalletClaim(LedgerImpl* ledger); - ~WalletClaim(); - - void Start(ledger::ResultCallback callback); - - private: - void OnBalance( - const type::Result result, - type::BalancePtr properties, - ledger::ResultCallback callback); - - void OnTransferFunds( - const type::Result result, - ledger::ResultCallback callback); - - LedgerImpl* ledger_; // NOT OWNED - std::unique_ptr promotion_server_; -}; - -} // namespace wallet -} // namespace ledger -#endif // BRAVELEDGER_WALLET_WALLET_CLAIM_H_ From 21b65173e17fb71b480427e0ca33c43ba480b51c Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Wed, 2 Jun 2021 10:20:50 +0200 Subject: [PATCH 03/15] Deprecating kAnonTransferChecked. It's cleared in the "authorization flow" to be able to tell if we need to do the linking in the "generate wallet flow". It's set in the "generate wallet flow" on successful linking or when the device limit is reached. Now that linking happens during authorization, we don't need it anymore. --- components/brave_rewards/common/pref_names.h | 2 +- .../src/bat/ledger/internal/state/state.cc | 11 ----------- .../src/bat/ledger/internal/state/state.h | 4 ---- .../src/bat/ledger/internal/state/state_keys.h | 2 +- .../ledger/internal/uphold/uphold_authorization.cc | 5 ----- .../src/bat/ledger/internal/wallet/wallet_recover.cc | 1 - 6 files changed, 2 insertions(+), 23 deletions(-) diff --git a/components/brave_rewards/common/pref_names.h b/components/brave_rewards/common/pref_names.h index 30e1511ac891..5485cee46133 100644 --- a/components/brave_rewards/common/pref_names.h +++ b/components/brave_rewards/common/pref_names.h @@ -28,7 +28,7 @@ extern const char kServerPublisherListStamp[]; extern const char kUpholdAnonAddress[]; // DEPRECATED extern const char kPromotionLastFetchStamp[]; extern const char kPromotionCorruptedMigrated[]; -extern const char kAnonTransferChecked[]; +extern const char kAnonTransferChecked[]; // DEPRECATED extern const char kVersion[]; extern const char kMinVisitTime[]; extern const char kMinVisits[]; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.cc index 40f8e8704f79..27d6d068465b 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.cc @@ -371,16 +371,5 @@ uint64_t State::GetPromotionLastFetchStamp() { return ledger_->ledger_client()->GetUint64State(kPromotionLastFetchStamp); } -void State::SetAnonTransferChecked(const bool checked) { - ledger_->database()->SaveEventLog( - kAnonTransferChecked, - std::to_string(checked)); - ledger_->ledger_client()->SetBooleanState(kAnonTransferChecked, checked); -} - -bool State::GetAnonTransferChecked() { - return ledger_->ledger_client()->GetBooleanState(kAnonTransferChecked); -} - } // namespace state } // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.h index 7ecc60ce2d36..6eb15da39143 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.h @@ -107,10 +107,6 @@ class State { uint64_t GetPromotionLastFetchStamp(); - void SetAnonTransferChecked(const bool checked); - - bool GetAnonTransferChecked(); - private: LedgerImpl* ledger_; // NOT OWNED std::unique_ptr migration_; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h index de8d7b168dcf..d88ce276a526 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h @@ -16,7 +16,7 @@ const char kUpholdAnonAddress[] = "uphold_anon_address"; // DEPRECATED const char kPromotionLastFetchStamp[] = "promotion_last_fetch_stamp"; const char kPromotionCorruptedMigrated[] = "promotion_corrupted_migrated2"; -const char kAnonTransferChecked[] = "anon_transfer_checked"; +const char kAnonTransferChecked[] = "anon_transfer_checked"; // DEPRECATED const char kVersion[] = "version"; const char kMinVisitTime[] = "ac.min_visit_time"; const char kMinVisits[] = "ac.min_visits"; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 90a659ece9cd..bd034cca0ba2 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -154,11 +154,6 @@ void UpholdAuthorization::OnAuthorize( break; } - // After a login, we want to attempt to relink the user's payment ID to their - // Uphold wallet address. Clear the flag that will cause relinking to be - // skipped. - ledger_->state()->SetAnonTransferChecked(false); - ledger_->uphold()->SetWallet(wallet_ptr->Clone()); auto user_callback = std::bind(&UpholdAuthorization::OnGetUser, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_recover.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_recover.cc index 763feefc16a9..2ef2134c2568 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_recover.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_recover.cc @@ -105,7 +105,6 @@ void WalletRecover::OnRecover( return; } - ledger_->state()->SetAnonTransferChecked(false); ledger_->state()->SetPromotionLastFetchStamp(0); ledger_->state()->SetPromotionCorruptedMigrated(true); if (legacy_wallet) { From c0880066a454c5271fe25c898ee87e03587165d7 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Thu, 3 Jun 2021 22:57:07 +0200 Subject: [PATCH 04/15] Adding uphold_authorization_unittest.cc and value-parameterized tests for UpholdAuthorization::GetAnonFunds(). --- .../internal/uphold/uphold_authorization.cc | 16 +- .../internal/uphold/uphold_authorization.h | 26 +-- .../uphold/uphold_authorization_unittest.cc | 189 ++++++++++++++++++ vendor/bat-native-ledger/test/BUILD.gn | 1 + 4 files changed, 211 insertions(+), 21 deletions(-) create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index bd034cca0ba2..8e4fa0e94e4f 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -33,7 +33,7 @@ UpholdAuthorization::~UpholdAuthorization() = default; void UpholdAuthorization::Authorize( const base::flat_map& args, - ledger::ExternalWalletAuthorizationCallback callback) { + ledger::ExternalWalletAuthorizationCallback callback) const { auto wallet = GetWallet(ledger_); if (!wallet) { @@ -113,7 +113,7 @@ void UpholdAuthorization::Authorize( void UpholdAuthorization::OnAuthorize( const type::Result result, const std::string& token, - ledger::ExternalWalletAuthorizationCallback callback) { + ledger::ExternalWalletAuthorizationCallback callback) const { if (result == type::Result::EXPIRED_TOKEN) { BLOG(0, "Expired token"); callback(type::Result::EXPIRED_TOKEN, {}); @@ -167,7 +167,7 @@ void UpholdAuthorization::OnAuthorize( void UpholdAuthorization::OnGetUser( const type::Result result, const User& user, - ledger::ExternalWalletAuthorizationCallback callback) { + ledger::ExternalWalletAuthorizationCallback callback) const { auto wallet_ptr = GetWallet(ledger_); base::flat_map args; @@ -208,7 +208,7 @@ void UpholdAuthorization::OnGetUser( void UpholdAuthorization::OnCardCreate( const type::Result result, const std::string& address, - ledger::ExternalWalletAuthorizationCallback callback) { + ledger::ExternalWalletAuthorizationCallback callback) const { if (result == type::Result::LEDGER_ERROR) { BLOG(0, "Card creation"); callback(type::Result::LEDGER_ERROR, {}); @@ -236,7 +236,7 @@ void UpholdAuthorization::OnCardCreate( } void UpholdAuthorization::GetAnonFunds( - endpoint::promotion::GetWalletBalanceCallback callback) { + endpoint::promotion::GetWalletBalanceCallback callback) const { if (ledger_->ledger_client()->GetBooleanOption( option::kContributionsDisabledForBAPMigration)) { BLOG(1, "Fetch balance disabled for BAP migration."); @@ -271,7 +271,7 @@ void UpholdAuthorization::GetAnonFunds( void UpholdAuthorization::OnGetAnonFunds( const type::Result result, type::BalancePtr balance, - ledger::ExternalWalletAuthorizationCallback callback) { + ledger::ExternalWalletAuthorizationCallback callback) const { if (result != type::Result::LEDGER_OK) { BLOG(0, "Couldn't get anonymous funds!"); callback(type::Result::LEDGER_ERROR, {}); @@ -287,7 +287,7 @@ void UpholdAuthorization::OnGetAnonFunds( void UpholdAuthorization::TransferAnonFunds( const double user_funds, - ledger::endpoint::promotion::PostClaimUpholdCallback callback) { + ledger::endpoint::promotion::PostClaimUpholdCallback callback) const { if (!uphold::GetWallet(ledger_)) { BLOG(0, "Wallet is null!"); callback(type::Result::LEDGER_ERROR); @@ -299,7 +299,7 @@ void UpholdAuthorization::TransferAnonFunds( void UpholdAuthorization::OnTransferAnonFunds( const type::Result result, - ledger::ExternalWalletAuthorizationCallback callback) { + ledger::ExternalWalletAuthorizationCallback callback) const { if (result == type::Result::LEDGER_OK) { callback(type::Result::LEDGER_OK, {}); return; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h index b60aea159a95..b604ae2195ab 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h @@ -28,39 +28,39 @@ class UpholdAuthorization { void Authorize( const base::flat_map& args, - ledger::ExternalWalletAuthorizationCallback callback); + ledger::ExternalWalletAuthorizationCallback callback) const; + + void GetAnonFunds( + endpoint::promotion::GetWalletBalanceCallback callback) const; + + void TransferAnonFunds( + const double user_funds, + endpoint::promotion::PostClaimUpholdCallback callback) const; private: void OnAuthorize( const type::Result result, const std::string& token, - ledger::ExternalWalletAuthorizationCallback callback); + ledger::ExternalWalletAuthorizationCallback callback) const; void OnGetUser( const type::Result result, const User& user, - ledger::ExternalWalletAuthorizationCallback callback); + ledger::ExternalWalletAuthorizationCallback callback) const; void OnCardCreate( const type::Result result, const std::string& address, - ledger::ExternalWalletAuthorizationCallback callback); - - void GetAnonFunds( - endpoint::promotion::GetWalletBalanceCallback callback); + ledger::ExternalWalletAuthorizationCallback callback) const; void OnGetAnonFunds( const type::Result result, type::BalancePtr balance, - ledger::ExternalWalletAuthorizationCallback callback); - - void TransferAnonFunds( - const double user_funds, - endpoint::promotion::PostClaimUpholdCallback callback); + ledger::ExternalWalletAuthorizationCallback callback) const; void OnTransferAnonFunds( const type::Result result, - ledger::ExternalWalletAuthorizationCallback callback); + ledger::ExternalWalletAuthorizationCallback callback) const; LedgerImpl* ledger_; // NOT OWNED std::unique_ptr promotion_server_; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc new file mode 100644 index 000000000000..ee10274f487b --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -0,0 +1,189 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "base/json/json_reader.h" +#include "base/test/task_environment.h" +#include "bat/ledger/internal/ledger_client_mock.h" +#include "bat/ledger/internal/ledger_impl_mock.h" +#include "bat/ledger/internal/state/state_keys.h" +#include "bat/ledger/internal/uphold/uphold_authorization.h" +#include "bat/ledger/option_keys.h" +#include "net/http/http_status_code.h" + +// npm run test -- brave_unit_tests --filter=UpholdAuthorizationTest.* +// npm run test -- brave_unit_tests Debug --filter=UpholdAuthorizationTest.* + +using ::testing::_; +using ::testing::Invoke; + +namespace ledger { +namespace uphold { + +using param_type = + std::tuple< + bool, // GetBooleanOption(option::kContributionsDisabledForBAPMigration) + bool, // GetBooleanState(ledger::state::kFetchOldBalance) + std::string, // GetEncryptedStringState(state::kWalletBrave) + type::UrlResponse, // LoadURL(_, _) - response from the balance server + type::Result // expected + > +; + +class Get : public testing::TestWithParam { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr authorization_; + + public: + Get() + : mock_ledger_client_{ std::make_unique() } + , mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get()) } + , authorization_{ std::make_unique(mock_ledger_impl_.get()) } + {} +}; + +INSTANTIATE_TEST_SUITE_P( + UpholdAuthorizationTest, + Get, + ::testing::Values( + param_type{ + true, + {}, + {}, + {}, + type::Result::LEDGER_OK + }, + param_type{ + false, + false, + {}, + {}, + type::Result::LEDGER_OK + }, + //param_type{ + // false, + // true, + // {}, + // {}, + // {} + //}, + param_type{ + false, + true, + R"({ "payment_id": "", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", + {}, + type::Result::LEDGER_ERROR + }, + param_type{ + false, + true, + R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", + type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_SERVICE_UNAVAILABLE, {}, {} }, + type::Result::LEDGER_ERROR + }, + param_type{ + false, + true, + R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", + type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, {}, {} }, + type::Result::LEDGER_ERROR + }, + param_type{ + false, + true, + R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", + type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, R"({ "total": 5.0, "spendable": 0.0, "confirmed": 5.0, "unconfirmed": 0.0 })", {} }, + type::Result::LEDGER_OK + } + ), + [](const ::testing::TestParamInfo& info) -> std::string { + const auto& params = info.param; + const bool kContributionsDisabledForBAPMigration = std::get<0>(params); + const bool kFetchOldBalance = std::get<1>(params); + const std::string& braveWallet = std::get<2>(params); + const type::UrlResponse& response = std::get<3>(params); + + if (kContributionsDisabledForBAPMigration) { + return "fetch_balance_disabled_for_BAP_migration"; + } else if (!kFetchOldBalance) { + return "get_fetch_old_balance_disabled"; + } else if (braveWallet.empty()) { + return "wallet_is_not_created"; + } else { + base::Optional value = base::JSONReader::Read(braveWallet); + EXPECT_TRUE(value && value->is_dict()); + + base::DictionaryValue* dictionary = nullptr; + EXPECT_TRUE(value->GetAsDictionary(&dictionary)); + + auto* payment_id = dictionary->FindStringKey("payment_id"); + EXPECT_TRUE(payment_id); + + if (payment_id->empty()) { + return "payment_id_is_empty"; + } else if (response.status_code == net::HttpStatusCode::HTTP_SERVICE_UNAVAILABLE) { + return "balance_server_error"; + } else if (response.body.empty()) { + return "invalid_body_in_response"; + } else { + return "happy_path"; + } + } + } +); + +TEST_P(Get, AnonFunds) { + const auto& param = GetParam(); + const bool kContributionsDisabledForBAPMigration = std::get<0>(param); + const bool kFetchOldBalance = std::get<1>(param); + const std::string& braveWallet = std::get<2>(param); + const type::UrlResponse& response = std::get<3>(param); + const type::Result expected = std::get<4>(param); + + ON_CALL( + *mock_ledger_client_, + GetBooleanOption(option::kContributionsDisabledForBAPMigration) + ).WillByDefault( + testing::Return(kContributionsDisabledForBAPMigration) + ); + + ON_CALL( + *mock_ledger_client_, + GetBooleanState(state::kFetchOldBalance) + ).WillByDefault( + testing::Return(kFetchOldBalance) + ); + + ON_CALL( + *mock_ledger_client_, + GetEncryptedStringState(state::kWalletBrave) + ).WillByDefault( + testing::Return(braveWallet) + ); + + ON_CALL( + *mock_ledger_client_, + LoadURL(_, _) + ).WillByDefault( + Invoke( + [&](type::UrlRequestPtr, client::LoadURLCallback callback) { + callback(response); + } + ) + ); + + authorization_->GetAnonFunds( + [&](const type::Result result, type::BalancePtr balance) { + EXPECT_TRUE(result == expected); + } + ); +} + +} // namespace uphold +} // namespace ledger diff --git a/vendor/bat-native-ledger/test/BUILD.gn b/vendor/bat-native-ledger/test/BUILD.gn index c605dc6ac4b0..733583ea5c2f 100644 --- a/vendor/bat-native-ledger/test/BUILD.gn +++ b/vendor/bat-native-ledger/test/BUILD.gn @@ -98,6 +98,7 @@ source_set("bat_native_ledger_tests") { "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/prefix_list_reader_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/publisher_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util_unittest.cc", ] From c519d85dcf039b1bf9168df570351165cd271c75 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Fri, 4 Jun 2021 13:01:33 +0200 Subject: [PATCH 05/15] Refactoring UpholdAuthorization::GetAnonFunds() tests. Adding ledger::database::Database mocking to support the "brave_wallet_is_not_created" scenario. --- .../uphold/uphold_authorization_unittest.cc | 189 ++++++++++-------- 1 file changed, 109 insertions(+), 80 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index ee10274f487b..863d4d646144 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -3,8 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "base/json/json_reader.h" #include "base/test/task_environment.h" +#include "bat/ledger/internal/database/database_mock.h" #include "bat/ledger/internal/ledger_client_mock.h" #include "bat/ledger/internal/ledger_impl_mock.h" #include "bat/ledger/internal/state/state_keys.h" @@ -12,159 +12,178 @@ #include "bat/ledger/option_keys.h" #include "net/http/http_status_code.h" -// npm run test -- brave_unit_tests --filter=UpholdAuthorizationTest.* -// npm run test -- brave_unit_tests Debug --filter=UpholdAuthorizationTest.* +// npm run test -- brave_unit_tests --filter=UpholdAuthorizationTest* using ::testing::_; using ::testing::Invoke; +using ::testing::TestWithParam; +using ::testing::TestParamInfo; +using ::testing::Values; namespace ledger { namespace uphold { +namespace anon_funds { -using param_type = +using GetResult = std::pair>; +using GetParamType = std::tuple< - bool, // GetBooleanOption(option::kContributionsDisabledForBAPMigration) - bool, // GetBooleanState(ledger::state::kFetchOldBalance) - std::string, // GetEncryptedStringState(state::kWalletBrave) - type::UrlResponse, // LoadURL(_, _) - response from the balance server - type::Result // expected + bool, // contributions disabled for BAP migration + bool, // fetch old balance enabled + std::string, // Brave wallet + type::UrlResponse, // balance server response + GetResult // expected result > ; -class Get : public testing::TestWithParam { +class Get : public TestWithParam { private: base::test::TaskEnvironment scoped_task_environment_; protected: - std::unique_ptr mock_ledger_client_; - std::unique_ptr mock_ledger_impl_; + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr mock_database_; std::unique_ptr authorization_; public: Get() - : mock_ledger_client_{ std::make_unique() } - , mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get()) } + : mock_ledger_client_{ std::make_unique() } + , mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get()) } + , mock_database_{ std::make_unique(mock_ledger_impl_.get()) } , authorization_{ std::make_unique(mock_ledger_impl_.get()) } {} }; +std::string GetNameSuffixGenerator(const TestParamInfo& info) { + const bool contributions_disabled_for_BAP_migration = std::get<0>(info.param); + const bool fetch_old_balance_enabled = std::get<1>(info.param); + const std::string& brave_wallet = std::get<2>(info.param); + const type::UrlResponse& balance_server_response = std::get<3>(info.param); + + if (contributions_disabled_for_BAP_migration) { + return "contributions_disabled_for_BAP_migration"; + } + + if (!fetch_old_balance_enabled) { + return "fetch_old_balance_disabled"; + } + + if (brave_wallet.empty()) { + return "brave_wallet_is_not_created"; + } + + if (brave_wallet.find(R"("payment_id": "")") != std::string::npos) { + return "brave_wallet_payment_id_is_empty"; + } + + if (balance_server_response.status_code == net::HttpStatusCode::HTTP_SERVICE_UNAVAILABLE) { + return "balance_server_error"; + } + + if (balance_server_response.body.empty()) { + return "invalid_body_in_balance_server_response"; + } + + return "happy_path"; +} + INSTANTIATE_TEST_SUITE_P( UpholdAuthorizationTest, Get, - ::testing::Values( - param_type{ + Values( + // "contributions_disabled_for_BAP_migration" + GetParamType{ true, {}, {}, {}, - type::Result::LEDGER_OK + { type::Result::LEDGER_OK, 0.0 } }, - param_type{ + // "fetch_old_balance_disabled" + GetParamType{ false, false, {}, {}, - type::Result::LEDGER_OK + { type::Result::LEDGER_OK, 0.0 } }, - //param_type{ - // false, - // true, - // {}, - // {}, - // {} - //}, - param_type{ + // "brave_wallet_is_not_created" + GetParamType{ + false, + true, + {}, + {}, + { type::Result::LEDGER_OK, 0.0 } + }, + // "brave_wallet_payment_id_is_empty" + GetParamType{ false, true, R"({ "payment_id": "", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", {}, - type::Result::LEDGER_ERROR + { type::Result::LEDGER_ERROR, 0.0 } }, - param_type{ + // "balance_server_error" + GetParamType{ false, true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_SERVICE_UNAVAILABLE, {}, {} }, - type::Result::LEDGER_ERROR + { type::Result::LEDGER_ERROR, {} } }, - param_type{ + // "invalid_body_in_balance_server_response" + GetParamType{ false, true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, {}, {} }, - type::Result::LEDGER_ERROR + { type::Result::LEDGER_ERROR, 0.0 } }, - param_type{ + // "happy_path" + GetParamType{ false, true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, R"({ "total": 5.0, "spendable": 0.0, "confirmed": 5.0, "unconfirmed": 0.0 })", {} }, - type::Result::LEDGER_OK + { type::Result::LEDGER_OK, 5.0 } } ), - [](const ::testing::TestParamInfo& info) -> std::string { - const auto& params = info.param; - const bool kContributionsDisabledForBAPMigration = std::get<0>(params); - const bool kFetchOldBalance = std::get<1>(params); - const std::string& braveWallet = std::get<2>(params); - const type::UrlResponse& response = std::get<3>(params); - - if (kContributionsDisabledForBAPMigration) { - return "fetch_balance_disabled_for_BAP_migration"; - } else if (!kFetchOldBalance) { - return "get_fetch_old_balance_disabled"; - } else if (braveWallet.empty()) { - return "wallet_is_not_created"; - } else { - base::Optional value = base::JSONReader::Read(braveWallet); - EXPECT_TRUE(value && value->is_dict()); - - base::DictionaryValue* dictionary = nullptr; - EXPECT_TRUE(value->GetAsDictionary(&dictionary)); - - auto* payment_id = dictionary->FindStringKey("payment_id"); - EXPECT_TRUE(payment_id); - - if (payment_id->empty()) { - return "payment_id_is_empty"; - } else if (response.status_code == net::HttpStatusCode::HTTP_SERVICE_UNAVAILABLE) { - return "balance_server_error"; - } else if (response.body.empty()) { - return "invalid_body_in_response"; - } else { - return "happy_path"; - } - } - } + GetNameSuffixGenerator ); TEST_P(Get, AnonFunds) { - const auto& param = GetParam(); - const bool kContributionsDisabledForBAPMigration = std::get<0>(param); - const bool kFetchOldBalance = std::get<1>(param); - const std::string& braveWallet = std::get<2>(param); - const type::UrlResponse& response = std::get<3>(param); - const type::Result expected = std::get<4>(param); - + const auto& params = GetParam(); + const bool contributions_disabled_for_BAP_migration = std::get<0>(params); + const bool fetch_old_balance_enabled = std::get<1>(params); + const std::string& brave_wallet = std::get<2>(params); + const type::UrlResponse& balance_server_response = std::get<3>(params); + const auto& expected = std::get<4>(params); + ON_CALL( *mock_ledger_client_, GetBooleanOption(option::kContributionsDisabledForBAPMigration) ).WillByDefault( - testing::Return(kContributionsDisabledForBAPMigration) + testing::Return(contributions_disabled_for_BAP_migration) ); ON_CALL( *mock_ledger_client_, GetBooleanState(state::kFetchOldBalance) ).WillByDefault( - testing::Return(kFetchOldBalance) + testing::Return(fetch_old_balance_enabled) ); ON_CALL( *mock_ledger_client_, GetEncryptedStringState(state::kWalletBrave) ).WillByDefault( - testing::Return(braveWallet) + testing::Return(brave_wallet) + ); + + ON_CALL( + *mock_ledger_impl_, database() + ).WillByDefault( + testing::Return(mock_database_.get()) ); ON_CALL( @@ -173,17 +192,27 @@ TEST_P(Get, AnonFunds) { ).WillByDefault( Invoke( [&](type::UrlRequestPtr, client::LoadURLCallback callback) { - callback(response); + callback(balance_server_response); } ) ); authorization_->GetAnonFunds( [&](const type::Result result, type::BalancePtr balance) { - EXPECT_TRUE(result == expected); + const auto expected_result = std::get<0>(expected); + EXPECT_TRUE(result == expected_result); + + const auto& expected_balance = std::get<1>(expected); + if (!balance) { + EXPECT_FALSE(expected_balance); + } else { + EXPECT_TRUE(expected_balance); + EXPECT_DOUBLE_EQ(balance->user_funds, *expected_balance); + } } ); } +} // anon_funds } // namespace uphold } // namespace ledger From e1b1d3efdaa1d64b527712585bd3022a7c7a58ef Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Fri, 4 Jun 2021 14:10:47 +0200 Subject: [PATCH 06/15] Adding value-parameterized tests for UpholdAuthorization::TransferAnonFunds(). --- .../uphold/uphold_authorization_unittest.cc | 171 ++++++++++++++++-- 1 file changed, 157 insertions(+), 14 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index 863d4d646144..c78c0343de9d 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -22,20 +22,22 @@ using ::testing::Values; namespace ledger { namespace uphold { +namespace tests { namespace anon_funds { +namespace get { -using GetResult = std::pair>; -using GetParamType = +using Result = std::pair>; +using ParamType = std::tuple< bool, // contributions disabled for BAP migration bool, // fetch old balance enabled std::string, // Brave wallet type::UrlResponse, // balance server response - GetResult // expected result + Result // expected result > ; -class Get : public TestWithParam { +class Get : public TestWithParam { private: base::test::TaskEnvironment scoped_task_environment_; @@ -54,7 +56,7 @@ class Get : public TestWithParam { {} }; -std::string GetNameSuffixGenerator(const TestParamInfo& info) { +std::string NameSuffixGenerator(const TestParamInfo& info) { const bool contributions_disabled_for_BAP_migration = std::get<0>(info.param); const bool fetch_old_balance_enabled = std::get<1>(info.param); const std::string& brave_wallet = std::get<2>(info.param); @@ -92,7 +94,7 @@ INSTANTIATE_TEST_SUITE_P( Get, Values( // "contributions_disabled_for_BAP_migration" - GetParamType{ + ParamType{ true, {}, {}, @@ -100,7 +102,7 @@ INSTANTIATE_TEST_SUITE_P( { type::Result::LEDGER_OK, 0.0 } }, // "fetch_old_balance_disabled" - GetParamType{ + ParamType{ false, false, {}, @@ -108,7 +110,7 @@ INSTANTIATE_TEST_SUITE_P( { type::Result::LEDGER_OK, 0.0 } }, // "brave_wallet_is_not_created" - GetParamType{ + ParamType{ false, true, {}, @@ -116,7 +118,7 @@ INSTANTIATE_TEST_SUITE_P( { type::Result::LEDGER_OK, 0.0 } }, // "brave_wallet_payment_id_is_empty" - GetParamType{ + ParamType{ false, true, R"({ "payment_id": "", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", @@ -124,7 +126,7 @@ INSTANTIATE_TEST_SUITE_P( { type::Result::LEDGER_ERROR, 0.0 } }, // "balance_server_error" - GetParamType{ + ParamType{ false, true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", @@ -132,7 +134,7 @@ INSTANTIATE_TEST_SUITE_P( { type::Result::LEDGER_ERROR, {} } }, // "invalid_body_in_balance_server_response" - GetParamType{ + ParamType{ false, true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", @@ -140,7 +142,7 @@ INSTANTIATE_TEST_SUITE_P( { type::Result::LEDGER_ERROR, 0.0 } }, // "happy_path" - GetParamType{ + ParamType{ false, true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", @@ -148,7 +150,7 @@ INSTANTIATE_TEST_SUITE_P( { type::Result::LEDGER_OK, 5.0 } } ), - GetNameSuffixGenerator + NameSuffixGenerator ); TEST_P(Get, AnonFunds) { @@ -213,6 +215,147 @@ TEST_P(Get, AnonFunds) { ); } -} // anon_funds +} // namespace get + +namespace transfer { + +using Result = type::Result; +using ParamType = + std::tuple< + std::string, // Uphold wallet + type::UrlResponse, // rewards web services response + Result // expected result + > +; + +class Transfer : public TestWithParam { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr authorization_; + + public: + Transfer() + : mock_ledger_client_{ std::make_unique() } + , mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get()) } + , authorization_{ std::make_unique(mock_ledger_impl_.get()) } + {} +}; + +std::string NameSuffixGenerator(const TestParamInfo& info) { + const std::string& uphold_wallet = std::get<0>(info.param); + const type::UrlResponse& rewards_web_services_response = std::get<1>(info.param); + + if (uphold_wallet.empty()) { + return "uphold_wallet_is_null"; + } + + if (rewards_web_services_response.status_code == net::HttpStatusCode::HTTP_NOT_FOUND) { + return "404"; + } + + if (rewards_web_services_response.status_code == net::HttpStatusCode::HTTP_CONFLICT) { + return "wallet_device_limit_reached"; + } + + if (rewards_web_services_response.status_code != net::HttpStatusCode::HTTP_OK) { + return "rewards_web_services_error"; + } + + return "happy_path"; +} + +const std::string uphold_wallet{ R"( +{ + "account_url": "https://wallet-sandbox.uphold.com/dashboard", + "add_url": "", + "address": "962df5b1-bb72-4619-a349-c8087941b795", + "fees": {}, + "login_url": "https://wallet-sandbox.uphold.com/authorize/9a4b665ca983d912fec5c7395b34859a94418cc8?scope=accounts:read accounts:write cards:read cards:write user:read transactions:deposit transactions:read transactions:transfer:application transactions:transfer:others&intention=login&state=025C120562C517028DECD63780FD4B8D48791C4ED8E3747FB2F27445EBFFCFCA", + "one_time_string": "004603F48D8636C3E96EE049C326976EE432809841EA1E11E305EC5F7C96A1B7", + "status": 2, + "token": "0047c2fd8f023e067354dbdb5639ee67acf77150", + "user_name": "", + "verify_url": "https://wallet-sandbox.uphold.com/authorize/9a4b665ca983d912fec5c7395b34859a94418cc8?scope=accounts:read accounts:write cards:read cards:write user:read transactions:deposit transactions:read transactions:transfer:application transactions:transfer:others&intention=kyc&state=025C120562C517028DECD63780FD4B8D48791C4ED8E3747FB2F27445EBFFCFCA", + "withdraw_url": "" +} +)" }; + +INSTANTIATE_TEST_SUITE_P( + UpholdAuthorizationTest, + Transfer, + Values( + // "uphold_wallet_is_null" + ParamType{ + "", + type::UrlResponse{}, + type::Result::LEDGER_ERROR + }, + // "404" + ParamType{ + uphold_wallet, + type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_NOT_FOUND, {}, {} }, + type::Result::NOT_FOUND + }, + // "wallet_device_limit_reached" + ParamType{ + uphold_wallet, + type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_CONFLICT, {}, {} }, + type::Result::ALREADY_EXISTS + }, + // "rewards_web_services_error" + ParamType{ + uphold_wallet, + type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_INTERNAL_SERVER_ERROR, {}, {} }, + type::Result::LEDGER_ERROR + }, + // "happy_path" + ParamType{ + uphold_wallet, + type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, {}, {} }, + type::Result::LEDGER_OK + } + ), + NameSuffixGenerator +); + +TEST_P(Transfer, AnonFunds) { + const auto& params = GetParam(); + const std::string& uphold_wallet = std::get<0>(params); + const type::UrlResponse& rewards_web_services_response = std::get<1>(params); + const auto& expected_result = std::get<2>(params); + + ON_CALL( + *mock_ledger_client_, + GetEncryptedStringState(state::kWalletUphold) + ).WillByDefault( + testing::Return(uphold_wallet) + ); + + ON_CALL( + *mock_ledger_client_, + LoadURL(_, _) + ).WillByDefault( + Invoke( + [&](type::UrlRequestPtr, client::LoadURLCallback callback) { + callback(rewards_web_services_response); + } + ) + ); + + authorization_->TransferAnonFunds( + 0.0, + [&](const type::Result result) { + EXPECT_TRUE(result == expected_result); + } + ); +} + +} // namespace transfer +} // namespace anon_funds +} // namespace tests } // namespace uphold } // namespace ledger From 27092f2f2582a9bd510eb3f2f3b89db3ace1f956 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Fri, 4 Jun 2021 16:04:34 +0200 Subject: [PATCH 07/15] Fine-tuning the "balance_server_error" scenario for UpholdAuthorization::GetAnonFunds(). --- .../bat/ledger/internal/uphold/uphold_authorization_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index c78c0343de9d..63df8e901496 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -78,7 +78,7 @@ std::string NameSuffixGenerator(const TestParamInfo& info) { return "brave_wallet_payment_id_is_empty"; } - if (balance_server_response.status_code == net::HttpStatusCode::HTTP_SERVICE_UNAVAILABLE) { + if (balance_server_response.status_code != net::HttpStatusCode::HTTP_OK) { return "balance_server_error"; } From 9dc67c2320b26cb10b2722d0c5bd55fc0bf94820 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Fri, 4 Jun 2021 18:26:20 +0200 Subject: [PATCH 08/15] Passing nullptr (instead of type::Balance::New())) to callback (UpholdAuthorization::OnGetAnonFunds()), if the Brave wallet's payment_id is empty. --- .../src/bat/ledger/internal/uphold/uphold_authorization.cc | 2 +- .../bat/ledger/internal/uphold/uphold_authorization_unittest.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 8e4fa0e94e4f..6201caad5d53 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -261,7 +261,7 @@ void UpholdAuthorization::GetAnonFunds( if (wallet->payment_id.empty()) { BLOG(0, "Payment ID is empty!"); - callback(type::Result::LEDGER_ERROR, type::Balance::New()); + callback(type::Result::LEDGER_ERROR, nullptr); return; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index 63df8e901496..2637d1ca5d30 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -123,7 +123,7 @@ INSTANTIATE_TEST_SUITE_P( true, R"({ "payment_id": "", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", {}, - { type::Result::LEDGER_ERROR, 0.0 } + { type::Result::LEDGER_ERROR, {} } }, // "balance_server_error" ParamType{ From 1bdbf273d0a0ea9f767942af88043036373037dd Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Fri, 4 Jun 2021 19:08:00 +0200 Subject: [PATCH 09/15] Removing BAP-related code (and adjusting test parameters). --- .../internal/uphold/uphold_authorization.cc | 9 +--- .../uphold/uphold_authorization_unittest.cc | 42 ++++--------------- 2 files changed, 8 insertions(+), 43 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 6201caad5d53..74c16a0bc9a2 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -237,13 +237,6 @@ void UpholdAuthorization::OnCardCreate( void UpholdAuthorization::GetAnonFunds( endpoint::promotion::GetWalletBalanceCallback callback) const { - if (ledger_->ledger_client()->GetBooleanOption( - option::kContributionsDisabledForBAPMigration)) { - BLOG(1, "Fetch balance disabled for BAP migration."); - callback(type::Result::LEDGER_OK, type::Balance::New()); - return; - } - // if we don't have user funds in anon card anymore // we can skip balance server ping if (!ledger_->state()->GetFetchOldBalanceEnabled()) { @@ -272,7 +265,7 @@ void UpholdAuthorization::OnGetAnonFunds( const type::Result result, type::BalancePtr balance, ledger::ExternalWalletAuthorizationCallback callback) const { - if (result != type::Result::LEDGER_OK) { + if (result != type::Result::LEDGER_OK || !balance) { BLOG(0, "Couldn't get anonymous funds!"); callback(type::Result::LEDGER_ERROR, {}); return; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index 2637d1ca5d30..5f1a1751ffa2 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -29,7 +29,6 @@ namespace get { using Result = std::pair>; using ParamType = std::tuple< - bool, // contributions disabled for BAP migration bool, // fetch old balance enabled std::string, // Brave wallet type::UrlResponse, // balance server response @@ -57,14 +56,9 @@ class Get : public TestWithParam { }; std::string NameSuffixGenerator(const TestParamInfo& info) { - const bool contributions_disabled_for_BAP_migration = std::get<0>(info.param); - const bool fetch_old_balance_enabled = std::get<1>(info.param); - const std::string& brave_wallet = std::get<2>(info.param); - const type::UrlResponse& balance_server_response = std::get<3>(info.param); - - if (contributions_disabled_for_BAP_migration) { - return "contributions_disabled_for_BAP_migration"; - } + const bool fetch_old_balance_enabled = std::get<0>(info.param); + const std::string& brave_wallet = std::get<1>(info.param); + const type::UrlResponse& balance_server_response = std::get<2>(info.param); if (!fetch_old_balance_enabled) { return "fetch_old_balance_disabled"; @@ -93,17 +87,8 @@ INSTANTIATE_TEST_SUITE_P( UpholdAuthorizationTest, Get, Values( - // "contributions_disabled_for_BAP_migration" - ParamType{ - true, - {}, - {}, - {}, - { type::Result::LEDGER_OK, 0.0 } - }, // "fetch_old_balance_disabled" ParamType{ - false, false, {}, {}, @@ -111,7 +96,6 @@ INSTANTIATE_TEST_SUITE_P( }, // "brave_wallet_is_not_created" ParamType{ - false, true, {}, {}, @@ -119,7 +103,6 @@ INSTANTIATE_TEST_SUITE_P( }, // "brave_wallet_payment_id_is_empty" ParamType{ - false, true, R"({ "payment_id": "", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", {}, @@ -127,7 +110,6 @@ INSTANTIATE_TEST_SUITE_P( }, // "balance_server_error" ParamType{ - false, true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_SERVICE_UNAVAILABLE, {}, {} }, @@ -135,7 +117,6 @@ INSTANTIATE_TEST_SUITE_P( }, // "invalid_body_in_balance_server_response" ParamType{ - false, true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, {}, {} }, @@ -143,7 +124,6 @@ INSTANTIATE_TEST_SUITE_P( }, // "happy_path" ParamType{ - false, true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, R"({ "total": 5.0, "spendable": 0.0, "confirmed": 5.0, "unconfirmed": 0.0 })", {} }, @@ -155,18 +135,10 @@ INSTANTIATE_TEST_SUITE_P( TEST_P(Get, AnonFunds) { const auto& params = GetParam(); - const bool contributions_disabled_for_BAP_migration = std::get<0>(params); - const bool fetch_old_balance_enabled = std::get<1>(params); - const std::string& brave_wallet = std::get<2>(params); - const type::UrlResponse& balance_server_response = std::get<3>(params); - const auto& expected = std::get<4>(params); - - ON_CALL( - *mock_ledger_client_, - GetBooleanOption(option::kContributionsDisabledForBAPMigration) - ).WillByDefault( - testing::Return(contributions_disabled_for_BAP_migration) - ); + const bool fetch_old_balance_enabled = std::get<0>(params); + const std::string& brave_wallet = std::get<1>(params); + const type::UrlResponse& balance_server_response = std::get<2>(params); + const auto& expected = std::get<3>(params); ON_CALL( *mock_ledger_client_, From 38ca1399de2591ee5fd2d8e7ae7a47d692ef9785 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Mon, 7 Jun 2021 15:34:38 +0200 Subject: [PATCH 10/15] Fixing lint findings. --- .../internal/uphold/uphold_authorization.cc | 12 +- .../internal/uphold/uphold_authorization.h | 6 +- .../uphold/uphold_authorization_unittest.cc | 344 ++++++++---------- .../src/bat/ledger/internal/wallet/wallet.cc | 5 +- .../src/bat/ledger/internal/wallet/wallet.h | 6 +- 5 files changed, 173 insertions(+), 200 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 74c16a0bc9a2..567f54f8e4ad 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -232,7 +232,8 @@ void UpholdAuthorization::OnCardCreate( callback(type::Result::LEDGER_OK, args); } - GetAnonFunds(std::bind(&UpholdAuthorization::OnGetAnonFunds, this, _1, _2, callback)); + GetAnonFunds( + std::bind(&UpholdAuthorization::OnGetAnonFunds, this, _1, _2, callback)); } void UpholdAuthorization::GetAnonFunds( @@ -271,11 +272,13 @@ void UpholdAuthorization::OnGetAnonFunds( return; } - if (balance->user_funds == 0.0) { // TODO: floating-point comparison + if (balance->user_funds == 0.0) { // == floating-point comparison! ledger_->state()->SetFetchOldBalanceEnabled(false); } - TransferAnonFunds(balance->user_funds, std::bind(&UpholdAuthorization::OnTransferAnonFunds, this, _1, callback)); + TransferAnonFunds( + balance->user_funds, + std::bind(&UpholdAuthorization::OnTransferAnonFunds, this, _1, callback)); } void UpholdAuthorization::TransferAnonFunds( @@ -299,7 +302,8 @@ void UpholdAuthorization::OnTransferAnonFunds( } if (result == type::Result::ALREADY_EXISTS) { - ledger_->ledger_client()->ShowNotification("wallet_device_limit_reached", {}, [](type::Result) {}); + ledger_->ledger_client()->ShowNotification("wallet_device_limit_reached", + {}, [](type::Result) {}); std::string event_text = "uphold"; if (auto wallet_ptr = uphold::GetWallet(ledger_)) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h index b604ae2195ab..ccc287e48c7e 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h @@ -3,8 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BRAVELEDGER_UPHOLD_UPHOLD_AUTHORIZATION_H_ -#define BRAVELEDGER_UPHOLD_UPHOLD_AUTHORIZATION_H_ +#ifndef BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_UPHOLD_UPHOLD_AUTHORIZATION_H_ +#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_UPHOLD_UPHOLD_AUTHORIZATION_H_ #include #include @@ -69,4 +69,4 @@ class UpholdAuthorization { } // namespace uphold } // namespace ledger -#endif // BRAVELEDGER_UPHOLD_UPHOLD_AUTHORIZATION_H_ +#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_UPHOLD_UPHOLD_AUTHORIZATION_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index 5f1a1751ffa2..db71147ce676 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -3,6 +3,9 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include +#include + #include "base/test/task_environment.h" #include "bat/ledger/internal/database/database_mock.h" #include "bat/ledger/internal/ledger_client_mock.h" @@ -16,8 +19,8 @@ using ::testing::_; using ::testing::Invoke; -using ::testing::TestWithParam; using ::testing::TestParamInfo; +using ::testing::TestWithParam; using ::testing::Values; namespace ledger { @@ -27,14 +30,11 @@ namespace anon_funds { namespace get { using Result = std::pair>; -using ParamType = - std::tuple< - bool, // fetch old balance enabled - std::string, // Brave wallet - type::UrlResponse, // balance server response - Result // expected result - > -; +using ParamType = std::tuple; class Get : public TestWithParam { private: @@ -48,11 +48,13 @@ class Get : public TestWithParam { public: Get() - : mock_ledger_client_{ std::make_unique() } - , mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get()) } - , mock_database_{ std::make_unique(mock_ledger_impl_.get()) } - , authorization_{ std::make_unique(mock_ledger_impl_.get()) } - {} + : mock_ledger_client_{std::make_unique()}, + mock_ledger_impl_{ + std::make_unique(mock_ledger_client_.get())}, + mock_database_{ + std::make_unique(mock_ledger_impl_.get())}, + authorization_{ + std::make_unique(mock_ledger_impl_.get())} {} }; std::string NameSuffixGenerator(const TestParamInfo& info) { @@ -71,11 +73,11 @@ std::string NameSuffixGenerator(const TestParamInfo& info) { if (brave_wallet.find(R"("payment_id": "")") != std::string::npos) { return "brave_wallet_payment_id_is_empty"; } - + if (balance_server_response.status_code != net::HttpStatusCode::HTTP_OK) { return "balance_server_error"; } - + if (balance_server_response.body.empty()) { return "invalid_body_in_balance_server_response"; } @@ -84,54 +86,47 @@ std::string NameSuffixGenerator(const TestParamInfo& info) { } INSTANTIATE_TEST_SUITE_P( - UpholdAuthorizationTest, - Get, - Values( - // "fetch_old_balance_disabled" - ParamType{ - false, - {}, - {}, - { type::Result::LEDGER_OK, 0.0 } - }, - // "brave_wallet_is_not_created" - ParamType{ - true, - {}, - {}, - { type::Result::LEDGER_OK, 0.0 } - }, - // "brave_wallet_payment_id_is_empty" - ParamType{ - true, - R"({ "payment_id": "", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", - {}, - { type::Result::LEDGER_ERROR, {} } - }, - // "balance_server_error" - ParamType{ - true, - R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", - type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_SERVICE_UNAVAILABLE, {}, {} }, - { type::Result::LEDGER_ERROR, {} } - }, - // "invalid_body_in_balance_server_response" - ParamType{ - true, - R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", - type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, {}, {} }, - { type::Result::LEDGER_ERROR, 0.0 } - }, - // "happy_path" - ParamType{ - true, - R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", - type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, R"({ "total": 5.0, "spendable": 0.0, "confirmed": 5.0, "unconfirmed": 0.0 })", {} }, - { type::Result::LEDGER_OK, 5.0 } - } - ), - NameSuffixGenerator -); + UpholdAuthorizationTest, + Get, + Values( + // "fetch_old_balance_disabled" + ParamType{false, {}, {}, {type::Result::LEDGER_OK, 0.0}}, + // "brave_wallet_is_not_created" + ParamType{true, {}, {}, {type::Result::LEDGER_OK, 0.0}}, + // "brave_wallet_payment_id_is_empty" + ParamType{ + true, + R"({ "payment_id": "", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", + {}, + {type::Result::LEDGER_ERROR, {}}}, + // "balance_server_error" + ParamType{ + true, + R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", + type::UrlResponse{{}, + {}, + net::HttpStatusCode::HTTP_SERVICE_UNAVAILABLE, + {}, + {}}, + {type::Result::LEDGER_ERROR, {}}}, + // "invalid_body_in_balance_server_response" + ParamType{ + true, + R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", + type::UrlResponse{{}, {}, net::HttpStatusCode::HTTP_OK, {}, {}}, + {type::Result::LEDGER_ERROR, 0.0}}, + // "happy_path" + ParamType{ + true, + R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", + type::UrlResponse{ + {}, + {}, + net::HttpStatusCode::HTTP_OK, + R"({ "total": 5.0, "spendable": 0.0, "confirmed": 5.0, "unconfirmed": 0.0 })", + {}}, + {type::Result::LEDGER_OK, 5.0}}), + NameSuffixGenerator); TEST_P(Get, AnonFunds) { const auto& params = GetParam(); @@ -140,51 +135,34 @@ TEST_P(Get, AnonFunds) { const type::UrlResponse& balance_server_response = std::get<2>(params); const auto& expected = std::get<3>(params); - ON_CALL( - *mock_ledger_client_, - GetBooleanState(state::kFetchOldBalance) - ).WillByDefault( - testing::Return(fetch_old_balance_enabled) - ); - - ON_CALL( - *mock_ledger_client_, - GetEncryptedStringState(state::kWalletBrave) - ).WillByDefault( - testing::Return(brave_wallet) - ); - - ON_CALL( - *mock_ledger_impl_, database() - ).WillByDefault( - testing::Return(mock_database_.get()) - ); - - ON_CALL( - *mock_ledger_client_, - LoadURL(_, _) - ).WillByDefault( - Invoke( - [&](type::UrlRequestPtr, client::LoadURLCallback callback) { - callback(balance_server_response); - } - ) - ); + ON_CALL(*mock_ledger_client_, GetBooleanState(state::kFetchOldBalance)) + .WillByDefault(testing::Return(fetch_old_balance_enabled)); + + ON_CALL(*mock_ledger_client_, GetEncryptedStringState(state::kWalletBrave)) + .WillByDefault(testing::Return(brave_wallet)); + + ON_CALL(*mock_ledger_impl_, database()) + .WillByDefault(testing::Return(mock_database_.get())); + + ON_CALL(*mock_ledger_client_, LoadURL(_, _)) + .WillByDefault( + Invoke([&](type::UrlRequestPtr, client::LoadURLCallback callback) { + callback(balance_server_response); + })); authorization_->GetAnonFunds( - [&](const type::Result result, type::BalancePtr balance) { - const auto expected_result = std::get<0>(expected); - EXPECT_TRUE(result == expected_result); - - const auto& expected_balance = std::get<1>(expected); - if (!balance) { - EXPECT_FALSE(expected_balance); - } else { - EXPECT_TRUE(expected_balance); - EXPECT_DOUBLE_EQ(balance->user_funds, *expected_balance); - } - } - ); + [&](const type::Result result, type::BalancePtr balance) { + const auto expected_result = std::get<0>(expected); + EXPECT_TRUE(result == expected_result); + + const auto& expected_balance = std::get<1>(expected); + if (!balance) { + EXPECT_FALSE(expected_balance); + } else { + EXPECT_TRUE(expected_balance); + EXPECT_DOUBLE_EQ(balance->user_funds, *expected_balance); + } + }); } } // namespace get @@ -193,12 +171,10 @@ namespace transfer { using Result = type::Result; using ParamType = - std::tuple< - std::string, // Uphold wallet - type::UrlResponse, // rewards web services response - Result // expected result - > -; + std::tuple; class Transfer : public TestWithParam { private: @@ -211,88 +187,94 @@ class Transfer : public TestWithParam { public: Transfer() - : mock_ledger_client_{ std::make_unique() } - , mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get()) } - , authorization_{ std::make_unique(mock_ledger_impl_.get()) } - {} + : mock_ledger_client_{std::make_unique()}, + mock_ledger_impl_{ + std::make_unique(mock_ledger_client_.get())}, + authorization_{ + std::make_unique(mock_ledger_impl_.get())} {} }; -std::string NameSuffixGenerator(const TestParamInfo& info) { +std::string NameSuffixGenerator( + const TestParamInfo& info) { const std::string& uphold_wallet = std::get<0>(info.param); - const type::UrlResponse& rewards_web_services_response = std::get<1>(info.param); + const type::UrlResponse& rewards_web_services_response = + std::get<1>(info.param); if (uphold_wallet.empty()) { return "uphold_wallet_is_null"; } - if (rewards_web_services_response.status_code == net::HttpStatusCode::HTTP_NOT_FOUND) { + if (rewards_web_services_response.status_code == + net::HttpStatusCode::HTTP_NOT_FOUND) { return "404"; } - if (rewards_web_services_response.status_code == net::HttpStatusCode::HTTP_CONFLICT) { + if (rewards_web_services_response.status_code == + net::HttpStatusCode::HTTP_CONFLICT) { return "wallet_device_limit_reached"; } - if (rewards_web_services_response.status_code != net::HttpStatusCode::HTTP_OK) { + if (rewards_web_services_response.status_code != + net::HttpStatusCode::HTTP_OK) { return "rewards_web_services_error"; } return "happy_path"; } -const std::string uphold_wallet{ R"( +const char uphold_wallet[]{R"( { "account_url": "https://wallet-sandbox.uphold.com/dashboard", "add_url": "", "address": "962df5b1-bb72-4619-a349-c8087941b795", "fees": {}, - "login_url": "https://wallet-sandbox.uphold.com/authorize/9a4b665ca983d912fec5c7395b34859a94418cc8?scope=accounts:read accounts:write cards:read cards:write user:read transactions:deposit transactions:read transactions:transfer:application transactions:transfer:others&intention=login&state=025C120562C517028DECD63780FD4B8D48791C4ED8E3747FB2F27445EBFFCFCA", - "one_time_string": "004603F48D8636C3E96EE049C326976EE432809841EA1E11E305EC5F7C96A1B7", + "login_url": "https://wallet-sandbox.uphold.com/authorize/...", + "one_time_string": "...", "status": 2, "token": "0047c2fd8f023e067354dbdb5639ee67acf77150", "user_name": "", - "verify_url": "https://wallet-sandbox.uphold.com/authorize/9a4b665ca983d912fec5c7395b34859a94418cc8?scope=accounts:read accounts:write cards:read cards:write user:read transactions:deposit transactions:read transactions:transfer:application transactions:transfer:others&intention=kyc&state=025C120562C517028DECD63780FD4B8D48791C4ED8E3747FB2F27445EBFFCFCA", + "verify_url": "https://wallet-sandbox.uphold.com/authorize/...", "withdraw_url": "" } -)" }; +)"}; INSTANTIATE_TEST_SUITE_P( - UpholdAuthorizationTest, - Transfer, - Values( - // "uphold_wallet_is_null" - ParamType{ - "", - type::UrlResponse{}, - type::Result::LEDGER_ERROR - }, - // "404" - ParamType{ - uphold_wallet, - type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_NOT_FOUND, {}, {} }, - type::Result::NOT_FOUND - }, - // "wallet_device_limit_reached" - ParamType{ - uphold_wallet, - type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_CONFLICT, {}, {} }, - type::Result::ALREADY_EXISTS - }, - // "rewards_web_services_error" - ParamType{ - uphold_wallet, - type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_INTERNAL_SERVER_ERROR, {}, {} }, - type::Result::LEDGER_ERROR - }, - // "happy_path" - ParamType{ - uphold_wallet, - type::UrlResponse{ {}, {}, net::HttpStatusCode::HTTP_OK, {}, {} }, - type::Result::LEDGER_OK - } - ), - NameSuffixGenerator -); + UpholdAuthorizationTest, + Transfer, + Values( + // "uphold_wallet_is_null" + ParamType{"", type::UrlResponse{}, type::Result::LEDGER_ERROR}, + // "404" + ParamType{uphold_wallet, + type::UrlResponse{{}, + {}, + net::HttpStatusCode::HTTP_NOT_FOUND, + {}, + {}}, + type::Result::NOT_FOUND}, + // "wallet_device_limit_reached" + ParamType{uphold_wallet, + type::UrlResponse{{}, + {}, + net::HttpStatusCode::HTTP_CONFLICT, + {}, + {}}, + type::Result::ALREADY_EXISTS}, + // "rewards_web_services_error" + ParamType{ + uphold_wallet, + type::UrlResponse{{}, + {}, + net::HttpStatusCode::HTTP_INTERNAL_SERVER_ERROR, + {}, + {}}, + type::Result::LEDGER_ERROR}, + // "happy_path" + ParamType{ + uphold_wallet, + type::UrlResponse{{}, {}, net::HttpStatusCode::HTTP_OK, {}, {}}, + type::Result::LEDGER_OK}), + NameSuffixGenerator); TEST_P(Transfer, AnonFunds) { const auto& params = GetParam(); @@ -300,30 +282,18 @@ TEST_P(Transfer, AnonFunds) { const type::UrlResponse& rewards_web_services_response = std::get<1>(params); const auto& expected_result = std::get<2>(params); - ON_CALL( - *mock_ledger_client_, - GetEncryptedStringState(state::kWalletUphold) - ).WillByDefault( - testing::Return(uphold_wallet) - ); - - ON_CALL( - *mock_ledger_client_, - LoadURL(_, _) - ).WillByDefault( - Invoke( - [&](type::UrlRequestPtr, client::LoadURLCallback callback) { - callback(rewards_web_services_response); - } - ) - ); - - authorization_->TransferAnonFunds( - 0.0, - [&](const type::Result result) { - EXPECT_TRUE(result == expected_result); - } - ); + ON_CALL(*mock_ledger_client_, GetEncryptedStringState(state::kWalletUphold)) + .WillByDefault(testing::Return(uphold_wallet)); + + ON_CALL(*mock_ledger_client_, LoadURL(_, _)) + .WillByDefault( + Invoke([&](type::UrlRequestPtr, client::LoadURLCallback callback) { + callback(rewards_web_services_response); + })); + + authorization_->TransferAnonFunds(0.0, [&](const type::Result result) { + EXPECT_TRUE(result == expected_result); + }); } } // namespace transfer 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 291e03578c90..eba5f6c0634a 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 @@ -169,10 +169,9 @@ void Wallet::ClaimFunds(ledger::ResultCallback callback) { callback(type::Result::CONTINUE); return; } - + callback(type::Result::LEDGER_OK); - } - ); + }); } void Wallet::GetAnonWalletStatus(ledger::ResultCallback callback) { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h index e4b660c608a5..5a88f1c39286 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h @@ -3,8 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BRAVELEDGER_WALLET_WALLET_H_ -#define BRAVELEDGER_WALLET_WALLET_H_ +#ifndef BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_WALLET_WALLET_H_ +#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_WALLET_WALLET_H_ #include @@ -75,4 +75,4 @@ class Wallet { } // namespace wallet } // namespace ledger -#endif // BRAVELEDGER_WALLET_WALLET_H_ +#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_WALLET_WALLET_H_ From 918a4cc0b9a379a803682bcf4124344050a290fa Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Mon, 7 Jun 2021 21:47:19 +0200 Subject: [PATCH 11/15] Fixing lint findings in continuous-integration/post-init/pr-head. --- .../internal/uphold/uphold_authorization.cc | 9 +++---- .../internal/uphold/uphold_authorization.h | 26 ++++++++----------- .../src/bat/ledger/internal/wallet/wallet.cc | 16 ++++++------ 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 567f54f8e4ad..473741004ad4 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -23,11 +23,10 @@ using std::placeholders::_3; namespace ledger { namespace uphold { -UpholdAuthorization::UpholdAuthorization(LedgerImpl* ledger) : - ledger_(ledger), - promotion_server_(std::make_unique(ledger)), - uphold_server_(std::make_unique(ledger)) { -} +UpholdAuthorization::UpholdAuthorization(LedgerImpl* ledger) + : ledger_(ledger), + promotion_server_(std::make_unique(ledger)), + uphold_server_(std::make_unique(ledger)) {} UpholdAuthorization::~UpholdAuthorization() = default; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h index ccc287e48c7e..d6cdbbd00327 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h @@ -26,9 +26,8 @@ class UpholdAuthorization { ~UpholdAuthorization(); - void Authorize( - const base::flat_map& args, - ledger::ExternalWalletAuthorizationCallback callback) const; + void Authorize(const base::flat_map& args, + ledger::ExternalWalletAuthorizationCallback callback) const; void GetAnonFunds( endpoint::promotion::GetWalletBalanceCallback callback) const; @@ -38,20 +37,17 @@ class UpholdAuthorization { endpoint::promotion::PostClaimUpholdCallback callback) const; private: - void OnAuthorize( - const type::Result result, - const std::string& token, - ledger::ExternalWalletAuthorizationCallback callback) const; + void OnAuthorize(const type::Result result, + const std::string& token, + ledger::ExternalWalletAuthorizationCallback callback) const; - void OnGetUser( - const type::Result result, - const User& user, - ledger::ExternalWalletAuthorizationCallback callback) const; + void OnGetUser(const type::Result result, + const User& user, + ledger::ExternalWalletAuthorizationCallback callback) const; - void OnCardCreate( - const type::Result result, - const std::string& address, - ledger::ExternalWalletAuthorizationCallback callback) const; + void OnCardCreate(const type::Result result, + const std::string& address, + ledger::ExternalWalletAuthorizationCallback callback) const; void OnGetAnonFunds( const type::Result result, 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 eba5f6c0634a..b2960e661085 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 @@ -163,15 +163,15 @@ void Wallet::DisconnectWallet( void Wallet::ClaimFunds(ledger::ResultCallback callback) { // tokens claim ledger_->promotion()->TransferTokens( - [callback](const type::Result result, std::string drain_id) { - if (result != type::Result::LEDGER_OK) { - BLOG(0, "Claiming tokens failed"); - callback(type::Result::CONTINUE); - return; - } + [callback](const type::Result result, std::string drain_id) { + if (result != type::Result::LEDGER_OK) { + BLOG(0, "Claiming tokens failed"); + callback(type::Result::CONTINUE); + return; + } - callback(type::Result::LEDGER_OK); - }); + callback(type::Result::LEDGER_OK); + }); } void Wallet::GetAnonWalletStatus(ledger::ResultCallback callback) { From 3676eea2750e29f2b09dba257808e515ec6b4929 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Tue, 8 Jun 2021 13:41:58 +0200 Subject: [PATCH 12/15] Addressing pull request findings. Removing Wallet::ClaimFunds() and performing the call to Promotion::TransferTokens() directly in uphold_wallet.cc. Renaming UpholdAuthorization::TransferAnonFunds() to UpholdAuthorization::LinkWallet(). --- .../internal/uphold/uphold_authorization.cc | 8 +- .../internal/uphold/uphold_authorization.h | 4 +- .../uphold/uphold_authorization_unittest.cc | 137 ++++++++++-------- .../ledger/internal/uphold/uphold_wallet.cc | 19 ++- .../ledger/internal/uphold/uphold_wallet.h | 11 +- .../src/bat/ledger/internal/wallet/wallet.cc | 14 -- .../src/bat/ledger/internal/wallet/wallet.h | 2 - 7 files changed, 106 insertions(+), 89 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 473741004ad4..9271944d8f97 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -275,12 +275,12 @@ void UpholdAuthorization::OnGetAnonFunds( ledger_->state()->SetFetchOldBalanceEnabled(false); } - TransferAnonFunds( + LinkWallet( balance->user_funds, - std::bind(&UpholdAuthorization::OnTransferAnonFunds, this, _1, callback)); + std::bind(&UpholdAuthorization::OnLinkWallet, this, _1, callback)); } -void UpholdAuthorization::TransferAnonFunds( +void UpholdAuthorization::LinkWallet( const double user_funds, ledger::endpoint::promotion::PostClaimUpholdCallback callback) const { if (!uphold::GetWallet(ledger_)) { @@ -292,7 +292,7 @@ void UpholdAuthorization::TransferAnonFunds( promotion_server_->post_claim_uphold()->Request(user_funds, callback); } -void UpholdAuthorization::OnTransferAnonFunds( +void UpholdAuthorization::OnLinkWallet( const type::Result result, ledger::ExternalWalletAuthorizationCallback callback) const { if (result == type::Result::LEDGER_OK) { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h index d6cdbbd00327..b7bd4e481863 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h @@ -32,7 +32,7 @@ class UpholdAuthorization { void GetAnonFunds( endpoint::promotion::GetWalletBalanceCallback callback) const; - void TransferAnonFunds( + void LinkWallet( const double user_funds, endpoint::promotion::PostClaimUpholdCallback callback) const; @@ -54,7 +54,7 @@ class UpholdAuthorization { type::BalancePtr balance, ledger::ExternalWalletAuthorizationCallback callback) const; - void OnTransferAnonFunds( + void OnLinkWallet( const type::Result result, ledger::ExternalWalletAuthorizationCallback callback) const; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index db71147ce676..7065e93a276e 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -26,17 +26,16 @@ using ::testing::Values; namespace ledger { namespace uphold { namespace tests { -namespace anon_funds { -namespace get { -using Result = std::pair>; -using ParamType = std::tuple; +using GetAnonFundsParamType = + std::tuple> // expected result + >; -class Get : public TestWithParam { +class GetAnonFunds : public TestWithParam { private: base::test::TaskEnvironment scoped_task_environment_; @@ -47,7 +46,7 @@ class Get : public TestWithParam { std::unique_ptr authorization_; public: - Get() + GetAnonFunds() : mock_ledger_client_{std::make_unique()}, mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get())}, @@ -57,7 +56,8 @@ class Get : public TestWithParam { std::make_unique(mock_ledger_impl_.get())} {} }; -std::string NameSuffixGenerator(const TestParamInfo& info) { +std::string GetAnonFundsNameSuffixGenerator( + const TestParamInfo& info) { const bool fetch_old_balance_enabled = std::get<0>(info.param); const std::string& brave_wallet = std::get<1>(info.param); const type::UrlResponse& balance_server_response = std::get<2>(info.param); @@ -87,20 +87,28 @@ std::string NameSuffixGenerator(const TestParamInfo& info) { INSTANTIATE_TEST_SUITE_P( UpholdAuthorizationTest, - Get, + GetAnonFunds, Values( // "fetch_old_balance_disabled" - ParamType{false, {}, {}, {type::Result::LEDGER_OK, 0.0}}, + GetAnonFundsParamType{ + false, + {}, + {}, + {type::Result::LEDGER_OK, 0.0}}, // "brave_wallet_is_not_created" - ParamType{true, {}, {}, {type::Result::LEDGER_OK, 0.0}}, + GetAnonFundsParamType{ + true, + {}, + {}, + {type::Result::LEDGER_OK, 0.0}}, // "brave_wallet_payment_id_is_empty" - ParamType{ + GetAnonFundsParamType{ true, R"({ "payment_id": "", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", {}, {type::Result::LEDGER_ERROR, {}}}, // "balance_server_error" - ParamType{ + GetAnonFundsParamType{ true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{{}, @@ -110,25 +118,28 @@ INSTANTIATE_TEST_SUITE_P( {}}, {type::Result::LEDGER_ERROR, {}}}, // "invalid_body_in_balance_server_response" - ParamType{ + GetAnonFundsParamType{ true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", - type::UrlResponse{{}, {}, net::HttpStatusCode::HTTP_OK, {}, {}}, + type::UrlResponse{{}, + {}, + net::HttpStatusCode::HTTP_OK, + {}, + {}}, {type::Result::LEDGER_ERROR, 0.0}}, // "happy_path" - ParamType{ + GetAnonFundsParamType{ true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", - type::UrlResponse{ - {}, - {}, - net::HttpStatusCode::HTTP_OK, - R"({ "total": 5.0, "spendable": 0.0, "confirmed": 5.0, "unconfirmed": 0.0 })", - {}}, + type::UrlResponse{{}, + {}, + net::HttpStatusCode::HTTP_OK, + R"({ "total": 5.0, "spendable": 0.0, "confirmed": 5.0, "unconfirmed": 0.0 })", + {}}, {type::Result::LEDGER_OK, 5.0}}), - NameSuffixGenerator); + GetAnonFundsNameSuffixGenerator); -TEST_P(Get, AnonFunds) { +TEST_P(GetAnonFunds, ) { const auto& params = GetParam(); const bool fetch_old_balance_enabled = std::get<0>(params); const std::string& brave_wallet = std::get<1>(params); @@ -165,18 +176,13 @@ TEST_P(Get, AnonFunds) { }); } -} // namespace get - -namespace transfer { - -using Result = type::Result; -using ParamType = +using LinkWalletParamType = std::tuple; -class Transfer : public TestWithParam { +class LinkWallet : public TestWithParam { private: base::test::TaskEnvironment scoped_task_environment_; @@ -186,7 +192,7 @@ class Transfer : public TestWithParam { std::unique_ptr authorization_; public: - Transfer() + LinkWallet() : mock_ledger_client_{std::make_unique()}, mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get())}, @@ -194,8 +200,8 @@ class Transfer : public TestWithParam { std::make_unique(mock_ledger_impl_.get())} {} }; -std::string NameSuffixGenerator( - const TestParamInfo& info) { +std::string LinkWalletNameSuffixGenerator( + const TestParamInfo& info) { const std::string& uphold_wallet = std::get<0>(info.param); const type::UrlResponse& rewards_web_services_response = std::get<1>(info.param); @@ -240,28 +246,33 @@ const char uphold_wallet[]{R"( INSTANTIATE_TEST_SUITE_P( UpholdAuthorizationTest, - Transfer, + LinkWallet, Values( // "uphold_wallet_is_null" - ParamType{"", type::UrlResponse{}, type::Result::LEDGER_ERROR}, + LinkWalletParamType{ + "", + type::UrlResponse{}, + type::Result::LEDGER_ERROR}, // "404" - ParamType{uphold_wallet, - type::UrlResponse{{}, - {}, - net::HttpStatusCode::HTTP_NOT_FOUND, - {}, - {}}, - type::Result::NOT_FOUND}, + LinkWalletParamType{ + uphold_wallet, + type::UrlResponse{{}, + {}, + net::HttpStatusCode::HTTP_NOT_FOUND, + {}, + {}}, + type::Result::NOT_FOUND}, // "wallet_device_limit_reached" - ParamType{uphold_wallet, - type::UrlResponse{{}, - {}, - net::HttpStatusCode::HTTP_CONFLICT, - {}, - {}}, - type::Result::ALREADY_EXISTS}, + LinkWalletParamType{ + uphold_wallet, + type::UrlResponse{{}, + {}, + net::HttpStatusCode::HTTP_CONFLICT, + {}, + {}}, + type::Result::ALREADY_EXISTS}, // "rewards_web_services_error" - ParamType{ + LinkWalletParamType{ uphold_wallet, type::UrlResponse{{}, {}, @@ -270,13 +281,17 @@ INSTANTIATE_TEST_SUITE_P( {}}, type::Result::LEDGER_ERROR}, // "happy_path" - ParamType{ + LinkWalletParamType{ uphold_wallet, - type::UrlResponse{{}, {}, net::HttpStatusCode::HTTP_OK, {}, {}}, + type::UrlResponse{{}, + {}, + net::HttpStatusCode::HTTP_OK, + {}, + {}}, type::Result::LEDGER_OK}), - NameSuffixGenerator); + LinkWalletNameSuffixGenerator); -TEST_P(Transfer, AnonFunds) { +TEST_P(LinkWallet, ) { const auto& params = GetParam(); const std::string& uphold_wallet = std::get<0>(params); const type::UrlResponse& rewards_web_services_response = std::get<1>(params); @@ -291,13 +306,11 @@ TEST_P(Transfer, AnonFunds) { callback(rewards_web_services_response); })); - authorization_->TransferAnonFunds(0.0, [&](const type::Result result) { + authorization_->LinkWallet(0.0, [&](const type::Result result) { EXPECT_TRUE(result == expected_result); }); } -} // namespace transfer -} // namespace anon_funds } // namespace tests } // namespace uphold } // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc index 5366851a1ad1..68a79516c334 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc @@ -107,7 +107,8 @@ void UpholdWallet::OnGenerate( } if (user.verified) { - ledger_->wallet()->ClaimFunds(callback); + ledger_->promotion()->TransferTokens( + std::bind(&UpholdWallet::OnTransferTokens, this, _1, _2, callback)); return; } @@ -130,7 +131,21 @@ void UpholdWallet::OnCreateCard( ledger_->uphold()->SetWallet(wallet_ptr->Clone()); if (wallet_ptr->status == type::WalletStatus::VERIFIED) { - ledger_->wallet()->ClaimFunds(callback); + ledger_->promotion()->TransferTokens( + std::bind(&UpholdWallet::OnTransferTokens, this, _1, _2, callback)); + return; + } + + callback(type::Result::LEDGER_OK); +} + +void UpholdWallet::OnTransferTokens( + const type::Result result, + std::string drain_id, + ledger::ResultCallback callback) { + if (result != type::Result::LEDGER_OK) { + BLOG(0, "Claiming tokens failed"); + callback(type::Result::CONTINUE); return; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h index b3a785ffd71c..93defeb05317 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h @@ -3,8 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BRAVELEDGER_UPHOLD_UPHOLD_WALLET_H_ -#define BRAVELEDGER_UPHOLD_UPHOLD_WALLET_H_ +#ifndef BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_UPHOLD_UPHOLD_WALLET_H_ +#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_UPHOLD_UPHOLD_WALLET_H_ #include @@ -34,6 +34,11 @@ class UpholdWallet { const std::string& address, ledger::ResultCallback callback); + void OnTransferTokens( + const type::Result result, + std::string drain_id, + ledger::ResultCallback callback); + type::WalletStatus GetNewStatus( const type::WalletStatus old_status, const User& user); @@ -43,4 +48,4 @@ class UpholdWallet { } // namespace uphold } // namespace ledger -#endif // BRAVELEDGER_UPHOLD_UPHOLD_WALLET_H_ +#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_UPHOLD_UPHOLD_WALLET_H_ 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 b2960e661085..8c55ce2246a6 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 @@ -160,20 +160,6 @@ void Wallet::DisconnectWallet( callback(type::Result::LEDGER_OK); } -void Wallet::ClaimFunds(ledger::ResultCallback callback) { - // tokens claim - ledger_->promotion()->TransferTokens( - [callback](const type::Result result, std::string drain_id) { - if (result != type::Result::LEDGER_OK) { - BLOG(0, "Claiming tokens failed"); - callback(type::Result::CONTINUE); - return; - } - - callback(type::Result::LEDGER_OK); - }); -} - void Wallet::GetAnonWalletStatus(ledger::ResultCallback callback) { const auto wallet = GetWallet(); if (!wallet) { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h index 5a88f1c39286..cd515ff80880 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.h @@ -47,8 +47,6 @@ class Wallet { const std::string& wallet_type, ledger::ResultCallback callback); - void ClaimFunds(ledger::ResultCallback callback); - void GetAnonWalletStatus(ledger::ResultCallback callback); void DisconnectAllWallets(ledger::ResultCallback callback); From 96e42182be9da9f864af9d6df80843d481bdd0ad Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Tue, 8 Jun 2021 15:32:01 +0200 Subject: [PATCH 13/15] Fixing lint findings. --- .../internal/uphold/uphold_authorization.cc | 31 +++++--------- .../internal/uphold/uphold_authorization.h | 10 ++--- .../uphold/uphold_authorization_unittest.cc | 41 ++++++------------- .../ledger/internal/uphold/uphold_wallet.cc | 7 ++-- .../ledger/internal/uphold/uphold_wallet.h | 32 +++++++-------- 5 files changed, 43 insertions(+), 78 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 9271944d8f97..fa17bd0ecfb3 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -100,11 +100,8 @@ void UpholdAuthorization::Authorize( return; } - auto url_callback = std::bind(&UpholdAuthorization::OnAuthorize, - this, - _1, - _2, - callback); + auto url_callback = + std::bind(&UpholdAuthorization::OnAuthorize, this, _1, _2, callback); uphold_server_->post_oauth()->Request(code, url_callback); } @@ -155,11 +152,8 @@ void UpholdAuthorization::OnAuthorize( ledger_->uphold()->SetWallet(wallet_ptr->Clone()); - auto user_callback = std::bind(&UpholdAuthorization::OnGetUser, - this, - _1, - _2, - callback); + auto user_callback = + std::bind(&UpholdAuthorization::OnGetUser, this, _1, _2, callback); ledger_->uphold()->GetUser(user_callback); } @@ -177,17 +171,13 @@ void UpholdAuthorization::OnGetUser( } if (user.status == UserStatus::OK) { - wallet_ptr->status = user.verified - ? type::WalletStatus::VERIFIED - : type::WalletStatus::CONNECTED; + wallet_ptr->status = user.verified ? type::WalletStatus::VERIFIED + : type::WalletStatus::CONNECTED; ledger_->uphold()->SetWallet(wallet_ptr->Clone()); if (wallet_ptr->address.empty()) { - auto new_callback = std::bind(&UpholdAuthorization::OnCardCreate, - this, - _1, - _2, - callback); + auto new_callback = + std::bind(&UpholdAuthorization::OnCardCreate, this, _1, _2, callback); ledger_->uphold()->CreateCard(new_callback); return; } @@ -275,9 +265,8 @@ void UpholdAuthorization::OnGetAnonFunds( ledger_->state()->SetFetchOldBalanceEnabled(false); } - LinkWallet( - balance->user_funds, - std::bind(&UpholdAuthorization::OnLinkWallet, this, _1, callback)); + LinkWallet(balance->user_funds, + std::bind(&UpholdAuthorization::OnLinkWallet, this, _1, callback)); } void UpholdAuthorization::LinkWallet( diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h index b7bd4e481863..8c3701e11e5f 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h @@ -32,9 +32,8 @@ class UpholdAuthorization { void GetAnonFunds( endpoint::promotion::GetWalletBalanceCallback callback) const; - void LinkWallet( - const double user_funds, - endpoint::promotion::PostClaimUpholdCallback callback) const; + void LinkWallet(const double user_funds, + endpoint::promotion::PostClaimUpholdCallback callback) const; private: void OnAuthorize(const type::Result result, @@ -54,9 +53,8 @@ class UpholdAuthorization { type::BalancePtr balance, ledger::ExternalWalletAuthorizationCallback callback) const; - void OnLinkWallet( - const type::Result result, - ledger::ExternalWalletAuthorizationCallback callback) const; + void OnLinkWallet(const type::Result result, + ledger::ExternalWalletAuthorizationCallback callback) const; LedgerImpl* ledger_; // NOT OWNED std::unique_ptr promotion_server_; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index 7065e93a276e..d9b8beed808d 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -90,17 +90,9 @@ INSTANTIATE_TEST_SUITE_P( GetAnonFunds, Values( // "fetch_old_balance_disabled" - GetAnonFundsParamType{ - false, - {}, - {}, - {type::Result::LEDGER_OK, 0.0}}, + GetAnonFundsParamType{false, {}, {}, {type::Result::LEDGER_OK, 0.0}}, // "brave_wallet_is_not_created" - GetAnonFundsParamType{ - true, - {}, - {}, - {type::Result::LEDGER_OK, 0.0}}, + GetAnonFundsParamType{true, {}, {}, {type::Result::LEDGER_OK, 0.0}}, // "brave_wallet_payment_id_is_empty" GetAnonFundsParamType{ true, @@ -121,21 +113,18 @@ INSTANTIATE_TEST_SUITE_P( GetAnonFundsParamType{ true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", - type::UrlResponse{{}, - {}, - net::HttpStatusCode::HTTP_OK, - {}, - {}}, + type::UrlResponse{{}, {}, net::HttpStatusCode::HTTP_OK, {}, {}}, {type::Result::LEDGER_ERROR, 0.0}}, // "happy_path" GetAnonFundsParamType{ true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", - type::UrlResponse{{}, - {}, - net::HttpStatusCode::HTTP_OK, - R"({ "total": 5.0, "spendable": 0.0, "confirmed": 5.0, "unconfirmed": 0.0 })", - {}}, + type::UrlResponse{ + {}, + {}, + net::HttpStatusCode::HTTP_OK, + R"({ "total": 5.0, "spendable": 0.0, "confirmed": 5.0, "unconfirmed": 0.0 })", + {}}, {type::Result::LEDGER_OK, 5.0}}), GetAnonFundsNameSuffixGenerator); @@ -249,10 +238,8 @@ INSTANTIATE_TEST_SUITE_P( LinkWallet, Values( // "uphold_wallet_is_null" - LinkWalletParamType{ - "", - type::UrlResponse{}, - type::Result::LEDGER_ERROR}, + LinkWalletParamType{"", type::UrlResponse{}, + type::Result::LEDGER_ERROR}, // "404" LinkWalletParamType{ uphold_wallet, @@ -283,11 +270,7 @@ INSTANTIATE_TEST_SUITE_P( // "happy_path" LinkWalletParamType{ uphold_wallet, - type::UrlResponse{{}, - {}, - net::HttpStatusCode::HTTP_OK, - {}, - {}}, + type::UrlResponse{{}, {}, net::HttpStatusCode::HTTP_OK, {}, {}}, type::Result::LEDGER_OK}), LinkWalletNameSuffixGenerator); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc index 68a79516c334..491e71abdec2 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc @@ -139,10 +139,9 @@ void UpholdWallet::OnCreateCard( callback(type::Result::LEDGER_OK); } -void UpholdWallet::OnTransferTokens( - const type::Result result, - std::string drain_id, - ledger::ResultCallback callback) { +void UpholdWallet::OnTransferTokens(const type::Result result, + std::string drain_id, + ledger::ResultCallback callback) { if (result != type::Result::LEDGER_OK) { BLOG(0, "Claiming tokens failed"); callback(type::Result::CONTINUE); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h index 93defeb05317..76e7ae68a6ac 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h @@ -24,24 +24,20 @@ class UpholdWallet { void Generate(ledger::ResultCallback callback); private: - void OnGenerate( - const type::Result result, - const User& user, - ledger::ResultCallback callback); - - void OnCreateCard( - const type::Result result, - const std::string& address, - ledger::ResultCallback callback); - - void OnTransferTokens( - const type::Result result, - std::string drain_id, - ledger::ResultCallback callback); - - type::WalletStatus GetNewStatus( - const type::WalletStatus old_status, - const User& user); + void OnGenerate(const type::Result result, + const User& user, + ledger::ResultCallback callback); + + void OnCreateCard(const type::Result result, + const std::string& address, + ledger::ResultCallback callback); + + void OnTransferTokens(const type::Result result, + std::string drain_id, + ledger::ResultCallback callback); + + type::WalletStatus GetNewStatus(const type::WalletStatus old_status, + const User& user); LedgerImpl* ledger_; // NOT OWNED }; From fbf7d7c64eb2d5bdc507a98454a6c34e9e7f4639 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Tue, 8 Jun 2021 22:56:13 +0200 Subject: [PATCH 14/15] Addressing pull request findings. Removing brave_rewards::prefs::kAnonTransferChecked. Making UpholdAuthorization::GetAnonFunds() and UpholdAuthorization::LinkWallet() private (using FRIEND_TEST_ALL_PREFIXES). Updating copyright year in uphold_authorization_unittest.cc. Adjusting TEST_P's and INSTANTIATE_TEST_SUITE_P's arguments in uphold_authorization_unittest.cc. Passing drain_id by reference-to-const in UpholdWallet::OnTransferTokens(). --- .../brave_rewards/browser/rewards_service.cc | 1 - components/brave_rewards/common/pref_names.cc | 1 - components/brave_rewards/common/pref_names.h | 1 - .../internal/uphold/uphold_authorization.h | 5 +- .../uphold/uphold_authorization_unittest.cc | 79 ++++++++++--------- .../ledger/internal/uphold/uphold_wallet.cc | 2 +- .../ledger/internal/uphold/uphold_wallet.h | 2 +- 7 files changed, 46 insertions(+), 45 deletions(-) diff --git a/components/brave_rewards/browser/rewards_service.cc b/components/brave_rewards/browser/rewards_service.cc index 4ec3c33ee5f5..84fd8beb1039 100644 --- a/components/brave_rewards/browser/rewards_service.cc +++ b/components/brave_rewards/browser/rewards_service.cc @@ -64,7 +64,6 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { #endif registry->RegisterUint64Pref(prefs::kPromotionLastFetchStamp, 0ull); registry->RegisterBooleanPref(prefs::kPromotionCorruptedMigrated, false); - registry->RegisterBooleanPref(prefs::kAnonTransferChecked, false); registry->RegisterIntegerPref(prefs::kVersion, 0); registry->RegisterIntegerPref(prefs::kMinVisitTime, 8); registry->RegisterIntegerPref(prefs::kMinVisits, 1); diff --git a/components/brave_rewards/common/pref_names.cc b/components/brave_rewards/common/pref_names.cc index da6c30300f9c..04c8b7937bae 100644 --- a/components/brave_rewards/common/pref_names.cc +++ b/components/brave_rewards/common/pref_names.cc @@ -33,7 +33,6 @@ const char kPromotionLastFetchStamp[] = "brave.rewards.promotion_last_fetch_stamp"; const char kPromotionCorruptedMigrated[] = "brave.rewards.promotion_corrupted_migrated2"; -const char kAnonTransferChecked[] = "brave.rewards.anon_transfer_checked"; const char kVersion[] = "brave.rewards.version"; const char kMinVisitTime[] = "brave.rewards.ac.min_visit_time"; const char kMinVisits[] = "brave.rewards.ac.min_visits"; diff --git a/components/brave_rewards/common/pref_names.h b/components/brave_rewards/common/pref_names.h index 5485cee46133..8dbec3cd45ae 100644 --- a/components/brave_rewards/common/pref_names.h +++ b/components/brave_rewards/common/pref_names.h @@ -28,7 +28,6 @@ extern const char kServerPublisherListStamp[]; extern const char kUpholdAnonAddress[]; // DEPRECATED extern const char kPromotionLastFetchStamp[]; extern const char kPromotionCorruptedMigrated[]; -extern const char kAnonTransferChecked[]; // DEPRECATED extern const char kVersion[]; extern const char kMinVisitTime[]; extern const char kMinVisits[]; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h index 8c3701e11e5f..33f0379dcda9 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.h @@ -29,13 +29,13 @@ class UpholdAuthorization { void Authorize(const base::flat_map& args, ledger::ExternalWalletAuthorizationCallback callback) const; + private: void GetAnonFunds( endpoint::promotion::GetWalletBalanceCallback callback) const; void LinkWallet(const double user_funds, endpoint::promotion::PostClaimUpholdCallback callback) const; - private: void OnAuthorize(const type::Result result, const std::string& token, ledger::ExternalWalletAuthorizationCallback callback) const; @@ -59,6 +59,9 @@ class UpholdAuthorization { LedgerImpl* ledger_; // NOT OWNED std::unique_ptr promotion_server_; std::unique_ptr uphold_server_; + + FRIEND_TEST_ALL_PREFIXES(GetAnonFundsTest, AnonFundsFlow); + FRIEND_TEST_ALL_PREFIXES(LinkWalletTest, LinkWalletFlow); }; } // namespace uphold diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index d9b8beed808d..fd23bf95d94e 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -1,4 +1,4 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2021 The Brave Authors. All rights reserved. * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -25,9 +25,8 @@ using ::testing::Values; namespace ledger { namespace uphold { -namespace tests { -using GetAnonFundsParamType = +using GetAnonFundsTestParamType = std::tuple> // expected result >; -class GetAnonFunds : public TestWithParam { +class GetAnonFundsTest : public TestWithParam { private: base::test::TaskEnvironment scoped_task_environment_; @@ -46,7 +45,7 @@ class GetAnonFunds : public TestWithParam { std::unique_ptr authorization_; public: - GetAnonFunds() + GetAnonFundsTest() : mock_ledger_client_{std::make_unique()}, mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get())}, @@ -57,50 +56,53 @@ class GetAnonFunds : public TestWithParam { }; std::string GetAnonFundsNameSuffixGenerator( - const TestParamInfo& info) { + const TestParamInfo& info) { const bool fetch_old_balance_enabled = std::get<0>(info.param); const std::string& brave_wallet = std::get<1>(info.param); const type::UrlResponse& balance_server_response = std::get<2>(info.param); if (!fetch_old_balance_enabled) { - return "fetch_old_balance_disabled"; + return "FetchOldBalanceDisabled"; } if (brave_wallet.empty()) { - return "brave_wallet_is_not_created"; + return "BraveWalletIsNotCreated"; } if (brave_wallet.find(R"("payment_id": "")") != std::string::npos) { - return "brave_wallet_payment_id_is_empty"; + return "BraveWalletPaymentIdIsEmpty"; } if (balance_server_response.status_code != net::HttpStatusCode::HTTP_OK) { - return "balance_server_error"; + return "BalanceServerError"; } if (balance_server_response.body.empty()) { - return "invalid_body_in_balance_server_response"; + return "InvalidBodyInBalanceServerResponse"; } - return "happy_path"; + return "HappyPath"; } INSTANTIATE_TEST_SUITE_P( - UpholdAuthorizationTest, - GetAnonFunds, + UpholdAuthorization, + GetAnonFundsTest, Values( // "fetch_old_balance_disabled" - GetAnonFundsParamType{false, {}, {}, {type::Result::LEDGER_OK, 0.0}}, + GetAnonFundsTestParamType{false, + {}, + {}, + {type::Result::LEDGER_OK, 0.0}}, // "brave_wallet_is_not_created" - GetAnonFundsParamType{true, {}, {}, {type::Result::LEDGER_OK, 0.0}}, + GetAnonFundsTestParamType{true, {}, {}, {type::Result::LEDGER_OK, 0.0}}, // "brave_wallet_payment_id_is_empty" - GetAnonFundsParamType{ + GetAnonFundsTestParamType{ true, R"({ "payment_id": "", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", {}, {type::Result::LEDGER_ERROR, {}}}, // "balance_server_error" - GetAnonFundsParamType{ + GetAnonFundsTestParamType{ true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{{}, @@ -110,13 +112,13 @@ INSTANTIATE_TEST_SUITE_P( {}}, {type::Result::LEDGER_ERROR, {}}}, // "invalid_body_in_balance_server_response" - GetAnonFundsParamType{ + GetAnonFundsTestParamType{ true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{{}, {}, net::HttpStatusCode::HTTP_OK, {}, {}}, {type::Result::LEDGER_ERROR, 0.0}}, // "happy_path" - GetAnonFundsParamType{ + GetAnonFundsTestParamType{ true, R"({ "payment_id": "f375da3c-c206-4f09-9422-665b8e5952db", "recovery_seed": "OG2zYotDSeZ81qLtr/uq5k/GC6WE5/7BclT1lHi4l+w=" })", type::UrlResponse{ @@ -128,7 +130,7 @@ INSTANTIATE_TEST_SUITE_P( {type::Result::LEDGER_OK, 5.0}}), GetAnonFundsNameSuffixGenerator); -TEST_P(GetAnonFunds, ) { +TEST_P(GetAnonFundsTest, AnonFundsFlow) { const auto& params = GetParam(); const bool fetch_old_balance_enabled = std::get<0>(params); const std::string& brave_wallet = std::get<1>(params); @@ -165,13 +167,13 @@ TEST_P(GetAnonFunds, ) { }); } -using LinkWalletParamType = +using LinkWalletTestParamType = std::tuple; -class LinkWallet : public TestWithParam { +class LinkWalletTest : public TestWithParam { private: base::test::TaskEnvironment scoped_task_environment_; @@ -181,7 +183,7 @@ class LinkWallet : public TestWithParam { std::unique_ptr authorization_; public: - LinkWallet() + LinkWalletTest() : mock_ledger_client_{std::make_unique()}, mock_ledger_impl_{ std::make_unique(mock_ledger_client_.get())}, @@ -190,13 +192,13 @@ class LinkWallet : public TestWithParam { }; std::string LinkWalletNameSuffixGenerator( - const TestParamInfo& info) { + const TestParamInfo& info) { const std::string& uphold_wallet = std::get<0>(info.param); const type::UrlResponse& rewards_web_services_response = std::get<1>(info.param); if (uphold_wallet.empty()) { - return "uphold_wallet_is_null"; + return "UpholdWalletIsNull"; } if (rewards_web_services_response.status_code == @@ -206,15 +208,15 @@ std::string LinkWalletNameSuffixGenerator( if (rewards_web_services_response.status_code == net::HttpStatusCode::HTTP_CONFLICT) { - return "wallet_device_limit_reached"; + return "WalletDeviceLimitReached"; } if (rewards_web_services_response.status_code != net::HttpStatusCode::HTTP_OK) { - return "rewards_web_services_error"; + return "RewardsWebServicesError"; } - return "happy_path"; + return "HappyPath"; } const char uphold_wallet[]{R"( @@ -234,14 +236,14 @@ const char uphold_wallet[]{R"( )"}; INSTANTIATE_TEST_SUITE_P( - UpholdAuthorizationTest, - LinkWallet, + UpholdAuthorization, + LinkWalletTest, Values( // "uphold_wallet_is_null" - LinkWalletParamType{"", type::UrlResponse{}, - type::Result::LEDGER_ERROR}, + LinkWalletTestParamType{"", type::UrlResponse{}, + type::Result::LEDGER_ERROR}, // "404" - LinkWalletParamType{ + LinkWalletTestParamType{ uphold_wallet, type::UrlResponse{{}, {}, @@ -250,7 +252,7 @@ INSTANTIATE_TEST_SUITE_P( {}}, type::Result::NOT_FOUND}, // "wallet_device_limit_reached" - LinkWalletParamType{ + LinkWalletTestParamType{ uphold_wallet, type::UrlResponse{{}, {}, @@ -259,7 +261,7 @@ INSTANTIATE_TEST_SUITE_P( {}}, type::Result::ALREADY_EXISTS}, // "rewards_web_services_error" - LinkWalletParamType{ + LinkWalletTestParamType{ uphold_wallet, type::UrlResponse{{}, {}, @@ -268,13 +270,13 @@ INSTANTIATE_TEST_SUITE_P( {}}, type::Result::LEDGER_ERROR}, // "happy_path" - LinkWalletParamType{ + LinkWalletTestParamType{ uphold_wallet, type::UrlResponse{{}, {}, net::HttpStatusCode::HTTP_OK, {}, {}}, type::Result::LEDGER_OK}), LinkWalletNameSuffixGenerator); -TEST_P(LinkWallet, ) { +TEST_P(LinkWalletTest, LinkWalletFlow) { const auto& params = GetParam(); const std::string& uphold_wallet = std::get<0>(params); const type::UrlResponse& rewards_web_services_response = std::get<1>(params); @@ -294,6 +296,5 @@ TEST_P(LinkWallet, ) { }); } -} // namespace tests } // namespace uphold } // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc index 491e71abdec2..642c52656881 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc @@ -140,7 +140,7 @@ void UpholdWallet::OnCreateCard( } void UpholdWallet::OnTransferTokens(const type::Result result, - std::string drain_id, + const std::string& drain_id, ledger::ResultCallback callback) { if (result != type::Result::LEDGER_OK) { BLOG(0, "Claiming tokens failed"); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h index 76e7ae68a6ac..1fe23dd4874f 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.h @@ -33,7 +33,7 @@ class UpholdWallet { ledger::ResultCallback callback); void OnTransferTokens(const type::Result result, - std::string drain_id, + const std::string& drain_id, ledger::ResultCallback callback); type::WalletStatus GetNewStatus(const type::WalletStatus old_status, From 6e1301e95ff53c6b8ffc3cce1933b891c8f6864c Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Thu, 10 Jun 2021 12:09:36 +0200 Subject: [PATCH 15/15] Addressing pull request findings. Removing #include "bat/ledger/option_keys.h" where applicable. --- .../src/bat/ledger/internal/uphold/uphold_authorization.cc | 1 - .../ledger/internal/uphold/uphold_authorization_unittest.cc | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index fa17bd0ecfb3..29d874cbccf5 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -14,7 +14,6 @@ #include "bat/ledger/internal/ledger_impl.h" #include "bat/ledger/internal/logging/event_log_keys.h" #include "bat/ledger/internal/uphold/uphold_util.h" -#include "bat/ledger/option_keys.h" using std::placeholders::_1; using std::placeholders::_2; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc index fd23bf95d94e..3e07bf51b2ef 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization_unittest.cc @@ -12,10 +12,9 @@ #include "bat/ledger/internal/ledger_impl_mock.h" #include "bat/ledger/internal/state/state_keys.h" #include "bat/ledger/internal/uphold/uphold_authorization.h" -#include "bat/ledger/option_keys.h" #include "net/http/http_status_code.h" -// npm run test -- brave_unit_tests --filter=UpholdAuthorizationTest* +// npm run test -- brave_unit_tests --filter=UpholdAuthorization* using ::testing::_; using ::testing::Invoke;