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