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.
5 // A complete set of unit tests for GaiaOAuthClient.
10 #include "base/json/json_reader.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "google_apis/gaia/gaia_oauth_client.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_status_code.h"
16 #include "net/url_request/test_url_fetcher_factory.h"
17 #include "net/url_request/url_fetcher_delegate.h"
18 #include "net/url_request/url_request_status.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
26 using ::testing::HasSubstr
;
27 using ::testing::Pointee
;
28 using ::testing::SaveArg
;
32 // Responds as though OAuth returned from the server.
33 class MockOAuthFetcher
: public net::TestURLFetcher
{
35 MockOAuthFetcher(int response_code
,
36 int max_failure_count
,
37 bool complete_immediately
,
39 const std::string
& results
,
40 net::URLFetcher::RequestType request_type
,
41 net::URLFetcherDelegate
* d
)
42 : net::TestURLFetcher(0, url
, d
),
43 max_failure_count_(max_failure_count
),
44 current_failure_count_(0),
45 complete_immediately_(complete_immediately
) {
47 set_response_code(response_code
);
48 SetResponseString(results
);
51 ~MockOAuthFetcher() override
{}
53 void Start() override
{
54 if ((GetResponseCode() != net::HTTP_OK
) && (max_failure_count_
!= -1) &&
55 (current_failure_count_
== max_failure_count_
)) {
56 set_response_code(net::HTTP_OK
);
59 net::URLRequestStatus::Status code
= net::URLRequestStatus::SUCCESS
;
60 if (GetResponseCode() != net::HTTP_OK
) {
61 code
= net::URLRequestStatus::FAILED
;
62 current_failure_count_
++;
64 set_status(net::URLRequestStatus(code
, 0));
66 if (complete_immediately_
)
67 delegate()->OnURLFetchComplete(this);
71 ASSERT_FALSE(complete_immediately_
);
72 delegate()->OnURLFetchComplete(this);
76 int max_failure_count_
;
77 int current_failure_count_
;
78 bool complete_immediately_
;
79 DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcher
);
82 class MockOAuthFetcherFactory
: public net::URLFetcherFactory
,
83 public net::ScopedURLFetcherFactory
{
85 MockOAuthFetcherFactory()
86 : net::ScopedURLFetcherFactory(this),
87 response_code_(net::HTTP_OK
),
88 complete_immediately_(true) {
90 ~MockOAuthFetcherFactory() override
{}
91 net::URLFetcher
* CreateURLFetcher(int id
,
93 net::URLFetcher::RequestType request_type
,
94 net::URLFetcherDelegate
* d
) override
{
95 url_fetcher_
= new MockOAuthFetcher(
98 complete_immediately_
,
105 void set_response_code(int response_code
) {
106 response_code_
= response_code
;
108 void set_max_failure_count(int count
) {
109 max_failure_count_
= count
;
111 void set_results(const std::string
& results
) {
114 MockOAuthFetcher
* get_url_fetcher() {
117 void set_complete_immediately(bool complete_immediately
) {
118 complete_immediately_
= complete_immediately
;
121 MockOAuthFetcher
* url_fetcher_
;
123 bool complete_immediately_
;
124 int max_failure_count_
;
125 std::string results_
;
126 DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcherFactory
);
129 const std::string kTestAccessToken
= "1/fFAGRNJru1FTz70BzhT3Zg";
130 const std::string kTestRefreshToken
=
131 "1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ";
132 const std::string kTestUserEmail
= "a_user@gmail.com";
133 const std::string kTestUserId
= "8675309";
134 const int kTestExpiresIn
= 3920;
136 const std::string kDummyGetTokensResult
=
137 "{\"access_token\":\"" + kTestAccessToken
+ "\","
138 "\"expires_in\":" + base::IntToString(kTestExpiresIn
) + ","
139 "\"refresh_token\":\"" + kTestRefreshToken
+ "\"}";
141 const std::string kDummyRefreshTokenResult
=
142 "{\"access_token\":\"" + kTestAccessToken
+ "\","
143 "\"expires_in\":" + base::IntToString(kTestExpiresIn
) + "}";
145 const std::string kDummyUserInfoResult
=
146 "{\"email\":\"" + kTestUserEmail
+ "\"}";
148 const std::string kDummyUserIdResult
=
149 "{\"id\":\"" + kTestUserId
+ "\"}";
151 const std::string kDummyFullUserInfoResult
=
153 "\"family_name\": \"Bar\", "
154 "\"name\": \"Foo Bar\", "
155 "\"picture\": \"https://lh4.googleusercontent.com/hash/photo.jpg\", "
156 "\"locale\": \"en\", "
157 "\"gender\": \"male\", "
158 "\"link\": \"https://plus.google.com/+FooBar\", "
159 "\"given_name\": \"Foo\", "
160 "\"id\": \"12345678901234567890\""
163 const std::string kDummyTokenInfoResult
=
164 "{\"issued_to\": \"1234567890.apps.googleusercontent.com\","
165 "\"audience\": \"1234567890.apps.googleusercontent.com\","
166 "\"scope\": \"https://googleapis.com/oauth2/v2/tokeninfo\","
167 "\"expires_in\":" + base::IntToString(kTestExpiresIn
) + "}";
172 class GaiaOAuthClientTest
: public testing::Test
{
174 void SetUp() override
{
175 client_info_
.client_id
= "test_client_id";
176 client_info_
.client_secret
= "test_client_secret";
177 client_info_
.redirect_uri
= "test_redirect_uri";
181 net::TestURLRequestContextGetter
* GetRequestContext() {
182 if (!request_context_getter_
.get()) {
183 request_context_getter_
= new net::TestURLRequestContextGetter(
184 message_loop_
.message_loop_proxy());
186 return request_context_getter_
.get();
189 base::MessageLoop message_loop_
;
190 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
191 OAuthClientInfo client_info_
;
194 class MockGaiaOAuthClientDelegate
: public gaia::GaiaOAuthClient::Delegate
{
196 MockGaiaOAuthClientDelegate() {}
197 ~MockGaiaOAuthClientDelegate() {}
199 MOCK_METHOD3(OnGetTokensResponse
, void(const std::string
& refresh_token
,
200 const std::string
& access_token
,
201 int expires_in_seconds
));
202 MOCK_METHOD2(OnRefreshTokenResponse
, void(const std::string
& access_token
,
203 int expires_in_seconds
));
204 MOCK_METHOD1(OnGetUserEmailResponse
, void(const std::string
& user_email
));
205 MOCK_METHOD1(OnGetUserIdResponse
, void(const std::string
& user_id
));
206 MOCK_METHOD0(OnOAuthError
, void());
207 MOCK_METHOD1(OnNetworkError
, void(int response_code
));
209 // gMock doesn't like methods that take or return scoped_ptr. A
210 // work-around is to create a mock method that takes a raw ptr, and
211 // override the problematic method to call through to it.
212 // https://groups.google.com/a/chromium.org/d/msg/chromium-dev/01sDxsJ1OYw/I_S0xCBRF2oJ
213 MOCK_METHOD1(OnGetUserInfoResponsePtr
,
214 void(const base::DictionaryValue
* user_info
));
215 virtual void OnGetUserInfoResponse(
216 scoped_ptr
<base::DictionaryValue
> user_info
) override
{
217 user_info_
.reset(user_info
.release());
218 OnGetUserInfoResponsePtr(user_info_
.get());
220 MOCK_METHOD1(OnGetTokenInfoResponsePtr
,
221 void(const base::DictionaryValue
* token_info
));
222 virtual void OnGetTokenInfoResponse(
223 scoped_ptr
<base::DictionaryValue
> token_info
) override
{
224 token_info_
.reset(token_info
.release());
225 OnGetTokenInfoResponsePtr(token_info_
.get());
229 scoped_ptr
<base::DictionaryValue
> user_info_
;
230 scoped_ptr
<base::DictionaryValue
> token_info_
;
231 DISALLOW_COPY_AND_ASSIGN(MockGaiaOAuthClientDelegate
);
234 TEST_F(GaiaOAuthClientTest
, NetworkFailure
) {
235 int response_code
= net::HTTP_INTERNAL_SERVER_ERROR
;
237 MockGaiaOAuthClientDelegate delegate
;
238 EXPECT_CALL(delegate
, OnNetworkError(response_code
))
241 MockOAuthFetcherFactory factory
;
242 factory
.set_response_code(response_code
);
243 factory
.set_max_failure_count(4);
245 GaiaOAuthClient
auth(GetRequestContext());
246 auth
.GetTokensFromAuthCode(client_info_
, "auth_code", 2, &delegate
);
249 TEST_F(GaiaOAuthClientTest
, NetworkFailureRecover
) {
250 int response_code
= net::HTTP_INTERNAL_SERVER_ERROR
;
252 MockGaiaOAuthClientDelegate delegate
;
253 EXPECT_CALL(delegate
, OnGetTokensResponse(kTestRefreshToken
, kTestAccessToken
,
254 kTestExpiresIn
)).Times(1);
256 MockOAuthFetcherFactory factory
;
257 factory
.set_response_code(response_code
);
258 factory
.set_max_failure_count(4);
259 factory
.set_results(kDummyGetTokensResult
);
261 GaiaOAuthClient
auth(GetRequestContext());
262 auth
.GetTokensFromAuthCode(client_info_
, "auth_code", -1, &delegate
);
265 TEST_F(GaiaOAuthClientTest
, OAuthFailure
) {
266 int response_code
= net::HTTP_BAD_REQUEST
;
268 MockGaiaOAuthClientDelegate delegate
;
269 EXPECT_CALL(delegate
, OnOAuthError()).Times(1);
271 MockOAuthFetcherFactory factory
;
272 factory
.set_response_code(response_code
);
273 factory
.set_max_failure_count(-1);
274 factory
.set_results(kDummyGetTokensResult
);
276 GaiaOAuthClient
auth(GetRequestContext());
277 auth
.GetTokensFromAuthCode(client_info_
, "auth_code", -1, &delegate
);
281 TEST_F(GaiaOAuthClientTest
, GetTokensSuccess
) {
282 MockGaiaOAuthClientDelegate delegate
;
283 EXPECT_CALL(delegate
, OnGetTokensResponse(kTestRefreshToken
, kTestAccessToken
,
284 kTestExpiresIn
)).Times(1);
286 MockOAuthFetcherFactory factory
;
287 factory
.set_results(kDummyGetTokensResult
);
289 GaiaOAuthClient
auth(GetRequestContext());
290 auth
.GetTokensFromAuthCode(client_info_
, "auth_code", -1, &delegate
);
293 TEST_F(GaiaOAuthClientTest
, RefreshTokenSuccess
) {
294 MockGaiaOAuthClientDelegate delegate
;
295 EXPECT_CALL(delegate
, OnRefreshTokenResponse(kTestAccessToken
,
296 kTestExpiresIn
)).Times(1);
298 MockOAuthFetcherFactory factory
;
299 factory
.set_results(kDummyRefreshTokenResult
);
300 factory
.set_complete_immediately(false);
302 GaiaOAuthClient
auth(GetRequestContext());
303 auth
.RefreshToken(client_info_
, "refresh_token", std::vector
<std::string
>(),
305 EXPECT_THAT(factory
.get_url_fetcher()->upload_data(),
306 Not(HasSubstr("scope")));
307 factory
.get_url_fetcher()->Finish();
310 TEST_F(GaiaOAuthClientTest
, RefreshTokenDownscopingSuccess
) {
311 MockGaiaOAuthClientDelegate delegate
;
312 EXPECT_CALL(delegate
, OnRefreshTokenResponse(kTestAccessToken
,
313 kTestExpiresIn
)).Times(1);
315 MockOAuthFetcherFactory factory
;
316 factory
.set_results(kDummyRefreshTokenResult
);
317 factory
.set_complete_immediately(false);
319 GaiaOAuthClient
auth(GetRequestContext());
320 auth
.RefreshToken(client_info_
, "refresh_token",
321 std::vector
<std::string
>(1, "scope4test"), -1, &delegate
);
322 EXPECT_THAT(factory
.get_url_fetcher()->upload_data(),
323 HasSubstr("&scope=scope4test"));
324 factory
.get_url_fetcher()->Finish();
328 TEST_F(GaiaOAuthClientTest
, GetUserEmail
) {
329 MockGaiaOAuthClientDelegate delegate
;
330 EXPECT_CALL(delegate
, OnGetUserEmailResponse(kTestUserEmail
)).Times(1);
332 MockOAuthFetcherFactory factory
;
333 factory
.set_results(kDummyUserInfoResult
);
335 GaiaOAuthClient
auth(GetRequestContext());
336 auth
.GetUserEmail("access_token", 1, &delegate
);
339 TEST_F(GaiaOAuthClientTest
, GetUserId
) {
340 MockGaiaOAuthClientDelegate delegate
;
341 EXPECT_CALL(delegate
, OnGetUserIdResponse(kTestUserId
)).Times(1);
343 MockOAuthFetcherFactory factory
;
344 factory
.set_results(kDummyUserIdResult
);
346 GaiaOAuthClient
auth(GetRequestContext());
347 auth
.GetUserId("access_token", 1, &delegate
);
350 TEST_F(GaiaOAuthClientTest
, GetUserInfo
) {
351 const base::DictionaryValue
* captured_result
;
353 MockGaiaOAuthClientDelegate delegate
;
354 EXPECT_CALL(delegate
, OnGetUserInfoResponsePtr(_
))
355 .WillOnce(SaveArg
<0>(&captured_result
));
357 MockOAuthFetcherFactory factory
;
358 factory
.set_results(kDummyFullUserInfoResult
);
360 GaiaOAuthClient
auth(GetRequestContext());
361 auth
.GetUserInfo("access_token", 1, &delegate
);
363 scoped_ptr
<base::Value
> value(
364 base::JSONReader::Read(kDummyFullUserInfoResult
));
366 ASSERT_TRUE(value
->IsType(base::Value::TYPE_DICTIONARY
));
367 base::DictionaryValue
* expected_result
;
368 value
->GetAsDictionary(&expected_result
);
370 ASSERT_TRUE(expected_result
->Equals(captured_result
));
373 TEST_F(GaiaOAuthClientTest
, GetTokenInfo
) {
374 const base::DictionaryValue
* captured_result
;
376 MockGaiaOAuthClientDelegate delegate
;
377 EXPECT_CALL(delegate
, OnGetTokenInfoResponsePtr(_
))
378 .WillOnce(SaveArg
<0>(&captured_result
));
380 MockOAuthFetcherFactory factory
;
381 factory
.set_results(kDummyTokenInfoResult
);
383 GaiaOAuthClient
auth(GetRequestContext());
384 auth
.GetTokenInfo("access_token", 1, &delegate
);
386 std::string issued_to
;
387 ASSERT_TRUE(captured_result
->GetString("issued_to", &issued_to
));
388 ASSERT_EQ("1234567890.apps.googleusercontent.com", issued_to
);