gaia_oauth_client_unittest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2012 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// A complete set of unit tests for GaiaOAuthClient.
6
7#include <string>
8
9#include "base/message_loop.h"
10#include "base/strings/string_number_conversions.h"
11#include "base/strings/string_util.h"
12#include "chrome/test/base/testing_profile.h"
13#include "google_apis/gaia/gaia_oauth_client.h"
14#include "googleurl/src/gurl.h"
15#include "net/base/net_errors.h"
16#include "net/http/http_status_code.h"
17#include "net/url_request/test_url_fetcher_factory.h"
18#include "net/url_request/url_fetcher_delegate.h"
19#include "net/url_request/url_request_status.h"
20#include "testing/gmock/include/gmock/gmock.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23using ::testing::_;
24
25namespace {
26
27const char kGaiaOAuth2Url[] = "https://accounts.google.com/o/oauth2/token";
28
29// Responds as though OAuth returned from the server.
30class MockOAuthFetcher : public net::TestURLFetcher {
31 public:
32  MockOAuthFetcher(int response_code,
33                   int max_failure_count,
34                   const GURL& url,
35                   const std::string& results,
36                   net::URLFetcher::RequestType request_type,
37                   net::URLFetcherDelegate* d)
38      : net::TestURLFetcher(0, url, d),
39        max_failure_count_(max_failure_count),
40        current_failure_count_(0) {
41    set_url(url);
42    set_response_code(response_code);
43    SetResponseString(results);
44  }
45
46  virtual ~MockOAuthFetcher() { }
47
48  virtual void Start() OVERRIDE {
49    if ((GetResponseCode() != net::HTTP_OK) && (max_failure_count_ != -1) &&
50        (current_failure_count_ == max_failure_count_)) {
51      set_response_code(net::HTTP_OK);
52    }
53
54    net::URLRequestStatus::Status code = net::URLRequestStatus::SUCCESS;
55    if (GetResponseCode() != net::HTTP_OK) {
56      code = net::URLRequestStatus::FAILED;
57      current_failure_count_++;
58    }
59    set_status(net::URLRequestStatus(code, 0));
60
61    delegate()->OnURLFetchComplete(this);
62  }
63
64 private:
65  int max_failure_count_;
66  int current_failure_count_;
67  DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcher);
68};
69
70class MockOAuthFetcherFactory : public net::URLFetcherFactory,
71                                public net::ScopedURLFetcherFactory {
72 public:
73  MockOAuthFetcherFactory()
74      : net::ScopedURLFetcherFactory(this),
75        response_code_(net::HTTP_OK) {
76  }
77  virtual ~MockOAuthFetcherFactory() {}
78  virtual net::URLFetcher* CreateURLFetcher(
79      int id,
80      const GURL& url,
81      net::URLFetcher::RequestType request_type,
82      net::URLFetcherDelegate* d) OVERRIDE {
83    return new MockOAuthFetcher(
84        response_code_,
85        max_failure_count_,
86        url,
87        results_,
88        request_type,
89        d);
90  }
91  void set_response_code(int response_code) {
92    response_code_ = response_code;
93  }
94  void set_max_failure_count(int count) {
95    max_failure_count_ = count;
96  }
97  void set_results(const std::string& results) {
98    results_ = results;
99  }
100 private:
101  int response_code_;
102  int max_failure_count_;
103  std::string results_;
104  DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcherFactory);
105};
106
107const std::string kTestAccessToken = "1/fFAGRNJru1FTz70BzhT3Zg";
108const std::string kTestRefreshToken =
109    "1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ";
110const std::string kTestUserEmail = "a_user@gmail.com";
111const int kTestExpiresIn = 3920;
112
113const std::string kDummyGetTokensResult =
114  "{\"access_token\":\"" + kTestAccessToken + "\","
115  "\"expires_in\":" + base::IntToString(kTestExpiresIn) + ","
116  "\"refresh_token\":\"" + kTestRefreshToken + "\"}";
117
118const std::string kDummyRefreshTokenResult =
119  "{\"access_token\":\"" + kTestAccessToken + "\","
120  "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}";
121
122const std::string kDummyUserInfoResult =
123  "{\"email\":\"" + kTestUserEmail + "\"}";
124}
125
126namespace gaia {
127
128class GaiaOAuthClientTest : public testing::Test {
129 public:
130  GaiaOAuthClientTest() {}
131
132  TestingProfile profile_;
133 protected:
134  base::MessageLoop message_loop_;
135};
136
137class MockGaiaOAuthClientDelegate : public gaia::GaiaOAuthClient::Delegate {
138 public:
139  MockGaiaOAuthClientDelegate() {}
140  ~MockGaiaOAuthClientDelegate() {}
141
142  MOCK_METHOD3(OnGetTokensResponse, void(const std::string& refresh_token,
143                                         const std::string& access_token,
144                                         int expires_in_seconds));
145  MOCK_METHOD2(OnRefreshTokenResponse, void(const std::string& access_token,
146                                            int expires_in_seconds));
147  MOCK_METHOD1(OnGetUserInfoResponse, void(const std::string& user_email));
148  MOCK_METHOD0(OnOAuthError, void());
149  MOCK_METHOD1(OnNetworkError, void(int response_code));
150};
151
152TEST_F(GaiaOAuthClientTest, NetworkFailure) {
153  int response_code = net::HTTP_INTERNAL_SERVER_ERROR;
154
155  MockGaiaOAuthClientDelegate delegate;
156  EXPECT_CALL(delegate, OnNetworkError(response_code))
157      .Times(1);
158
159  TestingProfile profile;
160
161  MockOAuthFetcherFactory factory;
162  factory.set_response_code(response_code);
163  factory.set_max_failure_count(4);
164
165  OAuthClientInfo client_info;
166  client_info.client_id = "test_client_id";
167  client_info.client_secret = "test_client_secret";
168  client_info.redirect_uri = "test_redirect_uri";
169  GaiaOAuthClient auth(kGaiaOAuth2Url,
170                       profile_.GetRequestContext());
171  auth.GetTokensFromAuthCode(client_info, "auth_code", 2, &delegate);
172}
173
174TEST_F(GaiaOAuthClientTest, NetworkFailureRecover) {
175  int response_code = net::HTTP_INTERNAL_SERVER_ERROR;
176
177  MockGaiaOAuthClientDelegate delegate;
178  EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken,
179      kTestExpiresIn)).Times(1);
180
181  TestingProfile profile;
182
183  MockOAuthFetcherFactory factory;
184  factory.set_response_code(response_code);
185  factory.set_max_failure_count(4);
186  factory.set_results(kDummyGetTokensResult);
187
188  OAuthClientInfo client_info;
189  client_info.client_id = "test_client_id";
190  client_info.client_secret = "test_client_secret";
191  client_info.redirect_uri = "test_redirect_uri";
192  GaiaOAuthClient auth(kGaiaOAuth2Url,
193                       profile_.GetRequestContext());
194  auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate);
195}
196
197TEST_F(GaiaOAuthClientTest, OAuthFailure) {
198  int response_code = net::HTTP_BAD_REQUEST;
199
200  MockGaiaOAuthClientDelegate delegate;
201  EXPECT_CALL(delegate, OnOAuthError()).Times(1);
202
203  TestingProfile profile;
204
205  MockOAuthFetcherFactory factory;
206  factory.set_response_code(response_code);
207  factory.set_max_failure_count(-1);
208  factory.set_results(kDummyGetTokensResult);
209
210  OAuthClientInfo client_info;
211  client_info.client_id = "test_client_id";
212  client_info.client_secret = "test_client_secret";
213  client_info.redirect_uri = "test_redirect_uri";
214  GaiaOAuthClient auth(kGaiaOAuth2Url,
215                       profile_.GetRequestContext());
216  auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate);
217}
218
219
220TEST_F(GaiaOAuthClientTest, GetTokensSuccess) {
221  MockGaiaOAuthClientDelegate delegate;
222  EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken,
223      kTestExpiresIn)).Times(1);
224
225  TestingProfile profile;
226
227  MockOAuthFetcherFactory factory;
228  factory.set_results(kDummyGetTokensResult);
229
230  OAuthClientInfo client_info;
231  client_info.client_id = "test_client_id";
232  client_info.client_secret = "test_client_secret";
233  client_info.redirect_uri = "test_redirect_uri";
234  GaiaOAuthClient auth(kGaiaOAuth2Url,
235                       profile_.GetRequestContext());
236  auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate);
237}
238
239TEST_F(GaiaOAuthClientTest, RefreshTokenSuccess) {
240  MockGaiaOAuthClientDelegate delegate;
241  EXPECT_CALL(delegate, OnRefreshTokenResponse(kTestAccessToken,
242      kTestExpiresIn)).Times(1);
243
244  TestingProfile profile;
245
246  MockOAuthFetcherFactory factory;
247  factory.set_results(kDummyRefreshTokenResult);
248
249  OAuthClientInfo client_info;
250  client_info.client_id = "test_client_id";
251  client_info.client_secret = "test_client_secret";
252  client_info.redirect_uri = "test_redirect_uri";
253  GaiaOAuthClient auth(kGaiaOAuth2Url,
254                       profile_.GetRequestContext());
255  auth.RefreshToken(client_info, "refresh_token", -1, &delegate);
256}
257
258TEST_F(GaiaOAuthClientTest, GetUserInfo) {
259  MockGaiaOAuthClientDelegate delegate;
260  EXPECT_CALL(delegate, OnGetUserInfoResponse(kTestUserEmail)).Times(1);
261
262  TestingProfile profile;
263
264  MockOAuthFetcherFactory factory;
265  factory.set_results(kDummyUserInfoResult);
266
267  OAuthClientInfo client_info;
268  client_info.client_id = "test_client_id";
269  client_info.client_secret = "test_client_secret";
270  client_info.redirect_uri = "test_redirect_uri";
271  GaiaOAuthClient auth(kGaiaOAuth2Url,
272                       profile_.GetRequestContext());
273  auth.GetUserInfo("access_token", 1, &delegate);
274}
275
276}  // namespace gaia
277