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/session/identity_source.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "blimp/client/test/test_blimp_client_context_delegate.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace blimp { |
| 18 namespace client { |
| 19 namespace { |
| 20 |
| 21 class MockIdentitySource : public IdentitySource { |
| 22 public: |
| 23 explicit MockIdentitySource(BlimpClientContextDelegate* delegate, |
| 24 const IdentitySource::TokenCallback& callback) |
| 25 : IdentitySource(delegate, callback), |
| 26 success_(0), |
| 27 fail_(0), |
| 28 refresh_(0), |
| 29 token_callback_count_(0) {} |
| 30 ~MockIdentitySource() override{}; |
| 31 |
| 32 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 33 const std::string& access_token, |
| 34 const base::Time& expiration_time) override { |
| 35 IdentitySource::OnGetTokenSuccess(request, access_token, expiration_time); |
| 36 success_++; |
| 37 token_ = access_token; |
| 38 } |
| 39 |
| 40 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 41 const GoogleServiceAuthError& error) override { |
| 42 IdentitySource::OnGetTokenFailure(request, error); |
| 43 fail_++; |
| 44 token_.clear(); |
| 45 } |
| 46 |
| 47 void OnRefreshTokenAvailable(const std::string& account_id) override { |
| 48 IdentitySource::OnRefreshTokenAvailable(account_id); |
| 49 refresh_++; |
| 50 } |
| 51 |
| 52 void MockTokenCall(const std::string& token) { |
| 53 token_callback_count_++; |
| 54 callback_token_ = token; |
| 55 } |
| 56 |
| 57 IdentityProvider* GetIdentityProvider() { return identity_provider_.get(); } |
| 58 |
| 59 int Succeeded() { return success_; } |
| 60 int Failed() { return fail_; } |
| 61 int Refreshed() { return refresh_; } |
| 62 int TokenCallbackCount() { return token_callback_count_; } |
| 63 // Return the token passed to TokenCallback. |
| 64 const std::string& CallbackToken() { return callback_token_; } |
| 65 // Return the token get from OAuth2TokenService. |
| 66 const std::string& Token() { return token_; } |
| 67 |
| 68 // Reset test recording data. |
| 69 void ResetTestRecords() { |
| 70 success_ = 0; |
| 71 fail_ = 0; |
| 72 refresh_ = 0; |
| 73 token_callback_count_ = 0; |
| 74 callback_token_.clear(); |
| 75 token_.clear(); |
| 76 } |
| 77 |
| 78 private: |
| 79 std::string token_; |
| 80 int success_; |
| 81 int fail_; |
| 82 int refresh_; |
| 83 |
| 84 int token_callback_count_; |
| 85 std::string callback_token_; |
| 86 DISALLOW_COPY_AND_ASSIGN(MockIdentitySource); |
| 87 }; |
| 88 |
| 89 class IdentitySourceTest : public testing::Test { |
| 90 public: |
| 91 IdentitySourceTest() = default; |
| 92 ~IdentitySourceTest() override = default; |
| 93 |
| 94 private: |
| 95 DISALLOW_COPY_AND_ASSIGN(IdentitySourceTest); |
| 96 }; |
| 97 |
| 98 TEST_F(IdentitySourceTest, TestConnect) { |
| 99 TestBlimpClientContextDelegate mock_blimp_delegate; |
| 100 MockIdentitySource auth( |
| 101 &mock_blimp_delegate, |
| 102 base::Bind(&MockIdentitySource::MockTokenCall, base::Unretained(&auth))); |
| 103 FakeIdentityProvider* id_provider = |
| 104 static_cast<FakeIdentityProvider*>(auth.GetIdentityProvider()); |
| 105 FakeOAuth2TokenService* token_service = mock_blimp_delegate.GetTokenService(); |
| 106 |
| 107 // Connect when user is not signed in. Nothing happens. |
| 108 id_provider->LogOut(); |
| 109 auth.Connect(); |
| 110 DCHECK_EQ(auth.Succeeded(), 0); |
| 111 DCHECK_EQ(auth.Failed(), 0); |
| 112 DCHECK_EQ(auth.Refreshed(), 0); |
| 113 DCHECK_EQ(auth.Token(), std::string()); |
| 114 auth.ResetTestRecords(); |
| 115 |
| 116 FakeOAuth2TokenServiceDelegate* mock_token_service_delegate = |
| 117 token_service->GetFakeOAuth2TokenServiceDelegate(); |
| 118 |
| 119 // Connect when user signed in, but no refresh token, refresh token observer |
| 120 // should be added. |
| 121 std::string account = "mock_account"; |
| 122 id_provider->LogIn(account); |
| 123 mock_token_service_delegate->RevokeCredentials(account); |
| 124 // Mock duplicate connect calls in this test. |
| 125 auth.Connect(); |
| 126 auth.Connect(); |
| 127 DCHECK_EQ(auth.Succeeded(), 0); |
| 128 DCHECK_EQ(auth.Failed(), 0); |
| 129 DCHECK_EQ(auth.Refreshed(), 0); |
| 130 DCHECK_EQ(auth.TokenCallbackCount(), 0); |
| 131 auth.ResetTestRecords(); |
| 132 |
| 133 // Issue refresh token, listener should be triggered, and request should be |
| 134 // sent. |
| 135 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token"); |
| 136 DCHECK_EQ(auth.Succeeded(), 0); |
| 137 DCHECK_EQ(auth.Failed(), 0); |
| 138 DCHECK_EQ(auth.Refreshed(), 1); |
| 139 DCHECK_EQ(auth.TokenCallbackCount(), 0); |
| 140 auth.ResetTestRecords(); |
| 141 |
| 142 // Fire access token success, first request should be fulfilled. |
| 143 base::Time time; |
| 144 std::string mock_access_token = "mock_access_token"; |
| 145 token_service->IssueAllTokensForAccount(account, mock_access_token, time); |
| 146 DCHECK_EQ(auth.Succeeded(), 1); |
| 147 DCHECK_EQ(auth.Failed(), 0); |
| 148 DCHECK_EQ(auth.Token(), mock_access_token); |
| 149 DCHECK_EQ(auth.TokenCallbackCount(), 1); |
| 150 DCHECK_EQ(auth.CallbackToken(), mock_access_token); |
| 151 auth.ResetTestRecords(); |
| 152 |
| 153 // Connect again and fire access token failed. |
| 154 GoogleServiceAuthError error(GoogleServiceAuthError::State::REQUEST_CANCELED); |
| 155 auth.Connect(); |
| 156 auth.Connect(); |
| 157 token_service->IssueErrorForAllPendingRequestsForAccount(account, error); |
| 158 DCHECK_EQ(auth.Succeeded(), 0); |
| 159 DCHECK_EQ(auth.Failed(), 1); |
| 160 DCHECK_EQ(auth.Token(), std::string()); |
| 161 DCHECK_EQ(auth.TokenCallbackCount(), 0); |
| 162 auth.ResetTestRecords(); |
| 163 |
| 164 // Refresh token listener should have been removed. |
| 165 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token"); |
| 166 DCHECK_EQ(auth.Refreshed(), 0); |
| 167 auth.ResetTestRecords(); |
| 168 |
| 169 // Direct connect with refresh token, and no listener should be |
| 170 // added. |
| 171 auth.Connect(); |
| 172 auth.Connect(); |
| 173 token_service->IssueAllTokensForAccount(account, mock_access_token, time); |
| 174 DCHECK_EQ(auth.Succeeded(), 1); |
| 175 DCHECK_EQ(auth.Token(), mock_access_token); |
| 176 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token"); |
| 177 DCHECK_EQ(auth.Refreshed(), 0); |
| 178 DCHECK_EQ(auth.TokenCallbackCount(), 1); |
| 179 DCHECK_EQ(auth.CallbackToken(), mock_access_token); |
| 180 } |
| 181 |
| 182 } // namespace |
| 183 } // namespace client |
| 184 } // namespace blimp |
OLD | NEW |