Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/message_loop/message_loop_proxy.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | |
| 14 #include "components/signin/core/browser/account_service_flag_fetcher.h" | |
| 15 #include "google_apis/gaia/gaia_urls.h" | |
| 16 #include "net/url_request/test_url_fetcher_factory.h" | |
| 17 #include "net/url_request/url_request_test_util.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 const char kAccountId[] = "user@gmail.com"; | |
| 22 const char kDifferentAccountId[] = "some_other_user@gmail.com"; | |
| 23 | |
| 24 // TODO(treib): This class should really live in components/signin/ next to the | |
| 25 // AccountServiceFlagFetcher, but it uses the FakePO2TS which lives in | |
| 26 // chrome/browser/ (because it uses the AndroidPO2TS which depends on stuff from | |
| 27 // chrome/browser/). So when the AndroidPO2TS is componentized, then this should | |
| 28 // move as well. | |
| 29 class AccountServiceFlagFetcherTest : public testing::Test { | |
| 30 public: | |
| 31 virtual void SetUp() OVERRIDE { | |
| 32 token_service_.reset(new FakeProfileOAuth2TokenService()); | |
| 33 request_context_ = new net::TestURLRequestContextGetter( | |
| 34 base::MessageLoopProxy::current()); | |
| 35 url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(NULL)); | |
| 36 service_flags_.clear(); | |
| 37 service_flags_.push_back("some_flag"); | |
| 38 service_flags_.push_back("another_flag"); | |
| 39 service_flags_.push_back("andonemore"); | |
| 40 } | |
| 41 | |
| 42 MOCK_METHOD2(OnFlagsFetched, | |
| 43 void(AccountServiceFlagFetcher::ResultCode result, | |
| 44 const std::vector<std::string>& flags)); | |
| 45 | |
| 46 protected: | |
| 47 void SetValidLoginResponse() { | |
| 48 url_fetcher_factory_->SetFakeResponse( | |
| 49 GaiaUrls::GetInstance()->oauth1_login_url(), | |
| 50 std::string("SID=sid\nLSID=lsid\nAuth=auth\n"), | |
| 51 net::HTTP_OK, | |
| 52 net::URLRequestStatus::SUCCESS); | |
| 53 } | |
| 54 | |
| 55 void SetFailedLoginResponse() { | |
| 56 url_fetcher_factory_->SetFakeResponse( | |
| 57 GaiaUrls::GetInstance()->oauth1_login_url(), | |
| 58 std::string(), | |
| 59 net::HTTP_OK, | |
| 60 net::URLRequestStatus::CANCELED); | |
| 61 } | |
| 62 | |
| 63 void SetValidGetUserInfoResponse() { | |
| 64 url_fetcher_factory_->SetFakeResponse( | |
| 65 GaiaUrls::GetInstance()->get_user_info_url(), | |
| 66 BuildGetUserInfoResponse(), | |
| 67 net::HTTP_OK, | |
| 68 net::URLRequestStatus::SUCCESS); | |
| 69 } | |
| 70 | |
| 71 void SetInvalidGetUserInfoResponse() { | |
| 72 url_fetcher_factory_->SetFakeResponse( | |
| 73 GaiaUrls::GetInstance()->get_user_info_url(), | |
| 74 std::string("allServicesIsMissing=true"), | |
| 75 net::HTTP_OK, | |
| 76 net::URLRequestStatus::SUCCESS); | |
| 77 } | |
| 78 | |
| 79 void SetFailedGetUserInfoResponse() { | |
| 80 url_fetcher_factory_->SetFakeResponse( | |
| 81 GaiaUrls::GetInstance()->get_user_info_url(), | |
| 82 std::string(), | |
| 83 net::HTTP_OK, | |
| 84 net::URLRequestStatus::CANCELED); | |
| 85 } | |
| 86 | |
| 87 std::string BuildGetUserInfoResponse() const { | |
| 88 return "allServices=" + JoinString(service_flags_, ','); | |
| 89 } | |
| 90 | |
| 91 base::MessageLoop message_loop_; | |
| 92 scoped_ptr<FakeProfileOAuth2TokenService> token_service_; | |
| 93 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | |
| 94 scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_; | |
| 95 net::ResponseCookies cookies_; | |
| 96 std::vector<std::string> service_flags_; | |
| 97 }; | |
| 98 | |
| 99 TEST_F(AccountServiceFlagFetcherTest, Success) { | |
| 100 token_service_->UpdateCredentials(kAccountId, "refresh_token"); | |
| 101 SetValidLoginResponse(); | |
| 102 SetValidGetUserInfoResponse(); | |
| 103 | |
| 104 AccountServiceFlagFetcher fetcher( | |
| 105 kAccountId, | |
| 106 token_service_.get(), | |
| 107 request_context_.get(), | |
| 108 base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, | |
| 109 base::Unretained(this))); | |
| 110 | |
| 111 // Since a refresh token is already available, we should immediately get a | |
| 112 // request for an access token. | |
| 113 EXPECT_EQ(1U, token_service_->GetPendingRequests().size()); | |
| 114 | |
| 115 token_service_->IssueAllTokensForAccount( | |
| 116 kAccountId, | |
| 117 "access_token", | |
| 118 base::Time::Now() + base::TimeDelta::FromHours(1)); | |
| 119 | |
| 120 EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SUCCESS, | |
| 121 service_flags_)); | |
| 122 base::RunLoop run_loop; | |
| 123 run_loop.RunUntilIdle(); | |
| 124 } | |
| 125 | |
| 126 TEST_F(AccountServiceFlagFetcherTest, SuccessAfterWaitingForRefreshToken) { | |
| 127 SetValidLoginResponse(); | |
| 128 SetValidGetUserInfoResponse(); | |
| 129 | |
| 130 AccountServiceFlagFetcher fetcher( | |
| 131 kAccountId, | |
| 132 token_service_.get(), | |
| 133 request_context_.get(), | |
| 134 base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, | |
| 135 base::Unretained(this))); | |
| 136 | |
| 137 // Since there is no refresh token yet, we should not get a request for an | |
| 138 // access token at this point. | |
| 139 EXPECT_EQ(0U, token_service_->GetPendingRequests().size()); | |
| 140 | |
| 141 token_service_->UpdateCredentials(kAccountId, "refresh_token"); | |
| 142 | |
| 143 // Now there is a refresh token and we should have got a request for an | |
| 144 // access token. | |
| 145 EXPECT_EQ(1U, token_service_->GetPendingRequests().size()); | |
| 146 | |
| 147 token_service_->IssueAllTokensForAccount( | |
| 148 kAccountId, | |
| 149 "access_token", | |
| 150 base::Time::Now() + base::TimeDelta::FromHours(1)); | |
| 151 | |
| 152 EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SUCCESS, | |
| 153 service_flags_)); | |
| 154 base::RunLoop run_loop; | |
| 155 run_loop.RunUntilIdle(); | |
| 156 } | |
| 157 | |
| 158 TEST_F(AccountServiceFlagFetcherTest, NoRefreshToken) { | |
| 159 AccountServiceFlagFetcher fetcher( | |
| 160 kAccountId, | |
| 161 token_service_.get(), | |
| 162 request_context_.get(), | |
| 163 base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, | |
| 164 base::Unretained(this))); | |
| 165 | |
| 166 token_service_->UpdateCredentials(kDifferentAccountId, "refresh_token"); | |
| 167 | |
| 168 // Credentials for a different user should be ignored, i.e. not result in a | |
| 169 // request for an access token. | |
| 170 EXPECT_EQ(0U, token_service_->GetPendingRequests().size()); | |
| 171 | |
| 172 // After all refresh tokens have been loaded, there is still no token for our | |
| 173 // user, so we expect a token error. | |
| 174 EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::TOKEN_ERROR, | |
| 175 std::vector<std::string>())); | |
| 176 token_service_->IssueAllRefreshTokensLoaded(); | |
| 177 } | |
| 178 | |
| 179 TEST_F(AccountServiceFlagFetcherTest, GetTokenFailure) { | |
| 180 token_service_->UpdateCredentials(kAccountId, "refresh_token"); | |
| 181 | |
| 182 AccountServiceFlagFetcher fetcher( | |
| 183 kAccountId, | |
| 184 token_service_.get(), | |
| 185 request_context_.get(), | |
| 186 base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, | |
| 187 base::Unretained(this))); | |
| 188 | |
| 189 // On failure to get an access token we expect a token error. | |
| 190 EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::TOKEN_ERROR, | |
| 191 std::vector<std::string>())); | |
| 192 token_service_->IssueErrorForAllPendingRequestsForAccount( | |
| 193 kAccountId, | |
| 194 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | |
| 195 } | |
| 196 | |
| 197 TEST_F(AccountServiceFlagFetcherTest, ClientLoginFailure) { | |
| 198 token_service_->UpdateCredentials(kAccountId, "refresh_token"); | |
| 199 SetFailedLoginResponse(); | |
| 200 | |
| 201 AccountServiceFlagFetcher fetcher( | |
| 202 kAccountId, | |
| 203 token_service_.get(), | |
| 204 request_context_.get(), | |
| 205 base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, | |
| 206 base::Unretained(this))); | |
| 207 | |
| 208 token_service_->IssueAllTokensForAccount( | |
| 209 kAccountId, | |
| 210 "access_token", | |
| 211 base::Time::Now() + base::TimeDelta::FromHours(1)); | |
| 212 | |
| 213 // Login failure should result in a service error. | |
| 214 EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SERVICE_ERROR, | |
| 215 std::vector<std::string>())); | |
| 216 base::RunLoop run_loop; | |
| 217 run_loop.RunUntilIdle(); | |
| 218 } | |
| 219 | |
| 220 TEST_F(AccountServiceFlagFetcherTest, GetUserInfoInvalidResponse) { | |
| 221 base::RunLoop run_loop; | |
|
Bernhard Bauer
2014/06/02 10:25:42
If you are using RunUntilIdle, for consistency you
Marc Treib
2014/06/02 16:19:56
With TestURLFetcherFactory instead of FakeURLFetch
| |
| 222 token_service_->UpdateCredentials(kAccountId, "refresh_token"); | |
| 223 SetValidLoginResponse(); | |
| 224 SetInvalidGetUserInfoResponse(); | |
| 225 | |
| 226 AccountServiceFlagFetcher fetcher( | |
| 227 kAccountId, | |
| 228 token_service_.get(), | |
| 229 request_context_.get(), | |
| 230 base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, | |
| 231 base::Unretained(this))); | |
| 232 | |
| 233 token_service_->IssueAllTokensForAccount( | |
| 234 kAccountId, | |
| 235 "access_token", | |
| 236 base::Time::Now() + base::TimeDelta::FromHours(1)); | |
| 237 | |
| 238 // Invalid response data from GetUserInfo should result in a service error. | |
| 239 EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SERVICE_ERROR, | |
| 240 std::vector<std::string>())); | |
| 241 run_loop.RunUntilIdle(); | |
| 242 } | |
| 243 | |
| 244 TEST_F(AccountServiceFlagFetcherTest, GetUserInfoFailure) { | |
| 245 token_service_->UpdateCredentials(kAccountId, "refresh_token"); | |
| 246 SetValidLoginResponse(); | |
| 247 SetFailedGetUserInfoResponse(); | |
| 248 | |
| 249 AccountServiceFlagFetcher fetcher( | |
| 250 kAccountId, | |
| 251 token_service_.get(), | |
| 252 request_context_.get(), | |
| 253 base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, | |
| 254 base::Unretained(this))); | |
| 255 | |
| 256 token_service_->IssueAllTokensForAccount( | |
| 257 kAccountId, | |
| 258 "access_token", | |
| 259 base::Time::Now() + base::TimeDelta::FromHours(1)); | |
| 260 | |
| 261 // Failed GetUserInfo call should result in a service error. | |
| 262 EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SERVICE_ERROR, | |
| 263 std::vector<std::string>())); | |
| 264 base::RunLoop run_loop; | |
| 265 run_loop.RunUntilIdle(); | |
| 266 } | |
| 267 | |
| 268 TEST_F(AccountServiceFlagFetcherTest, DestroyFetcher) { | |
| 269 token_service_->UpdateCredentials(kAccountId, "refresh_token"); | |
| 270 SetValidLoginResponse(); | |
| 271 SetValidGetUserInfoResponse(); | |
| 272 | |
| 273 // When the fetcher is destroyed before the request completes, OnFlagsFetched | |
| 274 // should not be called. | |
| 275 EXPECT_CALL(*this, OnFlagsFetched(testing::_, testing::_)).Times(0); | |
| 276 | |
| 277 { | |
| 278 AccountServiceFlagFetcher fetcher( | |
| 279 kAccountId, | |
| 280 token_service_.get(), | |
| 281 request_context_.get(), | |
| 282 base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, | |
| 283 base::Unretained(this))); | |
| 284 | |
| 285 token_service_->IssueAllTokensForAccount( | |
| 286 kAccountId, | |
| 287 "access_token", | |
| 288 base::Time::Now() + base::TimeDelta::FromHours(1)); | |
| 289 } | |
| 290 | |
| 291 base::RunLoop run_loop; | |
| 292 run_loop.RunUntilIdle(); | |
| 293 } | |
| OLD | NEW |