Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(595)

Side by Side Diff: chrome/common/net/gaia/gaia_auth_fetcher_unittest.cc

Issue 7524033: Add a scoper object for URLFetcher::Factory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 GaiaAuthFetcher. 5 // A complete set of unit tests for GaiaAuthFetcher.
6 // Originally ported from GoogleAuthenticator tests. 6 // Originally ported from GoogleAuthenticator tests.
7 7
8 #include "chrome/common/net/gaia/gaia_auth_fetcher_unittest.h" 8 #include "chrome/common/net/gaia/gaia_auth_fetcher_unittest.h"
9 9
10 #include <string> 10 #include <string>
11 11
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "chrome/common/net/gaia/gaia_auth_consumer.h" 14 #include "chrome/common/net/gaia/gaia_auth_consumer.h"
15 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" 15 #include "chrome/common/net/gaia/gaia_auth_fetcher.h"
16 #include "chrome/common/net/gaia/gaia_urls.h" 16 #include "chrome/common/net/gaia/gaia_urls.h"
17 #include "chrome/common/net/gaia/google_service_auth_error.h" 17 #include "chrome/common/net/gaia/google_service_auth_error.h"
18 #include "chrome/common/net/http_return.h" 18 #include "chrome/common/net/http_return.h"
19 #include "chrome/test/testing_profile.h" 19 #include "chrome/test/testing_profile.h"
20 #include "content/common/test_url_fetcher_factory.h"
21 #include "content/common/url_fetcher.h" 20 #include "content/common/url_fetcher.h"
21 #include "content/test/test_url_fetcher_factory.h"
22 #include "googleurl/src/gurl.h" 22 #include "googleurl/src/gurl.h"
23 #include "net/base/net_errors.h" 23 #include "net/base/net_errors.h"
24 #include "net/url_request/url_request_status.h" 24 #include "net/url_request/url_request_status.h"
25 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 27
28 using ::testing::_; 28 using ::testing::_;
29 29
30 MockFetcher::MockFetcher(bool success, 30 MockFetcher::MockFetcher(bool success,
31 const GURL& url, 31 const GURL& url,
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 } 379 }
380 380
381 TEST_F(GaiaAuthFetcherTest, FullLogin) { 381 TEST_F(GaiaAuthFetcherTest, FullLogin) {
382 MockGaiaConsumer consumer; 382 MockGaiaConsumer consumer;
383 EXPECT_CALL(consumer, OnClientLoginSuccess(_)) 383 EXPECT_CALL(consumer, OnClientLoginSuccess(_))
384 .Times(1); 384 .Times(1);
385 385
386 TestingProfile profile; 386 TestingProfile profile;
387 387
388 MockFactory<MockFetcher> factory; 388 MockFactory<MockFetcher> factory;
389 URLFetcher::set_factory(&factory);
390 389
391 GaiaAuthFetcher auth(&consumer, std::string(), 390 GaiaAuthFetcher auth(&consumer, std::string(),
392 profile_.GetRequestContext()); 391 profile_.GetRequestContext());
393 auth.StartClientLogin("username", 392 auth.StartClientLogin("username",
394 "password", 393 "password",
395 "service", 394 "service",
396 std::string(), 395 std::string(),
397 std::string(), 396 std::string(),
398 GaiaAuthFetcher::HostedAccountsAllowed); 397 GaiaAuthFetcher::HostedAccountsAllowed);
399
400 URLFetcher::set_factory(NULL);
401 } 398 }
402 399
403 TEST_F(GaiaAuthFetcherTest, FullLoginFailure) { 400 TEST_F(GaiaAuthFetcherTest, FullLoginFailure) {
404 MockGaiaConsumer consumer; 401 MockGaiaConsumer consumer;
405 EXPECT_CALL(consumer, OnClientLoginFailure(_)) 402 EXPECT_CALL(consumer, OnClientLoginFailure(_))
406 .Times(1); 403 .Times(1);
407 404
408 TestingProfile profile; 405 TestingProfile profile;
409 406
410 MockFactory<MockFetcher> factory; 407 MockFactory<MockFetcher> factory;
411 URLFetcher::set_factory(&factory);
412 factory.set_success(false); 408 factory.set_success(false);
413 409
414 GaiaAuthFetcher auth(&consumer, std::string(), 410 GaiaAuthFetcher auth(&consumer, std::string(),
415 profile_.GetRequestContext()); 411 profile_.GetRequestContext());
416 auth.StartClientLogin("username", 412 auth.StartClientLogin("username",
417 "password", 413 "password",
418 "service", 414 "service",
419 std::string(), 415 std::string(),
420 std::string(), 416 std::string(),
421 GaiaAuthFetcher::HostedAccountsAllowed); 417 GaiaAuthFetcher::HostedAccountsAllowed);
422
423 URLFetcher::set_factory(NULL);
424 } 418 }
425 419
426 TEST_F(GaiaAuthFetcherTest, ClientFetchPending) { 420 TEST_F(GaiaAuthFetcherTest, ClientFetchPending) {
427 MockGaiaConsumer consumer; 421 MockGaiaConsumer consumer;
428 EXPECT_CALL(consumer, OnClientLoginSuccess(_)) 422 EXPECT_CALL(consumer, OnClientLoginSuccess(_))
429 .Times(1); 423 .Times(1);
430 424
431 TestingProfile profile; 425 TestingProfile profile;
432 TestURLFetcherFactory factory; 426 TestURLFetcherFactory factory;
433 URLFetcher::set_factory(&factory);
434 427
435 GaiaAuthFetcher auth(&consumer, std::string(), 428 GaiaAuthFetcher auth(&consumer, std::string(),
436 profile_.GetRequestContext()); 429 profile_.GetRequestContext());
437 auth.StartClientLogin("username", 430 auth.StartClientLogin("username",
438 "password", 431 "password",
439 "service", 432 "service",
440 std::string(), 433 std::string(),
441 std::string(), 434 std::string(),
442 GaiaAuthFetcher::HostedAccountsAllowed); 435 GaiaAuthFetcher::HostedAccountsAllowed);
443 436
444 URLFetcher::set_factory(NULL);
445 EXPECT_TRUE(auth.HasPendingFetch()); 437 EXPECT_TRUE(auth.HasPendingFetch());
446 auth.OnURLFetchComplete( 438 auth.OnURLFetchComplete(
447 NULL, 439 NULL,
448 client_login_source_, 440 client_login_source_,
449 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 441 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
450 RC_REQUEST_OK, 442 RC_REQUEST_OK,
451 cookies_, 443 cookies_,
452 "SID=sid\nLSID=lsid\nAuth=auth\n"); 444 "SID=sid\nLSID=lsid\nAuth=auth\n");
453 EXPECT_FALSE(auth.HasPendingFetch()); 445 EXPECT_FALSE(auth.HasPendingFetch());
454 } 446 }
455 447
456 TEST_F(GaiaAuthFetcherTest, FullTokenSuccess) { 448 TEST_F(GaiaAuthFetcherTest, FullTokenSuccess) {
457 MockGaiaConsumer consumer; 449 MockGaiaConsumer consumer;
458 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess("service", "token")) 450 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess("service", "token"))
459 .Times(1); 451 .Times(1);
460 452
461 TestingProfile profile; 453 TestingProfile profile;
454
462 TestURLFetcherFactory factory; 455 TestURLFetcherFactory factory;
463 URLFetcher::set_factory(&factory);
464
465 GaiaAuthFetcher auth(&consumer, std::string(), 456 GaiaAuthFetcher auth(&consumer, std::string(),
466 profile_.GetRequestContext()); 457 profile_.GetRequestContext());
467 auth.StartIssueAuthToken("sid", "lsid", "service"); 458 auth.StartIssueAuthToken("sid", "lsid", "service");
468 459
469 URLFetcher::set_factory(NULL);
470 EXPECT_TRUE(auth.HasPendingFetch()); 460 EXPECT_TRUE(auth.HasPendingFetch());
471 auth.OnURLFetchComplete( 461 auth.OnURLFetchComplete(
472 NULL, 462 NULL,
473 issue_auth_token_source_, 463 issue_auth_token_source_,
474 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 464 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
475 RC_REQUEST_OK, 465 RC_REQUEST_OK,
476 cookies_, 466 cookies_,
477 "token"); 467 "token");
478 EXPECT_FALSE(auth.HasPendingFetch()); 468 EXPECT_FALSE(auth.HasPendingFetch());
479 } 469 }
480 470
481 TEST_F(GaiaAuthFetcherTest, FullTokenFailure) { 471 TEST_F(GaiaAuthFetcherTest, FullTokenFailure) {
482 MockGaiaConsumer consumer; 472 MockGaiaConsumer consumer;
483 EXPECT_CALL(consumer, OnIssueAuthTokenFailure("service", _)) 473 EXPECT_CALL(consumer, OnIssueAuthTokenFailure("service", _))
484 .Times(1); 474 .Times(1);
485 475
486 TestingProfile profile; 476 TestingProfile profile;
487 TestURLFetcherFactory factory; 477 TestURLFetcherFactory factory;
488 URLFetcher::set_factory(&factory);
489 478
490 GaiaAuthFetcher auth(&consumer, std::string(), 479 GaiaAuthFetcher auth(&consumer, std::string(),
491 profile_.GetRequestContext()); 480 profile_.GetRequestContext());
492 auth.StartIssueAuthToken("sid", "lsid", "service"); 481 auth.StartIssueAuthToken("sid", "lsid", "service");
493 482
494 URLFetcher::set_factory(NULL);
495 EXPECT_TRUE(auth.HasPendingFetch()); 483 EXPECT_TRUE(auth.HasPendingFetch());
496 auth.OnURLFetchComplete( 484 auth.OnURLFetchComplete(
497 NULL, 485 NULL,
498 issue_auth_token_source_, 486 issue_auth_token_source_,
499 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 487 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
500 RC_FORBIDDEN, 488 RC_FORBIDDEN,
501 cookies_, 489 cookies_,
502 ""); 490 "");
503 EXPECT_FALSE(auth.HasPendingFetch()); 491 EXPECT_FALSE(auth.HasPendingFetch());
504 } 492 }
505 493
506 TEST_F(GaiaAuthFetcherTest, TokenAuthSuccess) { 494 TEST_F(GaiaAuthFetcherTest, TokenAuthSuccess) {
507 MockGaiaConsumer consumer; 495 MockGaiaConsumer consumer;
508 EXPECT_CALL(consumer, OnTokenAuthSuccess("<html></html>")) 496 EXPECT_CALL(consumer, OnTokenAuthSuccess("<html></html>"))
509 .Times(1); 497 .Times(1);
510 498
511 TestingProfile profile; 499 TestingProfile profile;
512 TestURLFetcherFactory factory; 500 TestURLFetcherFactory factory;
513 URLFetcher::set_factory(&factory);
514 501
515 GaiaAuthFetcher auth(&consumer, std::string(), 502 GaiaAuthFetcher auth(&consumer, std::string(),
516 profile_.GetRequestContext()); 503 profile_.GetRequestContext());
517 auth.StartTokenAuth("myubertoken"); 504 auth.StartTokenAuth("myubertoken");
518 505
519 URLFetcher::set_factory(NULL);
520 EXPECT_TRUE(auth.HasPendingFetch()); 506 EXPECT_TRUE(auth.HasPendingFetch());
521 auth.OnURLFetchComplete( 507 auth.OnURLFetchComplete(
522 NULL, 508 NULL,
523 token_auth_source_, 509 token_auth_source_,
524 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 510 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
525 RC_REQUEST_OK, 511 RC_REQUEST_OK,
526 cookies_, 512 cookies_,
527 "<html></html>"); 513 "<html></html>");
528 EXPECT_FALSE(auth.HasPendingFetch()); 514 EXPECT_FALSE(auth.HasPendingFetch());
529 } 515 }
530 516
531 TEST_F(GaiaAuthFetcherTest, TokenAuthUnauthorizedFailure) { 517 TEST_F(GaiaAuthFetcherTest, TokenAuthUnauthorizedFailure) {
532 MockGaiaConsumer consumer; 518 MockGaiaConsumer consumer;
533 EXPECT_CALL(consumer, OnTokenAuthFailure(_)) 519 EXPECT_CALL(consumer, OnTokenAuthFailure(_))
534 .Times(1); 520 .Times(1);
535 521
536 TestingProfile profile; 522 TestingProfile profile;
537 TestURLFetcherFactory factory; 523 TestURLFetcherFactory factory;
538 URLFetcher::set_factory(&factory);
539 524
540 GaiaAuthFetcher auth(&consumer, std::string(), 525 GaiaAuthFetcher auth(&consumer, std::string(),
541 profile_.GetRequestContext()); 526 profile_.GetRequestContext());
542 auth.StartTokenAuth("badubertoken"); 527 auth.StartTokenAuth("badubertoken");
543 528
544 URLFetcher::set_factory(NULL);
545 EXPECT_TRUE(auth.HasPendingFetch()); 529 EXPECT_TRUE(auth.HasPendingFetch());
546 auth.OnURLFetchComplete( 530 auth.OnURLFetchComplete(
547 NULL, 531 NULL,
548 token_auth_source_, 532 token_auth_source_,
549 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 533 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
550 RC_UNAUTHORIZED, 534 RC_UNAUTHORIZED,
551 cookies_, 535 cookies_,
552 ""); 536 "");
553 EXPECT_FALSE(auth.HasPendingFetch()); 537 EXPECT_FALSE(auth.HasPendingFetch());
554 } 538 }
555 539
556 TEST_F(GaiaAuthFetcherTest, TokenAuthNetFailure) { 540 TEST_F(GaiaAuthFetcherTest, TokenAuthNetFailure) {
557 MockGaiaConsumer consumer; 541 MockGaiaConsumer consumer;
558 EXPECT_CALL(consumer, OnTokenAuthFailure(_)) 542 EXPECT_CALL(consumer, OnTokenAuthFailure(_))
559 .Times(1); 543 .Times(1);
560 544
561 TestingProfile profile; 545 TestingProfile profile;
562 TestURLFetcherFactory factory; 546 TestURLFetcherFactory factory;
563 URLFetcher::set_factory(&factory);
564 547
565 GaiaAuthFetcher auth(&consumer, std::string(), 548 GaiaAuthFetcher auth(&consumer, std::string(),
566 profile_.GetRequestContext()); 549 profile_.GetRequestContext());
567 auth.StartTokenAuth("badubertoken"); 550 auth.StartTokenAuth("badubertoken");
568 551
569 URLFetcher::set_factory(NULL);
570 EXPECT_TRUE(auth.HasPendingFetch()); 552 EXPECT_TRUE(auth.HasPendingFetch());
571 auth.OnURLFetchComplete( 553 auth.OnURLFetchComplete(
572 NULL, 554 NULL,
573 token_auth_source_, 555 token_auth_source_,
574 net::URLRequestStatus(net::URLRequestStatus::FAILED, 0), 556 net::URLRequestStatus(net::URLRequestStatus::FAILED, 0),
575 RC_REQUEST_OK, 557 RC_REQUEST_OK,
576 cookies_, 558 cookies_,
577 ""); 559 "");
578 EXPECT_FALSE(auth.HasPendingFetch()); 560 EXPECT_FALSE(auth.HasPendingFetch());
579 } 561 }
OLDNEW
« no previous file with comments | « chrome/common/net/gaia/gaia_auth_fetcher_unittest.h ('k') | chrome/common/net/gaia/gaia_oauth_client_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698