diff --git a/components/brave_rewards/browser/test/common/rewards_browsertest_response.cc b/components/brave_rewards/browser/test/common/rewards_browsertest_response.cc index 24d37d6647d3..66e1fe40a23a 100644 --- a/components/brave_rewards/browser/test/common/rewards_browsertest_response.cc +++ b/components/brave_rewards/browser/test/common/rewards_browsertest_response.cc @@ -282,18 +282,12 @@ void RewardsBrowserTestResponse::Get( } } - if (base::StartsWith( - url, - braveledger_uphold::GetAPIUrl("/oauth2/token"), - base::CompareCase::INSENSITIVE_ASCII)) { + if (url.find("/oauth2/token") != std::string::npos) { *response = uphold_auth_resp_; return; } - if (base::StartsWith( - url, - braveledger_uphold::GetAPIUrl("/v0/me/cards"), - base::CompareCase::INSENSITIVE_ASCII)) { + if (url.find("/v0/me/cards") != std::string::npos) { if (base::EndsWith( url, "transactions", @@ -313,10 +307,7 @@ void RewardsBrowserTestResponse::Get( return; } - if (base::StartsWith( - url, - braveledger_uphold::GetAPIUrl("/v0/me"), - base::CompareCase::INSENSITIVE_ASCII)) { + if (url.find("/v0/me") != std::string::npos) { *response = rewards_browsertest_util::GetUpholdUser(verified_wallet_); return; } diff --git a/components/brave_rewards/test/BUILD.gn b/components/brave_rewards/test/BUILD.gn index 21e8bfda3aa3..48c71eff03c4 100644 --- a/components/brave_rewards/test/BUILD.gn +++ b/components/brave_rewards/test/BUILD.gn @@ -79,6 +79,15 @@ source_set("brave_rewards_unit_tests") { "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/promotion/put_safetynet/put_safetynet_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/rewards/get_prefix_list/get_prefix_list_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/rewards/rewards_util_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils_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", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_util_unittest.cc", diff --git a/vendor/bat-native-ledger/BUILD.gn b/vendor/bat-native-ledger/BUILD.gn index 1bbabf836c2b..ded7814837f9 100644 --- a/vendor/bat-native-ledger/BUILD.gn +++ b/vendor/bat-native-ledger/BUILD.gn @@ -309,8 +309,6 @@ source_set("ledger") { "src/bat/ledger/internal/report/report.h", "src/bat/ledger/internal/request/request_util.cc", "src/bat/ledger/internal/request/request_util.h", - "src/bat/ledger/internal/response/response_uphold.cc", - "src/bat/ledger/internal/response/response_uphold.h", "src/bat/ledger/internal/endpoint/api/api_server.cc", "src/bat/ledger/internal/endpoint/api/api_server.h", "src/bat/ledger/internal/endpoint/api/api_util.cc", @@ -385,6 +383,26 @@ source_set("ledger") { "src/bat/ledger/internal/endpoint/rewards/rewards_server.h", "src/bat/ledger/internal/endpoint/rewards/rewards_util.cc", "src/bat/ledger/internal/endpoint/rewards/rewards_util.h", + "src/bat/ledger/internal/endpoint/uphold/get_card/get_card.cc", + "src/bat/ledger/internal/endpoint/uphold/get_card/get_card.h", + "src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.cc", + "src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.h", + "src/bat/ledger/internal/endpoint/uphold/get_me/get_me.cc", + "src/bat/ledger/internal/endpoint/uphold/get_me/get_me.h", + "src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.cc", + "src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.h", + "src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.cc", + "src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.h", + "src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.cc", + "src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.h", + "src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.cc", + "src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.h", + "src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.cc", + "src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.h", + "src/bat/ledger/internal/endpoint/uphold/uphold_server.cc", + "src/bat/ledger/internal/endpoint/uphold/uphold_server.h", + "src/bat/ledger/internal/endpoint/uphold/uphold_utils.cc", + "src/bat/ledger/internal/endpoint/uphold/uphold_utils.h", "src/bat/ledger/internal/sku/sku.h", "src/bat/ledger/internal/sku/sku_brave.cc", "src/bat/ledger/internal/sku/sku_brave.h", diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_sku.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_sku.cc index b4ac8a9a95b0..192fbf647aba 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_sku.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_sku.cc @@ -13,7 +13,6 @@ #include "bat/ledger/internal/ledger_impl.h" #include "bat/ledger/internal/sku/sku_brave.h" #include "bat/ledger/internal/static_values.h" -#include "bat/ledger/internal/uphold/uphold_util.h" using std::placeholders::_1; using std::placeholders::_2; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card.cc new file mode 100644 index 000000000000..ca18092f6a83 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card.cc @@ -0,0 +1,114 @@ +/* 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/endpoint/uphold/get_card/get_card.h" + +#include "base/json/json_reader.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/stringprintf.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" +#include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/uphold/uphold_card.h" +#include "net/http/http_status_code.h" + +using std::placeholders::_1; + +namespace ledger { +namespace endpoint { +namespace uphold { + +GetCard::GetCard(bat_ledger::LedgerImpl* ledger): + ledger_(ledger) { + DCHECK(ledger_); +} + +GetCard::~GetCard() = default; + +std::string GetCard::GetUrl(const std::string& address) { + return GetServerUrl("/v0/me/cards/" + address); +} + +ledger::Result GetCard::CheckStatusCode(const int status_code) { + if (status_code == net::HTTP_UNAUTHORIZED || + status_code == net::HTTP_NOT_FOUND || + status_code == net::HTTP_FORBIDDEN) { + return ledger::Result::EXPIRED_TOKEN; + } + + if (status_code != net::HTTP_OK) { + return ledger::Result::LEDGER_ERROR; + } + + return ledger::Result::LEDGER_OK; +} + +ledger::Result GetCard::ParseBody( + const std::string& body, + double* available) { + DCHECK(available); + + base::Optional value = base::JSONReader::Read(body); + if (!value || !value->is_dict()) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + base::DictionaryValue* dictionary = nullptr; + if (!value->GetAsDictionary(&dictionary)) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + const auto* available_str = dictionary->FindStringKey("available"); + if (!available_str) { + BLOG(0, "Missing available"); + return ledger::Result::LEDGER_ERROR; + } + + const bool success = base::StringToDouble(*available_str, available); + if (!success) { + *available = 0.0; + } + + return ledger::Result::LEDGER_OK; +} + +void GetCard::Request( + const std::string& address, + const std::string& token, + GetCardCallback callback) { + auto url_callback = std::bind(&GetCard::OnRequest, + this, + _1, + callback); + ledger_->LoadURL( + GetUrl(address), + RequestAuthorization(token), + "", + "", + ledger::UrlMethod::GET, + url_callback); +} + +void GetCard::OnRequest( + const ledger::UrlResponse& response, + GetCardCallback callback) { + ledger::LogUrlResponse(__func__, response); + + ledger::Result result = CheckStatusCode(response.status_code); + + if (result != ledger::Result::LEDGER_OK) { + callback(result, 0.0); + return; + } + + double available; + result = ParseBody(response.body, &available); + callback(result, available); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card.h new file mode 100644 index 000000000000..97639c717244 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card.h @@ -0,0 +1,115 @@ +/* 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_ENDPOINT_UPHOLD_GET_CARD_GET_CARD_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_GET_CARD_GET_CARD_H_ + +#include + +#include "bat/ledger/ledger.h" + +// GET https://api.uphold.com/v0/me/cards/{wallet_address} +// +// Success code: +// HTTP_OK (200) +// +// Error codes: +// HTTP_UNAUTHORIZED (401) +// +// Response body: +// { +// "CreatedByApplicationId": "193a77cf-02e8-4e10-8127-8a1b5a8bfece", +// "address": { +// "wire": "XXXXXXXXXX" +// }, +// "available": "0.00", +// "balance": "0.00", +// "currency": "BAT", +// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", +// "label": "Brave Browser", +// "lastTransactionAt": null, +// "settings": { +// "position": 1, +// "protected": false, +// "starred": true +// }, +// "createdByApplicationClientId": "4c2b665ca060d912fec5c735c734859a06118cc8", +// "normalized": [ +// { +// "available": "0.00", +// "balance": "0.00", +// "currency": "USD" +// } +// ], +// "wire": [ +// { +// "accountName": "Uphold Europe Limited", +// "address": { +// "line1": "Tartu mnt 2", +// "line2": "10145 Tallinn, Estonia" +// }, +// "bic": "LHVBEE22", +// "currency": "EUR", +// "iban": "EE76 7700 7710 0159 0178", +// "name": "AS LHV Pank" +// }, +// { +// "accountName": "Uphold HQ, Inc.", +// "accountNumber": "XXXXXXXXXX", +// "address": { +// "line1": "1359 Broadway", +// "line2": "New York, NY 10018" +// }, +// "bic": "MCBEUS33", +// "currency": "USD", +// "name": "Metropolitan Bank", +// "routingNumber": "XXXXXXXXX" +// } +// ] +// } + +namespace bat_ledger { +class LedgerImpl; +} + +namespace ledger { +namespace endpoint { +namespace uphold { + +using GetCardCallback = std::function; + +class GetCard { + public: + explicit GetCard(bat_ledger::LedgerImpl* ledger); + ~GetCard(); + + void Request( + const std::string& address, + const std::string& token, + GetCardCallback callback); + + private: + std::string GetUrl(const std::string& address); + + ledger::Result CheckStatusCode(const int status_code); + + ledger::Result ParseBody( + const std::string& body, + double* available); + + void OnRequest( + const ledger::UrlResponse& response, + GetCardCallback callback); + + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace uphold +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_GET_CARD_GET_CARD_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card_unittest.cc new file mode 100644 index 000000000000..9de1507006ea --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card_unittest.cc @@ -0,0 +1,173 @@ +/* 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 +#include +#include + +#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/endpoint/uphold/get_card/get_card.h" +#include "bat/ledger/ledger.h" +#include "net/http/http_status_code.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=GetCardTest.* + +using ::testing::_; +using ::testing::Invoke; + +namespace ledger { +namespace endpoint { +namespace uphold { + +class GetCardTest : public testing::Test { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr card_; + + GetCardTest() { + mock_ledger_client_ = std::make_unique(); + mock_ledger_impl_ = + std::make_unique(mock_ledger_client_.get()); + card_ = std::make_unique(mock_ledger_impl_.get()); + } +}; + +TEST_F(GetCardTest, ServerOK) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 200; + response.url = url; + response.body = R"({ + "CreatedByApplicationId": "193a77cf-02e8-4e10-8127-8a1b5a8bfece", + "address": { + "wire": "XXXXXXXXXX" + }, + "available": "4.00", + "balance": "4.00", + "currency": "BAT", + "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "label": "Brave Browser", + "lastTransactionAt": null, + "settings": { + "position": 1, + "protected": false, + "starred": true + }, + "createdByApplicationClientId": "4c2b665ca060d912fec5c735c734859a06118cc8", + "normalized": [ + { + "available": "0.00", + "balance": "0.00", + "currency": "USD" + } + ], + "wire": [ + { + "accountName": "Uphold Europe Limited", + "address": { + "line1": "Tartu mnt 2", + "line2": "10145 Tallinn, Estonia" + }, + "bic": "LHVBEE22", + "currency": "EUR", + "iban": "EE76 7700 7710 0159 0178", + "name": "AS LHV Pank" + }, + { + "accountName": "Uphold HQ, Inc.", + "accountNumber": "XXXXXXXXXX", + "address": { + "line1": "1359 Broadway", + "line2": "New York, NY 10018" + }, + "bic": "MCBEUS33", + "currency": "USD", + "name": "Metropolitan Bank", + "routingNumber": "XXXXXXXXX" + } + ] + })"; + callback(response); + })); + + card_->Request( + "193a77cf-02e8-4e10-8127-8a1b5a8bfece", + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const double available) { + EXPECT_EQ(result, ledger::Result::LEDGER_OK); + EXPECT_EQ(available, 4.0); + }); +} + +TEST_F(GetCardTest, ServerError401) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 401; + response.url = url; + response.body = ""; + callback(response); + })); + + card_->Request( + "193a77cf-02e8-4e10-8127-8a1b5a8bfece", + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const double available) { + EXPECT_EQ(result, ledger::Result::EXPIRED_TOKEN); + EXPECT_EQ(available, 0.0); + }); +} + +TEST_F(GetCardTest, ServerErrorRandom) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 453; + response.url = url; + response.body = ""; + callback(response); + })); + + card_->Request( + "193a77cf-02e8-4e10-8127-8a1b5a8bfece", + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const double available) { + EXPECT_EQ(result, ledger::Result::LEDGER_ERROR); + EXPECT_EQ(available, 0.0); + }); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.cc new file mode 100644 index 000000000000..c136cc578fc4 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.cc @@ -0,0 +1,116 @@ +/* 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/endpoint/uphold/get_cards/get_cards.h" + +#include "base/json/json_reader.h" +#include "base/strings/stringprintf.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" +#include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/uphold/uphold_card.h" +#include "net/http/http_status_code.h" + +using std::placeholders::_1; + +namespace ledger { +namespace endpoint { +namespace uphold { + +GetCards::GetCards(bat_ledger::LedgerImpl* ledger): + ledger_(ledger) { + DCHECK(ledger_); +} + +GetCards::~GetCards() = default; + +std::string GetCards::GetUrl() { + return GetServerUrl("/v0/me/cards?q=currency:BAT"); +} + +ledger::Result GetCards::CheckStatusCode(const int status_code) { + if (status_code == net::HTTP_UNAUTHORIZED) { + return ledger::Result::EXPIRED_TOKEN; + } + + if (status_code != net::HTTP_OK) { + return ledger::Result::LEDGER_ERROR; + } + + return ledger::Result::LEDGER_OK; +} + +ledger::Result GetCards::ParseBody( + const std::string& body, + std::string* id) { + DCHECK(id); + + base::Optional value = base::JSONReader::Read(body); + if (!value || !value->is_list()) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + base::ListValue* list = nullptr; + if (!value->GetAsList(&list)) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + for (const auto& it : list->GetList()) { + const auto* label = it.FindStringKey("label"); + if (!label) { + continue; + } + + if (*label == braveledger_uphold::kCardName) { + const auto* id_str = it.FindStringKey("id"); + if (!id_str) { + continue; + } + + *id = *id_str; + return ledger::Result::LEDGER_OK; + } + } + + return ledger::Result::LEDGER_ERROR; +} + +void GetCards::Request( + const std::string& token, + GetCardsCallback callback) { + auto url_callback = std::bind(&GetCards::OnRequest, + this, + _1, + callback); + ledger_->LoadURL( + GetUrl(), + RequestAuthorization(token), + "", + "", + ledger::UrlMethod::GET, + url_callback); +} + +void GetCards::OnRequest( + const ledger::UrlResponse& response, + GetCardsCallback callback) { + ledger::LogUrlResponse(__func__, response); + + ledger::Result result = CheckStatusCode(response.status_code); + + if (result != ledger::Result::LEDGER_OK) { + callback(result, ""); + return; + } + + std::string id; + result = ParseBody(response.body, &id); + callback(result, id); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.h new file mode 100644 index 000000000000..c6042a7825dc --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.h @@ -0,0 +1,115 @@ +/* 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_ENDPOINT_UPHOLD_GET_CARDS_GET_CARDS_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_GET_CARDS_GET_CARDS_H_ + +#include + +#include "bat/ledger/ledger.h" + +// GET https://api.uphold.com/v0/me/cards?q=currency:BAT +// +// Success code: +// HTTP_OK (200) +// +// Error codes: +// HTTP_UNAUTHORIZED (401) +// +// Response body: +// [ +// { +// "CreatedByApplicationId": null, +// "address": { +// "wire": "XXXXXXXXXX" +// }, +// "available": "12.35", +// "balance": "12.35", +// "currency": "BAT", +// "id": "3ed3b2c4-a715-4c01-b302-fa2681a971ea", +// "label": "Twitter - User - Brave Rewards", +// "lastTransactionAt": "2020-03-31T19:27:57.552Z", +// "settings": { +// "position": 7, +// "protected": false, +// "starred": true +// }, +// "normalized": [ +// { +// "available": "3.15", +// "balance": "3.15", +// "currency": "USD" +// } +// ], +// "wire": [ +// { +// "accountName": "Uphold Europe Limited", +// "address": { +// "line1": "Tartu mnt 2", +// "line2": "10145 Tallinn, Estonia" +// }, +// "bic": "LHVBEE22", +// "currency": "EUR", +// "iban": "EE76 7700 7710 0159 0178", +// "name": "AS LHV Pank" +// }, +// { +// "accountName": "Uphold HQ, Inc.", +// "accountNumber": "XXXXXXXXXX", +// "address": { +// "line1": "1359 Broadway", +// "line2": "New York, NY 10018" +// }, +// "bic": "MCBEUS33", +// "currency": "USD", +// "name": "Metropolitan Bank", +// "routingNumber": "XXXXXXXXX" +// } +// ] +// } +// ] + +namespace bat_ledger { +class LedgerImpl; +} + +namespace ledger { +namespace endpoint { +namespace uphold { + +using GetCardsCallback = std::function; + +class GetCards { + public: + explicit GetCards(bat_ledger::LedgerImpl* ledger); + ~GetCards(); + + void Request( + const std::string& token, + GetCardsCallback callback); + + private: + std::string GetUrl(); + + ledger::Result CheckStatusCode(const int status_code); + + ledger::Result ParseBody( + const std::string& body, + std::string* id); + + void OnRequest( + const ledger::UrlResponse& response, + GetCardsCallback callback); + + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace uphold +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_GET_CARDS_GET_CARDS_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards_unittest.cc new file mode 100644 index 000000000000..3db7a69333da --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards_unittest.cc @@ -0,0 +1,246 @@ +/* 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 +#include +#include + +#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/endpoint/uphold/get_cards/get_cards.h" +#include "bat/ledger/ledger.h" +#include "net/http/http_status_code.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=GetCardsTest.* + +using ::testing::_; +using ::testing::Invoke; + +namespace ledger { +namespace endpoint { +namespace uphold { + +class GetCardsTest : public testing::Test { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr card_; + + GetCardsTest() { + mock_ledger_client_ = std::make_unique(); + mock_ledger_impl_ = + std::make_unique(mock_ledger_client_.get()); + card_ = std::make_unique(mock_ledger_impl_.get()); + } +}; + +TEST_F(GetCardsTest, ServerOK) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 200; + response.url = url; + response.body = R"([ + { + "CreatedByApplicationId": null, + "address": { + "wire": "XXXXXXXXXX" + }, + "available": "12.35", + "balance": "12.35", + "currency": "BAT", + "id": "3ed3b2c4-a715-4c01-b302-fa2681a971ea", + "label": "Brave Browser", + "lastTransactionAt": "2020-03-31T19:27:57.552Z", + "settings": { + "position": 7, + "protected": false, + "starred": true + }, + "normalized": [ + { + "available": "3.15", + "balance": "3.15", + "currency": "USD" + } + ], + "wire": [ + { + "accountName": "Uphold Europe Limited", + "address": { + "line1": "Tartu mnt 2", + "line2": "10145 Tallinn, Estonia" + }, + "bic": "LHVBEE22", + "currency": "EUR", + "iban": "EE76 7700 7710 0159 0178", + "name": "AS LHV Pank" + }, + { + "accountName": "Uphold HQ, Inc.", + "accountNumber": "XXXXXXXXXX", + "address": { + "line1": "1359 Broadway", + "line2": "New York, NY 10018" + }, + "bic": "MCBEUS33", + "currency": "USD", + "name": "Metropolitan Bank", + "routingNumber": "XXXXXXXXX" + } + ] + } + ])"; + callback(response); + })); + + card_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::LEDGER_OK); + EXPECT_EQ(id, "3ed3b2c4-a715-4c01-b302-fa2681a971ea"); + }); +} + +TEST_F(GetCardsTest, CardNotFound) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 200; + response.url = url; + response.body = R"([ + { + "CreatedByApplicationId": null, + "address": { + "wire": "XXXXXXXXXX" + }, + "available": "12.35", + "balance": "12.35", + "currency": "BAT", + "id": "3ed3b2c4-a715-4c01-b302-fa2681a971ea", + "label": "Test Brave Browser", + "lastTransactionAt": "2020-03-31T19:27:57.552Z", + "settings": { + "position": 7, + "protected": false, + "starred": true + }, + "normalized": [ + { + "available": "3.15", + "balance": "3.15", + "currency": "USD" + } + ], + "wire": [ + { + "accountName": "Uphold Europe Limited", + "address": { + "line1": "Tartu mnt 2", + "line2": "10145 Tallinn, Estonia" + }, + "bic": "LHVBEE22", + "currency": "EUR", + "iban": "EE76 7700 7710 0159 0178", + "name": "AS LHV Pank" + }, + { + "accountName": "Uphold HQ, Inc.", + "accountNumber": "XXXXXXXXXX", + "address": { + "line1": "1359 Broadway", + "line2": "New York, NY 10018" + }, + "bic": "MCBEUS33", + "currency": "USD", + "name": "Metropolitan Bank", + "routingNumber": "XXXXXXXXX" + } + ] + } + ])"; + callback(response); + })); + + card_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::LEDGER_ERROR); + EXPECT_EQ(id, ""); + }); +} + +TEST_F(GetCardsTest, ServerError401) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 401; + response.url = url; + response.body = ""; + callback(response); + })); + + card_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::EXPIRED_TOKEN); + EXPECT_EQ(id, ""); + }); +} + +TEST_F(GetCardsTest, ServerErrorRandom) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 453; + response.url = url; + response.body = ""; + callback(response); + })); + + card_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::LEDGER_ERROR); + EXPECT_EQ(id, ""); + }); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me.cc new file mode 100644 index 000000000000..05ec4304de37 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me.cc @@ -0,0 +1,147 @@ +/* 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/endpoint/uphold/get_me/get_me.h" + +#include "base/json/json_reader.h" +#include "base/strings/stringprintf.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" +#include "bat/ledger/internal/ledger_impl.h" +#include "net/http/http_status_code.h" + +using std::placeholders::_1; + +namespace { + +braveledger_uphold::UserStatus GetUserStatus(const std::string& status) { + if (status == "pending") { + return braveledger_uphold::UserStatus::PENDING; + } + + if (status == "restricted") { + return braveledger_uphold::UserStatus::RESTRICTED; + } + + if (status == "blocked") { + return braveledger_uphold::UserStatus::BLOCKED; + } + + if (status == "ok") { + return braveledger_uphold::UserStatus::OK; + } + + return braveledger_uphold::UserStatus::EMPTY; +} + +} // namespace + +namespace ledger { +namespace endpoint { +namespace uphold { + +GetMe::GetMe(bat_ledger::LedgerImpl* ledger): + ledger_(ledger) { + DCHECK(ledger_); +} + +GetMe::~GetMe() = default; + +std::string GetMe::GetUrl() { + return GetServerUrl("/v0/me"); +} + +ledger::Result GetMe::CheckStatusCode(const int status_code) { + if (status_code == net::HTTP_UNAUTHORIZED) { + return ledger::Result::EXPIRED_TOKEN; + } + + if (status_code != net::HTTP_OK) { + return ledger::Result::LEDGER_ERROR; + } + + return ledger::Result::LEDGER_OK; +} + +ledger::Result GetMe::ParseBody( + const std::string& body, + braveledger_uphold::User* user) { + DCHECK(user); + + base::Optional value = base::JSONReader::Read(body); + if (!value || !value->is_dict()) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + base::DictionaryValue* dictionary = nullptr; + if (!value->GetAsDictionary(&dictionary)) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + const auto* name = dictionary->FindStringKey("firstName"); + if (name) { + user->name = *name; + } + + const auto* member_at = dictionary->FindStringKey("memberAt"); + if (member_at) { + user->member_at = *member_at; + user->verified = !user->member_at.empty(); + } + + const auto* currencies = dictionary->FindListKey("currencies"); + if (currencies) { + const std::string currency = "BAT"; + auto bat_in_list = std::find( + currencies->GetList().begin(), + currencies->GetList().end(), + base::Value(currency)); + user->bat_not_allowed = bat_in_list == currencies->GetList().end(); + } + + const auto* status = dictionary->FindStringKey("status"); + if (status) { + user->status = GetUserStatus(*status); + } + + return ledger::Result::LEDGER_OK; +} + +void GetMe::Request( + const std::string& token, + GetMeCallback callback) { + auto url_callback = std::bind(&GetMe::OnRequest, + this, + _1, + callback); + ledger_->LoadURL( + GetUrl(), + RequestAuthorization(token), + "", + "", + ledger::UrlMethod::GET, + url_callback); +} + +void GetMe::OnRequest( + const ledger::UrlResponse& response, + GetMeCallback callback) { + ledger::LogUrlResponse(__func__, response, true); + + braveledger_uphold::User user; + ledger::Result result = CheckStatusCode(response.status_code); + if (result != ledger::Result::LEDGER_OK) { + callback(result, user); + return; + } + + result = ParseBody(response.body, &user); + callback(result, user); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me.h new file mode 100644 index 000000000000..f634227cf7e0 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me.h @@ -0,0 +1,153 @@ +/* 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_ENDPOINT_UPHOLD_GET_ME_GET_ME_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_GET_ME_GET_ME_H_ + +#include + +#include "bat/ledger/internal/uphold/uphold_user.h" +#include "bat/ledger/ledger.h" + +// GET https://api.uphold.com/v0/me +// +// Success code: +// HTTP_OK (200) +// +// Error codes: +// HTTP_UNAUTHORIZED (401) +// +// Response body: +// { +// "address": { +// "city": "Anytown", +// "line1": "123 Main Street", +// "zipCode": "12345" +// }, +// "birthdate": "1971-06-22", +// "country": "US", +// "email": "john@example.com", +// "firstName": "John", +// "fullName": "John Smith", +// "id": "b34060c9-5ca3-4bdb-bc32-1f826ecea36e", +// "identityCountry": "US", +// "lastName": "Smith", +// "name": "John Smith", +// "settings": { +// "currency": "USD", +// "hasMarketingConsent": false, +// "hasNewsSubscription": false, +// "intl": { +// "dateTimeFormat": { +// "locale": "en-US" +// }, +// "language": { +// "locale": "en-US" +// }, +// "numberFormat": { +// "locale": "en-US" +// } +// }, +// "otp": { +// "login": { +// "enabled": true +// }, +// "transactions": { +// "transfer": { +// "enabled": false +// }, +// "send": { +// "enabled": true +// }, +// "withdraw": { +// "crypto": { +// "enabled": true +// } +// } +// } +// }, +// "theme": "vintage" +// }, +// "memberAt": "2019-07-27T11:32:33.310Z", +// "state": "US-MA", +// "status": "ok", +// "type": "individual", +// "username": null, +// "verifications": { +// "termsEquities": { +// "status": "required" +// } +// }, +// "balances": { +// "available": "3.15", +// "currencies": { +// "BAT": { +// "amount": "3.15", +// "balance": "12.35", +// "currency": "USD", +// "rate": "0.25521" +// } +// }, +// "pending": "0.00", +// "total": "3.15" +// }, +// "currencies": [ +// "BAT" +// ], +// "phones": [ +// { +// "e164Masked": "+XXXXXXXXX83", +// "id": "8037c7ed-fe5a-4ad2-abfd-7c941f066cab", +// "internationalMasked": "+X XXX-XXX-XX83", +// "nationalMasked": "(XXX) XXX-XX83", +// "primary": false, +// "verified": false +// } +// ], +// "tier": "other" +// } + +namespace bat_ledger { +class LedgerImpl; +} + +namespace ledger { +namespace endpoint { +namespace uphold { + +using GetMeCallback = std::function; + +class GetMe { + public: + explicit GetMe(bat_ledger::LedgerImpl* ledger); + ~GetMe(); + + void Request( + const std::string& token, + GetMeCallback callback); + + private: + std::string GetUrl(); + + ledger::Result CheckStatusCode(const int status_code); + + ledger::Result ParseBody( + const std::string& body, + braveledger_uphold::User* user); + + void OnRequest( + const ledger::UrlResponse& response, + GetMeCallback callback); + + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace uphold +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_GET_ME_GET_ME_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me_unittest.cc new file mode 100644 index 000000000000..d77c31b79bc9 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me_unittest.cc @@ -0,0 +1,215 @@ +/* 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 +#include +#include + +#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/endpoint/uphold/get_me/get_me.h" +#include "bat/ledger/ledger.h" +#include "net/http/http_status_code.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=GetMeTest.* + +using ::testing::_; +using ::testing::Invoke; + +namespace ledger { +namespace endpoint { +namespace uphold { + +class GetMeTest : public testing::Test { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr me_; + + GetMeTest() { + mock_ledger_client_ = std::make_unique(); + mock_ledger_impl_ = + std::make_unique(mock_ledger_client_.get()); + me_ = std::make_unique(mock_ledger_impl_.get()); + } +}; + +TEST_F(GetMeTest, ServerOK) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 200; + response.url = url; + response.body = R"({ + "address": { + "city": "Anytown", + "line1": "123 Main Street", + "zipCode": "12345" + }, + "birthdate": "1971-06-22", + "country": "US", + "email": "john@example.com", + "firstName": "John", + "fullName": "John Smith", + "id": "b34060c9-5ca3-4bdb-bc32-1f826ecea36e", + "identityCountry": "US", + "lastName": "Smith", + "name": "John Smith", + "settings": { + "currency": "USD", + "hasMarketingConsent": false, + "hasNewsSubscription": false, + "intl": { + "dateTimeFormat": { + "locale": "en-US" + }, + "language": { + "locale": "en-US" + }, + "numberFormat": { + "locale": "en-US" + } + }, + "otp": { + "login": { + "enabled": true + }, + "transactions": { + "transfer": { + "enabled": false + }, + "send": { + "enabled": true + }, + "withdraw": { + "crypto": { + "enabled": true + } + } + } + }, + "theme": "vintage" + }, + "memberAt": "2019-07-27T11:32:33.310Z", + "state": "US-MA", + "status": "ok", + "type": "individual", + "username": null, + "verifications": { + "termsEquities": { + "status": "required" + } + }, + "balances": { + "available": "3.15", + "currencies": { + "BAT": { + "amount": "3.15", + "balance": "12.35", + "currency": "USD", + "rate": "0.25521" + } + }, + "pending": "0.00", + "total": "3.15" + }, + "currencies": [ + "BAT" + ], + "phones": [ + { + "e164Masked": "+XXXXXXXXX83", + "id": "8037c7ed-fe5a-4ad2-abfd-7c941f066cab", + "internationalMasked": "+X XXX-XXX-XX83", + "nationalMasked": "(XXX) XXX-XX83", + "primary": false, + "verified": false + } + ], + "tier": "other" + })"; + callback(response); + })); + + braveledger_uphold::User expected_user; + + + me_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [&](const ledger::Result result, const braveledger_uphold::User& user) { + EXPECT_EQ(result, ledger::Result::LEDGER_OK); + EXPECT_EQ(user.name, "John"); + EXPECT_EQ(user.member_at, "2019-07-27T11:32:33.310Z"); + EXPECT_EQ(user.verified, true); + EXPECT_EQ(user.bat_not_allowed, false); + EXPECT_EQ(user.status, braveledger_uphold::UserStatus::OK); + }); +} + +TEST_F(GetMeTest, ServerError401) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 401; + response.url = url; + response.body = ""; + callback(response); + })); + + braveledger_uphold::User expected_user; + me_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [&](const ledger::Result result, const braveledger_uphold::User& user) { + EXPECT_EQ(result, ledger::Result::EXPIRED_TOKEN); + }); +} + +TEST_F(GetMeTest, ServerErrorRandom) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 453; + response.url = url; + response.body = ""; + callback(response); + })); + + braveledger_uphold::User expected_user; + me_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [&](const ledger::Result result, const braveledger_uphold::User& user) { + EXPECT_EQ(result, ledger::Result::LEDGER_ERROR); + }); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.cc new file mode 100644 index 000000000000..99ae9a458145 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.cc @@ -0,0 +1,92 @@ +/* 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/endpoint/uphold/patch_card/patch_card.h" + +#include + +#include "base/json/json_writer.h" +#include "base/strings/stringprintf.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" +#include "bat/ledger/internal/ledger_impl.h" +#include "net/http/http_status_code.h" + +using std::placeholders::_1; + +namespace ledger { +namespace endpoint { +namespace uphold { + +PatchCard::PatchCard(bat_ledger::LedgerImpl* ledger): + ledger_(ledger) { + DCHECK(ledger_); +} + +PatchCard::~PatchCard() = default; + +std::string PatchCard::GetUrl(const std::string& address) { + return GetServerUrl("/v0/me/cards/" + address); +} + +std::string PatchCard::GeneratePayload( + const braveledger_uphold::UpdateCard& card) { + base::Value payload(base::Value::Type::DICTIONARY); + + if (!card.label.empty()) { + payload.SetStringKey("label", card.label); + } + + base::Value settings(base::Value::Type::DICTIONARY); + if (card.position > -1) { + settings.SetIntKey("position", card.position); + } + settings.SetBoolKey("starred", card.starred); + payload.SetKey("settings", std::move(settings)); + + std::string json; + base::JSONWriter::Write(payload, &json); + return json; +} + +ledger::Result PatchCard::CheckStatusCode(const int status_code) { + if (status_code == net::HTTP_UNAUTHORIZED) { + return ledger::Result::EXPIRED_TOKEN; + } + + if (status_code != net::HTTP_OK) { + return ledger::Result::LEDGER_ERROR; + } + + return ledger::Result::LEDGER_OK; +} + +void PatchCard::Request( + const std::string& token, + const std::string& address, + const braveledger_uphold::UpdateCard& card, + PatchCardCallback callback) { + auto url_callback = std::bind(&PatchCard::OnRequest, + this, + _1, + callback); + ledger_->LoadURL( + GetUrl(address), + RequestAuthorization(token), + GeneratePayload(card), + "application/json; charset=utf-8", + ledger::UrlMethod::PATCH, + url_callback); +} + +void PatchCard::OnRequest( + const ledger::UrlResponse& response, + PatchCardCallback callback) { + ledger::LogUrlResponse(__func__, response); + callback(CheckStatusCode(response.status_code)); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.h new file mode 100644 index 000000000000..ba1650eac4d3 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.h @@ -0,0 +1,122 @@ +/* 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_ENDPOINT_UPHOLD_PATCH_CARD_PATCH_CARD_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_PATCH_CARD_PATCH_CARD_H_ + +#include + +#include "bat/ledger/internal/uphold/uphold_card.h" +#include "bat/ledger/ledger.h" + +// PATCH https://api.uphold.com/v0/me/cards/{wallet_address} +// +// Request body: +// { +// "label": "Brave Browser", +// "settings": { +// "position": -1, +// "starred": true +// } +// } +// +// Success code: +// HTTP_OK (200) +// +// Error codes: +// HTTP_UNAUTHORIZED (401) +// +// Response body: +// { +// "CreatedByApplicationId": "193a77cf-02e8-4e10-8127-8a1b5a8bfece", +// "address": { +// "wire": "XXXXXXXXXX" +// }, +// "available": "0.00", +// "balance": "0.00", +// "currency": "BAT", +// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", +// "label": "Brave Browser", +// "lastTransactionAt": null, +// "settings": { +// "position": 8, +// "protected": false, +// "starred": false +// }, +// "createdByApplicationClientId": "4c2b665ca060d912fec5c735c734859a06118cc8", +// "normalized": [ +// { +// "available": "0.00", +// "balance": "0.00", +// "currency": "USD" +// } +// ], +// "wire": [ +// { +// "accountName": "Uphold Europe Limited", +// "address": { +// "line1": "Tartu mnt 2", +// "line2": "10145 Tallinn, Estonia" +// }, +// "bic": "LHVBEE22", +// "currency": "EUR", +// "iban": "EE76 7700 7710 0159 0178", +// "name": "AS LHV Pank" +// }, +// { +// "accountName": "Uphold HQ, Inc.", +// "accountNumber": "XXXXXXXXXX", +// "address": { +// "line1": "1359 Broadway", +// "line2": "New York, NY 10018" +// }, +// "bic": "MCBEUS33", +// "currency": "USD", +// "name": "Metropolitan Bank", +// "routingNumber": "XXXXXXXXX" +// } +// ] +// } + +namespace bat_ledger { +class LedgerImpl; +} + +namespace ledger { +namespace endpoint { +namespace uphold { + +using PatchCardCallback = std::function; + +class PatchCard { + public: + explicit PatchCard(bat_ledger::LedgerImpl* ledger); + ~PatchCard(); + + void Request( + const std::string& token, + const std::string& address, + const braveledger_uphold::UpdateCard& card, + PatchCardCallback callback); + + private: + std::string GetUrl(const std::string& address); + + std::string GeneratePayload(const braveledger_uphold::UpdateCard& card); + + ledger::Result CheckStatusCode(const int status_code); + + void OnRequest( + const ledger::UrlResponse& response, + PatchCardCallback callback); + + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace uphold +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_PATCH_CARD_PATCH_CARD_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card_unittest.cc new file mode 100644 index 000000000000..66b700f527f2 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card_unittest.cc @@ -0,0 +1,185 @@ +/* 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 +#include +#include + +#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/endpoint/uphold/patch_card/patch_card.h" +#include "bat/ledger/ledger.h" +#include "net/http/http_status_code.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=PatchCardTest.* + +using ::testing::_; +using ::testing::Invoke; + +namespace ledger { +namespace endpoint { +namespace uphold { + +class PatchCardTest : public testing::Test { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr card_; + + PatchCardTest() { + mock_ledger_client_ = std::make_unique(); + mock_ledger_impl_ = + std::make_unique(mock_ledger_client_.get()); + card_ = std::make_unique(mock_ledger_impl_.get()); + } +}; + +TEST_F(PatchCardTest, ServerOK) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 200; + response.url = url; + response.body = R"({ + "CreatedByApplicationId": "193a77cf-02e8-4e10-8127-8a1b5a8bfece", + "address": { + "wire": "XXXXXXXXXX" + }, + "available": "0.00", + "balance": "0.00", + "currency": "BAT", + "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "label": "Brave Browser", + "lastTransactionAt": null, + "settings": { + "position": 8, + "protected": false, + "starred": false + }, + "createdByApplicationClientId": "4c2b665ca060d912fec5c735c734859a06118cc8", + "normalized": [ + { + "available": "0.00", + "balance": "0.00", + "currency": "USD" + } + ], + "wire": [ + { + "accountName": "Uphold Europe Limited", + "address": { + "line1": "Tartu mnt 2", + "line2": "10145 Tallinn, Estonia" + }, + "bic": "LHVBEE22", + "currency": "EUR", + "iban": "EE76 7700 7710 0159 0178", + "name": "AS LHV Pank" + }, + { + "accountName": "Uphold HQ, Inc.", + "accountNumber": "XXXXXXXXXX", + "address": { + "line1": "1359 Broadway", + "line2": "New York, NY 10018" + }, + "bic": "MCBEUS33", + "currency": "USD", + "name": "Metropolitan Bank", + "routingNumber": "XXXXXXXXX" + } + ] + })"; + callback(response); + })); + + braveledger_uphold::UpdateCard card; + card.label = "Brave Browser"; + card.position = -1; + card.starred = true; + card_->Request( + "193a77cf-02e8-4e10-8127-8a1b5a8bfece", + "4c2b665ca060d912fec5c735c734859a06118cc8", + card, + [](const ledger::Result result) { + EXPECT_EQ(result, ledger::Result::LEDGER_OK); + }); +} + +TEST_F(PatchCardTest, ServerError401) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 401; + response.url = url; + response.body = ""; + callback(response); + })); + + braveledger_uphold::UpdateCard card; + card.label = "Brave Browser"; + card.position = -1; + card.starred = true; + card_->Request( + "193a77cf-02e8-4e10-8127-8a1b5a8bfece", + "4c2b665ca060d912fec5c735c734859a06118cc8", + card, + [](const ledger::Result result) { + EXPECT_EQ(result, ledger::Result::EXPIRED_TOKEN); + }); +} + +TEST_F(PatchCardTest, ServerErrorRandom) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 453; + response.url = url; + response.body = ""; + callback(response); + })); + + braveledger_uphold::UpdateCard card; + card.label = "Brave Browser"; + card.position = -1; + card.starred = true; + card_->Request( + "193a77cf-02e8-4e10-8127-8a1b5a8bfece", + "4c2b665ca060d912fec5c735c734859a06118cc8", + card, + [](const ledger::Result result) { + EXPECT_EQ(result, ledger::Result::LEDGER_ERROR); + }); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.cc new file mode 100644 index 000000000000..faf7f41943fd --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.cc @@ -0,0 +1,117 @@ +/* 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/endpoint/uphold/post_cards/post_cards.h" + +#include "base/json/json_reader.h" +#include "base/json/json_writer.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" +#include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/uphold/uphold_card.h" +#include "net/http/http_status_code.h" + +using std::placeholders::_1; + +namespace ledger { +namespace endpoint { +namespace uphold { + +PostCards::PostCards(bat_ledger::LedgerImpl* ledger): + ledger_(ledger) { + DCHECK(ledger_); +} + +PostCards::~PostCards() = default; + +std::string PostCards::GetUrl() { + return GetServerUrl("/v0/me/cards"); +} + +std::string PostCards::GeneratePayload() { + base::Value payload(base::Value::Type::DICTIONARY); + payload.SetStringKey("label", braveledger_uphold::kCardName); + payload.SetStringKey("currency", "BAT"); + + std::string json; + base::JSONWriter::Write(payload, &json); + return json; +} + +ledger::Result PostCards::CheckStatusCode(const int status_code) { + if (status_code == net::HTTP_UNAUTHORIZED) { + return ledger::Result::EXPIRED_TOKEN; + } + + if (status_code != net::HTTP_OK) { + return ledger::Result::LEDGER_ERROR; + } + + return ledger::Result::LEDGER_OK; +} + +ledger::Result PostCards::ParseBody( + const std::string& body, + std::string* id) { + DCHECK(id); + + base::Optional value = base::JSONReader::Read(body); + if (!value || !value->is_dict()) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + base::DictionaryValue* dictionary = nullptr; + if (!value->GetAsDictionary(&dictionary)) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + const auto* id_str = dictionary->FindStringKey("id"); + if (!id_str) { + BLOG(0, "Missing id"); + return ledger::Result::LEDGER_ERROR; + } + + *id = *id_str; + + return ledger::Result::LEDGER_OK; +} + +void PostCards::Request( + const std::string& token, + PostCardsCallback callback) { + auto url_callback = std::bind(&PostCards::OnRequest, + this, + _1, + callback); + ledger_->LoadURL( + GetUrl(), + RequestAuthorization(token), + GeneratePayload(), + "application/json; charset=utf-8", + ledger::UrlMethod::POST, + url_callback); +} + +void PostCards::OnRequest( + const ledger::UrlResponse& response, + PostCardsCallback callback) { + ledger::LogUrlResponse(__func__, response, true); + + ledger::Result result = CheckStatusCode(response.status_code); + + if (result != ledger::Result::LEDGER_OK) { + callback(result, ""); + return; + } + + std::string id; + result = ParseBody(response.body, &id); + callback(result, id); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.h new file mode 100644 index 000000000000..d092a89f960d --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.h @@ -0,0 +1,122 @@ +/* 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_ENDPOINT_UPHOLD_POST_CARDS_POST_CARDS_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_POST_CARDS_POST_CARDS_H_ + +#include + +#include "bat/ledger/ledger.h" + +// POST https://api.uphold.com/v0/me/cards +// +// Request body: +// { +// "label": "Brave Browser", +// "currency": "BAT" +// } +// +// Success code: +// HTTP_OK (200) +// +// Error codes: +// HTTP_UNAUTHORIZED (401) +// +// Response body: +// { +// "CreatedByApplicationId": "193a77cf-02e8-4e10-8127-8a1b5a8bfece", +// "address": { +// "wire": "XXXXXXXXXX" +// }, +// "available": "0.00", +// "balance": "0.00", +// "currency": "BAT", +// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", +// "label": "Brave Browser", +// "lastTransactionAt": null, +// "settings": { +// "position": 8, +// "protected": false, +// "starred": false +// }, +// "createdByApplicationClientId": "4c2b665ca060d912fec5c735c734859a06118cc8", +// "normalized": [ +// { +// "available": "0.00", +// "balance": "0.00", +// "currency": "USD" +// } +// ], +// "wire": [ +// { +// "accountName": "Uphold Europe Limited", +// "address": { +// "line1": "Tartu mnt 2", +// "line2": "10145 Tallinn, Estonia" +// }, +// "bic": "LHVBEE22", +// "currency": "EUR", +// "iban": "EE76 7700 7710 0159 0178", +// "name": "AS LHV Pank" +// }, +// { +// "accountName": "Uphold HQ, Inc.", +// "accountNumber": "XXXXXXXXXX", +// "address": { +// "line1": "1359 Broadway", +// "line2": "New York, NY 10018" +// }, +// "bic": "MCBEUS33", +// "currency": "USD", +// "name": "Metropolitan Bank", +// "routingNumber": "XXXXXXXXX" +// } +// ] +// } + +namespace bat_ledger { +class LedgerImpl; +} + +namespace ledger { +namespace endpoint { +namespace uphold { + +using PostCardsCallback = std::function; + +class PostCards { + public: + explicit PostCards(bat_ledger::LedgerImpl* ledger); + ~PostCards(); + + void Request( + const std::string& token, + PostCardsCallback callback); + + private: + std::string GetUrl(); + + std::string GeneratePayload(); + + ledger::Result CheckStatusCode(const int status_code); + + ledger::Result ParseBody( + const std::string& body, + std::string* id); + + void OnRequest( + const ledger::UrlResponse& response, + PostCardsCallback callback); + + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace uphold +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_POST_CARDS_POST_CARDS_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards_unittest.cc new file mode 100644 index 000000000000..9791da944188 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards_unittest.cc @@ -0,0 +1,170 @@ +/* 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 +#include +#include + +#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/endpoint/uphold/post_cards/post_cards.h" +#include "bat/ledger/ledger.h" +#include "net/http/http_status_code.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=PostCardsTest.* + +using ::testing::_; +using ::testing::Invoke; + +namespace ledger { +namespace endpoint { +namespace uphold { + +class PostCardsTest : public testing::Test { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr card_; + + PostCardsTest() { + mock_ledger_client_ = std::make_unique(); + mock_ledger_impl_ = + std::make_unique(mock_ledger_client_.get()); + card_ = std::make_unique(mock_ledger_impl_.get()); + } +}; + +TEST_F(PostCardsTest, ServerOK) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 200; + response.url = url; + response.body = R"({ + "CreatedByApplicationId": "193a77cf-02e8-4e10-8127-8a1b5a8bfece", + "address": { + "wire": "XXXXXXXXXX" + }, + "available": "0.00", + "balance": "0.00", + "currency": "BAT", + "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "label": "Brave Browser", + "lastTransactionAt": null, + "settings": { + "position": 8, + "protected": false, + "starred": false + }, + "createdByApplicationClientId": "4c2b665ca060d912fec5c735c734859a06118cc8", + "normalized": [ + { + "available": "0.00", + "balance": "0.00", + "currency": "USD" + } + ], + "wire": [ + { + "accountName": "Uphold Europe Limited", + "address": { + "line1": "Tartu mnt 2", + "line2": "10145 Tallinn, Estonia" + }, + "bic": "LHVBEE22", + "currency": "EUR", + "iban": "EE76 7700 7710 0159 0178", + "name": "AS LHV Pank" + }, + { + "accountName": "Uphold HQ, Inc.", + "accountNumber": "XXXXXXXXXX", + "address": { + "line1": "1359 Broadway", + "line2": "New York, NY 10018" + }, + "bic": "MCBEUS33", + "currency": "USD", + "name": "Metropolitan Bank", + "routingNumber": "XXXXXXXXX" + } + ] + })"; + callback(response); + })); + + card_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::LEDGER_OK); + EXPECT_EQ(id, "bd91a720-f3f9-42f8-b2f5-19548004f6a7"); + }); +} + +TEST_F(PostCardsTest, ServerError401) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 401; + response.url = url; + response.body = ""; + callback(response); + })); + + card_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::EXPIRED_TOKEN); + EXPECT_EQ(id, ""); + }); +} + +TEST_F(PostCardsTest, ServerErrorRandom) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 453; + response.url = url; + response.body = ""; + callback(response); + })); + + card_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::LEDGER_ERROR); + EXPECT_EQ(id, ""); + }); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.cc new file mode 100644 index 000000000000..e2ec17e9a309 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.cc @@ -0,0 +1,112 @@ +/* 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/endpoint/uphold/post_oauth/post_oauth.h" + +#include "base/json/json_reader.h" +#include "base/strings/stringprintf.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" +#include "bat/ledger/internal/ledger_impl.h" +#include "net/http/http_status_code.h" + +using std::placeholders::_1; + +namespace ledger { +namespace endpoint { +namespace uphold { + +PostOauth::PostOauth(bat_ledger::LedgerImpl* ledger): + ledger_(ledger) { + DCHECK(ledger_); +} + +PostOauth::~PostOauth() = default; + +std::string PostOauth::GetUrl() { + return GetServerUrl("/oauth2/token"); +} + +std::string PostOauth::GeneratePayload(const std::string& code) { + return base::StringPrintf( + "code=%s&grant_type=authorization_code", + code.c_str()); +} + +ledger::Result PostOauth::CheckStatusCode(const int status_code) { + if (status_code == net::HTTP_UNAUTHORIZED) { + return ledger::Result::EXPIRED_TOKEN; + } + + if (status_code != net::HTTP_OK) { + return ledger::Result::LEDGER_ERROR; + } + + return ledger::Result::LEDGER_OK; +} + +ledger::Result PostOauth::ParseBody( + const std::string& body, + std::string* token) { + DCHECK(token); + + base::Optional value = base::JSONReader::Read(body); + if (!value || !value->is_dict()) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + base::DictionaryValue* dictionary = nullptr; + if (!value->GetAsDictionary(&dictionary)) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + const auto* access_token = dictionary->FindStringKey("access_token"); + if (!access_token) { + BLOG(0, "Missing access token"); + return ledger::Result::LEDGER_ERROR; + } + + *token = *access_token; + + return ledger::Result::LEDGER_OK; +} + +void PostOauth::Request( + const std::string& code, + PostOauthCallback callback) { + auto url_callback = std::bind(&PostOauth::OnRequest, + this, + _1, + callback); + ledger_->LoadURL( + GetUrl(), + RequestAuthorization(), + GeneratePayload(code), + "application/x-www-form-urlencoded", + ledger::UrlMethod::POST, + url_callback); +} + +void PostOauth::OnRequest( + const ledger::UrlResponse& response, + PostOauthCallback callback) { + ledger::LogUrlResponse(__func__, response, true); + + ledger::Result result = CheckStatusCode(response.status_code); + + if (result != ledger::Result::LEDGER_OK) { + callback(result, ""); + return; + } + + std::string token; + result = ParseBody(response.body, &token); + callback(result, token); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.h new file mode 100644 index 000000000000..4936b434ad9d --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.h @@ -0,0 +1,75 @@ +/* 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_ENDPOINT_UPHOLD_POST_OAUTH_POST_OAUTH_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_POST_OAUTH_POST_OAUTH_H_ + +#include + +#include "bat/ledger/ledger.h" + +// POST https://api.uphold.com/oauth2/token +// +// Request body: +// code=wewfwkfpkwpfkwofkwpofk&grant_type=authorization_code +// +// Success code: +// HTTP_OK (200) +// +// Error codes: +// HTTP_UNAUTHORIZED (401) +// +// Response body: +// { +// "access_token": "edc8b465fe2e2a26ce553d937ccc6c7195e9f909", +// "token_type": "bearer", +// "expires_in": 7775999, +// "scope": "accounts:read accounts:write cards:read cards:write user:read" +// } + +namespace bat_ledger { +class LedgerImpl; +} + +namespace ledger { +namespace endpoint { +namespace uphold { + +using PostOauthCallback = std::function; + +class PostOauth { + public: + explicit PostOauth(bat_ledger::LedgerImpl* ledger); + ~PostOauth(); + + void Request( + const std::string& code, + PostOauthCallback callback); + + private: + std::string GetUrl(); + + std::string GeneratePayload(const std::string& code); + + ledger::Result CheckStatusCode(const int status_code); + + ledger::Result ParseBody( + const std::string& body, + std::string* token); + + void OnRequest( + const ledger::UrlResponse& response, + PostOauthCallback callback); + + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace uphold +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_POST_OAUTH_POST_OAUTH_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth_unittest.cc new file mode 100644 index 000000000000..1009b4fe4f55 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth_unittest.cc @@ -0,0 +1,126 @@ +/* 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 +#include +#include + +#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/endpoint/uphold/post_oauth/post_oauth.h" +#include "bat/ledger/ledger.h" +#include "net/http/http_status_code.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=PostOauthTest.* + +using ::testing::_; +using ::testing::Invoke; + +namespace ledger { +namespace endpoint { +namespace uphold { + +class PostOauthTest : public testing::Test { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr oauth_; + + PostOauthTest() { + mock_ledger_client_ = std::make_unique(); + mock_ledger_impl_ = + std::make_unique(mock_ledger_client_.get()); + oauth_ = std::make_unique(mock_ledger_impl_.get()); + } +}; + +TEST_F(PostOauthTest, ServerOK) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 200; + response.url = url; + response.body = R"({ + "access_token": "edc8b465fe2e2a26ce553d937ccc6c7195e9f909", + "token_type": "bearer", + "expires_in": 7775999, + "scope": "accounts:read accounts:write cards:read cards:write" + })"; + callback(response); + })); + + oauth_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& token) { + EXPECT_EQ(result, ledger::Result::LEDGER_OK); + EXPECT_EQ(token, "edc8b465fe2e2a26ce553d937ccc6c7195e9f909"); + }); +} + +TEST_F(PostOauthTest, ServerError401) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 401; + response.url = url; + response.body = ""; + callback(response); + })); + + oauth_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& token) { + EXPECT_EQ(result, ledger::Result::EXPIRED_TOKEN); + EXPECT_EQ(token, ""); + }); +} + +TEST_F(PostOauthTest, ServerErrorRandom) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 453; + response.url = url; + response.body = ""; + callback(response); + })); + + oauth_->Request( + "4c2b665ca060d912fec5c735c734859a06118cc8", + [](const ledger::Result result, const std::string& token) { + EXPECT_EQ(result, ledger::Result::LEDGER_ERROR); + EXPECT_EQ(token, ""); + }); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.cc new file mode 100644 index 000000000000..9ba74bd83dcb --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.cc @@ -0,0 +1,131 @@ +/* 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/endpoint/uphold/post_transaction/post_transaction.h" + +#include + +#include "base/json/json_reader.h" +#include "base/json/json_writer.h" +#include "base/strings/stringprintf.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" +#include "bat/ledger/internal/ledger_impl.h" +#include "net/http/http_status_code.h" + +using std::placeholders::_1; + +namespace ledger { +namespace endpoint { +namespace uphold { + +PostTransaction::PostTransaction(bat_ledger::LedgerImpl* ledger): + ledger_(ledger) { + DCHECK(ledger_); +} + +PostTransaction::~PostTransaction() = default; + +std::string PostTransaction::GetUrl(const std::string& address) { + const std::string path = base::StringPrintf( + "/v0/me/cards/%s/transactions", + address.c_str()); + + return GetServerUrl(path); +} + +std::string PostTransaction::GeneratePayload( + const braveledger_uphold::Transaction& transaction) { + base::Value denomination(base::Value::Type::DICTIONARY); + denomination.SetDoubleKey("amount", transaction.amount); + denomination.SetStringKey("currency", "BAT"); + + base::Value payload(base::Value::Type::DICTIONARY); + payload.SetStringKey("destination", transaction.address); + payload.SetStringKey("message", transaction.message); + payload.SetKey("denomination", std::move(denomination)); + + std::string json; + base::JSONWriter::Write(payload, &json); + return json; +} + +ledger::Result PostTransaction::CheckStatusCode(const int status_code) { + if (status_code == net::HTTP_UNAUTHORIZED) { + return ledger::Result::EXPIRED_TOKEN; + } + + if (status_code != net::HTTP_ACCEPTED) { + return ledger::Result::LEDGER_ERROR; + } + + return ledger::Result::LEDGER_OK; +} + +ledger::Result PostTransaction::ParseBody( + const std::string& body, + std::string* id) { + DCHECK(id); + + base::Optional value = base::JSONReader::Read(body); + if (!value || !value->is_dict()) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + base::DictionaryValue* dictionary = nullptr; + if (!value->GetAsDictionary(&dictionary)) { + BLOG(0, "Invalid JSON"); + return ledger::Result::LEDGER_ERROR; + } + + const auto* id_str = dictionary->FindStringKey("id"); + if (!id_str) { + BLOG(0, "Missing id"); + return ledger::Result::LEDGER_ERROR; + } + + *id = *id_str; + + return ledger::Result::LEDGER_OK; +} + +void PostTransaction::Request( + const std::string& token, + const std::string& address, + const braveledger_uphold::Transaction& transaction, + PostTransactionCallback callback) { + auto url_callback = std::bind(&PostTransaction::OnRequest, + this, + _1, + callback); + ledger_->LoadURL( + GetUrl(address), + RequestAuthorization(token), + GeneratePayload(transaction), + "application/json; charset=utf-8", + ledger::UrlMethod::POST, + url_callback); +} + +void PostTransaction::OnRequest( + const ledger::UrlResponse& response, + PostTransactionCallback callback) { + ledger::LogUrlResponse(__func__, response); + + ledger::Result result = CheckStatusCode(response.status_code); + + if (result != ledger::Result::LEDGER_OK) { + callback(result, ""); + return; + } + + std::string id; + result = ParseBody(response.body, &id); + callback(result, id); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.h new file mode 100644 index 000000000000..880f1b7d6578 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.h @@ -0,0 +1,153 @@ +/* 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_ENDPOINT_UPHOLD_POST_TRANSACTION_POST_TRANSACTION_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_POST_TRANSACTION_POST_TRANSACTION_H_ + +#include + +#include "bat/ledger/internal/uphold/uphold.h" +#include "bat/ledger/ledger.h" + +// POST https://api.uphold.com/v0/me/cards/{wallet_address}/transactions +// +// Request body: +// { +// "denomination": { +// "amount": 1.0, +// "currency: "BAT +// } +// "destination": "f5e37294-68f1-49ae-89e2-b24b64aedd37", +// "message": "Hi" +// } +// +// Success code: +// HTTP_ACCEPTED (202) +// +// Error codes: +// HTTP_UNAUTHORIZED (401) +// +// Response body: +// { +// "createdAt": "2020-06-10T18:58:21.683Z", +// "denomination": { +// "amount": "1.00", +// "currency": "BAT", +// "pair": "BATBAT", +// "rate": "1.00" +// }, +// "fees": [], +// "id": "d382d3ae-8462-4b2c-9b60-b669539f41b2", +// "network": "uphold", +// "normalized": [ +// { +// "commission": "0.00", +// "currency": "USD", +// "fee": "0.00", +// "rate": "0.24688", +// "target": "origin", +// "amount": "0.25" +// } +// ], +// "params": { +// "currency": "BAT", +// "margin": "0.00", +// "pair": "BATBAT", +// "rate": "1.00", +// "ttl": 3599588, +// "type": "internal" +// }, +// "priority": "normal", +// "status": "pending", +// "type": "transfer", +// "destination": { +// "amount": "1.00", +// "base": "1.00", +// "commission": "0.00", +// "currency": "BAT", +// "description": "Brave Software International", +// "fee": "0.00", +// "isMember": true, +// "node": { +// "id": "6654ecb0-6079-4f6c-ba58-791cc890a561", +// "type": "card", +// "user": { +// "id": "f5e37294-68f1-49ae-89e2-b24b64aedd37", +// "username": "braveintl" +// } +// }, +// "rate": "1.00", +// "type": "card", +// "username": "braveintl" +// }, +// "origin": { +// "amount": "1.00", +// "base": "1.00", +// "CardId": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", +// "commission": "0.00", +// "currency": "BAT", +// "description": "User", +// "fee": "0.00", +// "isMember": true, +// "node": { +// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", +// "type": "card", +// "user": { +// "id": "b34060c9-5ca3-4bdb-bc32-1f826ecea36e" +// } +// }, +// "rate": "1.00", +// "sources": [], +// "type": "card" +// } +// } + +namespace bat_ledger { +class LedgerImpl; +} + +namespace ledger { +namespace endpoint { +namespace uphold { + +using PostTransactionCallback = std::function; + +class PostTransaction { + public: + explicit PostTransaction(bat_ledger::LedgerImpl* ledger); + ~PostTransaction(); + + void Request( + const std::string& token, + const std::string& address, + const braveledger_uphold::Transaction& transaction, + PostTransactionCallback callback); + + private: + std::string GetUrl(const std::string& address); + + std::string GeneratePayload( + const braveledger_uphold::Transaction& transaction); + + ledger::Result CheckStatusCode(const int status_code); + + ledger::Result ParseBody( + const std::string& body, + std::string* id); + + void OnRequest( + const ledger::UrlResponse& response, + PostTransactionCallback callback); + + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace uphold +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_POST_TRANSACTION_POST_TRANSACTION_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction_unittest.cc new file mode 100644 index 000000000000..4294f66839ea --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction_unittest.cc @@ -0,0 +1,211 @@ +/* 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 +#include +#include + +#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/endpoint/uphold/post_transaction/post_transaction.h" +#include "bat/ledger/ledger.h" +#include "net/http/http_status_code.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=PostTransactionTest.* + +using ::testing::_; +using ::testing::Invoke; + +namespace ledger { +namespace endpoint { +namespace uphold { + +class PostTransactionTest : public testing::Test { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr transaction_; + + PostTransactionTest() { + mock_ledger_client_ = std::make_unique(); + mock_ledger_impl_ = + std::make_unique(mock_ledger_client_.get()); + transaction_ = std::make_unique(mock_ledger_impl_.get()); + } +}; + +TEST_F(PostTransactionTest, ServerOK) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 202; + response.url = url; + response.body = R"({ + "createdAt": "2020-06-10T18:58:21.683Z", + "denomination": { + "amount": "1.00", + "currency": "BAT", + "pair": "BATBAT", + "rate": "1.00" + }, + "fees": [], + "id": "d382d3ae-8462-4b2c-9b60-b669539f41b2", + "network": "uphold", + "normalized": [ + { + "commission": "0.00", + "currency": "USD", + "fee": "0.00", + "rate": "0.24688", + "target": "origin", + "amount": "0.25" + } + ], + "params": { + "currency": "BAT", + "margin": "0.00", + "pair": "BATBAT", + "rate": "1.00", + "ttl": 3599588, + "type": "internal" + }, + "priority": "normal", + "status": "pending", + "type": "transfer", + "destination": { + "amount": "1.00", + "base": "1.00", + "commission": "0.00", + "currency": "BAT", + "description": "Brave Software International", + "fee": "0.00", + "isMember": true, + "node": { + "id": "6654ecb0-6079-4f6c-ba58-791cc890a561", + "type": "card", + "user": { + "id": "f5e37294-68f1-49ae-89e2-b24b64aedd37", + "username": "braveintl" + } + }, + "rate": "1.00", + "type": "card", + "username": "braveintl" + }, + "origin": { + "amount": "1.00", + "base": "1.00", + "CardId": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "commission": "0.00", + "currency": "BAT", + "description": "User", + "fee": "0.00", + "isMember": true, + "node": { + "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "type": "card", + "user": { + "id": "b34060c9-5ca3-4bdb-bc32-1f826ecea36e" + } + }, + "rate": "1.00", + "sources": [], + "type": "card" + } + })"; + callback(response); + })); + + braveledger_uphold::Transaction transaction; + transaction.amount = 1.0; + transaction.address = "6654ecb0-6079-4f6c-ba58-791cc890a561"; + + transaction_->Request( + "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "4c2b665ca060d912fec5c735c734859a06118cc8", + transaction, + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::LEDGER_OK); + EXPECT_EQ(id, "d382d3ae-8462-4b2c-9b60-b669539f41b2"); + }); +} + +TEST_F(PostTransactionTest, ServerError401) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 401; + response.url = url; + response.body = ""; + callback(response); + })); + + braveledger_uphold::Transaction transaction; + transaction.amount = 1.0; + transaction.address = "6654ecb0-6079-4f6c-ba58-791cc890a561"; + + transaction_->Request( + "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "4c2b665ca060d912fec5c735c734859a06118cc8", + transaction, + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::EXPIRED_TOKEN); + EXPECT_EQ(id, ""); + }); +} + +TEST_F(PostTransactionTest, ServerErrorRandom) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 453; + response.url = url; + response.body = ""; + callback(response); + })); + + braveledger_uphold::Transaction transaction; + transaction.amount = 1.0; + transaction.address = "6654ecb0-6079-4f6c-ba58-791cc890a561"; + + transaction_->Request( + "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "4c2b665ca060d912fec5c735c734859a06118cc8", + transaction, + [](const ledger::Result result, const std::string& id) { + EXPECT_EQ(result, ledger::Result::LEDGER_ERROR); + EXPECT_EQ(id, ""); + }); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.cc new file mode 100644 index 000000000000..bbd50245c7ca --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.cc @@ -0,0 +1,78 @@ +/* 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/endpoint/uphold/post_transaction_commit/post_transaction_commit.h" + +#include + +#include "base/strings/stringprintf.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" +#include "bat/ledger/internal/ledger_impl.h" +#include "net/http/http_status_code.h" + +using std::placeholders::_1; + +namespace ledger { +namespace endpoint { +namespace uphold { + +PostTransactionCommit::PostTransactionCommit(bat_ledger::LedgerImpl* ledger): + ledger_(ledger) { + DCHECK(ledger_); +} + +PostTransactionCommit::~PostTransactionCommit() = default; + +std::string PostTransactionCommit::GetUrl( + const std::string& address, + const std::string& transaction_id) { + const std::string path = base::StringPrintf( + "/v0/me/cards/%s/transactions/%s/commit", + address.c_str(), + transaction_id.c_str()); + + return GetServerUrl(path); +} + +ledger::Result PostTransactionCommit::CheckStatusCode(const int status_code) { + if (status_code == net::HTTP_UNAUTHORIZED) { + return ledger::Result::EXPIRED_TOKEN; + } + + if (status_code != net::HTTP_OK) { + return ledger::Result::LEDGER_ERROR; + } + + return ledger::Result::LEDGER_OK; +} + +void PostTransactionCommit::Request( + const std::string& token, + const std::string& address, + const std::string& transaction_id, + PostTransactionCommitCallback callback) { + auto url_callback = std::bind(&PostTransactionCommit::OnRequest, + this, + _1, + callback); + ledger_->LoadURL( + GetUrl(address, transaction_id), + RequestAuthorization(token), + "", + "application/json; charset=utf-8", + ledger::UrlMethod::POST, + url_callback); +} + +void PostTransactionCommit::OnRequest( + const ledger::UrlResponse& response, + PostTransactionCommitCallback callback) { + ledger::LogUrlResponse(__func__, response); + callback(CheckStatusCode(response.status_code)); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.h new file mode 100644 index 000000000000..572f038ecc6c --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.h @@ -0,0 +1,150 @@ +/* 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_ENDPOINT_UPHOLD_POST_TRANSACTION_COMMIT_\ +POST_TRANSACTION_COMMIT_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_POST_TRANSACTION_COMMIT_\ +POST_TRANSACTION_COMMIT_H_ + +#include + +#include "bat/ledger/ledger.h" + +// POST https://api.uphold.com/v0/me/cards/{wallet_address}/transactions/{transaction_id}/commit //NOLINT +// +// Success code: +// HTTP_OK (200) +// +// Error codes: +// HTTP_UNAUTHORIZED (401) +// +// Response body: +// { +// "application": { +// "name": "Brave Browser" +// }, +// "createdAt": "2020-06-10T18:58:22.351Z", +// "denomination": { +// "pair": "BATBAT", +// "rate": "1.00", +// "amount": "1.00", +// "currency": "BAT" +// }, +// "fees": [], +// "id": "d382d3ae-8462-4b2c-9b60-b669539f41b2", +// "message": null, +// "network": "uphold", +// "normalized": [ +// { +// "fee": "0.00", +// "rate": "0.24688", +// "amount": "0.25", +// "target": "origin", +// "currency": "USD", +// "commission": "0.00" +// } +// ], +// "params": { +// "currency": "BAT", +// "margin": "0.00", +// "pair": "BATBAT", +// "progress": "1", +// "rate": "1.00", +// "ttl": 3599588, +// "type": "internal" +// }, +// "priority": "normal", +// "reference": null, +// "status": "completed", +// "type": "transfer", +// "destination": { +// "amount": "1.00", +// "base": "1.00", +// "commission": "0.00", +// "currency": "BAT", +// "description": "Brave Software International", +// "fee": "0.00", +// "isMember": true, +// "node": { +// "id": "6654ecb0-6079-4f6c-ba58-791cc890a561", +// "type": "card", +// "user": { +// "id": "f5e37294-68f1-49ae-89e2-b24b64aedd37", +// "username": "braveintl" +// } +// }, +// "rate": "1.00", +// "type": "card", +// "username": "braveintl" +// }, +// "origin": { +// "amount": "1.00", +// "base": "1.00", +// "CardId": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", +// "commission": "0.00", +// "currency": "BAT", +// "description": "User", +// "fee": "0.00", +// "isMember": true, +// "node": { +// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", +// "type": "card", +// "user": { +// "id": "b34060c9-5ca3-4bdb-bc32-1f826ecea36e" +// } +// }, +// "rate": "1.00", +// "sources": [ +// { +// "id": "463dca02-83ec-4bd6-93b0-73bf5dbe35ac", +// "amount": "1.00" +// } +// ], +// "type": "card" +// } +// } + +namespace bat_ledger { +class LedgerImpl; +} + +namespace ledger { +namespace endpoint { +namespace uphold { + +using PostTransactionCommitCallback = std::function; + +class PostTransactionCommit { + public: + explicit PostTransactionCommit(bat_ledger::LedgerImpl* ledger); + ~PostTransactionCommit(); + + void Request( + const std::string& token, + const std::string& address, + const std::string& transaction_id, + PostTransactionCommitCallback callback); + + private: + std::string GetUrl( + const std::string& address, + const std::string& transaction_id); + + ledger::Result CheckStatusCode(const int status_code); + + void OnRequest( + const ledger::UrlResponse& response, + PostTransactionCommitCallback callback); + + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace uphold +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_POST_TRANSACTION_COMMIT_\ +// POST_TRANSACTION_COMMIT_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit_unittest.cc new file mode 100644 index 000000000000..688c08c934ea --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit_unittest.cc @@ -0,0 +1,208 @@ +/* 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 +#include +#include + +#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/endpoint/uphold/post_transaction_commit/post_transaction_commit.h" +#include "bat/ledger/ledger.h" +#include "net/http/http_status_code.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=PostTransactionCommitTest.* + +using ::testing::_; +using ::testing::Invoke; + +namespace ledger { +namespace endpoint { +namespace uphold { + +class PostTransactionCommitTest : public testing::Test { + private: + base::test::TaskEnvironment scoped_task_environment_; + + protected: + std::unique_ptr mock_ledger_client_; + std::unique_ptr mock_ledger_impl_; + std::unique_ptr transaction_; + + PostTransactionCommitTest() { + mock_ledger_client_ = std::make_unique(); + mock_ledger_impl_ = + std::make_unique(mock_ledger_client_.get()); + transaction_ = + std::make_unique(mock_ledger_impl_.get()); + } +}; + +TEST_F(PostTransactionCommitTest, ServerOK) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 200; + response.url = url; + response.body = R"({ + "application": { + "name": "Brave Browser" + }, + "createdAt": "2020-06-10T18:58:22.351Z", + "denomination": { + "pair": "BATBAT", + "rate": "1.00", + "amount": "1.00", + "currency": "BAT" + }, + "fees": [], + "id": "d382d3ae-8462-4b2c-9b60-b669539f41b2", + "message": null, + "network": "uphold", + "normalized": [ + { + "fee": "0.00", + "rate": "0.24688", + "amount": "0.25", + "target": "origin", + "currency": "USD", + "commission": "0.00" + } + ], + "params": { + "currency": "BAT", + "margin": "0.00", + "pair": "BATBAT", + "progress": "1", + "rate": "1.00", + "ttl": 3599588, + "type": "internal" + }, + "priority": "normal", + "reference": null, + "status": "completed", + "type": "transfer", + "destination": { + "amount": "1.00", + "base": "1.00", + "commission": "0.00", + "currency": "BAT", + "description": "Brave Software International", + "fee": "0.00", + "isMember": true, + "node": { + "id": "6654ecb0-6079-4f6c-ba58-791cc890a561", + "type": "card", + "user": { + "id": "f5e37294-68f1-49ae-89e2-b24b64aedd37", + "username": "braveintl" + } + }, + "rate": "1.00", + "type": "card", + "username": "braveintl" + }, + "origin": { + "amount": "1.00", + "base": "1.00", + "CardId": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "commission": "0.00", + "currency": "BAT", + "description": "User", + "fee": "0.00", + "isMember": true, + "node": { + "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "type": "card", + "user": { + "id": "b34060c9-5ca3-4bdb-bc32-1f826ecea36e" + } + }, + "rate": "1.00", + "sources": [ + { + "id": "463dca02-83ec-4bd6-93b0-73bf5dbe35ac", + "amount": "1.00" + } + ], + "type": "card" + } + })"; + callback(response); + })); + + transaction_->Request( + "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "4c2b665ca060d912fec5c735c734859a06118cc8", + "6654ecb0-6079-4f6c-ba58-791cc890a561", + [](const ledger::Result result) { + EXPECT_EQ(result, ledger::Result::LEDGER_OK); + }); +} + +TEST_F(PostTransactionCommitTest, ServerError401) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 401; + response.url = url; + response.body = ""; + callback(response); + })); + + transaction_->Request( + "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "4c2b665ca060d912fec5c735c734859a06118cc8", + "6654ecb0-6079-4f6c-ba58-791cc890a561", + [](const ledger::Result result) { + EXPECT_EQ(result, ledger::Result::EXPIRED_TOKEN); + }); +} + +TEST_F(PostTransactionCommitTest, ServerErrorRandom) { + ON_CALL(*mock_ledger_client_, LoadURL(_, _, _, _, _, _)) + .WillByDefault( + Invoke([]( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& contentType, + const ledger::UrlMethod method, + ledger::LoadURLCallback callback) { + ledger::UrlResponse response; + response.status_code = 453; + response.url = url; + response.body = ""; + callback(response); + })); + + transaction_->Request( + "bd91a720-f3f9-42f8-b2f5-19548004f6a7", + "4c2b665ca060d912fec5c735c734859a06118cc8", + "6654ecb0-6079-4f6c-ba58-791cc890a561", + [](const ledger::Result result) { + EXPECT_EQ(result, ledger::Result::LEDGER_ERROR); + }); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_server.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_server.cc new file mode 100644 index 000000000000..1864015786e2 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_server.cc @@ -0,0 +1,60 @@ +/* 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/endpoint/uphold/uphold_server.h" + +#include "bat/ledger/internal/ledger_impl.h" + +namespace ledger { +namespace endpoint { + +UpholdServer::UpholdServer(bat_ledger::LedgerImpl* ledger): + post_oauth_(std::make_unique(ledger)), + get_cards_(std::make_unique(ledger)), + get_card_(std::make_unique(ledger)), + get_me_(std::make_unique(ledger)), + post_cards_(std::make_unique(ledger)), + patch_card_(std::make_unique(ledger)), + post_transaction_(std::make_unique(ledger)), + post_transaction_commit_( + std::make_unique(ledger)) { +} + +UpholdServer::~UpholdServer() = default; + +uphold::PostOauth* UpholdServer::post_oauth() const { + return post_oauth_.get(); +} + +uphold::GetCards* UpholdServer::get_cards() const { + return get_cards_.get(); +} + +uphold::GetCard* UpholdServer::get_card() const { + return get_card_.get(); +} + +uphold::GetMe* UpholdServer::get_me() const { + return get_me_.get(); +} + +uphold::PostCards* UpholdServer::post_cards() const { + return post_cards_.get(); +} + +uphold::PatchCard* UpholdServer::patch_card() const { + return patch_card_.get(); +} + +uphold::PostTransaction* UpholdServer::post_transaction() const { + return post_transaction_.get(); +} + +uphold::PostTransactionCommit* UpholdServer::post_transaction_commit() const { + return post_transaction_commit_.get(); +} + +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_server.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_server.h new file mode 100644 index 000000000000..e8acd2e4ccfd --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_server.h @@ -0,0 +1,63 @@ +/* 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_ENDPOINT_UPHOLD_UPHOLD_SERVER_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_UPHOLD_SERVER_H_ + +#include + +#include "bat/ledger/internal/endpoint/uphold/get_card/get_card.h" +#include "bat/ledger/internal/endpoint/uphold/get_cards/get_cards.h" +#include "bat/ledger/internal/endpoint/uphold/get_me/get_me.h" +#include "bat/ledger/internal/endpoint/uphold/patch_card/patch_card.h" +#include "bat/ledger/internal/endpoint/uphold/post_cards/post_cards.h" +#include "bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.h" +#include "bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.h" +#include "bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.h" +#include "bat/ledger/ledger.h" + +namespace bat_ledger { +class LedgerImpl; +} + +namespace ledger { +namespace endpoint { + +class UpholdServer { + public: + explicit UpholdServer(bat_ledger::LedgerImpl* ledger); + ~UpholdServer(); + + uphold::PostOauth* post_oauth() const; + + uphold::GetCards* get_cards() const; + + uphold::GetCard* get_card() const; + + uphold::GetMe* get_me() const; + + uphold::PostCards* post_cards() const; + + uphold::PatchCard* patch_card() const; + + uphold::PostTransaction* post_transaction() const; + + uphold::PostTransactionCommit* post_transaction_commit() const; + + private: + std::unique_ptr post_oauth_; + std::unique_ptr get_cards_; + std::unique_ptr get_card_; + std::unique_ptr get_me_; + std::unique_ptr post_cards_; + std::unique_ptr patch_card_; + std::unique_ptr post_transaction_; + std::unique_ptr post_transaction_commit_; +}; + +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_UPHOLD_SERVER_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils.cc new file mode 100644 index 000000000000..bd60c8319e7b --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils.cc @@ -0,0 +1,68 @@ +/* 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/endpoint/uphold/uphold_utils.h" + +#include "base/base64.h" +#include "base/strings/stringprintf.h" +#include "bat/ledger/internal/uphold/uphold_util.h" +#include "bat/ledger/ledger.h" + +namespace ledger { +namespace endpoint { +namespace uphold { + +const char kStaging[] = "https://api-sandbox.uphold.com"; +const char kProduction[] = "https://api.uphold.com"; +const char kClientSecretStaging[] = "67bf87da096748c5bc1e195cfbdd59db006618a0"; +const char kClientSecretProduction[] = + "de1aa4196c8d4aa50c6bc1371734e3f57f781f72"; + +std::string GetClientSecret() { + return ledger::_environment == ledger::Environment::PRODUCTION + ? kClientSecretProduction + : kClientSecretStaging; +} + +std::vector RequestAuthorization(const std::string& token) { + std::vector headers; + + if (!token.empty()) { + headers.push_back("Authorization: Bearer " + token); + return headers; + } + + const std::string id = braveledger_uphold::GetClientId(); + const std::string secret = GetClientSecret(); + + std::string user; + base::Base64Encode( + base::StringPrintf( + "%s:%s", + id.c_str(), + secret.c_str()), + &user); + + headers.push_back("Authorization: Basic " + user); + + return headers; +} + +std::string GetServerUrl(const std::string& path) { + DCHECK(!path.empty()); + + std::string url; + if (ledger::_environment == ledger::Environment::PRODUCTION) { + url = kProduction; + } else { + url = kStaging; + } + + return url + path; +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils.h b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils.h new file mode 100644 index 000000000000..9e43bf224949 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils.h @@ -0,0 +1,26 @@ +/* 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_ENDPOINT_UPHOLD_UPHOLD_UTILS_H_ +#define BRAVELEDGER_ENDPOINT_UPHOLD_UPHOLD_UTILS_H_ + +#include +#include + +namespace ledger { +namespace endpoint { +namespace uphold { + +std::string GetClientSecret(); + +std::vector RequestAuthorization(const std::string& token = ""); + +std::string GetServerUrl(const std::string& path); + +} // namespace uphold +} // namespace endpoint +} // namespace ledger + +#endif // BRAVELEDGER_ENDPOINT_UPHOLD_UPHOLD_UTILS_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils_unittest.cc new file mode 100644 index 000000000000..422f3c6197d1 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils_unittest.cc @@ -0,0 +1,40 @@ +/* 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/global_constants.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" +#include "bat/ledger/ledger.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=UpholdUtilsTest.* + +namespace ledger { +namespace endpoint { +namespace uphold { + +class UpholdUtilsTest : public testing::Test { +}; + +TEST(UpholdUtilsTest, GetServerUrlDevelopment) { + ledger::_environment = ledger::Environment::DEVELOPMENT; + const std::string url = GetServerUrl("/test"); + ASSERT_EQ(url, "https://api-sandbox.uphold.com/test"); +} + +TEST(UpholdUtilsTest, GetServerUrlStaging) { + ledger::_environment = ledger::Environment::STAGING; + const std::string url = GetServerUrl("/test"); + ASSERT_EQ(url, "https://api-sandbox.uphold.com/test"); +} + +TEST(UpholdUtilsTest, GetServerUrlProduction) { + ledger::_environment = ledger::Environment::PRODUCTION; + const std::string url = GetServerUrl("/test"); + ASSERT_EQ(url, "https://api.uphold.com/test"); +} + +} // namespace uphold +} // namespace endpoint +} // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_uphold.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_uphold.cc deleted file mode 100644 index f36eb3bbeccf..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_uphold.cc +++ /dev/null @@ -1,906 +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/response/response_uphold.h" - -#include - -#include "base/json/json_reader.h" -#include "base/strings/string_number_conversions.h" -#include "bat/ledger/internal/logging/logging.h" -#include "net/http/http_status_code.h" - -namespace { - -braveledger_uphold::UserStatus GetUserStatus(const std::string& status) { - if (status == "pending") { - return braveledger_uphold::UserStatus::PENDING; - } - - if (status == "restricted") { - return braveledger_uphold::UserStatus::RESTRICTED; - } - - if (status == "blocked") { - return braveledger_uphold::UserStatus::BLOCKED; - } - - if (status == "ok") { - return braveledger_uphold::UserStatus::OK; - } - - return braveledger_uphold::UserStatus::EMPTY; -} - -} // namespace - -namespace braveledger_response_util { - -// Request Url: -// GET https://api.uphold.com/v0/me/cards/{wallet_address} -// -// Success: -// OK (200) -// -// Response Format: -// { -// "CreatedByApplicationId": "193a77cf-02e8-4e10-8127-8a1b5a8bfece", -// "address": { -// "wire": "XXXXXXXXXX" -// }, -// "available": "0.00", -// "balance": "0.00", -// "currency": "BAT", -// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", -// "label": "Brave Browser", -// "lastTransactionAt": null, -// "settings": { -// "position": 1, -// "protected": false, -// "starred": true -// }, -// "createdByApplicationClientId": "4c2b665ca060d912fec5c735c734859a06118cc8", -// "normalized": [ -// { -// "available": "0.00", -// "balance": "0.00", -// "currency": "USD" -// } -// ], -// "wire": [ -// { -// "accountName": "Uphold Europe Limited", -// "address": { -// "line1": "Tartu mnt 2", -// "line2": "10145 Tallinn, Estonia" -// }, -// "bic": "LHVBEE22", -// "currency": "EUR", -// "iban": "EE76 7700 7710 0159 0178", -// "name": "AS LHV Pank" -// }, -// { -// "accountName": "Uphold HQ, Inc.", -// "accountNumber": "XXXXXXXXXX", -// "address": { -// "line1": "1359 Broadway", -// "line2": "New York, NY 10018" -// }, -// "bic": "MCBEUS33", -// "currency": "USD", -// "name": "Metropolitan Bank", -// "routingNumber": "XXXXXXXXX" -// } -// ] -// } - -ledger::Result ParseFetchUpholdBalance( - const ledger::UrlResponse& response, - double* available) { - DCHECK(available); - - if (response.status_code == net::HTTP_UNAUTHORIZED || - response.status_code == net::HTTP_NOT_FOUND || - response.status_code == net::HTTP_FORBIDDEN) { - return ledger::Result::EXPIRED_TOKEN; - } - - if (response.status_code != net::HTTP_OK) { - return ledger::Result::LEDGER_ERROR; - } - - base::Optional value = base::JSONReader::Read(response.body); - if (!value || !value->is_dict()) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - base::DictionaryValue* dictionary = nullptr; - if (!value->GetAsDictionary(&dictionary)) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - const auto* available_str = dictionary->FindStringKey("available"); - if (!available_str) { - BLOG(0, "Missing available"); - return ledger::Result::LEDGER_ERROR; - } - - const bool success = base::StringToDouble(*available_str, available); - if (!success) { - *available = 0.0; - } - - return ledger::Result::LEDGER_OK; -} - -// Request Url: -// POST https://api.uphold.com/oauth2/token -// -// Success: -// OK (200) -// -// Response Format: -// { -// "access_token": "edc8b465fe2e2a26ce553d937ccc6c7195e9f909", -// "token_type": "bearer", -// "expires_in": 7775999, -// "scope": "accounts:read accounts:write cards:read cards:write user:read" -// } - -ledger::Result ParseUpholdAuthorization( - const ledger::UrlResponse& response, - std::string* token) { - DCHECK(token); - - if (response.status_code == net::HTTP_UNAUTHORIZED) { - return ledger::Result::EXPIRED_TOKEN; - } - - if (response.status_code != net::HTTP_OK) { - return ledger::Result::LEDGER_ERROR; - } - - base::Optional value = base::JSONReader::Read(response.body); - if (!value || !value->is_dict()) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - base::DictionaryValue* dictionary = nullptr; - if (!value->GetAsDictionary(&dictionary)) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - const auto* access_token = dictionary->FindStringKey("access_token"); - if (!access_token) { - BLOG(0, "Missing access token"); - return ledger::Result::LEDGER_ERROR; - } - - *token = *access_token; - - return ledger::Result::LEDGER_OK; -} - -// Request Url: -// GET https://api.uphold.com/v0/me -// -// Success: -// OK (200) -// -// Response Format: -// { -// "address": { -// "city": "Anytown", -// "line1": "123 Main Street", -// "zipCode": "12345" -// }, -// "birthdate": "1971-06-22", -// "country": "US", -// "email": "erogul@example.com", -// "firstName": "Emerick", -// "fullName": "Emerick Rogul", -// "id": "b34060c9-5ca3-4bdb-bc32-1f826ecea36e", -// "identityCountry": "US", -// "lastName": "Rogul", -// "name": "Emerick Rogul", -// "settings": { -// "currency": "USD", -// "hasMarketingConsent": false, -// "hasNewsSubscription": false, -// "intl": { -// "dateTimeFormat": { -// "locale": "en-US" -// }, -// "language": { -// "locale": "en-US" -// }, -// "numberFormat": { -// "locale": "en-US" -// } -// }, -// "otp": { -// "login": { -// "enabled": true -// }, -// "transactions": { -// "transfer": { -// "enabled": false -// }, -// "send": { -// "enabled": true -// }, -// "withdraw": { -// "crypto": { -// "enabled": true -// } -// } -// } -// }, -// "theme": "vintage" -// }, -// "memberAt": "2019-07-27T11:32:33.310Z", -// "state": "US-MA", -// "status": "ok", -// "type": "individual", -// "username": null, -// "verifications": { -// "termsEquities": { -// "status": "required" -// } -// }, -// "balances": { -// "available": "3.15", -// "currencies": { -// "BAT": { -// "amount": "3.15", -// "balance": "12.35", -// "currency": "USD", -// "rate": "0.25521" -// } -// }, -// "pending": "0.00", -// "total": "3.15" -// }, -// "currencies": [ -// "BAT" -// ], -// "phones": [ -// { -// "e164Masked": "+XXXXXXXXX83", -// "id": "8037c7ed-fe5a-4ad2-abfd-7c941f066cab", -// "internationalMasked": "+X XXX-XXX-XX83", -// "nationalMasked": "(XXX) XXX-XX83", -// "primary": false, -// "verified": false -// } -// ], -// "tier": "other" -// } - -ledger::Result ParseUpholdGetUser( - const ledger::UrlResponse& response, - braveledger_uphold::User* user) { - DCHECK(user); - - if (response.status_code == net::HTTP_UNAUTHORIZED) { - return ledger::Result::EXPIRED_TOKEN; - } - - if (response.status_code != net::HTTP_OK) { - return ledger::Result::LEDGER_ERROR; - } - - base::Optional value = base::JSONReader::Read(response.body); - if (!value || !value->is_dict()) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - base::DictionaryValue* dictionary = nullptr; - if (!value->GetAsDictionary(&dictionary)) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - const auto* name = dictionary->FindStringKey("firstName"); - if (name) { - user->name = *name; - } - - const auto* member_at = dictionary->FindStringKey("memberAt"); - if (member_at) { - user->member_at = *member_at; - user->verified = !user->member_at.empty(); - } - - const auto* currencies = dictionary->FindListKey("currencies"); - if (currencies) { - const std::string currency = "BAT"; - auto bat_in_list = std::find( - currencies->GetList().begin(), - currencies->GetList().end(), - base::Value(currency)); - user->bat_not_allowed = bat_in_list == currencies->GetList().end(); - } - - const auto* status = dictionary->FindStringKey("status"); - if (status) { - user->status = GetUserStatus(*status); - } - - return ledger::Result::LEDGER_OK; -} - -// Request Url: -// GET https://api.uphold.com/v0/me/cards/{wallet_address}/addresses -// -// Success: -// OK (200) -// -// Response Format: -// [ -// { -// "formats": [ -// { -// "format": "uuid", -// "value": "d3f67620-abda-4a6f-8d60-b16914341688" -// } -// ], -// "type": "anonymous" -// } -// ] - -ledger::Result ParseUpholdGetCardAddresses( - const ledger::UrlResponse& response, - std::map* addresses) { - DCHECK(addresses); - - if (response.status_code == net::HTTP_UNAUTHORIZED) { - return ledger::Result::EXPIRED_TOKEN; - } - - if (response.status_code != net::HTTP_OK) { - return ledger::Result::LEDGER_ERROR; - } - - base::Optional dictionary = - base::JSONReader::Read(response.body); - if (!dictionary || !dictionary->is_list()) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - base::ListValue* addresses_list = nullptr; - if (!dictionary->GetAsList(&addresses_list)) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - for (auto& address_item : *addresses_list) { - base::DictionaryValue* address = nullptr; - if (!address_item.GetAsDictionary(&address)) { - continue; - } - - const auto* type_key = address->FindStringKey("type"); - if (!type_key) { - continue; - } - const std::string type = *type_key; - - auto* formats = address->FindListKey("formats"); - if (!formats) { - continue; - } - - if (formats->GetList().size() == 0) { - continue; - } - - base::DictionaryValue* format = nullptr; - if (!formats->GetList()[0].GetAsDictionary(&format)) { - continue; - } - - const auto* address_value = format->FindStringKey("value"); - if (!address_value) { - continue; - } - - addresses->insert(std::make_pair(type, *address_value)); - } - - return ledger::Result::LEDGER_OK; -} - -// Request Url: -// PATCH https://api.uphold.com/v0/me/cards/{wallet_address} -// POST https://api.uphold.com/v0/me/cards/{wallet_address}/addresses -// -// Success: -// OK (200) -// -// Response Format (success): -// { -// "id": "d3f67620-abda-4a6f-8d60-b16914341688", -// "network": "anonymous" -// } -// -// Response Format (error): -// { -// "code": "validation_failed", -// "errors": { -// "network": [ -// { -// "code": "invalid", -// "message": "This value is not valid" -// } -// ] -// } -// } - -ledger::Result ParseUpholdCreateCard( - const ledger::UrlResponse& response, - std::string* id) { - DCHECK(id); - - if (response.status_code == net::HTTP_UNAUTHORIZED) { - return ledger::Result::EXPIRED_TOKEN; - } - - if (response.status_code != net::HTTP_OK) { - return ledger::Result::LEDGER_ERROR; - } - - base::Optional value = base::JSONReader::Read(response.body); - if (!value || !value->is_dict()) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - base::DictionaryValue* dictionary = nullptr; - if (!value->GetAsDictionary(&dictionary)) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - const auto* id_str = dictionary->FindStringKey("id"); - if (!id_str) { - BLOG(0, "Missing id"); - return ledger::Result::LEDGER_ERROR; - } - - *id = *id_str; - - return ledger::Result::LEDGER_OK; -} - -// Request Url: -// GET https://api.uphold.com/v0/me/cards?q=currency:BAT -// -// Success: -// OK (200) -// -// Response Format: -// [ -// { -// "CreatedByApplicationId": null, -// "address": { -// "wire": "XXXXXXXXXX" -// }, -// "available": "12.35", -// "balance": "12.35", -// "currency": "BAT", -// "id": "3ed3b2c4-a715-4c01-b302-fa2681a971ea", -// "label": "Twitter - Emerick Rogul - Brave Rewards", -// "lastTransactionAt": "2020-03-31T19:27:57.552Z", -// "settings": { -// "position": 7, -// "protected": false, -// "starred": true -// }, -// "normalized": [ -// { -// "available": "3.15", -// "balance": "3.15", -// "currency": "USD" -// } -// ], -// "wire": [ -// { -// "accountName": "Uphold Europe Limited", -// "address": { -// "line1": "Tartu mnt 2", -// "line2": "10145 Tallinn, Estonia" -// }, -// "bic": "LHVBEE22", -// "currency": "EUR", -// "iban": "EE76 7700 7710 0159 0178", -// "name": "AS LHV Pank" -// }, -// { -// "accountName": "Uphold HQ, Inc.", -// "accountNumber": "XXXXXXXXXX", -// "address": { -// "line1": "1359 Broadway", -// "line2": "New York, NY 10018" -// }, -// "bic": "MCBEUS33", -// "currency": "USD", -// "name": "Metropolitan Bank", -// "routingNumber": "XXXXXXXXX" -// } -// ] -// } -// ] - -ledger::Result ParseUpholdGetCards( - const ledger::UrlResponse& response, - const std::string& card_name, - std::string* id) { - DCHECK(id); - - if (response.status_code == net::HTTP_UNAUTHORIZED) { - return ledger::Result::EXPIRED_TOKEN; - } - - if (response.status_code != net::HTTP_OK) { - return ledger::Result::LEDGER_ERROR; - } - - base::Optional value = base::JSONReader::Read(response.body); - if (!value || !value->is_list()) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - base::ListValue* list = nullptr; - if (!value->GetAsList(&list)) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - for (const auto& it : list->GetList()) { - const auto* label = it.FindStringKey("label"); - if (!label) { - continue; - } - - if (*label == card_name) { - const auto* id_str = it.FindStringKey("id"); - if (!id_str) { - continue; - } - - *id = *id_str; - - return ledger::Result::LEDGER_OK; - } - } - - return ledger::Result::LEDGER_ERROR; -} - -// Request Url: -// POST https://api.uphold.com/v0/me/cards -// -// Success: -// OK (200) -// -// Response Format: -// { -// "CreatedByApplicationId": "193a77cf-02e8-4e10-8127-8a1b5a8bfece", -// "address": { -// "wire": "XXXXXXXXXX" -// }, -// "available": "0.00", -// "balance": "0.00", -// "currency": "BAT", -// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", -// "label": "Brave Browser", -// "lastTransactionAt": null, -// "settings": { -// "position": 8, -// "protected": false, -// "starred": false -// }, -// "createdByApplicationClientId": "4c2b665ca060d912fec5c735c734859a06118cc8", -// "normalized": [ -// { -// "available": "0.00", -// "balance": "0.00", -// "currency": "USD" -// } -// ], -// "wire": [ -// { -// "accountName": "Uphold Europe Limited", -// "address": { -// "line1": "Tartu mnt 2", -// "line2": "10145 Tallinn, Estonia" -// }, -// "bic": "LHVBEE22", -// "currency": "EUR", -// "iban": "EE76 7700 7710 0159 0178", -// "name": "AS LHV Pank" -// }, -// { -// "accountName": "Uphold HQ, Inc.", -// "accountNumber": "XXXXXXXXXX", -// "address": { -// "line1": "1359 Broadway", -// "line2": "New York, NY 10018" -// }, -// "bic": "MCBEUS33", -// "currency": "USD", -// "name": "Metropolitan Bank", -// "routingNumber": "XXXXXXXXX" -// } -// ] -// } - -ledger::Result ParseUpholdGetCard( - const ledger::UrlResponse& response, - std::string* id) { - DCHECK(id); - - if (response.status_code == net::HTTP_UNAUTHORIZED) { - return ledger::Result::EXPIRED_TOKEN; - } - - if (response.status_code != net::HTTP_OK) { - return ledger::Result::LEDGER_ERROR; - } - - base::Optional value = base::JSONReader::Read(response.body); - if (!value || !value->is_dict()) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - base::DictionaryValue* dictionary = nullptr; - if (!value->GetAsDictionary(&dictionary)) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - const auto* id_str = dictionary->FindStringKey("id"); - if (!id_str) { - BLOG(0, "Missing id"); - return ledger::Result::LEDGER_ERROR; - } - - *id = *id_str; - - return ledger::Result::LEDGER_OK; -} - -// Request Url: -// POST https://api.uphold.com/v0/me/cards/{wallet_address}/transactions -// -// Success: -// Accepted (202) -// -// Response Format: -// { -// "createdAt": "2020-06-10T18:58:21.683Z", -// "denomination": { -// "amount": "1.00", -// "currency": "BAT", -// "pair": "BATBAT", -// "rate": "1.00" -// }, -// "fees": [], -// "id": "d382d3ae-8462-4b2c-9b60-b669539f41b2", -// "network": "uphold", -// "normalized": [ -// { -// "commission": "0.00", -// "currency": "USD", -// "fee": "0.00", -// "rate": "0.24688", -// "target": "origin", -// "amount": "0.25" -// } -// ], -// "params": { -// "currency": "BAT", -// "margin": "0.00", -// "pair": "BATBAT", -// "rate": "1.00", -// "ttl": 3599588, -// "type": "internal" -// }, -// "priority": "normal", -// "status": "pending", -// "type": "transfer", -// "destination": { -// "amount": "1.00", -// "base": "1.00", -// "commission": "0.00", -// "currency": "BAT", -// "description": "Brave Software International", -// "fee": "0.00", -// "isMember": true, -// "node": { -// "id": "6654ecb0-6079-4f6c-ba58-791cc890a561", -// "type": "card", -// "user": { -// "id": "f5e37294-68f1-49ae-89e2-b24b64aedd37", -// "username": "braveintl" -// } -// }, -// "rate": "1.00", -// "type": "card", -// "username": "braveintl" -// }, -// "origin": { -// "amount": "1.00", -// "base": "1.00", -// "CardId": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", -// "commission": "0.00", -// "currency": "BAT", -// "description": "Emerick Rogul", -// "fee": "0.00", -// "isMember": true, -// "node": { -// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", -// "type": "card", -// "user": { -// "id": "b34060c9-5ca3-4bdb-bc32-1f826ecea36e" -// } -// }, -// "rate": "1.00", -// "sources": [], -// "type": "card" -// } -// } - -ledger::Result ParseUpholdCreateTransaction( - const ledger::UrlResponse& response, - std::string* id) { - DCHECK(id); - - if (response.status_code == net::HTTP_UNAUTHORIZED) { - return ledger::Result::EXPIRED_TOKEN; - } - - if (response.status_code != net::HTTP_ACCEPTED) { - return ledger::Result::LEDGER_ERROR; - } - - base::Optional value = base::JSONReader::Read(response.body); - if (!value || !value->is_dict()) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - base::DictionaryValue* dictionary = nullptr; - if (!value->GetAsDictionary(&dictionary)) { - BLOG(0, "Invalid JSON"); - return ledger::Result::LEDGER_ERROR; - } - - const auto* id_str = dictionary->FindStringKey("id"); - if (!id_str) { - BLOG(0, "Missing id"); - return ledger::Result::LEDGER_ERROR; - } - - *id = *id_str; - - return ledger::Result::LEDGER_OK; -} - -// Request Url: -// POST https://api.uphold.com/v0/me/cards/{wallet_address}/transactions/{transaction_id}/commit -// -// Success: -// OK (200) -// -// Response Format: -// { -// "application": { -// "name": "Brave Browser" -// }, -// "createdAt": "2020-06-10T18:58:22.351Z", -// "denomination": { -// "pair": "BATBAT", -// "rate": "1.00", -// "amount": "1.00", -// "currency": "BAT" -// }, -// "fees": [], -// "id": "d382d3ae-8462-4b2c-9b60-b669539f41b2", -// "message": null, -// "network": "uphold", -// "normalized": [ -// { -// "fee": "0.00", -// "rate": "0.24688", -// "amount": "0.25", -// "target": "origin", -// "currency": "USD", -// "commission": "0.00" -// } -// ], -// "params": { -// "currency": "BAT", -// "margin": "0.00", -// "pair": "BATBAT", -// "progress": "1", -// "rate": "1.00", -// "ttl": 3599588, -// "type": "internal" -// }, -// "priority": "normal", -// "reference": null, -// "status": "completed", -// "type": "transfer", -// "destination": { -// "amount": "1.00", -// "base": "1.00", -// "commission": "0.00", -// "currency": "BAT", -// "description": "Brave Software International", -// "fee": "0.00", -// "isMember": true, -// "node": { -// "id": "6654ecb0-6079-4f6c-ba58-791cc890a561", -// "type": "card", -// "user": { -// "id": "f5e37294-68f1-49ae-89e2-b24b64aedd37", -// "username": "braveintl" -// } -// }, -// "rate": "1.00", -// "type": "card", -// "username": "braveintl" -// }, -// "origin": { -// "amount": "1.00", -// "base": "1.00", -// "CardId": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", -// "commission": "0.00", -// "currency": "BAT", -// "description": "Emerick Rogul", -// "fee": "0.00", -// "isMember": true, -// "node": { -// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", -// "type": "card", -// "user": { -// "id": "b34060c9-5ca3-4bdb-bc32-1f826ecea36e" -// } -// }, -// "rate": "1.00", -// "sources": [ -// { -// "id": "463dca02-83ec-4bd6-93b0-73bf5dbe35ac", -// "amount": "1.00" -// } -// ], -// "type": "card" -// } -// } - -ledger::Result CheckUpholdCommitTransaction( - const ledger::UrlResponse& response) { - if (response.status_code == net::HTTP_UNAUTHORIZED) { - return ledger::Result::EXPIRED_TOKEN; - } - - if (response.status_code != net::HTTP_OK) { - return ledger::Result::LEDGER_ERROR; - } - - return ledger::Result::LEDGER_OK; -} - -} // namespace braveledger_response_util diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_uphold.h b/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_uphold.h deleted file mode 100644 index 6c2b864493f2..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_uphold.h +++ /dev/null @@ -1,55 +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_RESPONSE_RESPONSE_UPHOLD_H_ -#define BRAVELEDGER_RESPONSE_RESPONSE_UPHOLD_H_ - -#include -#include - -#include "bat/ledger/internal/uphold/uphold_user.h" -#include "bat/ledger/mojom_structs.h" - -namespace braveledger_response_util { - -ledger::Result ParseFetchUpholdBalance( - const ledger::UrlResponse& response, - double* available); - -ledger::Result ParseUpholdAuthorization( - const ledger::UrlResponse& response, - std::string* token); - -ledger::Result ParseUpholdGetUser( - const ledger::UrlResponse& response, - braveledger_uphold::User* user); - -ledger::Result ParseUpholdGetCardAddresses( - const ledger::UrlResponse& response, - std::map* addresses); - -ledger::Result ParseUpholdCreateCard( - const ledger::UrlResponse& response, - std::string* id); - -ledger::Result ParseUpholdGetCards( - const ledger::UrlResponse& response, - const std::string& card_name, - std::string* id); - -ledger::Result ParseUpholdGetCard( - const ledger::UrlResponse& response, - std::string* id); - -ledger::Result ParseUpholdCreateTransaction( - const ledger::UrlResponse& response, - std::string* id); - -ledger::Result CheckUpholdCommitTransaction( - const ledger::UrlResponse& response); - -} // namespace braveledger_response_util - -#endif // BRAVELEDGER_RESPONSE_RESPONSE_UPHOLD_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc index 65efec3f1b64..5eb4e032fea0 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc @@ -11,9 +11,9 @@ #include "base/strings/stringprintf.h" #include "bat/ledger/global_constants.h" #include "bat/ledger/internal/common/time_util.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_server.h" #include "bat/ledger/internal/ledger_impl.h" #include "bat/ledger/internal/logging/event_log_keys.h" -#include "bat/ledger/internal/response/response_uphold.h" #include "bat/ledger/internal/uphold/uphold.h" #include "bat/ledger/internal/uphold/uphold_authorization.h" #include "bat/ledger/internal/uphold/uphold_card.h" @@ -40,6 +40,7 @@ Uphold::Uphold(bat_ledger::LedgerImpl* ledger) : user_(std::make_unique(ledger)), authorization_(std::make_unique(ledger, this)), wallet_(std::make_unique(ledger, this)), + uphold_server_(std::make_unique(ledger)), ledger_(ledger) { } @@ -134,41 +135,32 @@ void Uphold::FetchBalance(FetchBalanceCallback callback) { return; } - auto headers = RequestAuthorization(wallet->token); - const std::string url = GetAPIUrl("/v0/me/cards/" + wallet->address); - - auto balance_callback = std::bind(&Uphold::OnFetchBalance, + auto url_callback = std::bind(&Uphold::OnFetchBalance, this, _1, + _2, callback); - ledger_->LoadURL( - url, - headers, - "", - "", - ledger::UrlMethod::GET, - balance_callback); + uphold_server_->get_card()->Request( + wallet->address, + wallet->token, + url_callback); } void Uphold::OnFetchBalance( - const ledger::UrlResponse& response, + const ledger::Result result, + const double available, FetchBalanceCallback callback) { - BLOG(6, ledger::UrlResponseToString(__func__, response)); - - double available = 0.0; - const ledger::Result result = - braveledger_response_util::ParseFetchUpholdBalance(response, &available); if (result == ledger::Result::EXPIRED_TOKEN) { BLOG(0, "Expired token"); DisconnectWallet(); - callback(ledger::Result::EXPIRED_TOKEN, available); + callback(ledger::Result::EXPIRED_TOKEN, 0.0); return; } if (result != ledger::Result::LEDGER_OK) { BLOG(0, "Couldn't get balance"); - callback(ledger::Result::LEDGER_ERROR, available); + callback(ledger::Result::LEDGER_ERROR, 0.0); return; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.h index ac0324a12019..22e5603aac21 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.h @@ -13,13 +13,19 @@ #include #include "base/timer/timer.h" -#include "bat/ledger/ledger.h" #include "bat/ledger/internal/uphold/uphold_user.h" +#include "bat/ledger/ledger.h" namespace bat_ledger { class LedgerImpl; } +namespace ledger { +namespace endpoint { +class UpholdServer; +} +} + namespace braveledger_uphold { struct Transaction { @@ -80,7 +86,8 @@ class Uphold { ledger::ResultCallback callback); void OnFetchBalance( - const ledger::UrlResponse& response, + const ledger::Result result, + const double available, FetchBalanceCallback callback); void SaveTransferFee(ledger::TransferFeePtr transfer_fee); @@ -101,6 +108,7 @@ class Uphold { std::unique_ptr user_; std::unique_ptr authorization_; std::unique_ptr wallet_; + std::unique_ptr uphold_server_; bat_ledger::LedgerImpl* ledger_; // NOT OWNED std::map transfer_fee_timers_; }; 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 33d7cd4ae793..28d86b46fa05 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 @@ -3,6 +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 "bat/ledger/internal/uphold/uphold_authorization.h" + #include #include "base/json/json_reader.h" @@ -10,8 +12,6 @@ #include "bat/ledger/global_constants.h" #include "bat/ledger/internal/ledger_impl.h" #include "bat/ledger/internal/logging/event_log_keys.h" -#include "bat/ledger/internal/response/response_uphold.h" -#include "bat/ledger/internal/uphold/uphold_authorization.h" #include "bat/ledger/internal/uphold/uphold_util.h" using std::placeholders::_1; @@ -23,7 +23,8 @@ namespace braveledger_uphold { UpholdAuthorization::UpholdAuthorization( bat_ledger::LedgerImpl* ledger, Uphold* uphold) : ledger_(ledger), - uphold_(uphold) { + uphold_(uphold), + uphold_server_(std::make_unique(ledger)) { } UpholdAuthorization::~UpholdAuthorization() = default; @@ -96,33 +97,19 @@ void UpholdAuthorization::Authorize( return; } - auto headers = RequestAuthorization(); - const std::string payload = - base::StringPrintf("code=%s&grant_type=authorization_code", code.c_str()); - const std::string url = GetAPIUrl("/oauth2/token"); - auto auth_callback = std::bind(&UpholdAuthorization::OnAuthorize, + auto url_callback = std::bind(&UpholdAuthorization::OnAuthorize, this, _1, + _2, callback); - ledger_->LoadURL( - url, - headers, - payload, - "application/x-www-form-urlencoded", - ledger::UrlMethod::POST, - auth_callback); + uphold_server_->post_oauth()->Request(code, url_callback); } void UpholdAuthorization::OnAuthorize( - const ledger::UrlResponse& response, + const ledger::Result result, + const std::string& token, ledger::ExternalWalletAuthorizationCallback callback) { - BLOG(7, ledger::UrlResponseToString(__func__, response)); - - std::string token; - const ledger::Result result = - braveledger_response_util::ParseUpholdAuthorization(response, &token); - if (result == ledger::Result::EXPIRED_TOKEN) { BLOG(0, "Expired token"); callback(ledger::Result::EXPIRED_TOKEN, {}); 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 5cd3accbcbe2..fb3d7ad3d80f 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 @@ -7,8 +7,10 @@ #define BRAVELEDGER_UPHOLD_UPHOLD_AUTHORIZATION_H_ #include +#include #include +#include "bat/ledger/internal/endpoint/uphold/uphold_server.h" #include "bat/ledger/internal/uphold/uphold.h" #include "bat/ledger/internal/uphold/uphold_user.h" #include "bat/ledger/ledger.h" @@ -31,7 +33,8 @@ class UpholdAuthorization { private: void OnAuthorize( - const ledger::UrlResponse& response, + const ledger::Result result, + const std::string& token, ledger::ExternalWalletAuthorizationCallback callback); void OnGetUser( @@ -46,6 +49,7 @@ class UpholdAuthorization { bat_ledger::LedgerImpl* ledger_; // NOT OWNED Uphold* uphold_; // NOT OWNED + std::unique_ptr uphold_server_; }; } // namespace braveledger_uphold diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_card.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_card.cc index 6f5f24aad88d..86ffab298f39 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_card.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_card.cc @@ -10,8 +10,8 @@ #include "base/strings/stringprintf.h" #include "base/values.h" #include "bat/ledger/global_constants.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_server.h" #include "bat/ledger/internal/ledger_impl.h" -#include "bat/ledger/internal/response/response_uphold.h" #include "bat/ledger/internal/uphold/uphold_card.h" #include "bat/ledger/internal/uphold/uphold_util.h" @@ -28,13 +28,10 @@ UpdateCard::UpdateCard() : UpdateCard::~UpdateCard() = default; -} // namespace braveledger_uphold - -namespace braveledger_uphold { - UpholdCard::UpholdCard(bat_ledger::LedgerImpl* ledger, Uphold* uphold) : ledger_(ledger), - uphold_(uphold) { + uphold_(uphold), + uphold_server_(std::make_unique(ledger)) { } UpholdCard::~UpholdCard() = default; @@ -48,29 +45,19 @@ void UpholdCard::CreateIfNecessary(CreateCardCallback callback) { return; } - auto headers = RequestAuthorization(wallet->token); - auto check_callback = std::bind(&UpholdCard::OnCreateIfNecessary, + auto url_callback = std::bind(&UpholdCard::OnCreateIfNecessary, this, _1, + _2, callback); - ledger_->LoadURL( - GetAPIUrl("/v0/me/cards?q=currency:BAT"), - headers, - "", - "application/json", - ledger::UrlMethod::GET, - check_callback); + + uphold_server_->get_cards()->Request(wallet->token, url_callback); } void UpholdCard::OnCreateIfNecessary( - const ledger::UrlResponse& response, + const ledger::Result result, + const std::string& id, CreateCardCallback callback) { - BLOG(6, ledger::UrlResponseToString(__func__, response)); - - std::string id; - const ledger::Result result = - braveledger_response_util::ParseUpholdGetCards(response, kCardName, &id); - if (result == ledger::Result::EXPIRED_TOKEN) { callback(ledger::Result::EXPIRED_TOKEN, ""); uphold_->DisconnectWallet(); @@ -95,37 +82,19 @@ void UpholdCard::Create( return; } - auto headers = RequestAuthorization(wallet->token); - const std::string payload = base::StringPrintf( - R"({ - "label": "%s", - "currency": "BAT" - })", - kCardName); - - auto create_callback = std::bind(&UpholdCard::OnCreate, + auto url_callback = std::bind(&UpholdCard::OnCreate, this, _1, + _2, callback); - ledger_->LoadURL( - GetAPIUrl("/v0/me/cards"), - headers, - payload, - "application/json", - ledger::UrlMethod::POST, - create_callback); + uphold_server_->post_cards()->Request(wallet->token, url_callback); } void UpholdCard::OnCreate( - const ledger::UrlResponse& response, + const ledger::Result result, + const std::string& id, CreateCardCallback callback) { - BLOG(6, ledger::UrlResponseToString(__func__, response)); - - std::string id; - const ledger::Result result = - braveledger_response_util::ParseUpholdGetCard(response, &id); - if (result == ledger::Result::EXPIRED_TOKEN) { BLOG(0, "Expired token"); callback(ledger::Result::EXPIRED_TOKEN, ""); @@ -177,7 +146,7 @@ void UpholdCard::OnCreateUpdate( void UpholdCard::Update( const UpdateCard& card, - UpdateCardCallback callback) { + ledger::ResultCallback callback) { auto wallets = ledger_->ledger_client()->GetExternalWallets(); auto wallet = GetWallet(std::move(wallets)); if (!wallet) { @@ -186,48 +155,21 @@ void UpholdCard::Update( return; } - auto headers = RequestAuthorization(wallet->token); - - base::Value payload(base::Value::Type::DICTIONARY); - - if (!card.label.empty()) { - payload.SetStringKey("label", card.label); - } - - base::Value settings(base::Value::Type::DICTIONARY); - if (card.position > -1) { - settings.SetIntKey("position", card.position); - } - settings.SetBoolKey("starred", card.starred); - payload.SetKey("settings", std::move(settings)); - - std::string json; - base::JSONWriter::Write(payload, &json); - - const auto url = GetAPIUrl((std::string)"/v0/me/cards/" + wallet->address); - auto update_callback = std::bind(&UpholdCard::OnUpdate, + auto url_callback = std::bind(&UpholdCard::OnUpdate, this, _1, callback); - ledger_->LoadURL( - url, - headers, - json, - "application/json", - ledger::UrlMethod::PATCH, - update_callback); + uphold_server_->patch_card()->Request( + wallet->token, + wallet->address, + card, + url_callback); } void UpholdCard::OnUpdate( - const ledger::UrlResponse& response, - UpdateCardCallback callback) { - BLOG(6, ledger::UrlResponseToString(__func__, response)); - - std::string id; - const ledger::Result result = - braveledger_response_util::ParseUpholdCreateCard(response, &id); - + const ledger::Result result, + ledger::ResultCallback callback) { if (result == ledger::Result::EXPIRED_TOKEN) { BLOG(0, "Expired token"); callback(ledger::Result::EXPIRED_TOKEN); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_card.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_card.h index 18ccf94296e3..48156b8d2486 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_card.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_card.h @@ -7,15 +7,22 @@ #define BRAVELEDGER_UPHOLD_UPHOLD_CARD_H_ #include +#include #include -#include "bat/ledger/ledger.h" #include "bat/ledger/internal/uphold/uphold.h" +#include "bat/ledger/ledger.h" namespace bat_ledger { class LedgerImpl; } +namespace ledger { +namespace endpoint { +class UpholdServer; +} +} + namespace braveledger_uphold { const char kCardName[] = "Brave Browser"; @@ -29,8 +36,6 @@ struct UpdateCard { ~UpdateCard(); }; -using UpdateCardCallback = std::function; - using GetCardAddressesCallback = std::function)>; @@ -44,13 +49,15 @@ class UpholdCard { private: void OnCreateIfNecessary( - const ledger::UrlResponse& response, + const ledger::Result result, + const std::string& id, CreateCardCallback callback); void Create(CreateCardCallback callback); void OnCreate( - const ledger::UrlResponse& response, + const ledger::Result result, + const std::string& id, CreateCardCallback callback); void OnCreateUpdate( @@ -60,17 +67,18 @@ class UpholdCard { void Update( const UpdateCard& card, - UpdateCardCallback callback); + ledger::ResultCallback callback); void OnUpdate( - const ledger::UrlResponse& response, - UpdateCardCallback callback); + const ledger::Result result, + ledger::ResultCallback callback); std::map ParseGetCardAddressResponse( const std::string& response); bat_ledger::LedgerImpl* ledger_; // NOT OWNED Uphold* uphold_; // NOT OWNED + std::unique_ptr uphold_server_; }; } // namespace braveledger_uphold diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.cc index 43406fbc0ea0..19b299d2ec8f 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.cc @@ -6,8 +6,8 @@ #include #include "base/strings/stringprintf.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_server.h" #include "bat/ledger/internal/ledger_impl.h" -#include "bat/ledger/internal/response/response_uphold.h" #include "bat/ledger/internal/uphold/uphold_transfer.h" #include "bat/ledger/internal/uphold/uphold_util.h" #include "net/http/http_status_code.h" @@ -20,7 +20,8 @@ namespace braveledger_uphold { UpholdTransfer::UpholdTransfer(bat_ledger::LedgerImpl* ledger, Uphold* uphold) : ledger_(ledger), - uphold_(uphold) { + uphold_(uphold), + uphold_server_(std::make_unique(ledger)) { } UpholdTransfer::~UpholdTransfer() = default; @@ -36,44 +37,22 @@ void UpholdTransfer::Start( return; } - auto headers = RequestAuthorization(wallet->token); - - const std::string path = base::StringPrintf( - "/v0/me/cards/%s/transactions", - wallet->address.c_str()); - - const std::string payload = base::StringPrintf( - R"({ - "denomination": { "amount": %f, "currency": "BAT" }, - "destination": "%s", - "message": "%s" - })", - transaction.amount, - transaction.address.c_str(), - transaction.message.c_str()); - - auto create_callback = std::bind(&UpholdTransfer::OnCreateTransaction, + auto url_callback = std::bind(&UpholdTransfer::OnCreateTransaction, this, _1, + _2, callback); - ledger_->LoadURL( - GetAPIUrl(path), - headers, - payload, - "application/json", - ledger::UrlMethod::POST, - create_callback); + uphold_server_->post_transaction()->Request( + wallet->token, + wallet->address, + transaction, + url_callback); } void UpholdTransfer::OnCreateTransaction( - const ledger::UrlResponse& response, + const ledger::Result result, + const std::string& id, ledger::TransactionCallback callback) { - BLOG(6, ledger::UrlResponseToString(__func__, response)); - - std::string id; - const ledger::Result result = - braveledger_response_util::ParseUpholdCreateTransaction(response, &id); - if (result == ledger::Result::EXPIRED_TOKEN) { callback(ledger::Result::EXPIRED_TOKEN, ""); uphold_->DisconnectWallet(); @@ -105,36 +84,23 @@ void UpholdTransfer::CommitTransaction( callback(ledger::Result::LEDGER_ERROR, ""); return; } - auto headers = RequestAuthorization(wallet->token); - const std::string path = base::StringPrintf( - "/v0/me/cards/%s/transactions/%s/commit", - wallet->address.c_str(), - transaction_id.c_str()); - - auto commit_callback = std::bind(&UpholdTransfer::OnCommitTransaction, + auto url_callback = std::bind(&UpholdTransfer::OnCommitTransaction, this, _1, transaction_id, callback); - ledger_->LoadURL( - GetAPIUrl(path), - headers, - "", - "application/json", - ledger::UrlMethod::POST, - commit_callback); + uphold_server_->post_transaction_commit()->Request( + wallet->token, + wallet->address, + transaction_id, + url_callback); } void UpholdTransfer::OnCommitTransaction( - const ledger::UrlResponse& response, + const ledger::Result result, const std::string& transaction_id, ledger::TransactionCallback callback) { - BLOG(6, ledger::UrlResponseToString(__func__, response)); - - const ledger::Result result = - braveledger_response_util::CheckUpholdCommitTransaction(response); - if (result == ledger::Result::EXPIRED_TOKEN) { callback(ledger::Result::EXPIRED_TOKEN, ""); uphold_->DisconnectWallet(); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.h index 2ee0a71aad1e..41aba2449a09 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.h @@ -7,6 +7,7 @@ #define BRAVELEDGER_UPHOLD_UPHOLD_TRANSFER_H_ #include +#include #include #include "bat/ledger/ledger.h" @@ -16,6 +17,12 @@ namespace bat_ledger { class LedgerImpl; } +namespace ledger { +namespace endpoint { +class UpholdServer; +} +} + namespace braveledger_uphold { class UpholdTransfer { @@ -30,7 +37,8 @@ class UpholdTransfer { private: void OnCreateTransaction( - const ledger::UrlResponse& response, + const ledger::Result result, + const std::string& id, ledger::TransactionCallback callback); void CommitTransaction( @@ -38,12 +46,13 @@ class UpholdTransfer { ledger::TransactionCallback callback); void OnCommitTransaction( - const ledger::UrlResponse& response, + const ledger::Result result, const std::string& transaction_id, ledger::TransactionCallback callback); bat_ledger::LedgerImpl* ledger_; // NOT OWNED Uphold* uphold_; // NOT OWNED + std::unique_ptr uphold_server_; }; } // namespace braveledger_uphold diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_user.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_user.cc index 3a81b926b150..7326df4ee42c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_user.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_user.cc @@ -9,8 +9,8 @@ #include #include "base/json/json_reader.h" +#include "bat/ledger/internal/endpoint/uphold/uphold_server.h" #include "bat/ledger/internal/ledger_impl.h" -#include "bat/ledger/internal/response/response_uphold.h" #include "bat/ledger/internal/uphold/uphold_util.h" using std::placeholders::_1; @@ -28,11 +28,9 @@ User::User() : User::~User() = default; -} // namespace braveledger_uphold - -namespace braveledger_uphold { - -UpholdUser::UpholdUser(bat_ledger::LedgerImpl* ledger) : ledger_(ledger) { +UpholdUser::UpholdUser(bat_ledger::LedgerImpl* ledger) : + ledger_(ledger), + uphold_server_(std::make_unique(ledger)) { } UpholdUser::~UpholdUser() = default; @@ -48,31 +46,19 @@ void UpholdUser::Get(GetUserCallback callback) { return; } - const auto headers = RequestAuthorization(wallet->token); - const std::string url = GetAPIUrl("/v0/me"); - - auto user_callback = std::bind(&UpholdUser::OnGet, + auto url_callback = std::bind(&UpholdUser::OnGet, this, _1, + _2, callback); - ledger_->LoadURL( - url, - headers, - "", - "", - ledger::UrlMethod::GET, - user_callback); + + uphold_server_->get_me()->Request(wallet->token, url_callback); } void UpholdUser::OnGet( - const ledger::UrlResponse& response, + const ledger::Result result, + const User& user, GetUserCallback callback) { - BLOG(7, ledger::UrlResponseToString(__func__, response)); - - User user; - const ledger::Result result = - braveledger_response_util::ParseUpholdGetUser(response, &user); - if (result == ledger::Result::EXPIRED_TOKEN) { BLOG(0, "Expired token"); callback(ledger::Result::EXPIRED_TOKEN, user); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_user.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_user.h index 4a16de992801..3865eb9df0af 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_user.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_user.h @@ -6,6 +6,7 @@ #ifndef BRAVELEDGER_UPHOLD_UPHOLD_USER_H_ #define BRAVELEDGER_UPHOLD_UPHOLD_USER_H_ +#include #include #include "bat/ledger/ledger.h" @@ -14,6 +15,12 @@ namespace bat_ledger { class LedgerImpl; } +namespace ledger { +namespace endpoint { +class UpholdServer; +} +} + namespace braveledger_uphold { enum UserStatus { @@ -35,7 +42,7 @@ struct User { ~User(); }; -using GetUserCallback = std::function; +using GetUserCallback = std::function; class UpholdUser { public: @@ -47,12 +54,14 @@ class UpholdUser { private: void OnGet( - const ledger::UrlResponse& response, + const ledger::Result result, + const User& user, GetUserCallback callback); UserStatus GetStatus(const std::string& status); bat_ledger::LedgerImpl* ledger_; // NOT OWNED + std::unique_ptr uphold_server_; }; } // namespace braveledger_uphold diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util.cc index 2339b36224dc..b8aa3703a910 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util.cc @@ -20,29 +20,12 @@ std::string GetClientId() { : kClientIdStaging; } -std::string GetClientSecret() { - return ledger::_environment == ledger::Environment::PRODUCTION - ? kClientSecretProduction - : kClientSecretStaging; -} - std::string GetUrl() { return ledger::_environment == ledger::Environment::PRODUCTION ? kUrlProduction : kUrlStaging; } -std::string GetAPIUrl(const std::string& path) { - std::string url; - if (ledger::_environment == ledger::Environment::PRODUCTION) { - url = kAPIUrlProduction; - } else { - url = kAPIUrlStaging; - } - - return url + path; -} - std::string GetFeeAddress() { return ledger::_environment == ledger::Environment::PRODUCTION ? kFeeAddressProduction @@ -128,30 +111,7 @@ ledger::ExternalWalletPtr GetWallet( return nullptr; } -std::vector RequestAuthorization( - const std::string& token) { - std::vector headers; - if (!token.empty()) { - headers.push_back("Authorization: Bearer " + token); - return headers; - } - - const std::string id = GetClientId(); - const std::string secret = GetClientSecret(); - - std::string user; - base::Base64Encode( - base::StringPrintf( - "%s:%s", - id.c_str(), - secret.c_str()), - &user); - - headers.push_back("Authorization: Basic " + user); - - return headers; -} std::string GenerateRandomString(bool testing) { if (testing) { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util.h index e0bec74f41ba..294eda77ff5a 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util.h @@ -16,13 +16,8 @@ namespace braveledger_uphold { const char kUrlStaging[] = "https://sandbox.uphold.com"; const char kUrlProduction[] = "https://uphold.com"; -const char kAPIUrlStaging[] = "https://api-sandbox.uphold.com"; -const char kAPIUrlProduction[] = "https://api.uphold.com"; const char kClientIdStaging[] = "4c2b665ca060d912fec5c735c734859a06118cc8"; const char kClientIdProduction[] = "6d8d9473ed20be627f71ed46e207f40c004c5b1a"; -const char kClientSecretStaging[] = "67bf87da096748c5bc1e195cfbdd59db006618a0"; -const char kClientSecretProduction[] = - "de1aa4196c8d4aa50c6bc1371734e3f57f781f72"; const char kFeeAddressStaging[] = "1b2b466f-5c15-49bf-995e-c91777d3da93"; const char kFeeAddressProduction[] = "b01e8c55-5004-4761-9e4b-01ec13e25c92"; const char kACAddressStaging[] = "1b2b466f-5c15-49bf-995e-c91777d3da93"; @@ -30,12 +25,8 @@ const char kACAddressProduction[] = "b01e8c55-5004-4761-9e4b-01ec13e25c92"; std::string GetClientId(); -std::string GetClientSecret(); - std::string GetUrl(); -std::string GetAPIUrl(const std::string& path); - std::string GetFeeAddress(); std::string GetACAddress(); @@ -51,9 +42,6 @@ std::string GetSecondStepVerify(); ledger::ExternalWalletPtr GetWallet( std::map wallets); -std::vector RequestAuthorization( - const std::string& token = ""); - std::string GenerateRandomString(bool testing); std::string GetAccountUrl(); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util_unittest.cc index b37303aeb653..b6fed692dafd 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util_unittest.cc @@ -30,35 +30,6 @@ TEST(UpholdUtilTest, GetClientId) { ASSERT_EQ(result, kClientIdStaging); } -TEST(UpholdUtilTest, GetClientSecret) { - // production - ledger::_environment = ledger::Environment::PRODUCTION; - std::string result = braveledger_uphold::GetClientSecret(); - ASSERT_EQ(result, kClientSecretProduction); - - // staging - ledger::_environment = ledger::Environment::STAGING; - result = braveledger_uphold::GetClientSecret(); - ASSERT_EQ(result, kClientSecretStaging); -} - -TEST(UpholdUtilTest, GetAPIUrl) { - // empty path, production - ledger::_environment = ledger::Environment::PRODUCTION; - std::string result = braveledger_uphold::GetAPIUrl(""); - ASSERT_EQ(result, kAPIUrlProduction); - - // empty path, staging - ledger::_environment = ledger::Environment::STAGING; - result = braveledger_uphold::GetAPIUrl(""); - ASSERT_EQ(result, kAPIUrlStaging); - - // with path - ledger::_environment = ledger::Environment::STAGING; - result = braveledger_uphold::GetAPIUrl("/v0/testing"); - ASSERT_EQ(result, static_cast(kAPIUrlStaging) + "/v0/testing"); -} - TEST(UpholdUtilTest, GetFeeAddress) { // production ledger::_environment = ledger::Environment::PRODUCTION; @@ -190,21 +161,6 @@ TEST(UpholdUtilTest, GetWallet) { ASSERT_EQ(result->address, "12355"); } -TEST(UpholdUtilTest, RequestAuthorization) { - // token is defined - auto result = braveledger_uphold::RequestAuthorization("2423423424"); - ASSERT_EQ(result.at(0), - "Authorization: Bearer 2423423424"); - - - // token is not defined - ledger::_environment = ledger::Environment::STAGING; - result = braveledger_uphold::RequestAuthorization(); - ASSERT_EQ(result.at(0), - "Authorization: Basic NGMyYjY2NWNhMDYwZDkxMmZlYzVjNzM1YzczNDg1OWEwNjEx" - "OGNjODo2N2JmODdkYTA5Njc0OGM1YmMxZTE5NWNmYmRkNTlkYjAwNjYxOGEw"); -} - TEST(UpholdUtilTest, GenerateRandomString) { // string for testing auto result = braveledger_uphold::GenerateRandomString(true);