Chromium Code Reviews| Index: chrome/browser/signin/account_service_flag_fetcher_unittest.cc |
| diff --git a/chrome/browser/signin/account_service_flag_fetcher_unittest.cc b/chrome/browser/signin/account_service_flag_fetcher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..757af01d2ccb88c66f7b510c05e14dd8395d1959 |
| --- /dev/null |
| +++ b/chrome/browser/signin/account_service_flag_fetcher_unittest.cc |
| @@ -0,0 +1,293 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/message_loop/message_loop_proxy.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/string_util.h" |
| +#include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| +#include "components/signin/core/browser/account_service_flag_fetcher.h" |
| +#include "google_apis/gaia/gaia_urls.h" |
| +#include "net/url_request/test_url_fetcher_factory.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +const char kAccountId[] = "user@gmail.com"; |
| +const char kDifferentAccountId[] = "some_other_user@gmail.com"; |
| + |
| +// TODO(treib): This class should really live in components/signin/ next to the |
| +// AccountServiceFlagFetcher, but it uses the FakePO2TS which lives in |
| +// chrome/browser/ (because it uses the AndroidPO2TS which depends on stuff from |
| +// chrome/browser/). So when the AndroidPO2TS is componentized, then this should |
| +// move as well. |
| +class AccountServiceFlagFetcherTest : public testing::Test { |
| + public: |
| + virtual void SetUp() OVERRIDE { |
| + token_service_.reset(new FakeProfileOAuth2TokenService()); |
| + request_context_ = new net::TestURLRequestContextGetter( |
| + base::MessageLoopProxy::current()); |
| + url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(NULL)); |
| + service_flags_.clear(); |
| + service_flags_.push_back("some_flag"); |
| + service_flags_.push_back("another_flag"); |
| + service_flags_.push_back("andonemore"); |
| + } |
| + |
| + MOCK_METHOD2(OnFlagsFetched, |
| + void(AccountServiceFlagFetcher::ResultCode result, |
| + const std::vector<std::string>& flags)); |
| + |
| + protected: |
| + void SetValidLoginResponse() { |
| + url_fetcher_factory_->SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth1_login_url(), |
| + std::string("SID=sid\nLSID=lsid\nAuth=auth\n"), |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + } |
| + |
| + void SetFailedLoginResponse() { |
| + url_fetcher_factory_->SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth1_login_url(), |
| + std::string(), |
| + net::HTTP_OK, |
| + net::URLRequestStatus::CANCELED); |
| + } |
| + |
| + void SetValidGetUserInfoResponse() { |
| + url_fetcher_factory_->SetFakeResponse( |
| + GaiaUrls::GetInstance()->get_user_info_url(), |
| + BuildGetUserInfoResponse(), |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + } |
| + |
| + void SetInvalidGetUserInfoResponse() { |
| + url_fetcher_factory_->SetFakeResponse( |
| + GaiaUrls::GetInstance()->get_user_info_url(), |
| + std::string("allServicesIsMissing=true"), |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + } |
| + |
| + void SetFailedGetUserInfoResponse() { |
| + url_fetcher_factory_->SetFakeResponse( |
| + GaiaUrls::GetInstance()->get_user_info_url(), |
| + std::string(), |
| + net::HTTP_OK, |
| + net::URLRequestStatus::CANCELED); |
| + } |
| + |
| + std::string BuildGetUserInfoResponse() const { |
| + return "allServices=" + JoinString(service_flags_, ','); |
| + } |
| + |
| + base::MessageLoop message_loop_; |
| + scoped_ptr<FakeProfileOAuth2TokenService> token_service_; |
| + scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| + scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_; |
| + net::ResponseCookies cookies_; |
| + std::vector<std::string> service_flags_; |
| +}; |
| + |
| +TEST_F(AccountServiceFlagFetcherTest, Success) { |
| + token_service_->UpdateCredentials(kAccountId, "refresh_token"); |
| + SetValidLoginResponse(); |
| + SetValidGetUserInfoResponse(); |
| + |
| + AccountServiceFlagFetcher fetcher( |
| + kAccountId, |
| + token_service_.get(), |
| + request_context_.get(), |
| + base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, |
| + base::Unretained(this))); |
| + |
| + // Since a refresh token is already available, we should immediately get a |
| + // request for an access token. |
| + EXPECT_EQ(1U, token_service_->GetPendingRequests().size()); |
| + |
| + token_service_->IssueAllTokensForAccount( |
| + kAccountId, |
| + "access_token", |
| + base::Time::Now() + base::TimeDelta::FromHours(1)); |
| + |
| + EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SUCCESS, |
| + service_flags_)); |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(AccountServiceFlagFetcherTest, SuccessAfterWaitingForRefreshToken) { |
| + SetValidLoginResponse(); |
| + SetValidGetUserInfoResponse(); |
| + |
| + AccountServiceFlagFetcher fetcher( |
| + kAccountId, |
| + token_service_.get(), |
| + request_context_.get(), |
| + base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, |
| + base::Unretained(this))); |
| + |
| + // Since there is no refresh token yet, we should not get a request for an |
| + // access token at this point. |
| + EXPECT_EQ(0U, token_service_->GetPendingRequests().size()); |
| + |
| + token_service_->UpdateCredentials(kAccountId, "refresh_token"); |
| + |
| + // Now there is a refresh token and we should have got a request for an |
| + // access token. |
| + EXPECT_EQ(1U, token_service_->GetPendingRequests().size()); |
| + |
| + token_service_->IssueAllTokensForAccount( |
| + kAccountId, |
| + "access_token", |
| + base::Time::Now() + base::TimeDelta::FromHours(1)); |
| + |
| + EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SUCCESS, |
| + service_flags_)); |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(AccountServiceFlagFetcherTest, NoRefreshToken) { |
| + AccountServiceFlagFetcher fetcher( |
| + kAccountId, |
| + token_service_.get(), |
| + request_context_.get(), |
| + base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, |
| + base::Unretained(this))); |
| + |
| + token_service_->UpdateCredentials(kDifferentAccountId, "refresh_token"); |
| + |
| + // Credentials for a different user should be ignored, i.e. not result in a |
| + // request for an access token. |
| + EXPECT_EQ(0U, token_service_->GetPendingRequests().size()); |
| + |
| + // After all refresh tokens have been loaded, there is still no token for our |
| + // user, so we expect a token error. |
| + EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::TOKEN_ERROR, |
| + std::vector<std::string>())); |
| + token_service_->IssueAllRefreshTokensLoaded(); |
| +} |
| + |
| +TEST_F(AccountServiceFlagFetcherTest, GetTokenFailure) { |
| + token_service_->UpdateCredentials(kAccountId, "refresh_token"); |
| + |
| + AccountServiceFlagFetcher fetcher( |
| + kAccountId, |
| + token_service_.get(), |
| + request_context_.get(), |
| + base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, |
| + base::Unretained(this))); |
| + |
| + // On failure to get an access token we expect a token error. |
| + EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::TOKEN_ERROR, |
| + std::vector<std::string>())); |
| + token_service_->IssueErrorForAllPendingRequestsForAccount( |
| + kAccountId, |
| + GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| +} |
| + |
| +TEST_F(AccountServiceFlagFetcherTest, ClientLoginFailure) { |
| + token_service_->UpdateCredentials(kAccountId, "refresh_token"); |
| + SetFailedLoginResponse(); |
| + |
| + AccountServiceFlagFetcher fetcher( |
| + kAccountId, |
| + token_service_.get(), |
| + request_context_.get(), |
| + base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, |
| + base::Unretained(this))); |
| + |
| + token_service_->IssueAllTokensForAccount( |
| + kAccountId, |
| + "access_token", |
| + base::Time::Now() + base::TimeDelta::FromHours(1)); |
| + |
| + // Login failure should result in a service error. |
| + EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SERVICE_ERROR, |
| + std::vector<std::string>())); |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(AccountServiceFlagFetcherTest, GetUserInfoInvalidResponse) { |
| + 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
|
| + token_service_->UpdateCredentials(kAccountId, "refresh_token"); |
| + SetValidLoginResponse(); |
| + SetInvalidGetUserInfoResponse(); |
| + |
| + AccountServiceFlagFetcher fetcher( |
| + kAccountId, |
| + token_service_.get(), |
| + request_context_.get(), |
| + base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, |
| + base::Unretained(this))); |
| + |
| + token_service_->IssueAllTokensForAccount( |
| + kAccountId, |
| + "access_token", |
| + base::Time::Now() + base::TimeDelta::FromHours(1)); |
| + |
| + // Invalid response data from GetUserInfo should result in a service error. |
| + EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SERVICE_ERROR, |
| + std::vector<std::string>())); |
| + run_loop.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(AccountServiceFlagFetcherTest, GetUserInfoFailure) { |
| + token_service_->UpdateCredentials(kAccountId, "refresh_token"); |
| + SetValidLoginResponse(); |
| + SetFailedGetUserInfoResponse(); |
| + |
| + AccountServiceFlagFetcher fetcher( |
| + kAccountId, |
| + token_service_.get(), |
| + request_context_.get(), |
| + base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, |
| + base::Unretained(this))); |
| + |
| + token_service_->IssueAllTokensForAccount( |
| + kAccountId, |
| + "access_token", |
| + base::Time::Now() + base::TimeDelta::FromHours(1)); |
| + |
| + // Failed GetUserInfo call should result in a service error. |
| + EXPECT_CALL(*this, OnFlagsFetched(AccountServiceFlagFetcher::SERVICE_ERROR, |
| + std::vector<std::string>())); |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(AccountServiceFlagFetcherTest, DestroyFetcher) { |
| + token_service_->UpdateCredentials(kAccountId, "refresh_token"); |
| + SetValidLoginResponse(); |
| + SetValidGetUserInfoResponse(); |
| + |
| + // When the fetcher is destroyed before the request completes, OnFlagsFetched |
| + // should not be called. |
| + EXPECT_CALL(*this, OnFlagsFetched(testing::_, testing::_)).Times(0); |
| + |
| + { |
| + AccountServiceFlagFetcher fetcher( |
| + kAccountId, |
| + token_service_.get(), |
| + request_context_.get(), |
| + base::Bind(&AccountServiceFlagFetcherTest::OnFlagsFetched, |
| + base::Unretained(this))); |
| + |
| + token_service_->IssueAllTokensForAccount( |
| + kAccountId, |
| + "access_token", |
| + base::Time::Now() + base::TimeDelta::FromHours(1)); |
| + } |
| + |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
| +} |