| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/signin/ubertoken_fetcher.h" | 5 #include "chrome/browser/signin/ubertoken_fetcher.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 8 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 9 #include "chrome/browser/signin/fake_signin_manager.h" | 9 #include "chrome/browser/signin/fake_signin_manager.h" |
| 10 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 11 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
| 12 #include "chrome/browser/signin/token_service_unittest.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "google_apis/gaia/gaia_constants.h" | 15 #include "google_apis/gaia/gaia_constants.h" |
| 14 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 21 const char kTestAccountId[] = "test@gmail.com"; |
| 22 |
| 19 class MockUbertokenConsumer : public UbertokenConsumer { | 23 class MockUbertokenConsumer : public UbertokenConsumer { |
| 20 public: | 24 public: |
| 21 MockUbertokenConsumer() | 25 MockUbertokenConsumer() |
| 22 : nb_correct_token_(0), | 26 : nb_correct_token_(0), |
| 23 last_error_(GoogleServiceAuthError::AuthErrorNone()), | 27 last_error_(GoogleServiceAuthError::AuthErrorNone()), |
| 24 nb_error_(0) { | 28 nb_error_(0) { |
| 25 } | 29 } |
| 26 virtual ~MockUbertokenConsumer() {} | 30 virtual ~MockUbertokenConsumer() {} |
| 27 | 31 |
| 28 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE { | 32 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE { |
| 29 last_token_ = token; | 33 last_token_ = token; |
| 30 ++ nb_correct_token_; | 34 ++ nb_correct_token_; |
| 31 } | 35 } |
| 32 | 36 |
| 33 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) | 37 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) |
| 34 OVERRIDE { | 38 OVERRIDE { |
| 35 last_error_ = error; | 39 last_error_ = error; |
| 36 ++nb_error_; | 40 ++nb_error_; |
| 37 } | 41 } |
| 38 | 42 |
| 39 std::string last_token_; | 43 std::string last_token_; |
| 40 int nb_correct_token_; | 44 int nb_correct_token_; |
| 41 GoogleServiceAuthError last_error_; | 45 GoogleServiceAuthError last_error_; |
| 42 int nb_error_; | 46 int nb_error_; |
| 43 }; | 47 }; |
| 44 | 48 |
| 45 } // namespace | 49 } // namespace |
| 46 | 50 |
| 47 class UbertokenFetcherTest : public TokenServiceTestHarness { | 51 class UbertokenFetcherTest : public testing::Test { |
| 48 public: | 52 public: |
| 49 virtual void SetUp() OVERRIDE { | 53 virtual void SetUp() OVERRIDE { |
| 50 TokenServiceTestHarness::SetUp(); | 54 profile_.reset(new TestingProfile()); |
| 51 UpdateCredentialsOnService(); | |
| 52 fetcher_.reset(new UbertokenFetcher(profile(), &consumer_)); | 55 fetcher_.reset(new UbertokenFetcher(profile(), &consumer_)); |
| 53 CreateSigninManager("test@gmail.com"); | |
| 54 } | 56 } |
| 55 | 57 |
| 56 virtual scoped_ptr<TestingProfile> CreateProfile() OVERRIDE { | 58 scoped_ptr<TestingProfile> CreateProfile() { |
| 57 TestingProfile::Builder builder; | 59 TestingProfile::Builder builder; |
| 58 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 60 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 59 &FakeProfileOAuth2TokenService::Build); | 61 &FakeProfileOAuth2TokenService::Build); |
| 60 return builder.Build().Pass(); | 62 return builder.Build().Pass(); |
| 61 } | 63 } |
| 62 | 64 |
| 63 virtual void TearDown() OVERRIDE { | 65 virtual void TearDown() OVERRIDE { |
| 64 fetcher_.reset(); | 66 fetcher_.reset(); |
| 65 TokenServiceTestHarness::TearDown(); | |
| 66 } | 67 } |
| 67 | 68 |
| 69 TestingProfile* profile() { return profile_.get(); } |
| 70 |
| 68 protected: | 71 protected: |
| 72 content::TestBrowserThreadBundle thread_bundle_; |
| 73 scoped_ptr<TestingProfile> profile_; |
| 69 net::TestURLFetcherFactory factory_; | 74 net::TestURLFetcherFactory factory_; |
| 70 MockUbertokenConsumer consumer_; | 75 MockUbertokenConsumer consumer_; |
| 71 scoped_ptr<UbertokenFetcher> fetcher_; | 76 scoped_ptr<UbertokenFetcher> fetcher_; |
| 72 }; | 77 }; |
| 73 | 78 |
| 74 TEST_F(UbertokenFetcherTest, Basic) { | 79 TEST_F(UbertokenFetcherTest, Basic) { |
| 75 } | 80 } |
| 76 | 81 |
| 77 TEST_F(UbertokenFetcherTest, Success) { | 82 TEST_F(UbertokenFetcherTest, Success) { |
| 78 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 83 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())-> |
| 79 "refreshToken"); | 84 UpdateCredentials(kTestAccountId, "refreshToken"); |
| 80 fetcher_->StartFetchingToken(); | 85 fetcher_->StartFetchingToken(); |
| 81 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time()); | 86 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time()); |
| 82 fetcher_->OnUberAuthTokenSuccess("uberToken"); | 87 fetcher_->OnUberAuthTokenSuccess("uberToken"); |
| 83 EXPECT_EQ(0, consumer_.nb_error_); | 88 EXPECT_EQ(0, consumer_.nb_error_); |
| 84 EXPECT_EQ(1, consumer_.nb_correct_token_); | 89 EXPECT_EQ(1, consumer_.nb_correct_token_); |
| 85 EXPECT_EQ("uberToken", consumer_.last_token_); | 90 EXPECT_EQ("uberToken", consumer_.last_token_); |
| 86 } | 91 } |
| 87 | 92 |
| 88 TEST_F(UbertokenFetcherTest, NoRefreshToken) { | 93 TEST_F(UbertokenFetcherTest, NoRefreshToken) { |
| 89 fetcher_->StartFetchingToken(); | 94 fetcher_->StartFetchingToken(); |
| 90 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP); | 95 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP); |
| 91 fetcher_->OnGetTokenFailure(NULL, error); | 96 fetcher_->OnGetTokenFailure(NULL, error); |
| 92 EXPECT_EQ(1, consumer_.nb_error_); | 97 EXPECT_EQ(1, consumer_.nb_error_); |
| 93 EXPECT_EQ(0, consumer_.nb_correct_token_); | 98 EXPECT_EQ(0, consumer_.nb_correct_token_); |
| 94 } | 99 } |
| 95 | 100 |
| 96 TEST_F(UbertokenFetcherTest, FailureToGetAccessToken) { | 101 TEST_F(UbertokenFetcherTest, FailureToGetAccessToken) { |
| 97 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP); | 102 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP); |
| 98 | 103 |
| 99 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 104 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())-> |
| 100 "refreshToken"); | 105 UpdateCredentials(kTestAccountId, "refreshToken"); |
| 101 fetcher_->StartFetchingToken(); | 106 fetcher_->StartFetchingToken(); |
| 102 fetcher_->OnGetTokenFailure(NULL, error); | 107 fetcher_->OnGetTokenFailure(NULL, error); |
| 103 | 108 |
| 104 EXPECT_EQ(1, consumer_.nb_error_); | 109 EXPECT_EQ(1, consumer_.nb_error_); |
| 105 EXPECT_EQ(0, consumer_.nb_correct_token_); | 110 EXPECT_EQ(0, consumer_.nb_correct_token_); |
| 106 EXPECT_EQ("", consumer_.last_token_); | 111 EXPECT_EQ("", consumer_.last_token_); |
| 107 } | 112 } |
| 108 | 113 |
| 109 TEST_F(UbertokenFetcherTest, FailureToGetUberToken) { | 114 TEST_F(UbertokenFetcherTest, FailureToGetUberToken) { |
| 110 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP); | 115 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP); |
| 111 | 116 |
| 112 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 117 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())-> |
| 113 "refreshToken"); | 118 UpdateCredentials(kTestAccountId, "refreshToken"); |
| 114 fetcher_->StartFetchingToken(); | 119 fetcher_->StartFetchingToken(); |
| 115 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time()); | 120 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time()); |
| 116 fetcher_->OnUberAuthTokenFailure(error); | 121 fetcher_->OnUberAuthTokenFailure(error); |
| 117 | 122 |
| 118 EXPECT_EQ(1, consumer_.nb_error_); | 123 EXPECT_EQ(1, consumer_.nb_error_); |
| 119 EXPECT_EQ(0, consumer_.nb_correct_token_); | 124 EXPECT_EQ(0, consumer_.nb_correct_token_); |
| 120 EXPECT_EQ("", consumer_.last_token_); | 125 EXPECT_EQ("", consumer_.last_token_); |
| 121 } | 126 } |
| OLD | NEW |