Skip to content

Commit

Permalink
Merge pull request #5485 from brave/promotion-guards
Browse files Browse the repository at this point in the history
Promotion guards
  • Loading branch information
NejcZdovc committed May 11, 2020
2 parents 85f0a44 + 1aff5ed commit e167e90
Show file tree
Hide file tree
Showing 24 changed files with 203 additions and 44 deletions.
8 changes: 5 additions & 3 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,13 @@ ExtensionFunction::ResponseAction BraveRewardsClaimPromotionFunction::Run() {
data->SetIntKey("result", 1);
return RespondNow(OneArgument(std::move(data)));
}

rewards_service->ClaimPromotion(
params->promotion_id,
base::BindOnce(
&BraveRewardsClaimPromotionFunction::OnClaimPromotion,
this,
params->promotion_id));
&BraveRewardsClaimPromotionFunction::OnClaimPromotion,
this,
params->promotion_id));
return RespondLater();
}

Expand Down
1 change: 1 addition & 0 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ void RewardsDOMHandler::ClaimPromotion(const base::ListValue* args) {

#if !defined(OS_ANDROID)
rewards_service_->ClaimPromotion(
promotion_id,
base::Bind(&RewardsDOMHandler::OnClaimPromotion,
weak_factory_.GetWeakPtr(),
promotion_id));
Expand Down
3 changes: 2 additions & 1 deletion components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class MockRewardsService : public RewardsService {
MOCK_METHOD1(GetExcludedList,
void(const brave_rewards::GetContentSiteListCallback&));
MOCK_METHOD0(FetchPromotions, void());
MOCK_METHOD1(ClaimPromotion, void(brave_rewards::ClaimPromotionCallback));
MOCK_METHOD2(ClaimPromotion, void(const std::string&,
brave_rewards::ClaimPromotionCallback));
MOCK_METHOD2(ClaimPromotion, void(const std::string&,
brave_rewards::AttestPromotionCallback));
MOCK_METHOD3(AttestPromotion, void(const std::string&,
Expand Down
4 changes: 3 additions & 1 deletion components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ class RewardsService : public KeyedService {
virtual void GetExcludedList(const GetContentSiteListCallback& callback) = 0;
virtual void FetchPromotions() = 0;
// Used by desktop
virtual void ClaimPromotion(ClaimPromotionCallback callback) = 0;
virtual void ClaimPromotion(
const std::string& promotion_id,
ClaimPromotionCallback callback) = 0;
// Used by Android
virtual void ClaimPromotion(
const std::string& promotion_id,
Expand Down
27 changes: 25 additions & 2 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ namespace brave_test_resp {
std::string registrarVK_;
std::string verification_;
std::string promotions_;
std::string promotion_empty_key_;
std::string promotion_claim_;
std::string creds_tokens_;
std::string creds_tokens_prod_;
Expand Down Expand Up @@ -380,7 +381,11 @@ class BraveRewardsBrowserTest
}
} else if (URLMatches(url, "/promotions?", PREFIX_V1,
ServerTypes::kPromotion)) {
*response = brave_test_resp::promotions_;
if (promotion_empty_key_) {
*response = brave_test_resp::promotion_empty_key_;
} else {
*response = brave_test_resp::promotions_;
}
} else if (URLMatches(url, "/promotions/", PREFIX_V1,
ServerTypes::kPromotion)) {
if (url.find("claims") != std::string::npos) {
Expand Down Expand Up @@ -679,6 +684,10 @@ class BraveRewardsBrowserTest
ASSERT_TRUE(base::ReadFileToString(path.AppendASCII("promotions_resp.json"),
&brave_test_resp::promotions_));

ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("promotion_empty_key_resp.json"),
&brave_test_resp::promotion_empty_key_));

ASSERT_TRUE(base::ReadFileToString(path.AppendASCII("captcha_resp.json"),
&brave_test_resp::captcha_));
ASSERT_TRUE(
Expand Down Expand Up @@ -1436,7 +1445,8 @@ class BraveRewardsBrowserTest
double reconciled_tip_total_ = 0;
double pending_balance_ = 0;
double external_balance_ = 0;
double verified_wallet_ = false;
bool verified_wallet_ = false;
bool promotion_empty_key_ = false;
const std::string external_wallet_address_ =
"abe5f454-fedd-4ea9-9203-470ae7315bb3";
};
Expand Down Expand Up @@ -2786,3 +2796,16 @@ IN_PROC_BROWSER_TEST_F(
"[color=contribute]",
"-50.0BAT");
}

IN_PROC_BROWSER_TEST_F(
BraveRewardsBrowserTest,
PromotionHasEmptyPublicKey) {
promotion_empty_key_ = true;
EnableRewards();

WaitForPromotionInitialization();
rewards_service_browsertest_utils::WaitForElementToAppear(
OpenRewardsPopup(),
"[data-test-id=notification-close]",
false);
}
5 changes: 3 additions & 2 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,7 @@ void RewardsServiceImpl::OnAttestationAndroid(
}

void RewardsServiceImpl::ClaimPromotion(
const std::string& promotion_id,
ClaimPromotionCallback callback) {
if (!Connected()) {
return;
Expand All @@ -1353,7 +1354,7 @@ void RewardsServiceImpl::ClaimPromotion(
AsWeakPtr(),
std::move(callback));

bat_ledger_->ClaimPromotion("", std::move(claim_callback));
bat_ledger_->ClaimPromotion(promotion_id, "", std::move(claim_callback));
}

void RewardsServiceImpl::ClaimPromotion(
Expand All @@ -1368,7 +1369,7 @@ void RewardsServiceImpl::ClaimPromotion(
promotion_id,
std::move(callback));

bat_ledger_->ClaimPromotion("", std::move(claim_callback));
bat_ledger_->ClaimPromotion(promotion_id, "", std::move(claim_callback));
}

void RewardsServiceImpl::GetWalletPassphrase(
Expand Down
4 changes: 3 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ class RewardsServiceImpl : public RewardsService,
void CreateWallet(CreateWalletCallback callback) override;
void FetchWalletProperties() override;
void FetchPromotions() override;
void ClaimPromotion(ClaimPromotionCallback callback) override;
void ClaimPromotion(
const std::string& promotion_id,
ClaimPromotionCallback callback) override;
void ClaimPromotion(
const std::string& promotion_id,
AttestPromotionCallback callback) override;
Expand Down
2 changes: 2 additions & 0 deletions components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,14 @@ void BatLedgerImpl::OnClaimPromotion(
delete holder;
}
void BatLedgerImpl::ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ClaimPromotionCallback callback) {
// deleted in OnClaimPromotion
auto* holder = new CallbackHolder<ClaimPromotionCallback>(
AsWeakPtr(), std::move(callback));
ledger_->ClaimPromotion(
promotion_id,
payload,
std::bind(BatLedgerImpl::OnClaimPromotion, holder, _1, _2));
}
Expand Down
1 change: 1 addition & 0 deletions components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class BatLedgerImpl : public mojom::BatLedger,

void FetchPromotions(FetchPromotionsCallback callback) override;
void ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ClaimPromotionCallback callback) override;
void AttestPromotion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface BatLedger {
SetPublisherExclude(string publisher_key, ledger.mojom.PublisherExclude exclude) => (ledger.mojom.Result result);
RestorePublishers() => (ledger.mojom.Result result);
FetchPromotions() => (ledger.mojom.Result result, array<ledger.mojom.Promotion> promotion);
ClaimPromotion(string payload) => (ledger.mojom.Result result, string response);
ClaimPromotion(string promotion_id, string payload) => (ledger.mojom.Result result, string response);
AttestPromotion(string promotion_id, string solution) => (ledger.mojom.Result result, ledger.mojom.Promotion? promotion);
GetWalletPassphrase() => (string wallet_passphrase);
RecoverWallet(string passPhrase) => (ledger.mojom.Result result, double balance);
Expand Down
16 changes: 16 additions & 0 deletions test/data/rewards-data/promotion_empty_key_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"promotions": [
{
"id": "6820f6a4-c6ef-481d-879c-d2c30c8928c3",
"createdAt": "2019-10-30T08:07:07.092836Z",
"expiresAt": "2025-02-29T08:07:07.092836Z",
"version": 5,
"suggestionsPerGrant": 120,
"approximateValue": "30.0",
"type": "ugp",
"available": true,
"platform": "",
"publicKeys": []
}
]
}
1 change: 1 addition & 0 deletions vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class LEDGER_EXPORT Ledger {
// desktop: { "captchaImage": "{{captchaImage}}", "hint": "{{hint}}" }
// iOS and Android: { "nonce": "{{nonce}}" }
virtual void ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ClaimPromotionCallback callback) const = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ enum Result {
AC_TABLE_EMPTY = 14,
NOT_ENOUGH_FUNDS = 15,
TIP_ERROR = 16,
CORRUPTED_WALLET = 17,
CORRUPTED_DATA = 17,
GRANT_ALREADY_CLAIMED = 18,

CONTRIBUTION_AMOUNT_TOO_LOW = 19,
Expand All @@ -249,7 +249,8 @@ enum Result {
RETRY = 29,
RETRY_SHORT = 30,
RETRY_LONG = 31,
CONTINUE = 32
CONTINUE = 32,
IN_PROGRESS= 33
};

enum PublisherStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,34 @@ void CredentialsCommon::GetSignedCredsFromResponse(
return;
}

auto* signed_creds = parsed_response.FindListKey("signed_creds");
if (!signed_creds || signed_creds->GetList().empty()) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) <<
"Failed to parse signed creds";
callback(ledger::Result::RETRY);
return;
}

auto* public_key = parsed_response.FindStringKey("public_key");
if (!public_key || public_key->empty()) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Public key is empty";
callback(ledger::Result::RETRY);
return;
}

auto* batch_proof = parsed_response.FindStringKey("batch_proof");
if (!batch_proof || batch_proof->empty()) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Batch proof is empty";
callback(ledger::Result::RETRY);
return;
}

auto creds_batch = ledger::CredsBatch::New();
creds_batch->trigger_id = trigger.id;
creds_batch->trigger_type = trigger.type;
base::JSONWriter::Write(
*parsed_response.FindListKey("signed_creds"),
&creds_batch->signed_creds);
creds_batch->public_key = *parsed_response.FindStringKey("public_key");
creds_batch->batch_proof = *parsed_response.FindStringKey("batch_proof");
base::JSONWriter::Write(*signed_creds, &creds_batch->signed_creds);
creds_batch->public_key = *public_key;
creds_batch->batch_proof = *batch_proof;

ledger_->SaveSignedCreds(std::move(creds_batch), callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ void LedgerImpl::OnPublisherStateLoaded(
}

if (GetPaymentId().empty() || GetWalletPassphrase().empty()) {
callback(ledger::Result::CORRUPTED_WALLET);
callback(ledger::Result::CORRUPTED_DATA);
return;
}

Expand Down Expand Up @@ -819,9 +819,10 @@ void LedgerImpl::FetchWalletProperties(
}

void LedgerImpl::ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ledger::ClaimPromotionCallback callback) const {
bat_promotion_->Claim(payload, std::move(callback));
bat_promotion_->Claim(promotion_id, payload, std::move(callback));
}

void LedgerImpl::AttestPromotion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class LedgerImpl : public ledger::Ledger {
void FetchPromotions(ledger::FetchPromotionCallback callback) const override;

void ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ledger::ClaimPromotionCallback callback) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ class MockLedgerImpl : public LedgerImpl {
MOCK_CONST_METHOD1(FetchPromotions,
void(ledger::FetchPromotionCallback));

MOCK_CONST_METHOD2(ClaimPromotion,
void(const std::string&, const ledger::ClaimPromotionCallback));
MOCK_CONST_METHOD3(ClaimPromotion, void(
const std::string&,
const std::string&,
const ledger::ClaimPromotionCallback));

MOCK_CONST_METHOD3(AttestPromotion,
void(const std::string&,
Expand Down
Loading

0 comments on commit e167e90

Please sign in to comment.