| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // A complete set of unit tests for OAuth2AccessTokenFetcher. | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "chrome/common/net/gaia/gaia_urls.h" | |
| 12 #include "chrome/common/net/gaia/google_service_auth_error.h" | |
| 13 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" | |
| 14 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" | |
| 15 #include "chrome/test/base/testing_profile.h" | |
| 16 #include "content/public/test/test_browser_thread.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 #include "net/http/http_status_code.h" | |
| 19 #include "net/url_request/test_url_fetcher_factory.h" | |
| 20 #include "net/url_request/url_fetcher.h" | |
| 21 #include "net/url_request/url_fetcher_delegate.h" | |
| 22 #include "net/url_request/url_fetcher_factory.h" | |
| 23 #include "net/url_request/url_request.h" | |
| 24 #include "net/url_request/url_request_status.h" | |
| 25 #include "testing/gmock/include/gmock/gmock.h" | |
| 26 #include "testing/gtest/include/gtest/gtest.h" | |
| 27 | |
| 28 using content::BrowserThread; | |
| 29 using net::ResponseCookies; | |
| 30 using net::ScopedURLFetcherFactory; | |
| 31 using net::TestURLFetcher; | |
| 32 using net::URLFetcher; | |
| 33 using net::URLFetcherDelegate; | |
| 34 using net::URLFetcherFactory; | |
| 35 using net::URLRequestStatus; | |
| 36 using testing::_; | |
| 37 using testing::Return; | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 typedef std::vector<std::string> ScopeList; | |
| 42 | |
| 43 static const char kValidTokenResponse[] = | |
| 44 "{" | |
| 45 " \"access_token\": \"at1\"," | |
| 46 " \"expires_in\": 3600," | |
| 47 " \"token_type\": \"Bearer\"" | |
| 48 "}"; | |
| 49 static const char kTokenResponseNoAccessToken[] = | |
| 50 "{" | |
| 51 " \"expires_in\": 3600," | |
| 52 " \"token_type\": \"Bearer\"" | |
| 53 "}"; | |
| 54 | |
| 55 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, | |
| 56 public URLFetcherFactory { | |
| 57 public: | |
| 58 MockUrlFetcherFactory() | |
| 59 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 60 } | |
| 61 virtual ~MockUrlFetcherFactory() {} | |
| 62 | |
| 63 MOCK_METHOD4( | |
| 64 CreateURLFetcher, | |
| 65 URLFetcher* (int id, | |
| 66 const GURL& url, | |
| 67 URLFetcher::RequestType request_type, | |
| 68 URLFetcherDelegate* d)); | |
| 69 }; | |
| 70 | |
| 71 class MockOAuth2AccessTokenConsumer : public OAuth2AccessTokenConsumer { | |
| 72 public: | |
| 73 MockOAuth2AccessTokenConsumer() {} | |
| 74 ~MockOAuth2AccessTokenConsumer() {} | |
| 75 | |
| 76 MOCK_METHOD2(OnGetTokenSuccess, void(const std::string& access_token, | |
| 77 const base::Time& expiration_time)); | |
| 78 MOCK_METHOD1(OnGetTokenFailure, | |
| 79 void(const GoogleServiceAuthError& error)); | |
| 80 }; | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 class OAuth2AccessTokenFetcherTest : public testing::Test { | |
| 85 public: | |
| 86 OAuth2AccessTokenFetcherTest() | |
| 87 : ui_thread_(BrowserThread::UI, &message_loop_), | |
| 88 fetcher_(&consumer_, profile_.GetRequestContext()) { | |
| 89 } | |
| 90 | |
| 91 virtual ~OAuth2AccessTokenFetcherTest() { } | |
| 92 | |
| 93 virtual TestURLFetcher* SetupGetAccessToken( | |
| 94 bool fetch_succeeds, int response_code, const std::string& body) { | |
| 95 GURL url(GaiaUrls::GetInstance()->oauth2_token_url()); | |
| 96 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &fetcher_); | |
| 97 URLRequestStatus::Status status = | |
| 98 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED; | |
| 99 url_fetcher->set_status(URLRequestStatus(status, 0)); | |
| 100 | |
| 101 if (response_code != 0) | |
| 102 url_fetcher->set_response_code(response_code); | |
| 103 | |
| 104 if (!body.empty()) | |
| 105 url_fetcher->SetResponseString(body); | |
| 106 | |
| 107 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) | |
| 108 .WillOnce(Return(url_fetcher)); | |
| 109 return url_fetcher; | |
| 110 } | |
| 111 | |
| 112 protected: | |
| 113 MessageLoop message_loop_; | |
| 114 content::TestBrowserThread ui_thread_; | |
| 115 MockUrlFetcherFactory factory_; | |
| 116 MockOAuth2AccessTokenConsumer consumer_; | |
| 117 TestingProfile profile_; | |
| 118 OAuth2AccessTokenFetcher fetcher_; | |
| 119 }; | |
| 120 | |
| 121 // These four tests time out, see http://crbug.com/113446. | |
| 122 TEST_F(OAuth2AccessTokenFetcherTest, DISABLED_GetAccessTokenRequestFailure) { | |
| 123 TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, ""); | |
| 124 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); | |
| 125 fetcher_.Start("client_id", "client_secret", "refresh_token", ScopeList()); | |
| 126 fetcher_.OnURLFetchComplete(url_fetcher); | |
| 127 } | |
| 128 | |
| 129 TEST_F(OAuth2AccessTokenFetcherTest, | |
| 130 DISABLED_GetAccessTokenResponseCodeFailure) { | |
| 131 TestURLFetcher* url_fetcher = | |
| 132 SetupGetAccessToken(true, net::HTTP_FORBIDDEN, ""); | |
| 133 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); | |
| 134 fetcher_.Start("client_id", "client_secret", "refresh_token", ScopeList()); | |
| 135 fetcher_.OnURLFetchComplete(url_fetcher); | |
| 136 } | |
| 137 | |
| 138 TEST_F(OAuth2AccessTokenFetcherTest, DISABLED_Success) { | |
| 139 TestURLFetcher* url_fetcher = SetupGetAccessToken( | |
| 140 true, net::HTTP_OK, kValidTokenResponse); | |
| 141 EXPECT_CALL(consumer_, OnGetTokenSuccess("at1", _)).Times(1); | |
| 142 fetcher_.Start("client_id", "client_secret", "refresh_token", ScopeList()); | |
| 143 fetcher_.OnURLFetchComplete(url_fetcher); | |
| 144 } | |
| 145 | |
| 146 TEST_F(OAuth2AccessTokenFetcherTest, DISABLED_MakeGetAccessTokenBody) { | |
| 147 { // No scope. | |
| 148 std::string body = | |
| 149 "client_id=cid1&" | |
| 150 "client_secret=cs1&" | |
| 151 "grant_type=refresh_token&" | |
| 152 "refresh_token=rt1"; | |
| 153 EXPECT_EQ(body, OAuth2AccessTokenFetcher::MakeGetAccessTokenBody( | |
| 154 "cid1", "cs1", "rt1", ScopeList())); | |
| 155 } | |
| 156 | |
| 157 { // One scope. | |
| 158 std::string body = | |
| 159 "client_id=cid1&" | |
| 160 "client_secret=cs1&" | |
| 161 "grant_type=refresh_token&" | |
| 162 "refresh_token=rt1&" | |
| 163 "scope=https://www.googleapis.com/foo"; | |
| 164 ScopeList scopes; | |
| 165 scopes.push_back("https://www.googleapis.com/foo"); | |
| 166 EXPECT_EQ(body, OAuth2AccessTokenFetcher::MakeGetAccessTokenBody( | |
| 167 "cid1", "cs1", "rt1", scopes)); | |
| 168 } | |
| 169 | |
| 170 { // Multiple scopes. | |
| 171 std::string body = | |
| 172 "client_id=cid1&" | |
| 173 "client_secret=cs1&" | |
| 174 "grant_type=refresh_token&" | |
| 175 "refresh_token=rt1&" | |
| 176 "scope=https://www.googleapis.com/foo+" | |
| 177 "https://www.googleapis.com/bar+" | |
| 178 "https://www.googleapis.com/baz"; | |
| 179 ScopeList scopes; | |
| 180 scopes.push_back("https://www.googleapis.com/foo"); | |
| 181 scopes.push_back("https://www.googleapis.com/bar"); | |
| 182 scopes.push_back("https://www.googleapis.com/baz"); | |
| 183 EXPECT_EQ(body, OAuth2AccessTokenFetcher::MakeGetAccessTokenBody( | |
| 184 "cid1", "cs1", "rt1", scopes)); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 // http://crbug.com/114215 | |
| 189 #if defined(OS_WIN) | |
| 190 #define MAYBE_ParseGetAccessTokenResponse DISABLED_ParseGetAccessTokenResponse | |
| 191 #else | |
| 192 #define MAYBE_ParseGetAccessTokenResponse ParseGetAccessTokenResponse | |
| 193 #endif // defined(OS_WIN) | |
| 194 TEST_F(OAuth2AccessTokenFetcherTest, MAYBE_ParseGetAccessTokenResponse) { | |
| 195 { // No body. | |
| 196 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | |
| 197 | |
| 198 std::string at; | |
| 199 int expires_in; | |
| 200 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | |
| 201 &url_fetcher, &at, &expires_in)); | |
| 202 EXPECT_TRUE(at.empty()); | |
| 203 } | |
| 204 { // Bad json. | |
| 205 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | |
| 206 url_fetcher.SetResponseString("foo"); | |
| 207 | |
| 208 std::string at; | |
| 209 int expires_in; | |
| 210 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | |
| 211 &url_fetcher, &at, &expires_in)); | |
| 212 EXPECT_TRUE(at.empty()); | |
| 213 } | |
| 214 { // Valid json: access token missing. | |
| 215 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | |
| 216 url_fetcher.SetResponseString(kTokenResponseNoAccessToken); | |
| 217 | |
| 218 std::string at; | |
| 219 int expires_in; | |
| 220 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | |
| 221 &url_fetcher, &at, &expires_in)); | |
| 222 EXPECT_TRUE(at.empty()); | |
| 223 } | |
| 224 { // Valid json: all good. | |
| 225 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | |
| 226 url_fetcher.SetResponseString(kValidTokenResponse); | |
| 227 | |
| 228 std::string at; | |
| 229 int expires_in; | |
| 230 EXPECT_TRUE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | |
| 231 &url_fetcher, &at, &expires_in)); | |
| 232 EXPECT_EQ("at1", at); | |
| 233 EXPECT_EQ(3600, expires_in); | |
| 234 } | |
| 235 } | |
| OLD | NEW |