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