| OLD | NEW |
| 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 GaiaOAuthFetcher. | 5 // A complete set of unit tests for GaiaOAuthFetcher. |
| 6 // Originally ported from GaiaAuthFetcher tests. | 6 // Originally ported from GaiaAuthFetcher tests. |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "chrome/browser/net/chrome_cookie_notification_details.h" | 12 #include "chrome/browser/net/chrome_cookie_notification_details.h" |
| 13 #include "chrome/browser/net/gaia/gaia_oauth_consumer.h" | 13 #include "chrome/browser/net/gaia/gaia_oauth_consumer.h" |
| 14 #include "chrome/browser/net/gaia/gaia_oauth_fetcher.h" | 14 #include "chrome/browser/net/gaia/gaia_oauth_fetcher.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/net/gaia/gaia_constants.h" |
| 16 #include "chrome/common/net/gaia/google_service_auth_error.h" | 17 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 17 #include "chrome/common/net/http_return.h" | 18 #include "chrome/common/net/http_return.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 19 #include "content/common/notification_service.h" | 20 #include "content/common/notification_service.h" |
| 20 #include "content/common/url_fetcher.h" | 21 #include "content/common/url_fetcher.h" |
| 21 #include "content/test/test_url_fetcher_factory.h" | 22 #include "content/test/test_url_fetcher_factory.h" |
| 22 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 23 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 24 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 55 ~MockGaiaOAuthConsumer() {} | 56 ~MockGaiaOAuthConsumer() {} |
| 56 | 57 |
| 57 MOCK_METHOD1(OnGetOAuthTokenSuccess, void(const std::string& oauth_token)); | 58 MOCK_METHOD1(OnGetOAuthTokenSuccess, void(const std::string& oauth_token)); |
| 58 MOCK_METHOD0(OnGetOAuthTokenFailure, void()); | 59 MOCK_METHOD0(OnGetOAuthTokenFailure, void()); |
| 59 | 60 |
| 60 MOCK_METHOD2(OnOAuthGetAccessTokenSuccess, void(const std::string& token, | 61 MOCK_METHOD2(OnOAuthGetAccessTokenSuccess, void(const std::string& token, |
| 61 const std::string& secret)); | 62 const std::string& secret)); |
| 62 MOCK_METHOD1(OnOAuthGetAccessTokenFailure, | 63 MOCK_METHOD1(OnOAuthGetAccessTokenFailure, |
| 63 void(const GoogleServiceAuthError& error)); | 64 void(const GoogleServiceAuthError& error)); |
| 64 | 65 |
| 65 MOCK_METHOD2(OnOAuthWrapBridgeSuccess, | 66 MOCK_METHOD3(OnOAuthWrapBridgeSuccess, |
| 66 void(const std::string& token, | 67 void(const std::string& service_name, |
| 68 const std::string& token, |
| 67 const std::string& expires_in)); | 69 const std::string& expires_in)); |
| 68 MOCK_METHOD1(OnOAuthWrapBridgeFailure, | 70 MOCK_METHOD1(OnOAuthWrapBridgeFailure, |
| 69 void(const GoogleServiceAuthError& error)); | 71 void(const GoogleServiceAuthError& error)); |
| 70 | 72 |
| 71 MOCK_METHOD1(OnUserInfoSuccess, void(const std::string& email)); | 73 MOCK_METHOD1(OnUserInfoSuccess, void(const std::string& email)); |
| 72 MOCK_METHOD1(OnUserInfoFailure, void(const GoogleServiceAuthError& error)); | 74 MOCK_METHOD1(OnUserInfoFailure, void(const GoogleServiceAuthError& error)); |
| 73 }; | 75 }; |
| 74 | 76 |
| 75 class MockGaiaOAuthFetcher : public GaiaOAuthFetcher { | 77 class MockGaiaOAuthFetcher : public GaiaOAuthFetcher { |
| 76 public: | 78 public: |
| 77 MockGaiaOAuthFetcher(GaiaOAuthConsumer* consumer, | 79 MockGaiaOAuthFetcher(GaiaOAuthConsumer* consumer, |
| 78 net::URLRequestContextGetter* getter, | 80 net::URLRequestContextGetter* getter, |
| 79 Profile* profile, | 81 Profile* profile, |
| 82 const std::string& service_name, |
| 80 const std::string& service_scope) | 83 const std::string& service_scope) |
| 81 : GaiaOAuthFetcher(consumer, getter, profile, service_scope) {} | 84 : GaiaOAuthFetcher( |
| 85 consumer, getter, profile, service_name, service_scope) {} |
| 82 | 86 |
| 83 ~MockGaiaOAuthFetcher() {} | 87 ~MockGaiaOAuthFetcher() {} |
| 84 | 88 |
| 85 MOCK_METHOD1(StartOAuthGetAccessToken, | 89 MOCK_METHOD1(StartOAuthGetAccessToken, |
| 86 void(const std::string& oauth1_request_token)); | 90 void(const std::string& oauth1_request_token)); |
| 87 | 91 |
| 88 MOCK_METHOD4(StartOAuthWrapBridge, | 92 MOCK_METHOD5(StartOAuthWrapBridge, |
| 89 void(const std::string& oauth1_access_token, | 93 void(const std::string& oauth1_access_token, |
| 90 const std::string& oauth1_access_token_secret, | 94 const std::string& oauth1_access_token_secret, |
| 91 const std::string& wrap_token_duration, | 95 const std::string& wrap_token_duration, |
| 96 const std::string& service_name, |
| 92 const std::string& oauth2_scope)); | 97 const std::string& oauth2_scope)); |
| 93 | 98 |
| 94 MOCK_METHOD1(StartUserInfo, void(const std::string& oauth2_access_token)); | 99 MOCK_METHOD1(StartUserInfo, void(const std::string& oauth2_access_token)); |
| 95 }; | 100 }; |
| 96 | 101 |
| 97 #if 0 // Suppressing for now | 102 #if 0 // Suppressing for now |
| 98 TEST(GaiaOAuthFetcherTest, GetOAuthToken) { | 103 TEST(GaiaOAuthFetcherTest, GetOAuthToken) { |
| 99 const std::string oauth_token = "4/OAuth1-Request_Token-1234567"; | 104 const std::string oauth_token = "4/OAuth1-Request_Token-1234567"; |
| 100 base::Time creation = base::Time::Now(); | 105 base::Time creation = base::Time::Now(); |
| 101 base::Time expiration = base::Time::Time(); | 106 base::Time expiration = base::Time::Time(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 122 new ChromeCookieDetails::ChromeCookieDetails( | 127 new ChromeCookieDetails::ChromeCookieDetails( |
| 123 canonical_cookie.get(), | 128 canonical_cookie.get(), |
| 124 false, | 129 false, |
| 125 net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT)); | 130 net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT)); |
| 126 | 131 |
| 127 MockGaiaOAuthConsumer consumer; | 132 MockGaiaOAuthConsumer consumer; |
| 128 EXPECT_CALL(consumer, OnGetOAuthTokenSuccess(oauth_token)).Times(1); | 133 EXPECT_CALL(consumer, OnGetOAuthTokenSuccess(oauth_token)).Times(1); |
| 129 | 134 |
| 130 TestingProfile profile; | 135 TestingProfile profile; |
| 131 | 136 |
| 132 MockGaiaOAuthFetcher auth(&consumer, | 137 MockGaiaOAuthFetcher oauth_fetcher(&consumer, |
| 133 profile.GetRequestContext(), | 138 profile.GetRequestContext(), |
| 134 &profile, | 139 &profile, |
| 135 std::string()); | 140 std::string()); |
| 136 EXPECT_CALL(auth, StartOAuthGetAccessToken(oauth_token)).Times(1); | 141 EXPECT_CALL(oauth_fetcher, StartOAuthGetAccessToken(oauth_token)).Times(1); |
| 137 | 142 |
| 138 auth.Observe( | 143 oauth_fetcher.Observe( |
| 139 chrome::NOTIFICATION_COOKIE_CHANGED, | 144 chrome::NOTIFICATION_COOKIE_CHANGED, |
| 140 Source<Profile>(&profile), | 145 Source<Profile>(&profile), |
| 141 Details<ChromeCookieDetails>(cookie_details.get())); | 146 Details<ChromeCookieDetails>(cookie_details.get())); |
| 142 } | 147 } |
| 143 #endif // 0 // Suppressing for now | 148 #endif // 0 // Suppressing for now |
| 144 | 149 |
| 145 | 150 |
| 146 TEST(GaiaOAuthFetcherTest, OAuthGetAccessToken) { | 151 TEST(GaiaOAuthFetcherTest, OAuthGetAccessToken) { |
| 147 const std::string oauth_token="1/OAuth1-Access_Token-1234567890abcdefghijklm"; | 152 const std::string oauth_token="1/OAuth1-Access_Token-1234567890abcdefghijklm"; |
| 148 const std::string oauth_token_secret="Dont_tell_the_secret-123"; | 153 const std::string oauth_token_secret="Dont_tell_the_secret-123"; |
| 149 const std::string data("oauth_token=" | 154 const std::string data("oauth_token=" |
| 150 "1%2FOAuth1-Access_Token-1234567890abcdefghijklm" | 155 "1%2FOAuth1-Access_Token-1234567890abcdefghijklm" |
| 151 "&oauth_token_secret=Dont_tell_the_secret-123"); | 156 "&oauth_token_secret=Dont_tell_the_secret-123"); |
| 152 | 157 |
| 153 MockGaiaOAuthConsumer consumer; | 158 MockGaiaOAuthConsumer consumer; |
| 154 EXPECT_CALL(consumer, | 159 EXPECT_CALL(consumer, |
| 155 OnOAuthGetAccessTokenSuccess(oauth_token, | 160 OnOAuthGetAccessTokenSuccess(oauth_token, |
| 156 oauth_token_secret)).Times(1); | 161 oauth_token_secret)).Times(1); |
| 157 | 162 |
| 158 TestingProfile profile; | 163 TestingProfile profile; |
| 159 MockGaiaOAuthFetcher auth(&consumer, | 164 MockGaiaOAuthFetcher oauth_fetcher(&consumer, |
| 160 profile .GetRequestContext(), | 165 profile.GetRequestContext(), |
| 161 &profile, | 166 &profile, |
| 162 std::string()); | 167 "service_name-eKARPyky", |
| 163 EXPECT_CALL(auth, StartOAuthWrapBridge(oauth_token, | 168 "service_scope-JnG18MEE"); |
| 164 oauth_token_secret, | 169 EXPECT_CALL(oauth_fetcher, |
| 165 "3600", | 170 StartOAuthWrapBridge(oauth_token, |
| 166 _)).Times(1); | 171 oauth_token_secret, |
| 172 "3600", |
| 173 "service_name-eKARPyky", |
| 174 "service_scope-JnG18MEE")).Times(1); |
| 167 | 175 |
| 168 net::ResponseCookies cookies; | 176 net::ResponseCookies cookies; |
| 169 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); | 177 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 170 auth.OnURLFetchComplete(NULL, | 178 oauth_fetcher.OnURLFetchComplete(NULL, |
| 171 GURL(kOAuthGetAccessTokenUrl), | 179 GURL(kOAuthGetAccessTokenUrl), |
| 172 status, | 180 status, |
| 173 RC_REQUEST_OK, | 181 RC_REQUEST_OK, |
| 174 cookies, | 182 cookies, |
| 175 data); | 183 data); |
| 176 } | 184 } |
| 177 | 185 |
| 178 TEST(GaiaOAuthFetcherTest, OAuthWrapBridge) { | 186 TEST(GaiaOAuthFetcherTest, OAuthWrapBridge) { |
| 179 const std::string wrap_token="1/OAuth2-Access_Token-nopqrstuvwxyz1234567890"; | 187 const std::string wrap_token="1/OAuth2-Access_Token-nopqrstuvwxyz1234567890"; |
| 180 const std::string expires_in="3600"; | 188 const std::string expires_in="3600"; |
| 181 | 189 |
| 182 const std::string data("wrap_access_token=" | 190 const std::string data("wrap_access_token=" |
| 183 "1%2FOAuth2-Access_Token-nopqrstuvwxyz1234567890" | 191 "1%2FOAuth2-Access_Token-nopqrstuvwxyz1234567890" |
| 184 "&wrap_access_token_expires_in=3600"); | 192 "&wrap_access_token_expires_in=3600"); |
| 185 | 193 |
| 186 MockGaiaOAuthConsumer consumer; | 194 MockGaiaOAuthConsumer consumer; |
| 187 EXPECT_CALL(consumer, | 195 EXPECT_CALL(consumer, |
| 188 OnOAuthWrapBridgeSuccess(wrap_token, | 196 OnOAuthWrapBridgeSuccess("service_name-62Ykg3K1", |
| 197 wrap_token, |
| 189 expires_in)).Times(1); | 198 expires_in)).Times(1); |
| 190 | 199 |
| 191 TestingProfile profile; | 200 TestingProfile profile; |
| 192 MockGaiaOAuthFetcher auth(&consumer, | 201 MockGaiaOAuthFetcher oauth_fetcher(&consumer, |
| 193 profile .GetRequestContext(), | 202 profile .GetRequestContext(), |
| 194 &profile, | 203 &profile, |
| 195 std::string()); | 204 "service_name-62Ykg3K1", |
| 196 EXPECT_CALL(auth, StartUserInfo(wrap_token)).Times(1); | 205 "service_scope-0fL85iOi"); |
| 206 EXPECT_CALL(oauth_fetcher, StartUserInfo(wrap_token)).Times(1); |
| 197 | 207 |
| 198 net::ResponseCookies cookies; | 208 net::ResponseCookies cookies; |
| 199 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); | 209 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 200 auth.OnURLFetchComplete(NULL, | 210 oauth_fetcher.OnURLFetchComplete(NULL, |
| 201 GURL(kOAuthWrapBridgeUrl), | 211 GURL(kOAuthWrapBridgeUrl), |
| 202 status, | 212 status, |
| 203 RC_REQUEST_OK, | 213 RC_REQUEST_OK, |
| 204 cookies, | 214 cookies, |
| 205 data); | 215 data); |
| 206 } | 216 } |
| 207 | 217 |
| 208 TEST(GaiaOAuthFetcherTest, UserInfo) { | 218 TEST(GaiaOAuthFetcherTest, UserInfo) { |
| 209 const std::string email_address="someone@somewhere.net"; | 219 const std::string email_address="someone@somewhere.net"; |
| 210 const std::string wrap_token="1/OAuth2-Access_Token-nopqrstuvwxyz1234567890"; | 220 const std::string wrap_token="1/OAuth2-Access_Token-nopqrstuvwxyz1234567890"; |
| 211 const std::string expires_in="3600"; | 221 const std::string expires_in="3600"; |
| 212 | |
| 213 const std::string data("{\n \"email\": \"someone@somewhere.net\",\n" | 222 const std::string data("{\n \"email\": \"someone@somewhere.net\",\n" |
| 214 " \"verified_email\": true\n}\n"); | 223 " \"verified_email\": true\n}\n"); |
| 215 MockGaiaOAuthConsumer consumer; | 224 MockGaiaOAuthConsumer consumer; |
| 216 EXPECT_CALL(consumer, | 225 EXPECT_CALL(consumer, |
| 217 OnUserInfoSuccess(email_address)).Times(1); | 226 OnUserInfoSuccess(email_address)).Times(1); |
| 218 | 227 |
| 219 TestingProfile profile; | 228 TestingProfile profile; |
| 220 MockGaiaOAuthFetcher auth(&consumer, | 229 MockGaiaOAuthFetcher oauth_fetcher(&consumer, |
| 221 profile .GetRequestContext(), | 230 profile .GetRequestContext(), |
| 222 &profile, | 231 &profile, |
| 223 std::string()); | 232 "service_name-S2igVNUm", |
| 233 "service_scope-Nrj4LmgU"); |
| 224 | 234 |
| 225 net::ResponseCookies cookies; | 235 net::ResponseCookies cookies; |
| 226 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); | 236 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 227 auth.OnURLFetchComplete(NULL, | 237 oauth_fetcher.OnURLFetchComplete(NULL, |
| 228 GURL(kUserInfoUrl), | 238 GURL(kUserInfoUrl), |
| 229 status, | 239 status, |
| 230 RC_REQUEST_OK, | 240 RC_REQUEST_OK, |
| 231 cookies, | 241 cookies, |
| 232 data); | 242 data); |
| 233 } | 243 } |
| OLD | NEW |