blob: 493a0b69484d07924dbe9ed3a434a70874c7dd0c [file] [log] [blame]
blundell1e21bd32017-05-03 15:25:571// Copyright 2017 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 "base/run_loop.h"
Colin Blundell704d198e2017-06-05 11:27:136#include "components/signin/core/browser/account_info.h"
blundell1e21bd32017-05-03 15:25:577#include "components/signin/core/browser/account_tracker_service.h"
Colin Blundelle89886a2017-06-12 11:26:568#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
blundell1e21bd32017-05-03 15:25:579#include "components/signin/core/browser/fake_signin_manager.h"
10#include "components/signin/core/browser/test_signin_client.h"
11#include "components/sync_preferences/testing_pref_service_syncable.h"
12#include "mojo/public/cpp/bindings/binding_set.h"
13#include "services/identity/identity_service.h"
Colin Blundelle89886a2017-06-12 11:26:5614#include "services/identity/public/cpp/scope_set.h"
blundell1e21bd32017-05-03 15:25:5715#include "services/identity/public/interfaces/constants.mojom.h"
16#include "services/identity/public/interfaces/identity_manager.mojom.h"
17#include "services/service_manager/public/cpp/binder_registry.h"
18#include "services/service_manager/public/cpp/service_context.h"
19#include "services/service_manager/public/cpp/service_test.h"
20#include "services/service_manager/public/interfaces/service_factory.mojom.h"
21
22namespace identity {
23namespace {
24
25const std::string kTestGaiaId = "dummyId";
26const std::string kTestEmail = "[email protected]";
Colin Blundelle89886a2017-06-12 11:26:5627const std::string kTestRefreshToken = "dummy-refresh-token";
28const std::string kTestAccessToken = "access_token";
blundell1e21bd32017-05-03 15:25:5729
30class ServiceTestClient : public service_manager::test::ServiceTestClient,
31 public service_manager::mojom::ServiceFactory {
32 public:
33 ServiceTestClient(service_manager::test::ServiceTest* test,
Colin Blundell2f987fd2017-06-23 10:49:0634 AccountTrackerService* account_tracker,
Colin Blundelle89886a2017-06-12 11:26:5635 SigninManagerBase* signin_manager,
36 ProfileOAuth2TokenService* token_service)
blundell1e21bd32017-05-03 15:25:5737 : service_manager::test::ServiceTestClient(test),
Colin Blundell2f987fd2017-06-23 10:49:0638 account_tracker_(account_tracker),
Colin Blundelle89886a2017-06-12 11:26:5639 signin_manager_(signin_manager),
40 token_service_(token_service) {
blundell1e21bd32017-05-03 15:25:5741 registry_.AddInterface<service_manager::mojom::ServiceFactory>(
42 base::Bind(&ServiceTestClient::Create, base::Unretained(this)));
43 }
44
45 protected:
46 void OnBindInterface(const service_manager::BindSourceInfo& source_info,
47 const std::string& interface_name,
48 mojo::ScopedMessagePipeHandle interface_pipe) override {
49 registry_.BindInterface(source_info, interface_name,
50 std::move(interface_pipe));
51 }
52
53 void CreateService(service_manager::mojom::ServiceRequest request,
54 const std::string& name) override {
55 if (name == mojom::kServiceName) {
56 identity_service_context_.reset(new service_manager::ServiceContext(
Colin Blundell2f987fd2017-06-23 10:49:0657 base::MakeUnique<IdentityService>(account_tracker_, signin_manager_,
58 token_service_),
blundell1e21bd32017-05-03 15:25:5759 std::move(request)));
60 }
61 }
62
63 void Create(const service_manager::BindSourceInfo& source_info,
64 service_manager::mojom::ServiceFactoryRequest request) {
65 service_factory_bindings_.AddBinding(this, std::move(request));
66 }
67
68 private:
Colin Blundell2f987fd2017-06-23 10:49:0669 AccountTrackerService* account_tracker_;
blundell1e21bd32017-05-03 15:25:5770 SigninManagerBase* signin_manager_;
Colin Blundelle89886a2017-06-12 11:26:5671 ProfileOAuth2TokenService* token_service_;
blundell1e21bd32017-05-03 15:25:5772 service_manager::BinderRegistry registry_;
73 mojo::BindingSet<service_manager::mojom::ServiceFactory>
74 service_factory_bindings_;
75 std::unique_ptr<service_manager::ServiceContext> identity_service_context_;
76};
77
78class IdentityManagerTest : public service_manager::test::ServiceTest {
79 public:
80 IdentityManagerTest()
81 : ServiceTest("identity_unittests", false),
82 signin_client_(&pref_service_),
83 signin_manager_(&signin_client_, &account_tracker_) {
84 AccountTrackerService::RegisterPrefs(pref_service_.registry());
85 SigninManagerBase::RegisterProfilePrefs(pref_service_.registry());
86 SigninManagerBase::RegisterPrefs(pref_service_.registry());
87
88 account_tracker_.Initialize(&signin_client_);
89 }
90
Colin Blundell704d198e2017-06-05 11:27:1391 void OnReceivedPrimaryAccountInfo(
92 base::Closure quit_closure,
93 const base::Optional<AccountInfo>& account_info) {
94 primary_account_info_ = account_info;
blundell1e21bd32017-05-03 15:25:5795 quit_closure.Run();
96 }
97
Colin Blundell2f987fd2017-06-23 10:49:0698 void OnReceivedAccountInfoFromGaiaId(
99 base::Closure quit_closure,
100 const base::Optional<AccountInfo>& account_info) {
101 account_info_from_gaia_id_ = account_info;
102 quit_closure.Run();
103 }
104
Colin Blundelle89886a2017-06-12 11:26:56105 void OnReceivedAccessToken(base::Closure quit_closure,
106 const base::Optional<std::string>& access_token,
107 base::Time expiration_time,
108 const GoogleServiceAuthError& error) {
109 access_token_ = access_token;
110 access_token_error_ = error;
111 quit_closure.Run();
112 }
113
blundell1e21bd32017-05-03 15:25:57114 protected:
115 void SetUp() override {
116 ServiceTest::SetUp();
117
118 connector()->BindInterface(mojom::kServiceName, &identity_manager_);
119 }
120
121 // service_manager::test::ServiceTest:
122 std::unique_ptr<service_manager::Service> CreateService() override {
Colin Blundell2f987fd2017-06-23 10:49:06123 return base::MakeUnique<ServiceTestClient>(
124 this, &account_tracker_, &signin_manager_, &token_service_);
blundell1e21bd32017-05-03 15:25:57125 }
126
127 mojom::IdentityManagerPtr identity_manager_;
Colin Blundell704d198e2017-06-05 11:27:13128 base::Optional<AccountInfo> primary_account_info_;
Colin Blundell2f987fd2017-06-23 10:49:06129 base::Optional<AccountInfo> account_info_from_gaia_id_;
Colin Blundelle89886a2017-06-12 11:26:56130 base::Optional<std::string> access_token_;
131 GoogleServiceAuthError access_token_error_;
blundell1e21bd32017-05-03 15:25:57132
Colin Blundell2f987fd2017-06-23 10:49:06133 AccountTrackerService* account_tracker() { return &account_tracker_; }
blundell1e21bd32017-05-03 15:25:57134 SigninManagerBase* signin_manager() { return &signin_manager_; }
Colin Blundelle89886a2017-06-12 11:26:56135 FakeProfileOAuth2TokenService* token_service() { return &token_service_; }
blundell1e21bd32017-05-03 15:25:57136
137 private:
138 sync_preferences::TestingPrefServiceSyncable pref_service_;
139 AccountTrackerService account_tracker_;
140 TestSigninClient signin_client_;
141 FakeSigninManagerBase signin_manager_;
Colin Blundelle89886a2017-06-12 11:26:56142 FakeProfileOAuth2TokenService token_service_;
blundell1e21bd32017-05-03 15:25:57143
144 DISALLOW_COPY_AND_ASSIGN(IdentityManagerTest);
145};
146
Colin Blundell2f987fd2017-06-23 10:49:06147// Check that the primary account info is null if not signed in.
148TEST_F(IdentityManagerTest, GetPrimaryAccountInfoNotSignedIn) {
blundell1e21bd32017-05-03 15:25:57149 base::RunLoop run_loop;
Colin Blundell704d198e2017-06-05 11:27:13150 identity_manager_->GetPrimaryAccountInfo(
151 base::Bind(&IdentityManagerTest::OnReceivedPrimaryAccountInfo,
blundell1e21bd32017-05-03 15:25:57152 base::Unretained(this), run_loop.QuitClosure()));
153 run_loop.Run();
Colin Blundell704d198e2017-06-05 11:27:13154 EXPECT_FALSE(primary_account_info_);
blundell1e21bd32017-05-03 15:25:57155}
156
Colin Blundell2f987fd2017-06-23 10:49:06157// Check that the primary account info has expected values if signed in.
158TEST_F(IdentityManagerTest, GetPrimaryAccountInfoSignedIn) {
blundell1e21bd32017-05-03 15:25:57159 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
160 base::RunLoop run_loop;
Colin Blundell704d198e2017-06-05 11:27:13161 identity_manager_->GetPrimaryAccountInfo(
162 base::Bind(&IdentityManagerTest::OnReceivedPrimaryAccountInfo,
blundell1e21bd32017-05-03 15:25:57163 base::Unretained(this), run_loop.QuitClosure()));
164 run_loop.Run();
Colin Blundell704d198e2017-06-05 11:27:13165 EXPECT_TRUE(primary_account_info_);
166 EXPECT_EQ(kTestGaiaId, primary_account_info_->gaia);
167 EXPECT_EQ(kTestEmail, primary_account_info_->email);
blundell1e21bd32017-05-03 15:25:57168}
169
Colin Blundell2f987fd2017-06-23 10:49:06170// Check that the account info for a given GAIA ID is null if that GAIA ID is
171// unknown.
172TEST_F(IdentityManagerTest, GetAccountInfoForUnknownGaiaID) {
173 base::RunLoop run_loop;
174 identity_manager_->GetAccountInfoFromGaiaId(
175 kTestGaiaId,
176 base::Bind(&IdentityManagerTest::OnReceivedAccountInfoFromGaiaId,
177 base::Unretained(this), run_loop.QuitClosure()));
178 run_loop.Run();
179 EXPECT_FALSE(account_info_from_gaia_id_);
180}
181
182// Check that the account info for a given GAIA ID has expected values if that
183// GAIA ID is known.
184TEST_F(IdentityManagerTest, GetAccountInfoForKnownGaiaId) {
185 account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail);
186 base::RunLoop run_loop;
187 identity_manager_->GetAccountInfoFromGaiaId(
188 kTestGaiaId,
189 base::Bind(&IdentityManagerTest::OnReceivedAccountInfoFromGaiaId,
190 base::Unretained(this), run_loop.QuitClosure()));
191 run_loop.Run();
192 EXPECT_TRUE(account_info_from_gaia_id_);
193 EXPECT_EQ(kTestGaiaId, account_info_from_gaia_id_->gaia);
194 EXPECT_EQ(kTestEmail, account_info_from_gaia_id_->email);
195}
196
Colin Blundelle89886a2017-06-12 11:26:56197// Check that the expected error is received if requesting an access token when
198// not signed in.
199TEST_F(IdentityManagerTest, GetAccessTokenNotSignedIn) {
200 base::RunLoop run_loop;
201 identity_manager_->GetAccessToken(
202 kTestGaiaId, ScopeSet(), "dummy_consumer",
203 base::Bind(&IdentityManagerTest::OnReceivedAccessToken,
204 base::Unretained(this), run_loop.QuitClosure()));
205 run_loop.Run();
206 EXPECT_FALSE(access_token_);
207 EXPECT_EQ(GoogleServiceAuthError::State::USER_NOT_SIGNED_UP,
208 access_token_error_.state());
209}
210
211// Check that the expected access token is received if requesting an access
212// token when signed in.
213TEST_F(IdentityManagerTest, GetAccessTokenSignedIn) {
214 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
215 token_service()->UpdateCredentials(kTestGaiaId, kTestRefreshToken);
216 token_service()->set_auto_post_fetch_response_on_message_loop(true);
217 base::RunLoop run_loop;
218
219 identity_manager_->GetAccessToken(
220 kTestGaiaId, ScopeSet(), "dummy_consumer",
221 base::Bind(&IdentityManagerTest::OnReceivedAccessToken,
222 base::Unretained(this), run_loop.QuitClosure()));
223 run_loop.Run();
224 EXPECT_TRUE(access_token_);
225 EXPECT_EQ(kTestAccessToken, access_token_.value());
226 EXPECT_EQ(GoogleServiceAuthError::State::NONE, access_token_error_.state());
227}
228
blundell1e21bd32017-05-03 15:25:57229} // namespace
230} // namespace identity