ubertoken_fetcher_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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.
4
5#include "google_apis/gaia/ubertoken_fetcher.h"
6
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"
15
16namespace {
17
18const char kTestAccountId[] = "test@gmail.com";
19
20class MockUbertokenConsumer : public UbertokenConsumer {
21 public:
22  MockUbertokenConsumer()
23      : nb_correct_token_(0),
24        last_error_(GoogleServiceAuthError::AuthErrorNone()),
25        nb_error_(0) {
26  }
27  virtual ~MockUbertokenConsumer() {}
28
29  virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE {
30    last_token_ = token;
31    ++ nb_correct_token_;
32  }
33
34  virtual void OnUbertokenFailure(const GoogleServiceAuthError& error)
35      OVERRIDE {
36    last_error_ = error;
37    ++nb_error_;
38  }
39
40  std::string last_token_;
41  int nb_correct_token_;
42  GoogleServiceAuthError last_error_;
43  int nb_error_;
44};
45
46}  // namespace
47
48class 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()));
56  }
57
58  virtual void TearDown() OVERRIDE {
59    fetcher_.reset();
60  }
61
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_;
69};
70
71TEST_F(UbertokenFetcherTest, Basic) {
72}
73
74TEST_F(UbertokenFetcherTest, Success) {
75  fetcher_->StartFetchingToken(kTestAccountId);
76  fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
77  fetcher_->OnUberAuthTokenSuccess("uberToken");
78
79  EXPECT_EQ(0, consumer_.nb_error_);
80  EXPECT_EQ(1, consumer_.nb_correct_token_);
81  EXPECT_EQ("uberToken", consumer_.last_token_);
82}
83
84TEST_F(UbertokenFetcherTest, NoRefreshToken) {
85  fetcher_->StartFetchingToken(kTestAccountId);
86  GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
87  fetcher_->OnGetTokenFailure(NULL, error);
88
89  EXPECT_EQ(1, consumer_.nb_error_);
90  EXPECT_EQ(0, consumer_.nb_correct_token_);
91}
92
93TEST_F(UbertokenFetcherTest, FailureToGetAccessToken) {
94  fetcher_->StartFetchingToken(kTestAccountId);
95  GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
96  fetcher_->OnGetTokenFailure(NULL, error);
97
98  EXPECT_EQ(1, consumer_.nb_error_);
99  EXPECT_EQ(0, consumer_.nb_correct_token_);
100  EXPECT_EQ("", consumer_.last_token_);
101}
102
103TEST_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);
108
109  EXPECT_EQ(1, consumer_.nb_error_);
110  EXPECT_EQ(0, consumer_.nb_correct_token_);
111  EXPECT_EQ("", consumer_.last_token_);
112}
113