profile_oauth2_token_service_ios_unittest.mm revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
1// Copyright 2014 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" 6#include "components/signin/core/browser/profile_oauth2_token_service.h" 7#include "components/signin/core/browser/test_signin_client.h" 8#include "components/signin/ios/browser/profile_oauth2_token_service_ios.h" 9#include "google_apis/gaia/gaia_urls.h" 10#include "google_apis/gaia/oauth2_token_service_test_util.h" 11#include "ios/public/test/fake_profile_oauth2_token_service_ios_provider.h" 12#include "net/url_request/test_url_fetcher_factory.h" 13#include "testing/gtest/include/gtest/gtest.h" 14 15class ProfileOAuth2TokenServiceIOSTest : public testing::Test, 16 public OAuth2TokenService::Consumer, 17 public OAuth2TokenService::Observer { 18 public: 19 ProfileOAuth2TokenServiceIOSTest() 20 : OAuth2TokenService::Consumer("test_consumer_id"), 21 factory_(NULL), 22 token_available_count_(0), 23 token_revoked_count_(0), 24 tokens_loaded_count_(0), 25 access_token_success_(0), 26 access_token_failure_(0), 27 last_access_token_error_(GoogleServiceAuthError::NONE) {} 28 29 virtual void SetUp() OVERRIDE { 30 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_revoke_url(), 31 "", 32 net::HTTP_OK, 33 net::URLRequestStatus::SUCCESS); 34 fake_provider_ = client_.GetIOSProviderAsFake(); 35 oauth2_service_.Initialize(&client_); 36 oauth2_service_.AddObserver(this); 37 } 38 39 virtual void TearDown() OVERRIDE { 40 oauth2_service_.RemoveObserver(this); 41 oauth2_service_.Shutdown(); 42 } 43 44 // OAuth2TokenService::Consumer implementation. 45 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, 46 const std::string& access_token, 47 const base::Time& expiration_time) OVERRIDE { 48 ++access_token_success_; 49 } 50 51 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, 52 const GoogleServiceAuthError& error) OVERRIDE { 53 ++access_token_failure_; 54 last_access_token_error_ = error; 55 }; 56 57 // OAuth2TokenService::Observer implementation. 58 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE { 59 ++token_available_count_; 60 } 61 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE { 62 ++token_revoked_count_; 63 } 64 virtual void OnRefreshTokensLoaded() OVERRIDE { ++tokens_loaded_count_; } 65 66 void ResetObserverCounts() { 67 token_available_count_ = 0; 68 token_revoked_count_ = 0; 69 tokens_loaded_count_ = 0; 70 token_available_count_ = 0; 71 access_token_failure_ = 0; 72 } 73 74 protected: 75 base::MessageLoop message_loop_; 76 net::FakeURLFetcherFactory factory_; 77 TestSigninClient client_; 78 ios::FakeProfileOAuth2TokenServiceIOSProvider* fake_provider_; 79 ProfileOAuth2TokenServiceIOS oauth2_service_; 80 TestingOAuth2TokenServiceConsumer consumer_; 81 int token_available_count_; 82 int token_revoked_count_; 83 int tokens_loaded_count_; 84 int access_token_success_; 85 int access_token_failure_; 86 GoogleServiceAuthError last_access_token_error_; 87}; 88 89TEST_F(ProfileOAuth2TokenServiceIOSTest, LoadRevokeCredentialsOneAccount) { 90 fake_provider_->AddAccount("account_id"); 91 oauth2_service_.LoadCredentials("account_id"); 92 base::RunLoop().RunUntilIdle(); 93 EXPECT_EQ(1, token_available_count_); 94 EXPECT_EQ(1, tokens_loaded_count_); 95 EXPECT_EQ(0, token_revoked_count_); 96 EXPECT_EQ(1U, oauth2_service_.GetAccounts().size()); 97 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id")); 98 99 ResetObserverCounts(); 100 oauth2_service_.RevokeAllCredentials(); 101 EXPECT_EQ(0, token_available_count_); 102 EXPECT_EQ(0, tokens_loaded_count_); 103 EXPECT_EQ(1, token_revoked_count_); 104 EXPECT_EQ(0U, oauth2_service_.GetAccounts().size()); 105 EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); 106} 107 108TEST_F(ProfileOAuth2TokenServiceIOSTest, 109 LoadRevokeCredentialsMultipleAccounts) { 110 fake_provider_->AddAccount("account_id_1"); 111 fake_provider_->AddAccount("account_id_2"); 112 fake_provider_->AddAccount("account_id_3"); 113 oauth2_service_.LoadCredentials("account_id_1"); 114 base::RunLoop().RunUntilIdle(); 115 EXPECT_EQ(3, token_available_count_); 116 EXPECT_EQ(1, tokens_loaded_count_); 117 EXPECT_EQ(0, token_revoked_count_); 118 EXPECT_EQ(3U, oauth2_service_.GetAccounts().size()); 119 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); 120 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_2")); 121 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_3")); 122 123 ResetObserverCounts(); 124 oauth2_service_.RevokeAllCredentials(); 125 EXPECT_EQ(0, token_available_count_); 126 EXPECT_EQ(0, tokens_loaded_count_); 127 EXPECT_EQ(3, token_revoked_count_); 128 EXPECT_EQ(0U, oauth2_service_.GetAccounts().size()); 129 EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); 130 EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_2")); 131 EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_3")); 132} 133 134TEST_F(ProfileOAuth2TokenServiceIOSTest, ReloadCredentials) { 135 fake_provider_->AddAccount("account_id_1"); 136 fake_provider_->AddAccount("account_id_2"); 137 fake_provider_->AddAccount("account_id_3"); 138 oauth2_service_.LoadCredentials("account_id_1"); 139 base::RunLoop().RunUntilIdle(); 140 141 // Change the accounts. 142 ResetObserverCounts(); 143 fake_provider_->ClearAccounts(); 144 fake_provider_->AddAccount("account_id_1"); 145 fake_provider_->AddAccount("account_id_4"); 146 oauth2_service_.ReloadCredentials(); 147 148 EXPECT_EQ(1, token_available_count_); 149 EXPECT_EQ(0, tokens_loaded_count_); 150 EXPECT_EQ(2, token_revoked_count_); 151 EXPECT_EQ(2U, oauth2_service_.GetAccounts().size()); 152 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); 153 EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_2")); 154 EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_3")); 155 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_4")); 156} 157 158TEST_F(ProfileOAuth2TokenServiceIOSTest, StartRequestSuccess) { 159 fake_provider_->AddAccount("account_id_1"); 160 oauth2_service_.LoadCredentials("account_id_1"); 161 base::RunLoop().RunUntilIdle(); 162 163 // Fetch access tokens. 164 ResetObserverCounts(); 165 OAuth2TokenService::ScopeSet scopes; 166 scopes.insert("scope"); 167 scoped_ptr<OAuth2TokenService::Request> request( 168 oauth2_service_.StartRequest("account_id_1", scopes, this)); 169 EXPECT_EQ(0, access_token_success_); 170 EXPECT_EQ(0, access_token_failure_); 171 172 ResetObserverCounts(); 173 fake_provider_->IssueAccessTokenForAllRequests(); 174 base::RunLoop().RunUntilIdle(); 175 EXPECT_EQ(1, access_token_success_); 176 EXPECT_EQ(0, access_token_failure_); 177} 178 179TEST_F(ProfileOAuth2TokenServiceIOSTest, StartRequestFailure) { 180 fake_provider_->AddAccount("account_id_1"); 181 oauth2_service_.LoadCredentials("account_id_1"); 182 base::RunLoop().RunUntilIdle(); 183 184 // Fetch access tokens. 185 ResetObserverCounts(); 186 OAuth2TokenService::ScopeSet scopes; 187 scopes.insert("scope"); 188 scoped_ptr<OAuth2TokenService::Request> request( 189 oauth2_service_.StartRequest("account_id_1", scopes, this)); 190 EXPECT_EQ(0, access_token_success_); 191 EXPECT_EQ(0, access_token_failure_); 192 193 ResetObserverCounts(); 194 fake_provider_->IssueAccessTokenErrorForAllRequests(); 195 base::RunLoop().RunUntilIdle(); 196 EXPECT_EQ(0, access_token_success_); 197 EXPECT_EQ(1, access_token_failure_); 198} 199