Support policy registration using a preobtained access token.
[chromium-blink-merge.git] / google_apis / gaia / ubertoken_fetcher_unittest.cc
blob8352ee8cab9566b4a919067e7ea061be93455c75
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.
5 #include "google_apis/gaia/ubertoken_fetcher.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "google_apis/gaia/fake_oauth2_token_service.h"
11 #include "google_apis/gaia/gaia_constants.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace {
18 const char kTestAccountId[] = "test@gmail.com";
20 class MockUbertokenConsumer : public UbertokenConsumer {
21 public:
22 MockUbertokenConsumer()
23 : nb_correct_token_(0),
24 last_error_(GoogleServiceAuthError::AuthErrorNone()),
25 nb_error_(0) {
27 virtual ~MockUbertokenConsumer() {}
29 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE {
30 last_token_ = token;
31 ++ nb_correct_token_;
34 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error)
35 OVERRIDE {
36 last_error_ = error;
37 ++nb_error_;
40 std::string last_token_;
41 int nb_correct_token_;
42 GoogleServiceAuthError last_error_;
43 int nb_error_;
46 } // namespace
48 class UbertokenFetcherTest : public testing::Test {
49 public:
50 virtual void SetUp() OVERRIDE {
51 request_context_getter_ = new net::TestURLRequestContextGetter(
52 base::MessageLoopProxy::current());
53 fetcher_.reset(new UbertokenFetcher(&token_service_,
54 &consumer_,
55 request_context_getter_.get()));
58 virtual void TearDown() OVERRIDE {
59 fetcher_.reset();
62 protected:
63 base::MessageLoop message_loop_;
64 net::TestURLFetcherFactory factory_;
65 FakeOAuth2TokenService token_service_;
66 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
67 MockUbertokenConsumer consumer_;
68 scoped_ptr<UbertokenFetcher> fetcher_;
71 TEST_F(UbertokenFetcherTest, Basic) {
74 TEST_F(UbertokenFetcherTest, Success) {
75 fetcher_->StartFetchingToken(kTestAccountId);
76 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
77 fetcher_->OnUberAuthTokenSuccess("uberToken");
79 EXPECT_EQ(0, consumer_.nb_error_);
80 EXPECT_EQ(1, consumer_.nb_correct_token_);
81 EXPECT_EQ("uberToken", consumer_.last_token_);
84 TEST_F(UbertokenFetcherTest, NoRefreshToken) {
85 fetcher_->StartFetchingToken(kTestAccountId);
86 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
87 fetcher_->OnGetTokenFailure(NULL, error);
89 EXPECT_EQ(1, consumer_.nb_error_);
90 EXPECT_EQ(0, consumer_.nb_correct_token_);
93 TEST_F(UbertokenFetcherTest, FailureToGetAccessToken) {
94 fetcher_->StartFetchingToken(kTestAccountId);
95 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
96 fetcher_->OnGetTokenFailure(NULL, error);
98 EXPECT_EQ(1, consumer_.nb_error_);
99 EXPECT_EQ(0, consumer_.nb_correct_token_);
100 EXPECT_EQ("", consumer_.last_token_);
103 TEST_F(UbertokenFetcherTest, FailureToGetUberToken) {
104 fetcher_->StartFetchingToken(kTestAccountId);
105 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
106 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
107 fetcher_->OnUberAuthTokenFailure(error);
109 EXPECT_EQ(1, consumer_.nb_error_);
110 EXPECT_EQ(0, consumer_.nb_correct_token_);
111 EXPECT_EQ("", consumer_.last_token_);