-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate conversions when loading from database (uplift to 1.59.x) (#…
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...e_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_validation_util.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_validation_util.h" | ||
|
||
#include <vector> | ||
|
||
#include "base/strings/string_util.h" | ||
#include "brave/components/brave_ads/core/internal/conversions/conversion/conversion_info.h" | ||
#include "brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_info.h" | ||
|
||
namespace brave_ads { | ||
|
||
namespace { | ||
|
||
constexpr char kAdTypeFieldName[] = "ad_type"; | ||
constexpr char kCreativeInstanceIdFieldName[] = "creative_instance_id"; | ||
constexpr char kCreativeSetIdFieldName[] = "creative_set_id"; | ||
constexpr char kCampaignIdFieldName[] = "campaign_id"; | ||
constexpr char kAdvertiserIdFieldName[] = "advertiser_id"; | ||
constexpr char kActionTypeFieldName[] = "action_type"; | ||
constexpr char kProcessAtFieldName[] = "process_at"; | ||
constexpr char kSeparator[] = ","; | ||
|
||
std::vector<std::string> GetInvalidFieldsNamesList( | ||
const ConversionInfo& conversion_item) { | ||
std::vector<std::string> invalid_fields; | ||
|
||
if (conversion_item.ad_type == AdType::kUndefined) { | ||
invalid_fields.emplace_back(kAdTypeFieldName); | ||
} | ||
|
||
if (conversion_item.creative_instance_id.empty()) { | ||
invalid_fields.emplace_back(kCreativeInstanceIdFieldName); | ||
} | ||
|
||
if (conversion_item.creative_set_id.empty()) { | ||
invalid_fields.emplace_back(kCreativeSetIdFieldName); | ||
} | ||
|
||
if (conversion_item.campaign_id.empty()) { | ||
invalid_fields.emplace_back(kCampaignIdFieldName); | ||
} | ||
|
||
if (conversion_item.advertiser_id.empty()) { | ||
invalid_fields.emplace_back(kAdvertiserIdFieldName); | ||
} | ||
|
||
if (conversion_item.action_type == ConversionActionType::kUndefined) { | ||
invalid_fields.emplace_back(kActionTypeFieldName); | ||
} | ||
|
||
return invalid_fields; | ||
} | ||
|
||
std::vector<std::string> GetInvalidFieldsNamesList( | ||
const ConversionQueueItemInfo& conversion_queue_item) { | ||
std::vector<std::string> invalid_fields = | ||
GetInvalidFieldsNamesList(conversion_queue_item.conversion); | ||
if (conversion_queue_item.process_at.is_null()) { | ||
invalid_fields.emplace_back(kProcessAtFieldName); | ||
} | ||
return invalid_fields; | ||
} | ||
|
||
} // namespace | ||
|
||
std::string GetConversionQueueItemInvalidFieldsNames( | ||
const ConversionQueueItemInfo& conversion_queue_item) { | ||
return base::JoinString(GetInvalidFieldsNamesList(conversion_queue_item), | ||
kSeparator); | ||
} | ||
|
||
} // namespace brave_ads |
20 changes: 20 additions & 0 deletions
20
...ve_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_validation_util.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_CONVERSIONS_QUEUE_QUEUE_ITEM_CONVERSION_QUEUE_ITEM_VALIDATION_UTIL_H_ | ||
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_CONVERSIONS_QUEUE_QUEUE_ITEM_CONVERSION_QUEUE_ITEM_VALIDATION_UTIL_H_ | ||
|
||
#include <string> | ||
|
||
namespace brave_ads { | ||
|
||
struct ConversionQueueItemInfo; | ||
|
||
std::string GetConversionQueueItemInvalidFieldsNames( | ||
const ConversionQueueItemInfo& conversion_queue_item); | ||
|
||
} // namespace brave_ads | ||
|
||
#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_CONVERSIONS_QUEUE_QUEUE_ITEM_CONVERSION_QUEUE_ITEM_VALIDATION_UTIL_H_ |
63 changes: 63 additions & 0 deletions
63
...e/internal/conversions/queue/queue_item/conversion_queue_item_validation_util_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_validation_util.h" | ||
|
||
#include "brave/components/brave_ads/core/internal/ads/ad_unittest_util.h" | ||
#include "brave/components/brave_ads/core/internal/common/unittest/unittest_time_util.h" | ||
#include "brave/components/brave_ads/core/internal/conversions/conversion/conversion_builder.h" | ||
#include "brave/components/brave_ads/core/internal/conversions/conversion/conversion_info.h" | ||
#include "brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_unittest_util.h" | ||
#include "brave/components/brave_ads/core/internal/conversions/types/verifiable_conversion/verifiable_conversion_unittest_constants.h" | ||
#include "brave/components/brave_ads/core/internal/user_interaction/ad_events/ad_event_builder.h" | ||
#include "brave/components/brave_ads/core/internal/user_interaction/ad_events/ad_event_info.h" | ||
#include "brave/components/brave_ads/core/public/ad_info.h" | ||
#include "brave/components/brave_ads/core/public/ad_type.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
// npm run test -- brave_unit_tests --filter=BraveAds* | ||
|
||
namespace brave_ads { | ||
|
||
TEST(BraveAdsValidationUtilTest, InvalidConversionQueueItem) { | ||
// Arrange | ||
const AdInfo ad = BuildAdForTesting(AdType::kNotificationAd, | ||
/*should_use_random_uuids*/ true); | ||
ConversionInfo conversion = BuildConversion( | ||
BuildAdEvent(ad, ConfirmationType::kViewed, /*created_at*/ Now()), | ||
VerifiableConversionInfo{kVerifiableConversionId, | ||
kVerifiableConversionAdvertiserPublicKey}); | ||
conversion.ad_type = AdType::kUndefined; | ||
|
||
ConversionQueueItemList conversion_queue_items = | ||
BuildConversionQueueItemsForTesting(conversion, /*count*/ 1); | ||
conversion_queue_items[0].process_at = base::Time(); | ||
|
||
// Act | ||
|
||
// Assert | ||
EXPECT_EQ("ad_type,process_at", GetConversionQueueItemInvalidFieldsNames( | ||
conversion_queue_items[0])); | ||
} | ||
|
||
TEST(BraveAdsValidationUtilTest, ValidConversionQueueItem) { | ||
// Arrange | ||
const AdInfo ad = BuildAdForTesting(AdType::kNotificationAd, | ||
/*should_use_random_uuids*/ true); | ||
const ConversionInfo conversion = BuildConversion( | ||
BuildAdEvent(ad, ConfirmationType::kViewed, /*created_at*/ Now()), | ||
VerifiableConversionInfo{kVerifiableConversionId, | ||
kVerifiableConversionAdvertiserPublicKey}); | ||
const ConversionQueueItemList conversion_queue_items = | ||
BuildConversionQueueItemsForTesting(conversion, /*count*/ 1); | ||
|
||
// Act | ||
|
||
// Assert | ||
EXPECT_EQ( | ||
"", GetConversionQueueItemInvalidFieldsNames(conversion_queue_items[0])); | ||
} | ||
|
||
} // namespace brave_ads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters