OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A complete set of unit tests for GaiaOAuthClient. | 5 // A complete set of unit tests for GaiaOAuthClient. |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 }; | 127 }; |
128 | 128 |
129 const std::string kTestAccessToken = "1/fFAGRNJru1FTz70BzhT3Zg"; | 129 const std::string kTestAccessToken = "1/fFAGRNJru1FTz70BzhT3Zg"; |
130 const std::string kTestRefreshToken = | 130 const std::string kTestRefreshToken = |
131 "1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ"; | 131 "1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ"; |
132 const std::string kTestUserEmail = "a_user@gmail.com"; | 132 const std::string kTestUserEmail = "a_user@gmail.com"; |
133 const std::string kTestUserId = "8675309"; | 133 const std::string kTestUserId = "8675309"; |
134 const int kTestExpiresIn = 3920; | 134 const int kTestExpiresIn = 3920; |
135 | 135 |
136 const std::string kDummyGetTokensResult = | 136 const std::string kDummyGetTokensResult = |
137 "{\"access_token\":\"" + kTestAccessToken + "\"," | 137 "{\"access_token\":\"" + kTestAccessToken + "\"," |
138 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "," | 138 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "," |
139 "\"refresh_token\":\"" + kTestRefreshToken + "\"}"; | 139 "\"refresh_token\":\"" + kTestRefreshToken + "\"}"; |
140 | 140 |
141 const std::string kDummyRefreshTokenResult = | 141 const std::string kDummyRefreshTokenResult = |
142 "{\"access_token\":\"" + kTestAccessToken + "\"," | 142 "{\"access_token\":\"" + kTestAccessToken + "\"," |
143 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; | 143 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; |
144 | |
145 const std::string kDummyUserInfoResult = | |
146 "{\"email\":\"" + kTestUserEmail + "\"}"; | |
147 | 144 |
148 const std::string kDummyUserIdResult = | 145 const std::string kDummyUserIdResult = |
149 "{\"id\":\"" + kTestUserId + "\"}"; | 146 "{\"id\":\"" + kTestUserId + "\"}"; |
150 | 147 |
151 const std::string kDummyTokenInfoResult = | 148 const std::string kDummyTokenInfoResult = |
152 "{\"issued_to\": \"1234567890.apps.googleusercontent.com\"," | 149 "{\"issued_to\": \"1234567890.apps.googleusercontent.com\"," |
153 "\"audience\": \"1234567890.apps.googleusercontent.com\"," | 150 "\"audience\": \"1234567890.apps.googleusercontent.com\"," |
154 "\"scope\": \"https://googleapis.com/oauth2/v2/tokeninfo\"," | 151 "\"scope\": \"https://googleapis.com/oauth2/v2/tokeninfo\"," |
155 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; | 152 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; |
156 } | 153 } |
157 | 154 |
158 namespace gaia { | 155 namespace gaia { |
159 | 156 |
160 class GaiaOAuthClientTest : public testing::Test { | 157 class GaiaOAuthClientTest : public testing::Test { |
161 protected: | 158 protected: |
162 virtual void SetUp() OVERRIDE { | 159 virtual void SetUp() OVERRIDE { |
163 client_info_.client_id = "test_client_id"; | 160 client_info_.client_id = "test_client_id"; |
164 client_info_.client_secret = "test_client_secret"; | 161 client_info_.client_secret = "test_client_secret"; |
165 client_info_.redirect_uri = "test_redirect_uri"; | 162 client_info_.redirect_uri = "test_redirect_uri"; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 factory.set_complete_immediately(false); | 294 factory.set_complete_immediately(false); |
298 | 295 |
299 GaiaOAuthClient auth(GetRequestContext()); | 296 GaiaOAuthClient auth(GetRequestContext()); |
300 auth.RefreshToken(client_info_, "refresh_token", | 297 auth.RefreshToken(client_info_, "refresh_token", |
301 std::vector<std::string>(1, "scope4test"), -1, &delegate); | 298 std::vector<std::string>(1, "scope4test"), -1, &delegate); |
302 EXPECT_THAT(factory.get_url_fetcher()->upload_data(), | 299 EXPECT_THAT(factory.get_url_fetcher()->upload_data(), |
303 HasSubstr("&scope=scope4test")); | 300 HasSubstr("&scope=scope4test")); |
304 factory.get_url_fetcher()->Finish(); | 301 factory.get_url_fetcher()->Finish(); |
305 } | 302 } |
306 | 303 |
307 | |
308 TEST_F(GaiaOAuthClientTest, GetUserEmail) { | 304 TEST_F(GaiaOAuthClientTest, GetUserEmail) { |
309 MockGaiaOAuthClientDelegate delegate; | 305 MockGaiaOAuthClientDelegate delegate; |
310 EXPECT_CALL(delegate, OnGetUserEmailResponse(kTestUserEmail)).Times(1); | 306 EXPECT_CALL(delegate, OnGetUserEmailResponse(kTestUserEmail)).Times(1); |
311 | 307 |
| 308 const std::string kDummyUserInfoResult = |
| 309 "{\"emails\": [{\"value\":\"" + kTestUserEmail + |
| 310 "\", \"type\":\"account\"}]}"; |
| 311 |
312 MockOAuthFetcherFactory factory; | 312 MockOAuthFetcherFactory factory; |
313 factory.set_results(kDummyUserInfoResult); | 313 factory.set_results(kDummyUserInfoResult); |
314 | 314 |
| 315 GaiaOAuthClient auth(GetRequestContext()); |
| 316 auth.GetUserEmail("access_token", 1, &delegate); |
| 317 } |
| 318 |
| 319 TEST_F(GaiaOAuthClientTest, GetUserEmailSecondItemValid) { |
| 320 MockGaiaOAuthClientDelegate delegate; |
| 321 EXPECT_CALL(delegate, OnGetUserEmailResponse(kTestUserEmail)).Times(1); |
| 322 |
| 323 const std::string kDummyUserInfoResult = |
| 324 "{\"emails\": [{\"value\":\"foo\"}," |
| 325 "{\"value\":\"" + kTestUserEmail + |
| 326 "\", \"type\":\"account\"}]}"; |
| 327 |
| 328 MockOAuthFetcherFactory factory; |
| 329 factory.set_results(kDummyUserInfoResult); |
| 330 |
| 331 GaiaOAuthClient auth(GetRequestContext()); |
| 332 auth.GetUserEmail("access_token", 1, &delegate); |
| 333 } |
| 334 |
| 335 TEST_F(GaiaOAuthClientTest, GetUserEmailNoValidItems) { |
| 336 MockGaiaOAuthClientDelegate delegate; |
| 337 EXPECT_CALL(delegate, OnNetworkError(_)).Times(1); |
| 338 |
| 339 const std::string kDummyUserInfoResult = |
| 340 "{\"emails\": [{\"value\":\"" + kTestUserEmail + |
| 341 "\", \"type\":\"foo\"}]}"; |
| 342 |
| 343 MockOAuthFetcherFactory factory; |
| 344 factory.set_results(kDummyUserInfoResult); |
| 345 |
315 GaiaOAuthClient auth(GetRequestContext()); | 346 GaiaOAuthClient auth(GetRequestContext()); |
316 auth.GetUserEmail("access_token", 1, &delegate); | 347 auth.GetUserEmail("access_token", 1, &delegate); |
317 } | 348 } |
318 | 349 |
319 TEST_F(GaiaOAuthClientTest, GetUserId) { | 350 TEST_F(GaiaOAuthClientTest, GetUserId) { |
320 MockGaiaOAuthClientDelegate delegate; | 351 MockGaiaOAuthClientDelegate delegate; |
321 EXPECT_CALL(delegate, OnGetUserIdResponse(kTestUserId)).Times(1); | 352 EXPECT_CALL(delegate, OnGetUserIdResponse(kTestUserId)).Times(1); |
322 | 353 |
323 MockOAuthFetcherFactory factory; | 354 MockOAuthFetcherFactory factory; |
324 factory.set_results(kDummyUserIdResult); | 355 factory.set_results(kDummyUserIdResult); |
(...skipping 14 matching lines...) Expand all Loading... |
339 | 370 |
340 GaiaOAuthClient auth(GetRequestContext()); | 371 GaiaOAuthClient auth(GetRequestContext()); |
341 auth.GetTokenInfo("access_token", 1, &delegate); | 372 auth.GetTokenInfo("access_token", 1, &delegate); |
342 | 373 |
343 std::string issued_to; | 374 std::string issued_to; |
344 ASSERT_TRUE(captured_result->GetString("issued_to", &issued_to)); | 375 ASSERT_TRUE(captured_result->GetString("issued_to", &issued_to)); |
345 ASSERT_EQ("1234567890.apps.googleusercontent.com", issued_to); | 376 ASSERT_EQ("1234567890.apps.googleusercontent.com", issued_to); |
346 } | 377 } |
347 | 378 |
348 } // namespace gaia | 379 } // namespace gaia |
OLD | NEW |