Update {virtual,override} to follow C++11 style in google_apis.
[chromium-blink-merge.git] / google_apis / gaia / gaia_oauth_client_unittest.cc
blob56fdc9167819935a19e53011d99b0d7d789cb2f9
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.
7 #include <string>
8 #include <vector>
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"
22 #include "url/gurl.h"
24 using ::testing::_;
25 using ::testing::Eq;
26 using ::testing::HasSubstr;
27 using ::testing::Pointee;
28 using ::testing::SaveArg;
30 namespace {
32 // Responds as though OAuth returned from the server.
33 class MockOAuthFetcher : public net::TestURLFetcher {
34 public:
35 MockOAuthFetcher(int response_code,
36 int max_failure_count,
37 bool complete_immediately,
38 const GURL& url,
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) {
46 set_url(url);
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);
70 void Finish() {
71 ASSERT_FALSE(complete_immediately_);
72 delegate()->OnURLFetchComplete(this);
75 private:
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 {
84 public:
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,
92 const GURL& url,
93 net::URLFetcher::RequestType request_type,
94 net::URLFetcherDelegate* d) override {
95 url_fetcher_ = new MockOAuthFetcher(
96 response_code_,
97 max_failure_count_,
98 complete_immediately_,
99 url,
100 results_,
101 request_type,
103 return url_fetcher_;
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) {
112 results_ = results;
114 MockOAuthFetcher* get_url_fetcher() {
115 return url_fetcher_;
117 void set_complete_immediately(bool complete_immediately) {
118 complete_immediately_ = complete_immediately;
120 private:
121 MockOAuthFetcher* url_fetcher_;
122 int response_code_;
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 kTestAccessTokenHandle = "1/kjhH87dfgkj87Hhj5KJkjZ";
131 const std::string kTestRefreshToken =
132 "1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ";
133 const std::string kTestUserEmail = "a_user@gmail.com";
134 const std::string kTestUserId = "8675309";
135 const int kTestExpiresIn = 3920;
137 const std::string kDummyGetTokensResult =
138 "{\"access_token\":\"" + kTestAccessToken + "\","
139 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + ","
140 "\"refresh_token\":\"" + kTestRefreshToken + "\"}";
142 const std::string kDummyRefreshTokenResult =
143 "{\"access_token\":\"" + kTestAccessToken + "\","
144 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}";
146 const std::string kDummyUserInfoResult =
147 "{\"email\":\"" + kTestUserEmail + "\"}";
149 const std::string kDummyUserIdResult =
150 "{\"id\":\"" + kTestUserId + "\"}";
152 const std::string kDummyFullUserInfoResult =
154 "\"family_name\": \"Bar\", "
155 "\"name\": \"Foo Bar\", "
156 "\"picture\": \"https://lh4.googleusercontent.com/hash/photo.jpg\", "
157 "\"locale\": \"en\", "
158 "\"gender\": \"male\", "
159 "\"link\": \"https://plus.google.com/+FooBar\", "
160 "\"given_name\": \"Foo\", "
161 "\"id\": \"12345678901234567890\""
162 "}";
164 const std::string kDummyTokenInfoResult =
165 "{\"issued_to\": \"1234567890.apps.googleusercontent.com\","
166 "\"audience\": \"1234567890.apps.googleusercontent.com\","
167 "\"scope\": \"https://googleapis.com/oauth2/v2/tokeninfo\","
168 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}";
170 const std::string kDummyTokenHandleInfoResult =
171 "{\"audience\": \"1234567890.apps.googleusercontent.com\","
172 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}";
174 } // namespace
176 namespace gaia {
178 class GaiaOAuthClientTest : public testing::Test {
179 protected:
180 void SetUp() override {
181 client_info_.client_id = "test_client_id";
182 client_info_.client_secret = "test_client_secret";
183 client_info_.redirect_uri = "test_redirect_uri";
186 protected:
187 net::TestURLRequestContextGetter* GetRequestContext() {
188 if (!request_context_getter_.get()) {
189 request_context_getter_ = new net::TestURLRequestContextGetter(
190 message_loop_.message_loop_proxy());
192 return request_context_getter_.get();
195 base::MessageLoop message_loop_;
196 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
197 OAuthClientInfo client_info_;
200 class MockGaiaOAuthClientDelegate : public gaia::GaiaOAuthClient::Delegate {
201 public:
202 MockGaiaOAuthClientDelegate() {}
203 ~MockGaiaOAuthClientDelegate() {}
205 MOCK_METHOD3(OnGetTokensResponse, void(const std::string& refresh_token,
206 const std::string& access_token,
207 int expires_in_seconds));
208 MOCK_METHOD2(OnRefreshTokenResponse, void(const std::string& access_token,
209 int expires_in_seconds));
210 MOCK_METHOD1(OnGetUserEmailResponse, void(const std::string& user_email));
211 MOCK_METHOD1(OnGetUserIdResponse, void(const std::string& user_id));
212 MOCK_METHOD0(OnOAuthError, void());
213 MOCK_METHOD1(OnNetworkError, void(int response_code));
215 // gMock doesn't like methods that take or return scoped_ptr. A
216 // work-around is to create a mock method that takes a raw ptr, and
217 // override the problematic method to call through to it.
218 // https://groups.google.com/a/chromium.org/d/msg/chromium-dev/01sDxsJ1OYw/I_S0xCBRF2oJ
219 MOCK_METHOD1(OnGetUserInfoResponsePtr,
220 void(const base::DictionaryValue* user_info));
221 void OnGetUserInfoResponse(
222 scoped_ptr<base::DictionaryValue> user_info) override {
223 user_info_.reset(user_info.release());
224 OnGetUserInfoResponsePtr(user_info_.get());
226 MOCK_METHOD1(OnGetTokenInfoResponsePtr,
227 void(const base::DictionaryValue* token_info));
228 void OnGetTokenInfoResponse(
229 scoped_ptr<base::DictionaryValue> token_info) override {
230 token_info_.reset(token_info.release());
231 OnGetTokenInfoResponsePtr(token_info_.get());
234 private:
235 scoped_ptr<base::DictionaryValue> user_info_;
236 scoped_ptr<base::DictionaryValue> token_info_;
237 DISALLOW_COPY_AND_ASSIGN(MockGaiaOAuthClientDelegate);
240 TEST_F(GaiaOAuthClientTest, NetworkFailure) {
241 int response_code = net::HTTP_INTERNAL_SERVER_ERROR;
243 MockGaiaOAuthClientDelegate delegate;
244 EXPECT_CALL(delegate, OnNetworkError(response_code))
245 .Times(1);
247 MockOAuthFetcherFactory factory;
248 factory.set_response_code(response_code);
249 factory.set_max_failure_count(4);
251 GaiaOAuthClient auth(GetRequestContext());
252 auth.GetTokensFromAuthCode(client_info_, "auth_code", 2, &delegate);
255 TEST_F(GaiaOAuthClientTest, NetworkFailureRecover) {
256 int response_code = net::HTTP_INTERNAL_SERVER_ERROR;
258 MockGaiaOAuthClientDelegate delegate;
259 EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken,
260 kTestExpiresIn)).Times(1);
262 MockOAuthFetcherFactory factory;
263 factory.set_response_code(response_code);
264 factory.set_max_failure_count(4);
265 factory.set_results(kDummyGetTokensResult);
267 GaiaOAuthClient auth(GetRequestContext());
268 auth.GetTokensFromAuthCode(client_info_, "auth_code", -1, &delegate);
271 TEST_F(GaiaOAuthClientTest, OAuthFailure) {
272 int response_code = net::HTTP_BAD_REQUEST;
274 MockGaiaOAuthClientDelegate delegate;
275 EXPECT_CALL(delegate, OnOAuthError()).Times(1);
277 MockOAuthFetcherFactory factory;
278 factory.set_response_code(response_code);
279 factory.set_max_failure_count(-1);
280 factory.set_results(kDummyGetTokensResult);
282 GaiaOAuthClient auth(GetRequestContext());
283 auth.GetTokensFromAuthCode(client_info_, "auth_code", -1, &delegate);
287 TEST_F(GaiaOAuthClientTest, GetTokensSuccess) {
288 MockGaiaOAuthClientDelegate delegate;
289 EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken,
290 kTestExpiresIn)).Times(1);
292 MockOAuthFetcherFactory factory;
293 factory.set_results(kDummyGetTokensResult);
295 GaiaOAuthClient auth(GetRequestContext());
296 auth.GetTokensFromAuthCode(client_info_, "auth_code", -1, &delegate);
299 TEST_F(GaiaOAuthClientTest, RefreshTokenSuccess) {
300 MockGaiaOAuthClientDelegate delegate;
301 EXPECT_CALL(delegate, OnRefreshTokenResponse(kTestAccessToken,
302 kTestExpiresIn)).Times(1);
304 MockOAuthFetcherFactory factory;
305 factory.set_results(kDummyRefreshTokenResult);
306 factory.set_complete_immediately(false);
308 GaiaOAuthClient auth(GetRequestContext());
309 auth.RefreshToken(client_info_, "refresh_token", std::vector<std::string>(),
310 -1, &delegate);
311 EXPECT_THAT(factory.get_url_fetcher()->upload_data(),
312 Not(HasSubstr("scope")));
313 factory.get_url_fetcher()->Finish();
316 TEST_F(GaiaOAuthClientTest, RefreshTokenDownscopingSuccess) {
317 MockGaiaOAuthClientDelegate delegate;
318 EXPECT_CALL(delegate, OnRefreshTokenResponse(kTestAccessToken,
319 kTestExpiresIn)).Times(1);
321 MockOAuthFetcherFactory factory;
322 factory.set_results(kDummyRefreshTokenResult);
323 factory.set_complete_immediately(false);
325 GaiaOAuthClient auth(GetRequestContext());
326 auth.RefreshToken(client_info_, "refresh_token",
327 std::vector<std::string>(1, "scope4test"), -1, &delegate);
328 EXPECT_THAT(factory.get_url_fetcher()->upload_data(),
329 HasSubstr("&scope=scope4test"));
330 factory.get_url_fetcher()->Finish();
334 TEST_F(GaiaOAuthClientTest, GetUserEmail) {
335 MockGaiaOAuthClientDelegate delegate;
336 EXPECT_CALL(delegate, OnGetUserEmailResponse(kTestUserEmail)).Times(1);
338 MockOAuthFetcherFactory factory;
339 factory.set_results(kDummyUserInfoResult);
341 GaiaOAuthClient auth(GetRequestContext());
342 auth.GetUserEmail("access_token", 1, &delegate);
345 TEST_F(GaiaOAuthClientTest, GetUserId) {
346 MockGaiaOAuthClientDelegate delegate;
347 EXPECT_CALL(delegate, OnGetUserIdResponse(kTestUserId)).Times(1);
349 MockOAuthFetcherFactory factory;
350 factory.set_results(kDummyUserIdResult);
352 GaiaOAuthClient auth(GetRequestContext());
353 auth.GetUserId("access_token", 1, &delegate);
356 TEST_F(GaiaOAuthClientTest, GetUserInfo) {
357 const base::DictionaryValue* captured_result;
359 MockGaiaOAuthClientDelegate delegate;
360 EXPECT_CALL(delegate, OnGetUserInfoResponsePtr(_))
361 .WillOnce(SaveArg<0>(&captured_result));
363 MockOAuthFetcherFactory factory;
364 factory.set_results(kDummyFullUserInfoResult);
366 GaiaOAuthClient auth(GetRequestContext());
367 auth.GetUserInfo("access_token", 1, &delegate);
369 scoped_ptr<base::Value> value(
370 base::JSONReader::Read(kDummyFullUserInfoResult));
371 DCHECK(value);
372 ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY));
373 base::DictionaryValue* expected_result;
374 value->GetAsDictionary(&expected_result);
376 ASSERT_TRUE(expected_result->Equals(captured_result));
379 TEST_F(GaiaOAuthClientTest, GetTokenInfo) {
380 const base::DictionaryValue* captured_result;
382 MockGaiaOAuthClientDelegate delegate;
383 EXPECT_CALL(delegate, OnGetTokenInfoResponsePtr(_))
384 .WillOnce(SaveArg<0>(&captured_result));
386 MockOAuthFetcherFactory factory;
387 factory.set_results(kDummyTokenInfoResult);
389 GaiaOAuthClient auth(GetRequestContext());
390 auth.GetTokenInfo("some_token", 1, &delegate);
392 std::string issued_to;
393 ASSERT_TRUE(captured_result->GetString("issued_to", &issued_to));
394 ASSERT_EQ("1234567890.apps.googleusercontent.com", issued_to);
397 TEST_F(GaiaOAuthClientTest, GetTokenHandleInfo) {
398 const base::DictionaryValue* captured_result;
400 MockGaiaOAuthClientDelegate delegate;
401 EXPECT_CALL(delegate, OnGetTokenInfoResponsePtr(_))
402 .WillOnce(SaveArg<0>(&captured_result));
404 MockOAuthFetcherFactory factory;
405 factory.set_results(kDummyTokenHandleInfoResult);
407 GaiaOAuthClient auth(GetRequestContext());
408 auth.GetTokenHandleInfo("some_handle", 1, &delegate);
410 std::string audience;
411 ASSERT_TRUE(captured_result->GetString("audience", &audience));
412 ASSERT_EQ("1234567890.apps.googleusercontent.com", audience);
415 } // namespace gaia