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