| 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 OAuth2MintTokenFlow. | 5 // A complete set of unit tests for OAuth2MintTokenFlow. |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 scopes)); | 151 scopes)); |
| 152 access_token_fetcher_.reset(new MockAccessTokenFetcher( | 152 access_token_fetcher_.reset(new MockAccessTokenFetcher( |
| 153 flow_.get(), profile_.GetRequestContext())); | 153 flow_.get(), profile_.GetRequestContext())); |
| 154 } | 154 } |
| 155 | 155 |
| 156 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { | 156 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { |
| 157 std::string body(CreateBody()); | 157 std::string body(CreateBody()); |
| 158 GURL url(CreateApiUrl()); | 158 GURL url(CreateApiUrl()); |
| 159 EXPECT_CALL(*flow_, CreateApiCallBody()).WillOnce(Return(body)); | 159 EXPECT_CALL(*flow_, CreateApiCallBody()).WillOnce(Return(body)); |
| 160 EXPECT_CALL(*flow_, CreateApiCallUrl()).WillOnce(Return(url)); | 160 EXPECT_CALL(*flow_, CreateApiCallUrl()).WillOnce(Return(url)); |
| 161 TestURLFetcher* url_fetcher = CreateURLFetcher( | 161 TestURLFetcher* url_fetcher = |
| 162 url, succeeds, status, ""); | 162 CreateURLFetcher(url, succeeds, status, std::string()); |
| 163 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) | 163 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) |
| 164 .WillOnce(Return(url_fetcher)); | 164 .WillOnce(Return(url_fetcher)); |
| 165 return url_fetcher; | 165 return url_fetcher; |
| 166 } | 166 } |
| 167 | 167 |
| 168 MockUrlFetcherFactory factory_; | 168 MockUrlFetcherFactory factory_; |
| 169 scoped_ptr<MockApiCallFlow> flow_; | 169 scoped_ptr<MockApiCallFlow> flow_; |
| 170 scoped_ptr<MockAccessTokenFetcher> access_token_fetcher_; | 170 scoped_ptr<MockAccessTokenFetcher> access_token_fetcher_; |
| 171 TestingProfile profile_; | 171 TestingProfile profile_; |
| 172 }; | 172 }; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 233 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 234 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); | 234 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); |
| 235 flow_->OnGetTokenFailure(error); | 235 flow_->OnGetTokenFailure(error); |
| 236 } | 236 } |
| 237 | 237 |
| 238 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenFirstApiCallSucceeds) { | 238 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenFirstApiCallSucceeds) { |
| 239 std::string rt = "refresh_token"; | 239 std::string rt = "refresh_token"; |
| 240 std::string at = "access_token"; | 240 std::string at = "access_token"; |
| 241 std::vector<std::string> scopes(CreateTestScopes()); | 241 std::vector<std::string> scopes(CreateTestScopes()); |
| 242 | 242 |
| 243 CreateFlow(rt, "", scopes); | 243 CreateFlow(rt, std::string(), scopes); |
| 244 SetupAccessTokenFetcher(rt, scopes); | 244 SetupAccessTokenFetcher(rt, scopes); |
| 245 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); | 245 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); |
| 246 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher)); | 246 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher)); |
| 247 flow_->Start(); | 247 flow_->Start(); |
| 248 flow_->OnGetTokenSuccess( | 248 flow_->OnGetTokenSuccess( |
| 249 at, | 249 at, |
| 250 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); | 250 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); |
| 251 flow_->OnURLFetchComplete(url_fetcher); | 251 flow_->OnURLFetchComplete(url_fetcher); |
| 252 } | 252 } |
| 253 | 253 |
| 254 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenApiCallFails) { | 254 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenApiCallFails) { |
| 255 std::string rt = "refresh_token"; | 255 std::string rt = "refresh_token"; |
| 256 std::string at = "access_token"; | 256 std::string at = "access_token"; |
| 257 std::vector<std::string> scopes(CreateTestScopes()); | 257 std::vector<std::string> scopes(CreateTestScopes()); |
| 258 | 258 |
| 259 CreateFlow(rt, "", scopes); | 259 CreateFlow(rt, std::string(), scopes); |
| 260 SetupAccessTokenFetcher(rt, scopes); | 260 SetupAccessTokenFetcher(rt, scopes); |
| 261 TestURLFetcher* url_fetcher = SetupApiCall(false, net::HTTP_BAD_GATEWAY); | 261 TestURLFetcher* url_fetcher = SetupApiCall(false, net::HTTP_BAD_GATEWAY); |
| 262 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher)); | 262 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher)); |
| 263 flow_->Start(); | 263 flow_->Start(); |
| 264 flow_->OnGetTokenSuccess( | 264 flow_->OnGetTokenSuccess( |
| 265 at, | 265 at, |
| 266 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); | 266 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); |
| 267 flow_->OnURLFetchComplete(url_fetcher); | 267 flow_->OnURLFetchComplete(url_fetcher); |
| 268 } | 268 } |
| 269 | 269 |
| 270 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenNewTokenGenerationFails) { | 270 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenNewTokenGenerationFails) { |
| 271 std::string rt = "refresh_token"; | 271 std::string rt = "refresh_token"; |
| 272 std::string at = "access_token"; | 272 std::string at = "access_token"; |
| 273 std::vector<std::string> scopes(CreateTestScopes()); | 273 std::vector<std::string> scopes(CreateTestScopes()); |
| 274 | 274 |
| 275 CreateFlow(rt, "", scopes); | 275 CreateFlow(rt, std::string(), scopes); |
| 276 SetupAccessTokenFetcher(rt, scopes); | 276 SetupAccessTokenFetcher(rt, scopes); |
| 277 GoogleServiceAuthError error( | 277 GoogleServiceAuthError error( |
| 278 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 278 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 279 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); | 279 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); |
| 280 flow_->Start(); | 280 flow_->Start(); |
| 281 flow_->OnGetTokenFailure(error); | 281 flow_->OnGetTokenFailure(error); |
| 282 } | 282 } |
| 283 | 283 |
| 284 TEST_F(OAuth2ApiCallFlowTest, CreateURLFetcher) { | 284 TEST_F(OAuth2ApiCallFlowTest, CreateURLFetcher) { |
| 285 std::string rt = "refresh_token"; | 285 std::string rt = "refresh_token"; |
| 286 std::string at = "access_token"; | 286 std::string at = "access_token"; |
| 287 std::vector<std::string> scopes(CreateTestScopes()); | 287 std::vector<std::string> scopes(CreateTestScopes()); |
| 288 std::string body = CreateBody(); | 288 std::string body = CreateBody(); |
| 289 GURL url(CreateApiUrl()); | 289 GURL url(CreateApiUrl()); |
| 290 | 290 |
| 291 CreateFlow(rt, at, scopes); | 291 CreateFlow(rt, at, scopes); |
| 292 scoped_ptr<TestURLFetcher> url_fetcher(SetupApiCall(true, net::HTTP_OK)); | 292 scoped_ptr<TestURLFetcher> url_fetcher(SetupApiCall(true, net::HTTP_OK)); |
| 293 flow_->CreateURLFetcher(); | 293 flow_->CreateURLFetcher(); |
| 294 HttpRequestHeaders headers; | 294 HttpRequestHeaders headers; |
| 295 url_fetcher->GetExtraRequestHeaders(&headers); | 295 url_fetcher->GetExtraRequestHeaders(&headers); |
| 296 std::string auth_header; | 296 std::string auth_header; |
| 297 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header)); | 297 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header)); |
| 298 EXPECT_EQ("Bearer access_token", auth_header); | 298 EXPECT_EQ("Bearer access_token", auth_header); |
| 299 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); | 299 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); |
| 300 EXPECT_EQ(body, url_fetcher->upload_data()); | 300 EXPECT_EQ(body, url_fetcher->upload_data()); |
| 301 } | 301 } |
| OLD | NEW |