Roll src/third_party/WebKit 57aef96:a1089e6 (svn 201978:201979)
[chromium-blink-merge.git] / google_apis / gaia / ubertoken_fetcher_unittest.cc
blob58cef7c2b0189f197194c1aece3123cf5f90f0b0
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 "base/thread_task_runner_handle.h"
11 #include "google_apis/gaia/fake_oauth2_token_service.h"
12 #include "google_apis/gaia/gaia_constants.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace {
19 const char kTestAccountId[] = "test@gmail.com";
21 class MockUbertokenConsumer : public UbertokenConsumer {
22 public:
23 MockUbertokenConsumer()
24 : nb_correct_token_(0),
25 last_error_(GoogleServiceAuthError::AuthErrorNone()),
26 nb_error_(0) {
28 ~MockUbertokenConsumer() override {}
30 void OnUbertokenSuccess(const std::string& token) override {
31 last_token_ = token;
32 ++ nb_correct_token_;
35 void OnUbertokenFailure(const GoogleServiceAuthError& error) 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 void SetUp() override {
51 request_context_getter_ = new net::TestURLRequestContextGetter(
52 base::ThreadTaskRunnerHandle::Get());
53 fetcher_.reset(new UbertokenFetcher(&token_service_,
54 &consumer_,
55 GaiaConstants::kChromeSource,
56 request_context_getter_.get()));
59 void TearDown() override { fetcher_.reset(); }
61 protected:
62 base::MessageLoop message_loop_;
63 net::TestURLFetcherFactory factory_;
64 FakeOAuth2TokenService token_service_;
65 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
66 MockUbertokenConsumer consumer_;
67 scoped_ptr<UbertokenFetcher> fetcher_;
70 TEST_F(UbertokenFetcherTest, Basic) {
73 TEST_F(UbertokenFetcherTest, Success) {
74 fetcher_->StartFetchingToken(kTestAccountId);
75 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
76 fetcher_->OnUberAuthTokenSuccess("uberToken");
78 EXPECT_EQ(0, consumer_.nb_error_);
79 EXPECT_EQ(1, consumer_.nb_correct_token_);
80 EXPECT_EQ("uberToken", consumer_.last_token_);
83 TEST_F(UbertokenFetcherTest, NoRefreshToken) {
84 fetcher_->StartFetchingToken(kTestAccountId);
85 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
86 fetcher_->OnGetTokenFailure(NULL, error);
88 EXPECT_EQ(1, consumer_.nb_error_);
89 EXPECT_EQ(0, consumer_.nb_correct_token_);
92 TEST_F(UbertokenFetcherTest, FailureToGetAccessToken) {
93 fetcher_->StartFetchingToken(kTestAccountId);
94 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
95 fetcher_->OnGetTokenFailure(NULL, error);
97 EXPECT_EQ(1, consumer_.nb_error_);
98 EXPECT_EQ(0, consumer_.nb_correct_token_);
99 EXPECT_EQ("", consumer_.last_token_);
102 TEST_F(UbertokenFetcherTest, TransientFailureEventualFailure) {
103 fetcher_->StartFetchingToken(kTestAccountId);
104 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED);
105 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
107 for (int i = 0; i < UbertokenFetcher::kMaxRetries; ++i) {
108 fetcher_->OnUberAuthTokenFailure(error);
109 EXPECT_EQ(0, consumer_.nb_error_);
110 EXPECT_EQ(0, consumer_.nb_correct_token_);
111 EXPECT_EQ("", consumer_.last_token_);
114 fetcher_->OnUberAuthTokenFailure(error);
115 EXPECT_EQ(1, consumer_.nb_error_);
116 EXPECT_EQ(0, consumer_.nb_correct_token_);
117 EXPECT_EQ("", consumer_.last_token_);
120 TEST_F(UbertokenFetcherTest, TransientFailureEventualSuccess) {
121 fetcher_->StartFetchingToken(kTestAccountId);
122 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED);
123 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
125 for (int i = 0; i < UbertokenFetcher::kMaxRetries; ++i) {
126 fetcher_->OnUberAuthTokenFailure(error);
127 EXPECT_EQ(0, consumer_.nb_error_);
128 EXPECT_EQ(0, consumer_.nb_correct_token_);
129 EXPECT_EQ("", consumer_.last_token_);
132 fetcher_->OnUberAuthTokenSuccess("uberToken");
133 EXPECT_EQ(0, consumer_.nb_error_);
134 EXPECT_EQ(1, consumer_.nb_correct_token_);
135 EXPECT_EQ("uberToken", consumer_.last_token_);
138 TEST_F(UbertokenFetcherTest, PermanentFailureEventualFailure) {
139 fetcher_->StartFetchingToken(kTestAccountId);
140 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
142 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
143 fetcher_->OnUberAuthTokenFailure(error);
144 EXPECT_EQ(0, consumer_.nb_error_);
145 EXPECT_EQ(0, consumer_.nb_correct_token_);
146 EXPECT_EQ("", consumer_.last_token_);
148 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
149 fetcher_->OnUberAuthTokenFailure(error);
150 EXPECT_EQ(1, consumer_.nb_error_);
151 EXPECT_EQ(0, consumer_.nb_correct_token_);
152 EXPECT_EQ("", consumer_.last_token_);
155 TEST_F(UbertokenFetcherTest, PermanentFailureEventualSuccess) {
156 fetcher_->StartFetchingToken(kTestAccountId);
157 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
158 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
160 fetcher_->OnUberAuthTokenFailure(error);
161 EXPECT_EQ(0, consumer_.nb_error_);
162 EXPECT_EQ(0, consumer_.nb_correct_token_);
163 EXPECT_EQ("", consumer_.last_token_);
165 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
166 fetcher_->OnUberAuthTokenSuccess("uberToken");
167 EXPECT_EQ(0, consumer_.nb_error_);
168 EXPECT_EQ(1, consumer_.nb_correct_token_);
169 EXPECT_EQ("uberToken", consumer_.last_token_);