Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for virtual grants #4344

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/definitions/rewards.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ declare namespace Rewards {
export type CaptchaStatus = 'start' | 'wrongPosition' | 'generalError' | 'finished' | null

export enum PromotionTypes {
UNKNOWN = -1,
masparrow marked this conversation as resolved.
Show resolved Hide resolved
UGP = 0,
ADS = 1
}

export enum PromotionStatus {
UNKNOWN = -1,
ACTIVE = 0,
ATTESTED = 1,
CLAIMED = 2,
Expand Down Expand Up @@ -226,6 +228,7 @@ declare namespace Rewards {
}

export enum RewardsType {
UNKNOWN = -1,
AUTO_CONTRIBUTE = 2,
ONE_TIME_TIP = 8,
RECURRING_TIP = 21
Expand Down
2 changes: 2 additions & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ test("brave_unit_tests") {
if (brave_rewards_enabled) {
sources += [
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_activity_info_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/common/bind_util_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/common/security_helper_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_util_unittest.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ struct TransferFee {
};

enum RewardsType {
UNKNOWN = -1,
AUTO_CONTRIBUTE = 2,
tmancey marked this conversation as resolved.
Show resolved Hide resolved
ONE_TIME_TIP = 8,
RECURRING_TIP = 16,
Expand Down Expand Up @@ -348,11 +349,13 @@ enum Environment {
};

enum PromotionType {
UNKNOWN = -1,
UGP = 0,
ADS = 1
};

enum PromotionStatus {
UNKNOWN = -1,
ACTIVE = 0,
ATTESTED = 1,
CLAIMED = 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,58 @@

namespace braveledger_bind_util {

ledger::RewardsType ConvertIntToRewardsType(int type) {
ledger::RewardsType rewards_type = static_cast<ledger::RewardsType>(type);

switch (rewards_type) {
case ledger::RewardsType::AUTO_CONTRIBUTE:
case ledger::RewardsType::ONE_TIME_TIP:
case ledger::RewardsType::RECURRING_TIP: {
break;
}
default: {
rewards_type = ledger::RewardsType::UNKNOWN;
tmancey marked this conversation as resolved.
Show resolved Hide resolved
}
}
return rewards_type;
}

ledger::PromotionType ConvertIntToPromotionType(int type) {
ledger::PromotionType promotion_type =
static_cast<ledger::PromotionType>(type);

switch (promotion_type) {
case ledger::PromotionType::UGP:
case ledger::PromotionType::ADS: {
break;
}
default: {
promotion_type = ledger::PromotionType::UNKNOWN;
tmancey marked this conversation as resolved.
Show resolved Hide resolved
}
}
return promotion_type;
}

ledger::PromotionStatus ConvertIntToPromotionStatus(int type) {
ledger::PromotionStatus promotion_status =
static_cast<ledger::PromotionStatus>(type);

switch (promotion_status) {
case ledger::PromotionStatus::ACTIVE:
case ledger::PromotionStatus::ATTESTED:
case ledger::PromotionStatus::CLAIMED:
case ledger::PromotionStatus::SIGNED_TOKENS:
case ledger::PromotionStatus::FINISHED:
case ledger::PromotionStatus::OVER: {
break;
}
default: {
promotion_status = ledger::PromotionStatus::UNKNOWN;
tmancey marked this conversation as resolved.
Show resolved Hide resolved
}
}
return promotion_status;
}

masparrow marked this conversation as resolved.
Show resolved Hide resolved
std::string FromContributionQueueToString(ledger::ContributionQueuePtr info) {
base::Value publishers(base::Value::Type::LIST);
for (auto& item : info->publishers) {
Expand Down Expand Up @@ -57,7 +109,7 @@ ledger::ContributionQueuePtr FromStringToContributionQueue(

auto* type = dictionary->FindKey("type");
if (type && type->is_int()) {
queue->type = static_cast<ledger::RewardsType>(type->GetInt());
queue->type = ConvertIntToRewardsType(type->GetInt());
tmancey marked this conversation as resolved.
Show resolved Hide resolved
}

auto* amount = dictionary->FindKey("amount");
Expand Down Expand Up @@ -168,7 +220,7 @@ ledger::PromotionPtr FromStringToPromotion(const std::string& data) {

auto type = dictionary->FindIntKey("type");
if (type) {
promotion->type = static_cast<ledger::PromotionType>(*type);
promotion->type = ConvertIntToPromotionType(*type);
}

auto suggestions = dictionary->FindIntKey("suggestions");
Expand All @@ -178,7 +230,7 @@ ledger::PromotionPtr FromStringToPromotion(const std::string& data) {

auto status = dictionary->FindIntKey("status");
if (status) {
promotion->status = static_cast<ledger::PromotionStatus>(*status);
promotion->status = ConvertIntToPromotionStatus(*status);
}

auto legacy_claimed = dictionary->FindBoolKey("legacy_claimed");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <memory>
#include <string>
#include <utility>

#include "bat/ledger/internal/common/bind_util.h"
#include "testing/gtest/include/gtest/gtest.h"
// npm run test -- brave_unit_tests --filter=BindUtilTest.*

namespace braveledger_bind_util {

class BindUtilTest : public ::testing::Test {
protected:
BindUtilTest() {
}

void SetUp() override {
}
};

TEST_F(BindUtilTest, ExpectedStringFromContributionQueueToString) {
const int8_t expected_publisher_count = 2;

ledger::ContributionQueuePublisherList contribution_queue_publisher_list;

for (int8_t publisher = 0; publisher < expected_publisher_count;
publisher++) {
ledger::ContributionQueuePublisherPtr contribution_queue_publisher =
ledger::ContributionQueuePublisher::New();

contribution_queue_publisher->publisher_key =
std::to_string(1000 + publisher);
contribution_queue_publisher->amount_percent =
static_cast<double>(publisher * 25);
contribution_queue_publisher_list.push_back(
std::move(contribution_queue_publisher));
}

ledger::ContributionQueuePtr contribution_queue =
ledger::ContributionQueue::New();
contribution_queue->id = 1234;
contribution_queue->type = ledger::RewardsType::RECURRING_TIP;
contribution_queue->amount = 100.0;
contribution_queue->partial = false;
contribution_queue->publishers = std::move(contribution_queue_publisher_list);

const std::string expected_contribution_queue_as_string = "{\"amount\":\"100.000000\",\"id\":\"1234\",\"partial\":false,\"publishers\":[{\"amount_percent\":\"0.000000\",\"publisher_key\":\"1000\"},{\"amount_percent\":\"25.000000\",\"publisher_key\":\"1001\"}],\"type\":16}"; // NOLINT

std::string contribution_queue_as_string =
FromContributionQueueToString(std::move(contribution_queue));

EXPECT_EQ(expected_contribution_queue_as_string,
contribution_queue_as_string);
}

// ledger::ContributionQueuePtr FromStringToContributionQueue(
// const std::string& data);
TEST_F(BindUtilTest, ExpectedContributionQueueFromStringToContributionQueue) {
const std::string contribution_queue_as_string = "{\"amount\":\"100.000000\",\"id\":\"1234\",\"partial\":false,\"publishers\":[{\"amount_percent\":\"0.000000\",\"publisher_key\":\"1000\"},{\"amount_percent\":\"25.000000\",\"publisher_key\":\"1001\"}],\"type\":16}"; // NOLINT

const uint64_t expected_publisher_count = 2;

ledger::ContributionQueuePtr contribution_queue =
FromStringToContributionQueue(contribution_queue_as_string);

EXPECT_EQ(contribution_queue->id, (uint64_t)1234);
EXPECT_EQ(contribution_queue->type, ledger::RewardsType::RECURRING_TIP);
EXPECT_EQ(contribution_queue->amount, 100.0);
EXPECT_EQ(contribution_queue->partial, false);
EXPECT_EQ(contribution_queue->publishers.size(), expected_publisher_count);

int8_t publisher = 0;
for (auto& contribution_queue_publisher : contribution_queue->publishers) {
EXPECT_EQ(contribution_queue_publisher->publisher_key,
std::to_string(1000 + publisher));
EXPECT_EQ(contribution_queue_publisher->amount_percent,
static_cast<double>(publisher * 25));
publisher++;
}
}

TEST_F(BindUtilTest,
ContributionQueueTypeUnknownFromStringToContributionQueueWithInvalidType) {
const std::string contribution_queue_as_string = "{\"amount\":\"100.000000\",\"id\":\"1234\",\"partial\":false,\"publishers\":[{\"amount_percent\":\"0.000000\",\"publisher_key\":\"1000\"},{\"amount_percent\":\"25.000000\",\"publisher_key\":\"1001\"}],\"type\":17}"; // NOLINT

ledger::ContributionQueuePtr contribution_queue =
FromStringToContributionQueue(contribution_queue_as_string);

EXPECT_EQ(contribution_queue->type, ledger::RewardsType::UNKNOWN);
}

// std::string FromPromotionToString(const ledger::PromotionPtr info);
TEST_F(BindUtilTest, ExpectedStringFromPromotionToString) {
ledger::PromotionCredsPtr promotion_creds = ledger::PromotionCreds::New();
promotion_creds->tokens = "ABC";
promotion_creds->blinded_creds = "DEF";
promotion_creds->signed_creds = "GHI";
promotion_creds->public_key = "JKL";
promotion_creds->batch_proof = "MNO";
promotion_creds->claim_id = "PQR";

ledger::PromotionPtr promotion = ledger::Promotion::New();
promotion->id = "1234";
promotion->version = 1;
promotion->type = ledger::PromotionType::ADS;
promotion->public_keys = "5678";
promotion->suggestions = static_cast<int64_t>(1);
promotion->approximate_value = 100.0;
promotion->status = ledger::PromotionStatus::OVER;
promotion->expires_at = static_cast<uint64_t>(2);
promotion->claimed_at = static_cast<uint64_t>(3);
promotion->legacy_claimed = false;
promotion->credentials = std::move(promotion_creds);

const std::string expected_promotion_as_string = "{\"approximate_value\":\"100.000000\",\"claimed_at\":\"3\",\"credentials\":{\"batch_proof\":\"MNO\",\"blinded_creds\":\"DEF\",\"claim_id\":\"PQR\",\"public_key\":\"JKL\",\"signed_creds\":\"GHI\",\"tokens\":\"ABC\"},\"expires_at\":\"2\",\"id\":\"1234\",\"legacy_claimed\":false,\"public_keys\":\"5678\",\"status\":5,\"suggestions\":1,\"type\":1,\"version\":1}"; // NOLINT

std::string promotion_as_string =
FromPromotionToString(std::move(promotion));

// Nb. if this test fails due to changes to Promotion or PromotionCreds,
// you must also maintain ExpectedPromotionFromStringToPromotion.
EXPECT_EQ(expected_promotion_as_string,
promotion_as_string);
}

// ledger::PromotionPtr FromStringToPromotion(const std::string& data);
TEST_F(BindUtilTest, ExpectedPromotionFromStringToPromotion) {
const std::string promotion_as_string = "{\"approximate_value\":\"100.000000\",\"claimed_at\":\"3\",\"credentials\":{\"batch_proof\":\"MNO\",\"blinded_creds\":\"DEF\",\"claim_id\":\"PQR\",\"public_key\":\"JKL\",\"signed_creds\":\"GHI\",\"tokens\":\"ABC\"},\"expires_at\":\"2\",\"id\":\"1234\",\"legacy_claimed\":false,\"public_keys\":\"5678\",\"status\":5,\"suggestions\":1,\"type\":1,\"version\":1}"; // NOLINT

ledger::PromotionPtr promotion = FromStringToPromotion(promotion_as_string);

EXPECT_EQ(promotion->id, "1234");
EXPECT_EQ(promotion->version, static_cast<int64_t>(1));
EXPECT_EQ(promotion->type, ledger::PromotionType::ADS);
EXPECT_EQ(promotion->public_keys, "5678");
EXPECT_EQ(promotion->suggestions, static_cast<int64_t>(1));
EXPECT_EQ(promotion->approximate_value, 100.0);
EXPECT_EQ(promotion->status, ledger::PromotionStatus::OVER);
EXPECT_EQ(promotion->expires_at, static_cast<uint64_t>(2));
EXPECT_EQ(promotion->claimed_at, static_cast<uint64_t>(3));
EXPECT_EQ(promotion->legacy_claimed, false);
EXPECT_EQ(promotion->credentials->tokens, "ABC");
EXPECT_EQ(promotion->credentials->blinded_creds, "DEF");
EXPECT_EQ(promotion->credentials->signed_creds, "GHI");
EXPECT_EQ(promotion->credentials->public_key, "JKL");
EXPECT_EQ(promotion->credentials->batch_proof, "MNO");
EXPECT_EQ(promotion->credentials->claim_id, "PQR");
}

TEST_F(BindUtilTest, PromotionTypeUnknownFromStringToPromotionWithInvalidType) {
const std::string promotion_as_string = "{\"approximate_value\":\"100.000000\",\"credentials\":{\"batch_proof\":\"MNO\",\"blinded_creds\":\"DEF\",\"claim_id\":\"PQR\",\"public_key\":\"JKL\",\"signed_creds\":\"GHI\",\"tokens\":\"ABC\"},\"expires_at\":\"2\",\"id\":\"1234\",\"legacy_claimed\":false,\"public_keys\":\"5678\",\"status\":5,\"suggestions\":1,\"type\":100,\"version\":1}"; // NOLINT

ledger::PromotionPtr promotion = FromStringToPromotion(promotion_as_string);

EXPECT_EQ(promotion->type, ledger::PromotionType::UNKNOWN);
}

TEST_F(BindUtilTest,
PromotionStatusUnknownFromStringToPromotionWithInvalidStatus) {
const std::string promotion_as_string = "{\"approximate_value\":\"100.000000\",\"credentials\":{\"batch_proof\":\"MNO\",\"blinded_creds\":\"DEF\",\"claim_id\":\"PQR\",\"public_key\":\"JKL\",\"signed_creds\":\"GHI\",\"tokens\":\"ABC\"},\"expires_at\":\"2\",\"id\":\"1234\",\"legacy_claimed\":false,\"public_keys\":\"5678\",\"status\":-1,\"suggestions\":1,\"type\":1,\"version\":1}"; // NOLINT

ledger::PromotionPtr promotion = FromStringToPromotion(promotion_as_string);

EXPECT_EQ(promotion->status, ledger::PromotionStatus::UNKNOWN);
}

} // namespace braveledger_bind_util
Loading