blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 1 | // 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 Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 6 | #include "components/signin/core/browser/account_info.h" |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 7 | #include "components/signin/core/browser/account_tracker_service.h" |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 8 | #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 9 | #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 Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 14 | #include "services/identity/public/cpp/scope_set.h" |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 15 | #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 | |
| 22 | namespace identity { |
| 23 | namespace { |
| 24 | |
| 25 | const std::string kTestGaiaId = "dummyId"; |
| 26 | const std::string kTestEmail = "[email protected]"; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 27 | const std::string kTestRefreshToken = "dummy-refresh-token"; |
| 28 | const std::string kTestAccessToken = "access_token"; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 29 | |
| 30 | class ServiceTestClient : public service_manager::test::ServiceTestClient, |
| 31 | public service_manager::mojom::ServiceFactory { |
| 32 | public: |
| 33 | ServiceTestClient(service_manager::test::ServiceTest* test, |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 34 | AccountTrackerService* account_tracker, |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 35 | SigninManagerBase* signin_manager, |
| 36 | ProfileOAuth2TokenService* token_service) |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 37 | : service_manager::test::ServiceTestClient(test), |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 38 | account_tracker_(account_tracker), |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 39 | signin_manager_(signin_manager), |
| 40 | token_service_(token_service) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 41 | 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 Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 57 | base::MakeUnique<IdentityService>(account_tracker_, signin_manager_, |
| 58 | token_service_), |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 59 | 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 Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 69 | AccountTrackerService* account_tracker_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 70 | SigninManagerBase* signin_manager_; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 71 | ProfileOAuth2TokenService* token_service_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 72 | 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 | |
| 78 | class 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 Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 91 | void OnReceivedPrimaryAccountInfo( |
| 92 | base::Closure quit_closure, |
| 93 | const base::Optional<AccountInfo>& account_info) { |
| 94 | primary_account_info_ = account_info; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 95 | quit_closure.Run(); |
| 96 | } |
| 97 | |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 98 | 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 Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 105 | 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 | |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 114 | 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 Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 123 | return base::MakeUnique<ServiceTestClient>( |
| 124 | this, &account_tracker_, &signin_manager_, &token_service_); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | mojom::IdentityManagerPtr identity_manager_; |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 128 | base::Optional<AccountInfo> primary_account_info_; |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 129 | base::Optional<AccountInfo> account_info_from_gaia_id_; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 130 | base::Optional<std::string> access_token_; |
| 131 | GoogleServiceAuthError access_token_error_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 132 | |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 133 | AccountTrackerService* account_tracker() { return &account_tracker_; } |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 134 | SigninManagerBase* signin_manager() { return &signin_manager_; } |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 135 | FakeProfileOAuth2TokenService* token_service() { return &token_service_; } |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 136 | |
| 137 | private: |
| 138 | sync_preferences::TestingPrefServiceSyncable pref_service_; |
| 139 | AccountTrackerService account_tracker_; |
| 140 | TestSigninClient signin_client_; |
| 141 | FakeSigninManagerBase signin_manager_; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 142 | FakeProfileOAuth2TokenService token_service_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 143 | |
| 144 | DISALLOW_COPY_AND_ASSIGN(IdentityManagerTest); |
| 145 | }; |
| 146 | |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 147 | // Check that the primary account info is null if not signed in. |
| 148 | TEST_F(IdentityManagerTest, GetPrimaryAccountInfoNotSignedIn) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 149 | base::RunLoop run_loop; |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 150 | identity_manager_->GetPrimaryAccountInfo( |
| 151 | base::Bind(&IdentityManagerTest::OnReceivedPrimaryAccountInfo, |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 152 | base::Unretained(this), run_loop.QuitClosure())); |
| 153 | run_loop.Run(); |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 154 | EXPECT_FALSE(primary_account_info_); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 155 | } |
| 156 | |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 157 | // Check that the primary account info has expected values if signed in. |
| 158 | TEST_F(IdentityManagerTest, GetPrimaryAccountInfoSignedIn) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 159 | signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail); |
| 160 | base::RunLoop run_loop; |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 161 | identity_manager_->GetPrimaryAccountInfo( |
| 162 | base::Bind(&IdentityManagerTest::OnReceivedPrimaryAccountInfo, |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 163 | base::Unretained(this), run_loop.QuitClosure())); |
| 164 | run_loop.Run(); |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 165 | EXPECT_TRUE(primary_account_info_); |
| 166 | EXPECT_EQ(kTestGaiaId, primary_account_info_->gaia); |
| 167 | EXPECT_EQ(kTestEmail, primary_account_info_->email); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 168 | } |
| 169 | |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame^] | 170 | // Check that the account info for a given GAIA ID is null if that GAIA ID is |
| 171 | // unknown. |
| 172 | TEST_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. |
| 184 | TEST_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 Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 197 | // Check that the expected error is received if requesting an access token when |
| 198 | // not signed in. |
| 199 | TEST_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. |
| 213 | TEST_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 | |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 229 | } // namespace |
| 230 | } // namespace identity |