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

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

Issue 8632005: Part 1 of work to do user sign in based on OAuth2: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years 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>
(...skipping 10 matching lines...) Expand all
21 #include "content/test/test_url_fetcher_factory.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/load_flags.h" 23 #include "net/base/load_flags.h"
24 #include "net/base/net_errors.h" 24 #include "net/base/net_errors.h"
25 #include "net/url_request/url_request_status.h" 25 #include "net/url_request/url_request_status.h"
26 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
28 28
29 using ::testing::_; 29 using ::testing::_;
30 30
31 namespace {
32 static const char kGetAuthCodeValidCookie[] =
33 "oauth_code=test-code; Path=/test; Secure; HttpOnly";
34 static const char kGetAuthCodeCookieNoSecure[] =
35 "oauth_code=test-code; Path=/test; HttpOnly";
36 static const char kGetAuthCodeCookieNoHttpOnly[] =
37 "oauth_code=test-code; Path=/test; Secure";
38 static const char kGetAuthCodeCookieNoOAuthCode[] =
39 "Path=/test; Secure; HttpOnly";
40 static const char kGetTokenPairValidResponse[] =
41 "{"
42 " \"refresh_token\": \"rt1\","
43 " \"access_token\": \"at1\","
44 " \"expires_in\": 3600,"
45 " \"token_type\": \"Bearer\""
46 "}";
47 static const char kGetTokenPairResponseNoRefreshToken[] =
48 "{"
49 " \"access_token\": \"at1\","
50 " \"expires_in\": 3600,"
51 " \"token_type\": \"Bearer\""
52 "}";
53 static const char kGetTokenPairResponseNoAccessToken[] =
54 "{"
55 " \"refresh_token\": \"rt1\","
56 " \"expires_in\": 3600,"
57 " \"token_type\": \"Bearer\""
58 "}";
59 static const char kGetTokenPairResponseNoExpiresIn[] =
60 "{"
61 " \"refresh_token\": \"rt1\","
62 " \"access_token\": \"at1\","
63 " \"token_type\": \"Bearer\""
64 "}";
65 } // namespace
66
31 MockFetcher::MockFetcher(bool success, 67 MockFetcher::MockFetcher(bool success,
32 const GURL& url, 68 const GURL& url,
33 const std::string& results, 69 const std::string& results,
34 content::URLFetcher::RequestType request_type, 70 content::URLFetcher::RequestType request_type,
35 content::URLFetcherDelegate* d) 71 content::URLFetcherDelegate* d)
36 : TestURLFetcher(0, url, d) { 72 : TestURLFetcher(0, url, d) {
37 set_url(url); 73 set_url(url);
38 net::URLRequestStatus::Status code; 74 net::URLRequestStatus::Status code;
39 75
40 if (success) { 76 if (success) {
(...skipping 28 matching lines...) Expand all
69 void MockFetcher::Start() { 105 void MockFetcher::Start() {
70 delegate()->OnURLFetchComplete(this); 106 delegate()->OnURLFetchComplete(this);
71 } 107 }
72 108
73 class GaiaAuthFetcherTest : public testing::Test { 109 class GaiaAuthFetcherTest : public testing::Test {
74 public: 110 public:
75 GaiaAuthFetcherTest() 111 GaiaAuthFetcherTest()
76 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), 112 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()),
77 issue_auth_token_source_( 113 issue_auth_token_source_(
78 GaiaUrls::GetInstance()->issue_auth_token_url()), 114 GaiaUrls::GetInstance()->issue_auth_token_url()),
115 client_login_to_oauth2_source_(
116 GaiaUrls::GetInstance()->client_login_to_oauth2_url()),
117 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()),
79 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), 118 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()),
80 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()) {} 119 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()) {}
81 120
82 void RunParsingTest(const std::string& data, 121 void RunParsingTest(const std::string& data,
83 const std::string& sid, 122 const std::string& sid,
84 const std::string& lsid, 123 const std::string& lsid,
85 const std::string& token) { 124 const std::string& token) {
86 std::string out_sid; 125 std::string out_sid;
87 std::string out_lsid; 126 std::string out_lsid;
88 std::string out_token; 127 std::string out_token;
(...skipping 24 matching lines...) Expand all
113 &out_captcha_token); 152 &out_captcha_token);
114 EXPECT_EQ(error, out_error); 153 EXPECT_EQ(error, out_error);
115 EXPECT_EQ(error_url, out_error_url); 154 EXPECT_EQ(error_url, out_error_url);
116 EXPECT_EQ(captcha_url, out_captcha_url); 155 EXPECT_EQ(captcha_url, out_captcha_url);
117 EXPECT_EQ(captcha_token, out_captcha_token); 156 EXPECT_EQ(captcha_token, out_captcha_token);
118 } 157 }
119 158
120 net::ResponseCookies cookies_; 159 net::ResponseCookies cookies_;
121 GURL client_login_source_; 160 GURL client_login_source_;
122 GURL issue_auth_token_source_; 161 GURL issue_auth_token_source_;
162 GURL client_login_to_oauth2_source_;
163 GURL oauth2_token_source_;
123 GURL token_auth_source_; 164 GURL token_auth_source_;
124 GURL merge_session_source_; 165 GURL merge_session_source_;
125 TestingProfile profile_; 166 TestingProfile profile_;
126 protected: 167 protected:
127 MessageLoop message_loop_; 168 MessageLoop message_loop_;
128 }; 169 };
129 170
130 class MockGaiaConsumer : public GaiaAuthConsumer { 171 class MockGaiaConsumer : public GaiaAuthConsumer {
131 public: 172 public:
132 MockGaiaConsumer() {} 173 MockGaiaConsumer() {}
133 ~MockGaiaConsumer() {} 174 ~MockGaiaConsumer() {}
134 175
135 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); 176 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result));
136 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, 177 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service,
137 const std::string& token)); 178 const std::string& token));
179 MOCK_METHOD3(OnOAuthLoginTokenSuccess,
180 void(const std::string& refresh_token,
181 const std::string& access_token,
182 int expires_in_secs));
138 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data)); 183 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data));
139 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); 184 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data));
140 MOCK_METHOD1(OnClientLoginFailure, 185 MOCK_METHOD1(OnClientLoginFailure,
141 void(const GoogleServiceAuthError& error)); 186 void(const GoogleServiceAuthError& error));
142 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, 187 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service,
143 const GoogleServiceAuthError& error)); 188 const GoogleServiceAuthError& error));
189 MOCK_METHOD1(OnOAuthLoginTokenFailure,
190 void(const GoogleServiceAuthError& error));
144 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error)); 191 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error));
145 MOCK_METHOD1(OnMergeSessionFailure, void( 192 MOCK_METHOD1(OnMergeSessionFailure, void(
146 const GoogleServiceAuthError& error)); 193 const GoogleServiceAuthError& error));
147 }; 194 };
148 195
149 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { 196 TEST_F(GaiaAuthFetcherTest, ErrorComparator) {
150 GoogleServiceAuthError expected_error = 197 GoogleServiceAuthError expected_error =
151 GoogleServiceAuthError::FromConnectionError(-101); 198 GoogleServiceAuthError::FromConnectionError(-101);
152 199
153 GoogleServiceAuthError matching_error = 200 GoogleServiceAuthError matching_error =
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 563
517 EXPECT_TRUE(auth.HasPendingFetch()); 564 EXPECT_TRUE(auth.HasPendingFetch());
518 MockFetcher mock_fetcher( 565 MockFetcher mock_fetcher(
519 issue_auth_token_source_, 566 issue_auth_token_source_,
520 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 567 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
521 RC_FORBIDDEN, cookies_, "", content::URLFetcher::GET, &auth); 568 RC_FORBIDDEN, cookies_, "", content::URLFetcher::GET, &auth);
522 auth.OnURLFetchComplete(&mock_fetcher); 569 auth.OnURLFetchComplete(&mock_fetcher);
523 EXPECT_FALSE(auth.HasPendingFetch()); 570 EXPECT_FALSE(auth.HasPendingFetch());
524 } 571 }
525 572
573 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) {
574 MockGaiaConsumer consumer;
575 EXPECT_CALL(consumer, OnOAuthLoginTokenSuccess("rt1", "at1", 3600))
576 .Times(1);
577
578 TestingProfile profile;
579
580 TestURLFetcherFactory factory;
581 GaiaAuthFetcher auth(&consumer, std::string(),
582 profile_.GetRequestContext());
583 auth.StartOAuthLoginTokenFetch("lso_token");
584
585 net::ResponseCookies cookies;
586 cookies.push_back(kGetAuthCodeValidCookie);
587 EXPECT_TRUE(auth.HasPendingFetch());
588 MockFetcher mock_fetcher1(
589 client_login_to_oauth2_source_,
590 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
591 RC_REQUEST_OK, cookies, "",
592 content::URLFetcher::POST, &auth);
593 auth.OnURLFetchComplete(&mock_fetcher1);
594 EXPECT_TRUE(auth.HasPendingFetch());
595 MockFetcher mock_fetcher2(
596 oauth2_token_source_,
597 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
598 RC_REQUEST_OK, cookies_, kGetTokenPairValidResponse,
599 content::URLFetcher::POST, &auth);
600 auth.OnURLFetchComplete(&mock_fetcher2);
601 EXPECT_FALSE(auth.HasPendingFetch());
602 }
603
604 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenClientLoginToOAuth2Failure) {
605 MockGaiaConsumer consumer;
606 EXPECT_CALL(consumer, OnOAuthLoginTokenFailure(_))
607 .Times(1);
608
609 TestingProfile profile;
610
611 TestURLFetcherFactory factory;
612 GaiaAuthFetcher auth(&consumer, std::string(),
613 profile_.GetRequestContext());
614 auth.StartOAuthLoginTokenFetch("lso_token");
615
616 net::ResponseCookies cookies;
617 cookies.push_back(kGetAuthCodeCookieNoSecure);
618 EXPECT_TRUE(auth.HasPendingFetch());
619 MockFetcher mock_fetcher(
620 client_login_to_oauth2_source_,
621 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
622 RC_REQUEST_OK, cookies, "",
623 content::URLFetcher::POST, &auth);
624 auth.OnURLFetchComplete(&mock_fetcher);
625 EXPECT_FALSE(auth.HasPendingFetch());
626 }
627
628 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenOAuth2TokenPairFailure) {
629 MockGaiaConsumer consumer;
630 EXPECT_CALL(consumer, OnOAuthLoginTokenFailure(_))
631 .Times(1);
632
633 TestingProfile profile;
634
635 TestURLFetcherFactory factory;
636 GaiaAuthFetcher auth(&consumer, std::string(),
637 profile_.GetRequestContext());
638 auth.StartOAuthLoginTokenFetch("lso_token");
639
640 net::ResponseCookies cookies;
641 cookies.push_back(kGetAuthCodeValidCookie);
642 EXPECT_TRUE(auth.HasPendingFetch());
643 MockFetcher mock_fetcher1(
644 client_login_to_oauth2_source_,
645 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
646 RC_REQUEST_OK, cookies, "",
647 content::URLFetcher::POST, &auth);
648 auth.OnURLFetchComplete(&mock_fetcher1);
649 EXPECT_TRUE(auth.HasPendingFetch());
650 MockFetcher mock_fetcher2(
651 oauth2_token_source_,
652 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
653 RC_REQUEST_OK, cookies_, kGetTokenPairResponseNoRefreshToken,
654 content::URLFetcher::POST, &auth);
655 auth.OnURLFetchComplete(&mock_fetcher2);
656 EXPECT_FALSE(auth.HasPendingFetch());
657 }
658
526 TEST_F(GaiaAuthFetcherTest, TokenAuthSuccess) { 659 TEST_F(GaiaAuthFetcherTest, TokenAuthSuccess) {
527 MockGaiaConsumer consumer; 660 MockGaiaConsumer consumer;
528 EXPECT_CALL(consumer, OnTokenAuthSuccess("<html></html>")) 661 EXPECT_CALL(consumer, OnTokenAuthSuccess("<html></html>"))
529 .Times(1); 662 .Times(1);
530 663
531 TestingProfile profile; 664 TestingProfile profile;
532 TestURLFetcherFactory factory; 665 TestURLFetcherFactory factory;
533 666
534 GaiaAuthFetcher auth(&consumer, std::string(), 667 GaiaAuthFetcher auth(&consumer, std::string(),
535 profile_.GetRequestContext()); 668 profile_.GetRequestContext());
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 test_fetcher->set_url(final_url); 765 test_fetcher->set_url(final_url);
633 test_fetcher->set_status( 766 test_fetcher->set_status(
634 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); 767 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
635 test_fetcher->set_response_code(RC_REQUEST_OK); 768 test_fetcher->set_response_code(RC_REQUEST_OK);
636 test_fetcher->set_cookies(cookies_); 769 test_fetcher->set_cookies(cookies_);
637 test_fetcher->SetResponseString("<html></html>"); 770 test_fetcher->SetResponseString("<html></html>");
638 771
639 auth.OnURLFetchComplete(test_fetcher); 772 auth.OnURLFetchComplete(test_fetcher);
640 EXPECT_FALSE(auth.HasPendingFetch()); 773 EXPECT_FALSE(auth.HasPendingFetch());
641 } 774 }
775
776 TEST_F(GaiaAuthFetcherTest, ParseClientLoginToOAuth2Response) {
777 { // No cookies.
778 std::string auth_code;
779 net::ResponseCookies cookies;
780 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
781 cookies, &auth_code));
782 EXPECT_EQ("", auth_code);
783 }
784 { // Few cookies, nothing appropriate.
785 std::string auth_code;
786 net::ResponseCookies cookies;
787 cookies.push_back(kGetAuthCodeCookieNoSecure);
788 cookies.push_back(kGetAuthCodeCookieNoHttpOnly);
789 cookies.push_back(kGetAuthCodeCookieNoOAuthCode);
790 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
791 cookies, &auth_code));
792 EXPECT_EQ("", auth_code);
793 }
794 { // Few cookies, one of them is valid.
795 std::string auth_code;
796 net::ResponseCookies cookies;
797 cookies.push_back(kGetAuthCodeCookieNoSecure);
798 cookies.push_back(kGetAuthCodeCookieNoHttpOnly);
799 cookies.push_back(kGetAuthCodeCookieNoOAuthCode);
800 cookies.push_back(kGetAuthCodeValidCookie);
801 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
802 cookies, &auth_code));
803 EXPECT_EQ("test-code", auth_code);
804 }
805 { // Single valid cookie (like in real responses).
806 std::string auth_code;
807 net::ResponseCookies cookies;
808 cookies.push_back(kGetAuthCodeValidCookie);
809 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
810 cookies, &auth_code));
811 EXPECT_EQ("test-code", auth_code);
812 }
813 }
814
815 TEST_F(GaiaAuthFetcherTest, ParseOAuth2TokenPairResponse) {
816 { // No data.
817 std::string rt;
818 std::string at;
819 int exp = -1;
820 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
821 "", &rt, &at, &exp));
822 EXPECT_EQ("", rt);
823 EXPECT_EQ("", at);
824 EXPECT_EQ(-1, exp);
825 }
826 { // No refresh token.
827 std::string rt;
828 std::string at;
829 int exp = -1;
830 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
831 kGetTokenPairResponseNoRefreshToken, &rt, &at, &exp));
832 EXPECT_EQ("", rt);
833 EXPECT_EQ("", at);
834 EXPECT_EQ(-1, exp);
835 }
836 { // No access token.
837 std::string rt;
838 std::string at;
839 int exp = -1;
840 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
841 kGetTokenPairResponseNoAccessToken, &rt, &at, &exp));
842 EXPECT_EQ("", rt);
843 EXPECT_EQ("", at);
844 EXPECT_EQ(-1, exp);
845 }
846 { // No expiration.
847 std::string rt;
848 std::string at;
849 int exp = -1;
850 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
851 kGetTokenPairResponseNoExpiresIn, &rt, &at, &exp));
852 EXPECT_EQ("", rt);
853 EXPECT_EQ("", at);
854 EXPECT_EQ(-1, exp);
855 }
856 { // Valid response.
857 std::string rt;
858 std::string at;
859 int exp = -1;
860 EXPECT_TRUE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
861 kGetTokenPairValidResponse, &rt, &at, &exp));
862 EXPECT_EQ("rt1", rt);
863 EXPECT_EQ("at1", at);
864 EXPECT_EQ(3600, exp);
865 }
866 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698