| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "blimp/client/core/session/identity_source.h" | 5 #include "blimp/client/core/session/identity_source.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/callback.h" | 12 #include "base/callback.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "blimp/client/test/test_blimp_client_context_delegate.h" | 15 #include "blimp/client/test/test_blimp_client_context_delegate.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 using ::testing::_; | 18 using ::testing::_; |
| 18 using ::testing::Return; | 19 using ::testing::Return; |
| 19 | 20 |
| 20 namespace blimp { | 21 namespace blimp { |
| 21 namespace client { | 22 namespace client { |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 class MockIdentitySource : public IdentitySource { | 25 class MockIdentitySource : public IdentitySource { |
| 25 public: | 26 public: |
| 26 explicit MockIdentitySource(BlimpClientContextDelegate* delegate, | 27 MockIdentitySource( |
| 27 const IdentitySource::TokenCallback& callback) | 28 std::unique_ptr<IdentityProvider> identity_provider, |
| 28 : IdentitySource(delegate, callback), | 29 base::Callback<void(const GoogleServiceAuthError&)> error_callback, |
| 30 const IdentitySource::TokenCallback& callback) |
| 31 : IdentitySource(std::move(identity_provider), error_callback, callback), |
| 29 success_(0), | 32 success_(0), |
| 30 fail_(0), | 33 fail_(0), |
| 31 refresh_(0), | 34 refresh_(0), |
| 32 token_callback_count_(0) {} | 35 token_callback_count_(0) {} |
| 33 ~MockIdentitySource() override{}; | 36 ~MockIdentitySource() override{}; |
| 34 | 37 |
| 35 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 38 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 36 const std::string& access_token, | 39 const std::string& access_token, |
| 37 const base::Time& expiration_time) override { | 40 const base::Time& expiration_time) override { |
| 38 IdentitySource::OnGetTokenSuccess(request, access_token, expiration_time); | 41 IdentitySource::OnGetTokenSuccess(request, access_token, expiration_time); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 IdentitySourceTest() = default; | 97 IdentitySourceTest() = default; |
| 95 ~IdentitySourceTest() override = default; | 98 ~IdentitySourceTest() override = default; |
| 96 | 99 |
| 97 private: | 100 private: |
| 98 DISALLOW_COPY_AND_ASSIGN(IdentitySourceTest); | 101 DISALLOW_COPY_AND_ASSIGN(IdentitySourceTest); |
| 99 }; | 102 }; |
| 100 | 103 |
| 101 TEST_F(IdentitySourceTest, TestConnect) { | 104 TEST_F(IdentitySourceTest, TestConnect) { |
| 102 TestBlimpClientContextDelegate mock_blimp_delegate; | 105 TestBlimpClientContextDelegate mock_blimp_delegate; |
| 103 MockIdentitySource auth( | 106 MockIdentitySource auth( |
| 104 &mock_blimp_delegate, | 107 mock_blimp_delegate.CreateIdentityProvider(), |
| 108 base::Bind(&TestBlimpClientContextDelegate::OnAuthenticationError, |
| 109 base::Unretained(&mock_blimp_delegate)), |
| 105 base::Bind(&MockIdentitySource::MockTokenCall, base::Unretained(&auth))); | 110 base::Bind(&MockIdentitySource::MockTokenCall, base::Unretained(&auth))); |
| 106 FakeIdentityProvider* id_provider = | 111 FakeIdentityProvider* id_provider = |
| 107 static_cast<FakeIdentityProvider*>(auth.GetIdentityProvider()); | 112 static_cast<FakeIdentityProvider*>(auth.GetIdentityProvider()); |
| 108 FakeOAuth2TokenService* token_service = mock_blimp_delegate.GetTokenService(); | 113 FakeOAuth2TokenService* token_service = mock_blimp_delegate.GetTokenService(); |
| 109 | 114 |
| 110 // Connect when user is not signed in. Nothing happens. | 115 // Connect when user is not signed in. Nothing happens. |
| 111 id_provider->LogOut(); | 116 id_provider->LogOut(); |
| 112 auth.Connect(); | 117 auth.Connect(); |
| 113 DCHECK_EQ(auth.Succeeded(), 0); | 118 DCHECK_EQ(auth.Succeeded(), 0); |
| 114 DCHECK_EQ(auth.Failed(), 0); | 119 DCHECK_EQ(auth.Failed(), 0); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 DCHECK_EQ(auth.Refreshed(), 0); | 185 DCHECK_EQ(auth.Refreshed(), 0); |
| 181 DCHECK_EQ(auth.TokenCallbackCount(), 1); | 186 DCHECK_EQ(auth.TokenCallbackCount(), 1); |
| 182 DCHECK_EQ(auth.CallbackToken(), mock_access_token); | 187 DCHECK_EQ(auth.CallbackToken(), mock_access_token); |
| 183 } | 188 } |
| 184 | 189 |
| 185 // Test retry on token fetching when refresh token is updated during token | 190 // Test retry on token fetching when refresh token is updated during token |
| 186 // request. | 191 // request. |
| 187 TEST_F(IdentitySourceTest, TestConnectRetry) { | 192 TEST_F(IdentitySourceTest, TestConnectRetry) { |
| 188 TestBlimpClientContextDelegate mock_blimp_delegate; | 193 TestBlimpClientContextDelegate mock_blimp_delegate; |
| 189 MockIdentitySource auth( | 194 MockIdentitySource auth( |
| 190 &mock_blimp_delegate, | 195 mock_blimp_delegate.CreateIdentityProvider(), |
| 196 base::Bind(&TestBlimpClientContextDelegate::OnAuthenticationError, |
| 197 base::Unretained(&mock_blimp_delegate)), |
| 191 base::Bind(&MockIdentitySource::MockTokenCall, base::Unretained(&auth))); | 198 base::Bind(&MockIdentitySource::MockTokenCall, base::Unretained(&auth))); |
| 192 FakeOAuth2TokenService* token_service = mock_blimp_delegate.GetTokenService(); | 199 FakeOAuth2TokenService* token_service = mock_blimp_delegate.GetTokenService(); |
| 193 FakeIdentityProvider* id_provider = | 200 FakeIdentityProvider* id_provider = |
| 194 static_cast<FakeIdentityProvider*>(auth.GetIdentityProvider()); | 201 static_cast<FakeIdentityProvider*>(auth.GetIdentityProvider()); |
| 195 | 202 |
| 196 std::string account = "mock_account"; | 203 std::string account = "mock_account"; |
| 197 std::string mock_access_token = "mock_token"; | 204 std::string mock_access_token = "mock_token"; |
| 198 id_provider->LogIn(account); | 205 id_provider->LogIn(account); |
| 199 | 206 |
| 200 // Prepare refresh token. | 207 // Prepare refresh token. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 220 token_service->IssueAllTokensForAccount(account, mock_access_token, time); | 227 token_service->IssueAllTokensForAccount(account, mock_access_token, time); |
| 221 DCHECK_EQ(auth.Succeeded(), 1); | 228 DCHECK_EQ(auth.Succeeded(), 1); |
| 222 DCHECK_EQ(auth.Token(), mock_access_token); | 229 DCHECK_EQ(auth.Token(), mock_access_token); |
| 223 DCHECK_EQ(auth.TokenCallbackCount(), 1); | 230 DCHECK_EQ(auth.TokenCallbackCount(), 1); |
| 224 DCHECK_EQ(auth.CallbackToken(), mock_access_token); | 231 DCHECK_EQ(auth.CallbackToken(), mock_access_token); |
| 225 } | 232 } |
| 226 | 233 |
| 227 TEST_F(IdentitySourceTest, TestConnectFailDelegateCallback) { | 234 TEST_F(IdentitySourceTest, TestConnectFailDelegateCallback) { |
| 228 TestBlimpClientContextDelegate mock_blimp_delegate; | 235 TestBlimpClientContextDelegate mock_blimp_delegate; |
| 229 MockIdentitySource auth( | 236 MockIdentitySource auth( |
| 230 &mock_blimp_delegate, | 237 mock_blimp_delegate.CreateIdentityProvider(), |
| 238 base::Bind(&TestBlimpClientContextDelegate::OnAuthenticationError, |
| 239 base::Unretained(&mock_blimp_delegate)), |
| 231 base::Bind(&MockIdentitySource::MockTokenCall, base::Unretained(&auth))); | 240 base::Bind(&MockIdentitySource::MockTokenCall, base::Unretained(&auth))); |
| 232 FakeOAuth2TokenService* token_service = mock_blimp_delegate.GetTokenService(); | 241 FakeOAuth2TokenService* token_service = mock_blimp_delegate.GetTokenService(); |
| 233 FakeIdentityProvider* id_provider = | 242 FakeIdentityProvider* id_provider = |
| 234 static_cast<FakeIdentityProvider*>(auth.GetIdentityProvider()); | 243 static_cast<FakeIdentityProvider*>(auth.GetIdentityProvider()); |
| 235 | 244 |
| 236 std::string account = "mock_account"; | 245 std::string account = "mock_account"; |
| 237 std::string mock_access_token = "mock_token"; | 246 std::string mock_access_token = "mock_token"; |
| 238 id_provider->LogIn(account); | 247 id_provider->LogIn(account); |
| 239 | 248 |
| 240 // Prepare refresh token. | 249 // Prepare refresh token. |
| 241 FakeOAuth2TokenServiceDelegate* mock_token_service_delegate = | 250 FakeOAuth2TokenServiceDelegate* mock_token_service_delegate = |
| 242 token_service->GetFakeOAuth2TokenServiceDelegate(); | 251 token_service->GetFakeOAuth2TokenServiceDelegate(); |
| 243 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token"); | 252 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token"); |
| 244 | 253 |
| 245 // Expect delegate to show error message on non REQUEST_CANCELED errors. | 254 // Expect delegate to show error message on non REQUEST_CANCELED errors. |
| 246 auth.Connect(); | 255 auth.Connect(); |
| 247 GoogleServiceAuthError error( | 256 GoogleServiceAuthError error( |
| 248 GoogleServiceAuthError::State::CONNECTION_FAILED); | 257 GoogleServiceAuthError::State::CONNECTION_FAILED); |
| 249 | 258 |
| 250 EXPECT_CALL(mock_blimp_delegate, OnAuthenticationError(error)) | 259 EXPECT_CALL(mock_blimp_delegate, OnAuthenticationError(error)) |
| 251 .WillOnce(Return()); | 260 .WillOnce(Return()); |
| 252 token_service->IssueErrorForAllPendingRequestsForAccount(account, error); | 261 token_service->IssueErrorForAllPendingRequestsForAccount(account, error); |
| 253 | 262 |
| 254 DCHECK_EQ(auth.Failed(), 1); | 263 DCHECK_EQ(auth.Failed(), 1); |
| 255 } | 264 } |
| 256 | 265 |
| 257 } // namespace | 266 } // namespace |
| 258 } // namespace client | 267 } // namespace client |
| 259 } // namespace blimp | 268 } // namespace blimp |
| OLD | NEW |