1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/ui/passwords/account_storage_auth_helper.h"
6 
7 #include <string>
8 
9 #include "base/callback.h"
10 #include "build/build_config.h"
11 #include "chrome/browser/signin/identity_manager_factory.h"
12 #include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
13 #include "chrome/browser/signin/reauth_result.h"
14 #include "chrome/browser/ui/signin_view_controller.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/password_manager/core/browser/mock_password_feature_manager.h"
17 #include "components/signin/public/base/signin_metrics.h"
18 #include "components/signin/public/identity_manager/identity_test_utils.h"
19 #include "content/public/test/browser_task_environment.h"
20 #include "google_apis/gaia/core_account_id.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 
24 namespace {
25 
26 using testing::_;
27 
28 const signin_metrics::ReauthAccessPoint kReauthAccessPoint =
29     signin_metrics::ReauthAccessPoint::kAutofillDropdown;
30 
31 class MockSigninViewController : public SigninViewController {
32  public:
MockSigninViewController()33   MockSigninViewController() : SigninViewController(/*browser=*/nullptr) {}
34   ~MockSigninViewController() override = default;
35 
36   MOCK_METHOD(std::unique_ptr<ReauthAbortHandle>,
37               ShowReauthPrompt,
38               (const CoreAccountId&,
39                signin_metrics::ReauthAccessPoint,
40                base::OnceCallback<void(signin::ReauthResult)>),
41               (override));
42 
43   MOCK_METHOD(void,
44               ShowDiceAddAccountTab,
45               (signin_metrics::AccessPoint, const std::string&),
46               (override));
47 };
48 
49 }  // namespace
50 
51 class AccountStorageAuthHelperTest : public ::testing::Test {
52  public:
AccountStorageAuthHelperTest()53   AccountStorageAuthHelperTest()
54       : profile_(IdentityTestEnvironmentProfileAdaptor::
55                      CreateProfileForIdentityTestEnvironment()),
56         identity_test_env_adaptor_(profile_.get()),
57         auth_helper_(profile_.get(), &mock_password_feature_manager_) {
58     auth_helper_.SetSigninViewControllerGetterForTesting(base::BindRepeating(
59         [](SigninViewController* controller) { return controller; },
60         &mock_signin_view_controller_));
61   }
62   ~AccountStorageAuthHelperTest() override = default;
63 
GetIdentityManager()64   signin::IdentityManager* GetIdentityManager() {
65     return IdentityManagerFactory::GetForProfile(profile_.get());
66   }
67 
68  protected:
69   content::BrowserTaskEnvironment task_environment_;
70   std::unique_ptr<TestingProfile> profile_;
71   IdentityTestEnvironmentProfileAdaptor identity_test_env_adaptor_;
72   password_manager::MockPasswordFeatureManager mock_password_feature_manager_;
73   MockSigninViewController mock_signin_view_controller_;
74   AccountStorageAuthHelper auth_helper_;
75 };
76 
TEST_F(AccountStorageAuthHelperTest,ShouldTriggerReauthForPrimaryAccount)77 TEST_F(AccountStorageAuthHelperTest, ShouldTriggerReauthForPrimaryAccount) {
78   signin::MakePrimaryAccountAvailable(GetIdentityManager(), "alice@gmail.com");
79   EXPECT_CALL(mock_signin_view_controller_,
80               ShowReauthPrompt(GetIdentityManager()->GetPrimaryAccountId(),
81                                kReauthAccessPoint, _));
82 
83   auth_helper_.TriggerOptInReauth(kReauthAccessPoint, base::DoNothing());
84 }
85 
TEST_F(AccountStorageAuthHelperTest,ShouldSetOptInOnSucessfulReauth)86 TEST_F(AccountStorageAuthHelperTest, ShouldSetOptInOnSucessfulReauth) {
87   signin::MakePrimaryAccountAvailable(GetIdentityManager(), "alice@gmail.com");
88   EXPECT_CALL(mock_signin_view_controller_,
89               ShowReauthPrompt(GetIdentityManager()->GetPrimaryAccountId(),
90                                kReauthAccessPoint, _))
91       .WillOnce([](auto, auto,
92                    base::OnceCallback<void(signin::ReauthResult)> callback) {
93         std::move(callback).Run(signin::ReauthResult::kSuccess);
94         return nullptr;
95       });
96   EXPECT_CALL(mock_password_feature_manager_, OptInToAccountStorage);
97 
98   auth_helper_.TriggerOptInReauth(kReauthAccessPoint, base::DoNothing());
99 }
100 
TEST_F(AccountStorageAuthHelperTest,ShouldNotSetOptInOnFailedReauth)101 TEST_F(AccountStorageAuthHelperTest, ShouldNotSetOptInOnFailedReauth) {
102   signin::MakePrimaryAccountAvailable(GetIdentityManager(), "alice@gmail.com");
103   EXPECT_CALL(mock_signin_view_controller_,
104               ShowReauthPrompt(GetIdentityManager()->GetPrimaryAccountId(),
105                                kReauthAccessPoint, _))
106       .WillOnce([](auto, auto,
107                    base::OnceCallback<void(signin::ReauthResult)> callback) {
108         std::move(callback).Run(signin::ReauthResult::kCancelled);
109         return nullptr;
110       });
111   EXPECT_CALL(mock_password_feature_manager_, OptInToAccountStorage).Times(0);
112 
113   auth_helper_.TriggerOptInReauth(kReauthAccessPoint, base::DoNothing());
114 }
115 
TEST_F(AccountStorageAuthHelperTest,ShouldTriggerSigninIfDiceEnabled)116 TEST_F(AccountStorageAuthHelperTest, ShouldTriggerSigninIfDiceEnabled) {
117   const signin_metrics::AccessPoint kAcessPoint =
118       signin_metrics::AccessPoint::ACCESS_POINT_AUTOFILL_DROPDOWN;
119   EXPECT_CALL(mock_signin_view_controller_,
120               ShowDiceAddAccountTab(kAcessPoint, _));
121 
122   auth_helper_.TriggerSignIn(kAcessPoint);
123 }
124