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"
18 const char kTestAccountId
[] = "test@gmail.com";
20 class MockUbertokenConsumer
: public UbertokenConsumer
{
22 MockUbertokenConsumer()
23 : nb_correct_token_(0),
24 last_error_(GoogleServiceAuthError::AuthErrorNone()),
27 virtual ~MockUbertokenConsumer() {}
29 virtual void OnUbertokenSuccess(const std::string
& token
) OVERRIDE
{
34 virtual void OnUbertokenFailure(const GoogleServiceAuthError
& error
)
40 std::string last_token_
;
41 int nb_correct_token_
;
42 GoogleServiceAuthError last_error_
;
48 class UbertokenFetcherTest
: public testing::Test
{
50 virtual void SetUp() OVERRIDE
{
51 request_context_getter_
= new net::TestURLRequestContextGetter(
52 base::MessageLoopProxy::current());
53 fetcher_
.reset(new UbertokenFetcher(&token_service_
,
55 request_context_getter_
.get()));
58 virtual void TearDown() OVERRIDE
{
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_
);