OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "blimp/client/core/authenticator.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "google_apis/gaia/fake_identity_provider.h" |
| 10 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 11 #include "google_apis/gaia/google_service_auth_error.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace blimp { |
| 16 namespace client { |
| 17 namespace { |
| 18 |
| 19 class AuthenticatorTest : public testing::Test { |
| 20 public: |
| 21 class MockAuthenticator : public Authenticator { |
| 22 public: |
| 23 explicit MockAuthenticator( |
| 24 std::unique_ptr<IdentityProvider> identity_provider) |
| 25 : Authenticator(std::move(identity_provider)), |
| 26 success_(0), |
| 27 fail_(0), |
| 28 refresh_(0) {} |
| 29 ~MockAuthenticator() override {}; |
| 30 |
| 31 void OnGetTokenSuccess( |
| 32 const OAuth2TokenService::Request* request, |
| 33 const std::string& access_token, |
| 34 const base::Time& expiration_time) override { |
| 35 Authenticator::OnGetTokenSuccess(request, access_token, expiration_time); |
| 36 success_++; |
| 37 token_ = access_token; |
| 38 } |
| 39 |
| 40 void OnGetTokenFailure( |
| 41 const OAuth2TokenService::Request* request, |
| 42 const GoogleServiceAuthError& error) override { |
| 43 Authenticator::OnGetTokenFailure(request, error); |
| 44 fail_++; |
| 45 token_ = std::string(); |
| 46 } |
| 47 |
| 48 void OnRefreshTokenAvailable(const std::string& account_id) override { |
| 49 Authenticator::OnRefreshTokenAvailable(account_id); |
| 50 refresh_++; |
| 51 } |
| 52 |
| 53 int Succeeded() { return success_; } |
| 54 int Failed() { return fail_; } |
| 55 int Refreshed() { return refresh_; } |
| 56 const std::string& Token() { return token_; } |
| 57 |
| 58 private: |
| 59 std::string token_; |
| 60 int success_; |
| 61 int fail_; |
| 62 int refresh_; |
| 63 }; |
| 64 |
| 65 AuthenticatorTest() {} |
| 66 ~AuthenticatorTest() override {} |
| 67 |
| 68 void SetUp() override { |
| 69 } |
| 70 |
| 71 void TearDown() override { |
| 72 } |
| 73 |
| 74 private: |
| 75 DISALLOW_COPY_AND_ASSIGN(AuthenticatorTest); |
| 76 }; |
| 77 |
| 78 TEST_F(AuthenticatorTest, TestConnect) { |
| 79 std::unique_ptr<FakeOAuth2TokenService> token_service = |
| 80 base::MakeUnique<FakeOAuth2TokenService>(); |
| 81 FakeIdentityProvider* id_provider = new FakeIdentityProvider( |
| 82 token_service.get()); |
| 83 std::unique_ptr<IdentityProvider> id_ptr = |
| 84 base::WrapUnique<IdentityProvider>((IdentityProvider*)id_provider); |
| 85 |
| 86 MockAuthenticator auth(std::move(id_ptr)); |
| 87 |
| 88 // Connect when user is not signed in. Nothing happens. |
| 89 id_provider->LogOut(); |
| 90 auth.Connect(); |
| 91 DCHECK_EQ(auth.Succeeded(), 0); |
| 92 DCHECK_EQ(auth.Failed(), 0); |
| 93 DCHECK_EQ(auth.Refreshed(), 0); |
| 94 DCHECK_EQ(auth.Token(), std::string()); |
| 95 |
| 96 FakeOAuth2TokenServiceDelegate* mock_delegate = token_service |
| 97 ->GetFakeOAuth2TokenServiceDelegate(); |
| 98 |
| 99 // Connect when user signed in, but no refresh token, refresh token observer |
| 100 // should be added. |
| 101 std::string account = "mock_account"; |
| 102 id_provider->LogIn(account); |
| 103 mock_delegate->RevokeCredentials(account); |
| 104 auth.Connect(); |
| 105 DCHECK_EQ(auth.Succeeded(), 0); |
| 106 DCHECK_EQ(auth.Failed(), 0); |
| 107 DCHECK_EQ(auth.Refreshed(), 0); |
| 108 |
| 109 // Issue refresh token, listener should be triggered, and request should be |
| 110 // sent. |
| 111 mock_delegate->UpdateCredentials(account, "mock_refresh_token"); |
| 112 DCHECK_EQ(auth.Succeeded(), 0); |
| 113 DCHECK_EQ(auth.Failed(), 0); |
| 114 DCHECK_EQ(auth.Refreshed(), 1); |
| 115 |
| 116 // Fire access token success, first request should be fulfilled. |
| 117 base::Time time; |
| 118 std::string mock_access_token = "mock_access_token"; |
| 119 token_service->IssueAllTokensForAccount(account, mock_access_token, time); |
| 120 DCHECK_EQ(auth.Succeeded(), 1); |
| 121 DCHECK_EQ(auth.Failed(), 0); |
| 122 DCHECK_EQ(auth.Token(), mock_access_token); |
| 123 |
| 124 // Connect again and fire access token failed. |
| 125 GoogleServiceAuthError error(GoogleServiceAuthError::State::REQUEST_CANCELED); |
| 126 auth.Connect(); |
| 127 token_service->IssueErrorForAllPendingRequestsForAccount(account, error); |
| 128 DCHECK_EQ(auth.Succeeded(), 1); |
| 129 DCHECK_EQ(auth.Failed(), 1); |
| 130 DCHECK_EQ(auth.Token(), std::string()); |
| 131 |
| 132 // Refresh token listener should have been removed. |
| 133 mock_delegate->UpdateCredentials(account, "mock_refresh_token"); |
| 134 DCHECK_EQ(auth.Refreshed(), 1); |
| 135 |
| 136 // Direct connect with refresh token, and no listener should be added. |
| 137 auth.Connect(); |
| 138 token_service->IssueAllTokensForAccount(account, mock_access_token, time); |
| 139 DCHECK_EQ(auth.Succeeded(), 2); |
| 140 DCHECK_EQ(auth.Token(), mock_access_token); |
| 141 mock_delegate->UpdateCredentials(account, "mock_refresh_token"); |
| 142 DCHECK_EQ(auth.Refreshed(), 1); |
| 143 } |
| 144 |
| 145 } // namespace |
| 146 } // namespace client |
| 147 } // namespace blimp |
OLD | NEW |