blob: 8352ee8cab9566b4a919067e7ea061be93455c75 [file] [log] [blame]
// 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 "google_apis/gaia/ubertoken_fetcher.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "google_apis/gaia/fake_oauth2_token_service.h"
#include "google_apis/gaia/gaia_constants.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kTestAccountId[] = "test@gmail.com";
class MockUbertokenConsumer : public UbertokenConsumer {
public:
MockUbertokenConsumer()
: nb_correct_token_(0),
last_error_(GoogleServiceAuthError::AuthErrorNone()),
nb_error_(0) {
}
virtual ~MockUbertokenConsumer() {}
virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE {
last_token_ = token;
++ nb_correct_token_;
}
virtual void OnUbertokenFailure(const GoogleServiceAuthError& error)
OVERRIDE {
last_error_ = error;
++nb_error_;
}
std::string last_token_;
int nb_correct_token_;
GoogleServiceAuthError last_error_;
int nb_error_;
};
} // namespace
class UbertokenFetcherTest : public testing::Test {
public:
virtual void SetUp() OVERRIDE {
request_context_getter_ = new net::TestURLRequestContextGetter(
base::MessageLoopProxy::current());
fetcher_.reset(new UbertokenFetcher(&token_service_,
&consumer_,
request_context_getter_.get()));
}
virtual void TearDown() OVERRIDE {
fetcher_.reset();
}
protected:
base::MessageLoop message_loop_;
net::TestURLFetcherFactory factory_;
FakeOAuth2TokenService token_service_;
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
MockUbertokenConsumer consumer_;
scoped_ptr<UbertokenFetcher> fetcher_;
};
TEST_F(UbertokenFetcherTest, Basic) {
}
TEST_F(UbertokenFetcherTest, Success) {
fetcher_->StartFetchingToken(kTestAccountId);
fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
fetcher_->OnUberAuthTokenSuccess("uberToken");
EXPECT_EQ(0, consumer_.nb_error_);
EXPECT_EQ(1, consumer_.nb_correct_token_);
EXPECT_EQ("uberToken", consumer_.last_token_);
}
TEST_F(UbertokenFetcherTest, NoRefreshToken) {
fetcher_->StartFetchingToken(kTestAccountId);
GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
fetcher_->OnGetTokenFailure(NULL, error);
EXPECT_EQ(1, consumer_.nb_error_);
EXPECT_EQ(0, consumer_.nb_correct_token_);
}
TEST_F(UbertokenFetcherTest, FailureToGetAccessToken) {
fetcher_->StartFetchingToken(kTestAccountId);
GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
fetcher_->OnGetTokenFailure(NULL, error);
EXPECT_EQ(1, consumer_.nb_error_);
EXPECT_EQ(0, consumer_.nb_correct_token_);
EXPECT_EQ("", consumer_.last_token_);
}
TEST_F(UbertokenFetcherTest, FailureToGetUberToken) {
fetcher_->StartFetchingToken(kTestAccountId);
GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
fetcher_->OnUberAuthTokenFailure(error);
EXPECT_EQ(1, consumer_.nb_error_);
EXPECT_EQ(0, consumer_.nb_correct_token_);
EXPECT_EQ("", consumer_.last_token_);
}