| 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 + "\"}"; |
| 144 | 147 |
| 145 const std::string kDummyUserIdResult = | 148 const std::string kDummyUserIdResult = |
| 146 "{\"id\":\"" + kTestUserId + "\"}"; | 149 "{\"id\":\"" + kTestUserId + "\"}"; |
| 147 | 150 |
| 148 const std::string kDummyTokenInfoResult = | 151 const std::string kDummyTokenInfoResult = |
| 149 "{\"issued_to\": \"1234567890.apps.googleusercontent.com\"," | 152 "{\"issued_to\": \"1234567890.apps.googleusercontent.com\"," |
| 150 "\"audience\": \"1234567890.apps.googleusercontent.com\"," | 153 "\"audience\": \"1234567890.apps.googleusercontent.com\"," |
| 151 "\"scope\": \"https://googleapis.com/oauth2/v2/tokeninfo\"," | 154 "\"scope\": \"https://googleapis.com/oauth2/v2/tokeninfo\"," |
| 152 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; | 155 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; |
| 153 } | 156 } |
| 154 | 157 |
| 155 namespace gaia { | 158 namespace gaia { |
| 156 | 159 |
| 157 class GaiaOAuthClientTest : public testing::Test { | 160 class GaiaOAuthClientTest : public testing::Test { |
| 158 protected: | 161 protected: |
| 159 virtual void SetUp() OVERRIDE { | 162 virtual void SetUp() OVERRIDE { |
| 160 client_info_.client_id = "test_client_id"; | 163 client_info_.client_id = "test_client_id"; |
| 161 client_info_.client_secret = "test_client_secret"; | 164 client_info_.client_secret = "test_client_secret"; |
| 162 client_info_.redirect_uri = "test_redirect_uri"; | 165 client_info_.redirect_uri = "test_redirect_uri"; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 factory.set_complete_immediately(false); | 297 factory.set_complete_immediately(false); |
| 295 | 298 |
| 296 GaiaOAuthClient auth(GetRequestContext()); | 299 GaiaOAuthClient auth(GetRequestContext()); |
| 297 auth.RefreshToken(client_info_, "refresh_token", | 300 auth.RefreshToken(client_info_, "refresh_token", |
| 298 std::vector<std::string>(1, "scope4test"), -1, &delegate); | 301 std::vector<std::string>(1, "scope4test"), -1, &delegate); |
| 299 EXPECT_THAT(factory.get_url_fetcher()->upload_data(), | 302 EXPECT_THAT(factory.get_url_fetcher()->upload_data(), |
| 300 HasSubstr("&scope=scope4test")); | 303 HasSubstr("&scope=scope4test")); |
| 301 factory.get_url_fetcher()->Finish(); | 304 factory.get_url_fetcher()->Finish(); |
| 302 } | 305 } |
| 303 | 306 |
| 307 |
| 304 TEST_F(GaiaOAuthClientTest, GetUserEmail) { | 308 TEST_F(GaiaOAuthClientTest, GetUserEmail) { |
| 305 MockGaiaOAuthClientDelegate delegate; | 309 MockGaiaOAuthClientDelegate delegate; |
| 306 EXPECT_CALL(delegate, OnGetUserEmailResponse(kTestUserEmail)).Times(1); | 310 EXPECT_CALL(delegate, OnGetUserEmailResponse(kTestUserEmail)).Times(1); |
| 307 | 311 |
| 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()); | 315 GaiaOAuthClient auth(GetRequestContext()); |
| 316 auth.GetUserEmail("access_token", 1, &delegate); | 316 auth.GetUserEmail("access_token", 1, &delegate); |
| 317 } | 317 } |
| 318 | 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 | |
| 346 GaiaOAuthClient auth(GetRequestContext()); | |
| 347 auth.GetUserEmail("access_token", 1, &delegate); | |
| 348 } | |
| 349 | |
| 350 TEST_F(GaiaOAuthClientTest, GetUserId) { | 319 TEST_F(GaiaOAuthClientTest, GetUserId) { |
| 351 MockGaiaOAuthClientDelegate delegate; | 320 MockGaiaOAuthClientDelegate delegate; |
| 352 EXPECT_CALL(delegate, OnGetUserIdResponse(kTestUserId)).Times(1); | 321 EXPECT_CALL(delegate, OnGetUserIdResponse(kTestUserId)).Times(1); |
| 353 | 322 |
| 354 MockOAuthFetcherFactory factory; | 323 MockOAuthFetcherFactory factory; |
| 355 factory.set_results(kDummyUserIdResult); | 324 factory.set_results(kDummyUserIdResult); |
| 356 | 325 |
| 357 GaiaOAuthClient auth(GetRequestContext()); | 326 GaiaOAuthClient auth(GetRequestContext()); |
| 358 auth.GetUserId("access_token", 1, &delegate); | 327 auth.GetUserId("access_token", 1, &delegate); |
| 359 } | 328 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 370 | 339 |
| 371 GaiaOAuthClient auth(GetRequestContext()); | 340 GaiaOAuthClient auth(GetRequestContext()); |
| 372 auth.GetTokenInfo("access_token", 1, &delegate); | 341 auth.GetTokenInfo("access_token", 1, &delegate); |
| 373 | 342 |
| 374 std::string issued_to; | 343 std::string issued_to; |
| 375 ASSERT_TRUE(captured_result->GetString("issued_to", &issued_to)); | 344 ASSERT_TRUE(captured_result->GetString("issued_to", &issued_to)); |
| 376 ASSERT_EQ("1234567890.apps.googleusercontent.com", issued_to); | 345 ASSERT_EQ("1234567890.apps.googleusercontent.com", issued_to); |
| 377 } | 346 } |
| 378 | 347 |
| 379 } // namespace gaia | 348 } // namespace gaia |
| OLD | NEW |