1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/commerce/core/account_checker.h"
#include <queue>
#include <string>
#include <unordered_map>
#include "base/check.h"
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/commerce/core/commerce_constants.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/pref_names.h"
#include "components/endpoint_fetcher/mock_endpoint_fetcher.h"
#include "components/prefs/testing_pref_service.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/sync/base/features.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/test/test_sync_service.h"
#include "components/sync/test/test_sync_user_settings.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using endpoint_fetcher::EndpointFetcher;
using endpoint_fetcher::MockEndpointFetcher;
using testing::_;
using testing::InSequence;
namespace {
constexpr base::TimeDelta kTimeout = base::Milliseconds(10000);
const char kPostData[] = "{\"preferences\":{\"price_track_email\":true}}";
} // namespace
namespace commerce {
class SpyAccountChecker : public AccountChecker {
public:
SpyAccountChecker(
PrefService* pref_service,
signin::IdentityManager* identity_manager,
syncer::SyncService* sync_service,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: AccountChecker("us",
"en-us",
pref_service,
identity_manager,
sync_service,
std::move(url_loader_factory)) {}
SpyAccountChecker(const SpyAccountChecker&) = delete;
SpyAccountChecker operator=(const SpyAccountChecker&) = delete;
~SpyAccountChecker() override = default;
MOCK_METHOD(std::unique_ptr<EndpointFetcher>,
CreateEndpointFetcher,
(const std::string& oauth_consumer_name,
const GURL& url,
const endpoint_fetcher::HttpMethod http_method,
const std::string& content_type,
const std::vector<std::string>& scopes,
const base::TimeDelta& timeout,
const std::string& post_data,
const net::NetworkTrafficAnnotationTag& annotation_tag),
(override));
protected:
friend class AccountCheckerTest;
};
class AccountCheckerTest : public testing::Test {
public:
AccountCheckerTest() = default;
~AccountCheckerTest() override = default;
void SetUp() override {
test_features_.InitAndEnableFeature(kShoppingList);
RegisterPrefs(pref_service_.registry());
scoped_refptr<network::SharedURLLoaderFactory> test_url_loader_factory =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_);
fetcher_ = std::make_unique<MockEndpointFetcher>();
sync_service_ = std::make_unique<syncer::TestSyncService>();
account_checker_ = std::make_unique<SpyAccountChecker>(
&pref_service_, identity_test_env_.identity_manager(),
sync_service_.get(), std::move(test_url_loader_factory));
ASSERT_EQ(false, account_checker_->IsSignedIn());
ON_CALL(*account_checker_, CreateEndpointFetcher).WillByDefault([this]() {
return std::move(fetcher_);
});
}
void SetFetchResponse(std::string response) {
fetcher_.reset();
fetcher_ = std::make_unique<MockEndpointFetcher>();
fetcher_->SetFetchResponse(response);
}
void FetchPriceEmailPref() { account_checker_->FetchPriceEmailPref(); }
protected:
base::test::TaskEnvironment task_environment_;
signin::IdentityTestEnvironment identity_test_env_;
base::test::ScopedFeatureList test_features_;
TestingPrefServiceSimple pref_service_;
network::TestURLLoaderFactory test_url_loader_factory_;
data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
std::unique_ptr<MockEndpointFetcher> fetcher_;
std::unique_ptr<syncer::TestSyncService> sync_service_;
std::unique_ptr<SpyAccountChecker> account_checker_;
};
TEST_F(AccountCheckerTest,
TestFetchWaaStatusOnSignin_ReplaceSyncPromosWithSignInPromosDisabled) {
base::test::ScopedFeatureList test_specific_features;
test_specific_features.InitAndDisableFeature(
syncer::kReplaceSyncPromosWithSignInPromos);
const char waa_oauth_name[] = "web_history";
const char waa_query_url[] =
"https://history.google.com/history/api/lookup?client=web_app";
const char waa_oauth_scope[] = "https://www.googleapis.com/auth/chromesync";
const char waa_content_type[] = "application/json; charset=UTF-8";
constexpr base::TimeDelta waa_timeout = base::Milliseconds(30000);
const char waa_post_data[] = "";
// ReplaceSyncPromosWithSignInPromos is disabled, so signing in should not
// trigger WAA request.
EXPECT_CALL(*account_checker_,
CreateEndpointFetcher(waa_oauth_name, GURL(waa_query_url),
endpoint_fetcher::HttpMethod::kGet,
waa_content_type,
std::vector<std::string>{waa_oauth_scope},
waa_timeout, waa_post_data, _))
.Times(0);
identity_test_env_.MakePrimaryAccountAvailable("mock_email@gmail.com",
signin::ConsentLevel::kSignin);
ASSERT_EQ(false, account_checker_->IsSignedIn());
}
TEST_F(AccountCheckerTest, TestFetchPriceEmailPref) {
{
InSequence s;
// Fetch email pref.
EXPECT_CALL(
*account_checker_,
CreateEndpointFetcher(kOAuthName, GURL(kNotificationsPrefUrl),
endpoint_fetcher::HttpMethod::kGet, kContentType,
std::vector<std::string>{kOAuthScope}, kTimeout,
kEmptyPostData, _));
}
ASSERT_EQ(false, pref_service_.GetBoolean(kPriceEmailNotificationsEnabled));
identity_test_env_.MakePrimaryAccountAvailable("mock_email@gmail.com",
signin::ConsentLevel::kSync);
SetFetchResponse("{ \"preferences\": { \"price_track_email\" : true } }");
FetchPriceEmailPref();
pref_service_.user_prefs_store()->WaitForValue(
kPriceEmailNotificationsEnabled, base::Value(true));
ASSERT_EQ(true, pref_service_.GetBoolean(kPriceEmailNotificationsEnabled));
}
TEST_F(AccountCheckerTest, TestSendPriceEmailPrefOnPrefChange) {
{
InSequence s;
// Send email pref.
EXPECT_CALL(
*account_checker_,
CreateEndpointFetcher(kOAuthName, GURL(kNotificationsPrefUrl),
endpoint_fetcher::HttpMethod::kPost, kContentType,
std::vector<std::string>{kOAuthScope}, kTimeout,
kPostData, _));
}
ASSERT_EQ(false, pref_service_.GetBoolean(kPriceEmailNotificationsEnabled));
identity_test_env_.MakePrimaryAccountAvailable("mock_email@gmail.com",
signin::ConsentLevel::kSync);
SetFetchResponse("{ \"preferences\": { \"price_track_email\" : true } }");
pref_service_.SetBoolean(kPriceEmailNotificationsEnabled, true);
pref_service_.user_prefs_store()->WaitForValue(
kPriceEmailNotificationsEnabled, base::Value(true));
ASSERT_EQ(true, pref_service_.GetBoolean(kPriceEmailNotificationsEnabled));
}
} // namespace commerce
|