Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 #include "remoting/test/access_token_fetcher.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "google_apis/gaia/gaia_urls.h" | |
| 11 #include "net/url_request/test_url_fetcher_factory.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 const char kAuthCodeValue[] = "test_auth_code_value"; | |
| 16 const char kAccessTokenValue[] = "test_access_token_value"; | |
| 17 const char kRefreshTokenValue[] = "test_refresh_token_value"; | |
| 18 const char kAuthCodeExchangeValidResponse[] = | |
| 19 "{" | |
| 20 " \"refresh_token\": \"test_refresh_token_value\"," | |
| 21 " \"access_token\": \"test_access_token_value\"," | |
| 22 " \"expires_in\": 3600," | |
| 23 " \"token_type\": \"Bearer\"" | |
| 24 "}"; | |
| 25 const char kAuthCodeExchangeEmptyResponse[] = "{}"; | |
| 26 const char kRefreshTokenExchangeValidResponse[] = | |
| 27 "{" | |
| 28 " \"access_token\": \"test_access_token_value\"," | |
| 29 " \"expires_in\": 3600," | |
| 30 " \"token_type\": \"Bearer\"" | |
| 31 "}"; | |
| 32 const char kRefreshTokenExchangeEmptyResponse[] = "{}"; | |
| 33 const char kValidTokenInfoResponse[] = | |
| 34 "{" | |
| 35 " \"audience\": \"blah.apps.googleusercontent.blah.com\"," | |
| 36 " \"used_id\": \"1234567890\"," | |
| 37 " \"scope\": \"all the things\"," | |
| 38 " \"expires_in\": \"1800\"," | |
| 39 " \"token_type\": \"Bearer\"" | |
| 40 "}"; | |
| 41 const char kInvalidTokenInfoResponse[] = | |
| 42 "{" | |
| 43 " \"error\": \"invalid_token\"" | |
| 44 "}"; | |
| 45 } | |
| 46 | |
| 47 namespace remoting { | |
| 48 namespace test { | |
| 49 | |
| 50 // Provides base functionality for the AccessTokenFetcher Tests below. The | |
| 51 // FakeURLFetcherFactory allows us to override the response data and payload for | |
| 52 // specified URLs. We use this to stub out network calls made by the | |
| 53 // GaiaOAuthClient used by the AccessTokenFetcher. The other service provided | |
| 54 // by this test fixture is to ensure that a message loop is running on the | |
| 55 // current thread. This is needed to create a resource context getter which is | |
| 56 // passed to the GaiaOAuthClient instance on construction. | |
|
Wez
2015/02/19 22:00:23
This seems to be an irrelevant implementation deta
joedow
2015/02/20 02:58:35
Done.
| |
| 57 class AccessTokenFetcherTest : public ::testing::Test { | |
| 58 public: | |
| 59 AccessTokenFetcherTest() : | |
| 60 url_fetcher_factory_(nullptr) {} | |
| 61 ~AccessTokenFetcherTest() override {} | |
| 62 | |
| 63 void OnAccessTokenRetrieved( | |
| 64 base::Closure run_loop_quit_closure, | |
|
Wez
2015/02/19 22:00:23
Suggest calling this done_closure, since in princi
joedow
2015/02/20 02:58:35
Done.
| |
| 65 const std::string& access_token, | |
| 66 const std::string& refresh_token) { | |
| 67 access_token_retrieved = access_token; | |
| 68 refresh_token_retrieved = refresh_token; | |
| 69 | |
| 70 run_loop_quit_closure.Run(); | |
| 71 } | |
| 72 | |
| 73 protected: | |
| 74 // Test interface. | |
| 75 void SetUp() override { | |
| 76 if (!base::MessageLoop::current()) { | |
| 77 // Create a temporary message loop if the current thread does not already | |
| 78 // have one so we can use its task runner to create a request object. | |
| 79 message_loop_.reset(new base::MessageLoopForIO); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void SetFakeResponse(const GURL& url, | |
| 84 const std::string& data, | |
| 85 net::HttpStatusCode code, | |
| 86 net::URLRequestStatus::Status status) { | |
| 87 url_fetcher_factory_.SetFakeResponse(url, data, code, status); | |
| 88 } | |
| 89 | |
| 90 // Used for result verification | |
| 91 std::string access_token_retrieved; | |
|
Wez
2015/02/19 22:00:23
Members must end in _
joedow
2015/02/20 02:58:35
Done.
| |
| 92 std::string refresh_token_retrieved; | |
| 93 | |
| 94 private: | |
| 95 net::FakeURLFetcherFactory url_fetcher_factory_; | |
| 96 scoped_ptr<base::MessageLoopForIO> message_loop_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherTest); | |
| 99 }; | |
| 100 | |
| 101 TEST_F(AccessTokenFetcherTest, ExchangeAuthCodeForAccessToken) { | |
| 102 SetFakeResponse( | |
| 103 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 104 kAuthCodeExchangeValidResponse, | |
| 105 net::HTTP_OK, | |
| 106 net::URLRequestStatus::SUCCESS); | |
| 107 | |
| 108 SetFakeResponse( | |
| 109 GaiaUrls::GetInstance()->oauth2_token_info_url(), | |
| 110 kValidTokenInfoResponse, | |
| 111 net::HTTP_OK, | |
| 112 net::URLRequestStatus::SUCCESS); | |
| 113 | |
| 114 base::RunLoop network_request_run_loop; | |
|
Wez
2015/02/19 22:00:23
Why not just |run_loop|?
joedow
2015/02/20 02:58:35
Done.
| |
| 115 AccessTokenCallback access_token_callback = | |
| 116 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 117 base::Unretained(this), | |
| 118 network_request_run_loop.QuitClosure()); | |
| 119 | |
| 120 AccessTokenFetcher access_token_fetcher; | |
| 121 access_token_fetcher.GetAccessTokenFromAuthCode( | |
| 122 kAuthCodeValue, | |
| 123 access_token_callback); | |
| 124 | |
| 125 network_request_run_loop.Run(); | |
| 126 | |
| 127 EXPECT_STREQ(kAccessTokenValue, access_token_retrieved.c_str()); | |
| 128 EXPECT_STREQ(kRefreshTokenValue, refresh_token_retrieved.c_str()); | |
| 129 } | |
| 130 | |
| 131 TEST_F(AccessTokenFetcherTest, ExchangeRefreshTokenForAccessToken) { | |
| 132 SetFakeResponse( | |
| 133 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 134 kRefreshTokenExchangeValidResponse, | |
| 135 net::HTTP_OK, | |
| 136 net::URLRequestStatus::SUCCESS); | |
| 137 | |
| 138 SetFakeResponse( | |
| 139 GaiaUrls::GetInstance()->oauth2_token_info_url(), | |
| 140 kValidTokenInfoResponse, | |
| 141 net::HTTP_OK, | |
| 142 net::URLRequestStatus::SUCCESS); | |
| 143 | |
| 144 base::RunLoop network_request_run_loop; | |
|
Wez
2015/02/19 22:00:23
As above
joedow
2015/02/20 02:58:35
Done.
| |
| 145 AccessTokenCallback access_token_callback = | |
| 146 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 147 base::Unretained(this), | |
| 148 network_request_run_loop.QuitClosure()); | |
| 149 | |
| 150 AccessTokenFetcher access_token_fetcher; | |
| 151 access_token_fetcher.GetAccessTokenFromRefreshToken( | |
| 152 kRefreshTokenValue, | |
| 153 access_token_callback); | |
| 154 | |
| 155 network_request_run_loop.Run(); | |
| 156 | |
| 157 EXPECT_STREQ(kAccessTokenValue, access_token_retrieved.c_str()); | |
| 158 EXPECT_STREQ(kRefreshTokenValue, refresh_token_retrieved.c_str()); | |
| 159 } | |
| 160 | |
| 161 TEST_F(AccessTokenFetcherTest, MultipleAccessTokenCalls) { | |
| 162 SetFakeResponse( | |
| 163 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 164 kAuthCodeExchangeValidResponse, | |
| 165 net::HTTP_OK, | |
| 166 net::URLRequestStatus::SUCCESS); | |
| 167 | |
| 168 SetFakeResponse( | |
| 169 GaiaUrls::GetInstance()->oauth2_token_info_url(), | |
| 170 kValidTokenInfoResponse, | |
| 171 net::HTTP_OK, | |
| 172 net::URLRequestStatus::SUCCESS); | |
| 173 | |
| 174 scoped_ptr<base::RunLoop> network_request_run_loop; | |
|
Wez
2015/02/19 22:00:23
As above
joedow
2015/02/20 02:58:35
Done.
| |
| 175 network_request_run_loop.reset(new base::RunLoop()); | |
| 176 AccessTokenCallback access_token_callback = | |
| 177 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 178 base::Unretained(this), | |
| 179 network_request_run_loop->QuitClosure()); | |
| 180 | |
| 181 AccessTokenFetcher access_token_fetcher; | |
| 182 access_token_fetcher.GetAccessTokenFromAuthCode( | |
| 183 kAuthCodeValue, | |
| 184 access_token_callback); | |
| 185 | |
| 186 network_request_run_loop->Run(); | |
| 187 | |
| 188 EXPECT_STREQ(kAccessTokenValue, access_token_retrieved.c_str()); | |
| 189 EXPECT_STREQ(kRefreshTokenValue, refresh_token_retrieved.c_str()); | |
| 190 | |
| 191 // Reset our token data for the next iteration. | |
| 192 access_token_retrieved.clear(); | |
| 193 refresh_token_retrieved.clear(); | |
| 194 | |
| 195 // Update the response since we will call the refresh token method next. | |
| 196 SetFakeResponse( | |
| 197 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 198 kRefreshTokenExchangeValidResponse, | |
| 199 net::HTTP_OK, | |
| 200 net::URLRequestStatus::SUCCESS); | |
| 201 | |
| 202 network_request_run_loop.reset(new base::RunLoop()); | |
| 203 access_token_callback = | |
| 204 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 205 base::Unretained(this), | |
| 206 network_request_run_loop->QuitClosure()); | |
| 207 | |
| 208 access_token_fetcher.GetAccessTokenFromRefreshToken( | |
| 209 kRefreshTokenValue, | |
| 210 access_token_callback); | |
| 211 | |
| 212 network_request_run_loop->Run(); | |
| 213 | |
| 214 EXPECT_STREQ(kAccessTokenValue, | |
| 215 access_token_retrieved.c_str()); | |
| 216 EXPECT_STREQ(kRefreshTokenValue, | |
| 217 refresh_token_retrieved.c_str()); | |
| 218 | |
| 219 network_request_run_loop.reset(new base::RunLoop()); | |
| 220 access_token_callback = | |
| 221 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 222 base::Unretained(this), | |
| 223 network_request_run_loop->QuitClosure()); | |
| 224 | |
| 225 // Reset our token data for the next iteration. | |
| 226 access_token_retrieved.clear(); | |
| 227 refresh_token_retrieved.clear(); | |
| 228 | |
| 229 access_token_fetcher.GetAccessTokenFromRefreshToken( | |
| 230 kRefreshTokenValue, | |
| 231 access_token_callback); | |
| 232 | |
| 233 network_request_run_loop->Run(); | |
| 234 | |
| 235 EXPECT_STREQ(kAccessTokenValue, | |
| 236 access_token_retrieved.c_str()); | |
| 237 EXPECT_STREQ(kRefreshTokenValue, | |
| 238 refresh_token_retrieved.c_str()); | |
| 239 } | |
| 240 | |
| 241 TEST_F(AccessTokenFetcherTest, ExchangeAuthCode_Unauthorized_Error) { | |
| 242 SetFakeResponse( | |
| 243 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 244 kAuthCodeExchangeValidResponse, | |
| 245 net::HTTP_UNAUTHORIZED, | |
| 246 net::URLRequestStatus::FAILED); | |
| 247 | |
| 248 base::RunLoop network_request_run_loop; | |
| 249 AccessTokenCallback access_token_callback = | |
| 250 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 251 base::Unretained(this), | |
| 252 network_request_run_loop.QuitClosure()); | |
| 253 | |
| 254 AccessTokenFetcher access_token_fetcher; | |
| 255 access_token_fetcher.GetAccessTokenFromAuthCode( | |
| 256 kAuthCodeValue, | |
| 257 access_token_callback); | |
| 258 | |
| 259 network_request_run_loop.Run(); | |
| 260 | |
| 261 EXPECT_STREQ("", access_token_retrieved.c_str()); | |
| 262 EXPECT_STREQ("", refresh_token_retrieved.c_str()); | |
| 263 } | |
| 264 | |
| 265 TEST_F(AccessTokenFetcherTest, ExchangeRefreshToken_Unauthorized_Error) { | |
| 266 SetFakeResponse( | |
| 267 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 268 kRefreshTokenExchangeValidResponse, | |
| 269 net::HTTP_UNAUTHORIZED, | |
| 270 net::URLRequestStatus::FAILED); | |
| 271 | |
| 272 base::RunLoop network_request_run_loop; | |
| 273 AccessTokenCallback access_token_callback = | |
| 274 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 275 base::Unretained(this), | |
| 276 network_request_run_loop.QuitClosure()); | |
| 277 | |
| 278 AccessTokenFetcher access_token_fetcher; | |
| 279 access_token_fetcher.GetAccessTokenFromRefreshToken( | |
| 280 kRefreshTokenValue, | |
| 281 access_token_callback); | |
| 282 | |
| 283 network_request_run_loop.Run(); | |
| 284 | |
| 285 EXPECT_STREQ("", access_token_retrieved.c_str()); | |
| 286 EXPECT_STREQ("", refresh_token_retrieved.c_str()); | |
| 287 } | |
| 288 | |
| 289 TEST_F(AccessTokenFetcherTest, ExchangeAuthCode_NetworkError) { | |
| 290 SetFakeResponse( | |
| 291 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 292 kAuthCodeExchangeValidResponse, | |
| 293 net::HTTP_NOT_FOUND, | |
| 294 net::URLRequestStatus::FAILED); | |
| 295 | |
| 296 base::RunLoop network_request_run_loop; | |
| 297 AccessTokenCallback access_token_callback = | |
| 298 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 299 base::Unretained(this), | |
| 300 network_request_run_loop.QuitClosure()); | |
| 301 | |
| 302 AccessTokenFetcher access_token_fetcher; | |
| 303 access_token_fetcher.GetAccessTokenFromAuthCode( | |
| 304 kAuthCodeValue, | |
| 305 access_token_callback); | |
| 306 | |
| 307 network_request_run_loop.Run(); | |
| 308 | |
| 309 EXPECT_STREQ("", access_token_retrieved.c_str()); | |
| 310 EXPECT_STREQ("", refresh_token_retrieved.c_str()); | |
| 311 } | |
| 312 | |
| 313 TEST_F(AccessTokenFetcherTest, ExchangeRefreshToken_NetworkError) { | |
| 314 SetFakeResponse( | |
| 315 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 316 kRefreshTokenExchangeValidResponse, | |
| 317 net::HTTP_NOT_FOUND, | |
| 318 net::URLRequestStatus::FAILED); | |
| 319 | |
| 320 base::RunLoop network_request_run_loop; | |
| 321 AccessTokenCallback access_token_callback = | |
| 322 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 323 base::Unretained(this), | |
| 324 network_request_run_loop.QuitClosure()); | |
| 325 | |
| 326 AccessTokenFetcher access_token_fetcher; | |
| 327 access_token_fetcher.GetAccessTokenFromRefreshToken( | |
| 328 kRefreshTokenValue, | |
| 329 access_token_callback); | |
| 330 | |
| 331 network_request_run_loop.Run(); | |
| 332 | |
| 333 EXPECT_STREQ("", access_token_retrieved.c_str()); | |
| 334 EXPECT_STREQ("", refresh_token_retrieved.c_str()); | |
| 335 } | |
| 336 | |
| 337 TEST_F(AccessTokenFetcherTest, AuthCode_GetTokenInfoResponse_InvalidToken) { | |
| 338 SetFakeResponse( | |
| 339 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 340 kAuthCodeExchangeValidResponse, | |
| 341 net::HTTP_OK, | |
| 342 net::URLRequestStatus::SUCCESS); | |
| 343 | |
| 344 SetFakeResponse( | |
| 345 GaiaUrls::GetInstance()->oauth2_token_info_url(), | |
| 346 kInvalidTokenInfoResponse, | |
| 347 net::HTTP_OK, | |
| 348 net::URLRequestStatus::SUCCESS); | |
| 349 | |
| 350 base::RunLoop network_request_run_loop; | |
| 351 AccessTokenCallback access_token_callback = | |
| 352 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 353 base::Unretained(this), | |
| 354 network_request_run_loop.QuitClosure()); | |
| 355 | |
| 356 AccessTokenFetcher access_token_fetcher; | |
| 357 access_token_fetcher.GetAccessTokenFromAuthCode( | |
| 358 kAuthCodeValue, | |
| 359 access_token_callback); | |
| 360 | |
| 361 network_request_run_loop.Run(); | |
| 362 | |
| 363 // Our callback should have been called with empty strings. | |
| 364 EXPECT_STREQ("", access_token_retrieved.c_str()); | |
| 365 EXPECT_STREQ("", refresh_token_retrieved.c_str()); | |
| 366 } | |
| 367 | |
| 368 TEST_F(AccessTokenFetcherTest, ExchangeAuthCodeForAccessToken_EmptyToken) { | |
| 369 SetFakeResponse( | |
| 370 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 371 kAuthCodeExchangeEmptyResponse, | |
| 372 net::HTTP_OK, | |
| 373 net::URLRequestStatus::SUCCESS); | |
| 374 | |
| 375 base::RunLoop network_request_run_loop; | |
| 376 AccessTokenCallback access_token_callback = | |
| 377 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 378 base::Unretained(this), | |
| 379 network_request_run_loop.QuitClosure()); | |
| 380 | |
| 381 AccessTokenFetcher access_token_fetcher; | |
| 382 access_token_fetcher.GetAccessTokenFromAuthCode( | |
| 383 kAuthCodeValue, | |
| 384 access_token_callback); | |
| 385 | |
| 386 network_request_run_loop.Run(); | |
| 387 | |
| 388 // Our callback should have been called with empty strings. | |
| 389 EXPECT_STREQ("", access_token_retrieved.c_str()); | |
| 390 EXPECT_STREQ("", refresh_token_retrieved.c_str()); | |
| 391 } | |
| 392 | |
| 393 TEST_F(AccessTokenFetcherTest, RefreshToken_GetTokenInfoResponse_InvalidToken) { | |
| 394 SetFakeResponse( | |
| 395 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 396 kRefreshTokenExchangeValidResponse, | |
| 397 net::HTTP_OK, | |
| 398 net::URLRequestStatus::SUCCESS); | |
| 399 | |
| 400 SetFakeResponse( | |
| 401 GaiaUrls::GetInstance()->oauth2_token_info_url(), | |
| 402 kInvalidTokenInfoResponse, | |
| 403 net::HTTP_OK, | |
| 404 net::URLRequestStatus::SUCCESS); | |
| 405 | |
| 406 base::RunLoop network_request_run_loop; | |
| 407 AccessTokenCallback access_token_callback = | |
| 408 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 409 base::Unretained(this), | |
| 410 network_request_run_loop.QuitClosure()); | |
| 411 | |
| 412 AccessTokenFetcher access_token_fetcher; | |
| 413 access_token_fetcher.GetAccessTokenFromRefreshToken( | |
| 414 kRefreshTokenValue, | |
| 415 access_token_callback); | |
| 416 | |
| 417 network_request_run_loop.Run(); | |
| 418 | |
| 419 // Our callback should have been called with empty strings. | |
| 420 EXPECT_STREQ("", access_token_retrieved.c_str()); | |
| 421 EXPECT_STREQ("", refresh_token_retrieved.c_str()); | |
| 422 } | |
| 423 | |
| 424 TEST_F(AccessTokenFetcherTest, ExchangeRefreshTokenForAccessToken_EmptyToken) { | |
| 425 SetFakeResponse( | |
| 426 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 427 kRefreshTokenExchangeEmptyResponse, | |
| 428 net::HTTP_OK, | |
| 429 net::URLRequestStatus::SUCCESS); | |
| 430 | |
| 431 base::RunLoop network_request_run_loop; | |
| 432 AccessTokenCallback access_token_callback = | |
| 433 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, | |
| 434 base::Unretained(this), | |
| 435 network_request_run_loop.QuitClosure()); | |
| 436 | |
| 437 AccessTokenFetcher access_token_fetcher; | |
| 438 access_token_fetcher.GetAccessTokenFromRefreshToken( | |
| 439 kRefreshTokenValue, | |
| 440 access_token_callback); | |
| 441 | |
| 442 network_request_run_loop.Run(); | |
| 443 | |
| 444 // Our callback should have been called with empty strings. | |
| 445 EXPECT_STREQ("", access_token_retrieved.c_str()); | |
| 446 EXPECT_STREQ("", refresh_token_retrieved.c_str()); | |
| 447 } | |
| 448 | |
| 449 } // namespace test | |
| 450 } // namespace remoting | |
| OLD | NEW |