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 OAuth2MintTokenFlow. 6 7 #include <string> 8 #include <vector> 9 10 #include "base/memory/scoped_ptr.h" 11 #include "base/message_loop/message_loop.h" 12 #include "base/time/time.h" 13 #include "google_apis/gaia/gaia_urls.h" 14 #include "google_apis/gaia/google_service_auth_error.h" 15 #include "google_apis/gaia/oauth2_access_token_consumer.h" 16 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" 17 #include "google_apis/gaia/oauth2_api_call_flow.h" 18 #include "net/http/http_request_headers.h" 19 #include "net/http/http_status_code.h" 20 #include "net/url_request/test_url_fetcher_factory.h" 21 #include "net/url_request/url_fetcher.h" 22 #include "net/url_request/url_fetcher_delegate.h" 23 #include "net/url_request/url_fetcher_factory.h" 24 #include "net/url_request/url_request.h" 25 #include "net/url_request/url_request_status.h" 26 #include "net/url_request/url_request_test_util.h" 27 #include "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gtest/include/gtest/gtest.h" 29 30 using net::HttpRequestHeaders; 31 using net::ScopedURLFetcherFactory; 32 using net::TestURLFetcher; 33 using net::URLFetcher; 34 using net::URLFetcherDelegate; 35 using net::URLFetcherFactory; 36 using net::URLRequestStatus; 37 using testing::_; 38 using testing::Return; 39 40 namespace { 41 42 static std::string CreateBody() { 43 return "some body"; 44 } 45 46 static GURL CreateApiUrl() { 47 return GURL("https://www.googleapis.com/someapi"); 48 } 49 50 static std::vector<std::string> CreateTestScopes() { 51 std::vector<std::string> scopes; 52 scopes.push_back("scope1"); 53 scopes.push_back("scope2"); 54 return scopes; 55 } 56 57 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, 58 public URLFetcherFactory { 59 public: 60 MockUrlFetcherFactory() 61 : ScopedURLFetcherFactory(this) { 62 } 63 virtual ~MockUrlFetcherFactory() {} 64 65 MOCK_METHOD4( 66 CreateURLFetcher, 67 URLFetcher* (int id, 68 const GURL& url, 69 URLFetcher::RequestType request_type, 70 URLFetcherDelegate* d)); 71 }; 72 73 class MockAccessTokenFetcher : public OAuth2AccessTokenFetcherImpl { 74 public: 75 MockAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, 76 net::URLRequestContextGetter* getter, 77 const std::string& refresh_token) 78 : OAuth2AccessTokenFetcherImpl(consumer, getter, refresh_token) {} 79 ~MockAccessTokenFetcher() {} 80 81 MOCK_METHOD3(Start, 82 void(const std::string& client_id, 83 const std::string& client_secret, 84 const std::vector<std::string>& scopes)); 85 }; 86 87 class MockApiCallFlow : public OAuth2ApiCallFlow { 88 public: 89 MockApiCallFlow(net::URLRequestContextGetter* context, 90 const std::string& refresh_token, 91 const std::string& access_token, 92 const std::vector<std::string>& scopes) 93 : OAuth2ApiCallFlow(context, refresh_token, access_token, scopes) {} 94 ~MockApiCallFlow() {} 95 96 MOCK_METHOD0(CreateApiCallUrl, GURL ()); 97 MOCK_METHOD0(CreateApiCallBody, std::string ()); 98 MOCK_METHOD1(ProcessApiCallSuccess, 99 void (const URLFetcher* source)); 100 MOCK_METHOD1(ProcessApiCallFailure, 101 void (const URLFetcher* source)); 102 MOCK_METHOD1(ProcessNewAccessToken, 103 void (const std::string& access_token)); 104 MOCK_METHOD1(ProcessMintAccessTokenFailure, 105 void (const GoogleServiceAuthError& error)); 106 MOCK_METHOD0(CreateAccessTokenFetcher, OAuth2AccessTokenFetcher* ()); 107 }; 108 109 } // namespace 110 111 class OAuth2ApiCallFlowTest : public testing::Test { 112 protected: 113 void SetupAccessTokenFetcher(const std::vector<std::string>& scopes) { 114 EXPECT_CALL(*access_token_fetcher_, 115 Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), 116 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), 117 scopes)).Times(1); 118 EXPECT_CALL(*flow_, CreateAccessTokenFetcher()) 119 .WillOnce(Return(access_token_fetcher_.release())); 120 } 121 122 TestURLFetcher* CreateURLFetcher( 123 const GURL& url, bool fetch_succeeds, 124 int response_code, const std::string& body) { 125 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, flow_.get()); 126 URLRequestStatus::Status status = 127 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED; 128 url_fetcher->set_status(URLRequestStatus(status, 0)); 129 130 if (response_code != 0) 131 url_fetcher->set_response_code(response_code); 132 133 if (!body.empty()) 134 url_fetcher->SetResponseString(body); 135 136 return url_fetcher; 137 } 138 139 void CreateFlow(const std::string& refresh_token, 140 const std::string& access_token, 141 const std::vector<std::string>& scopes) { 142 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = 143 new net::TestURLRequestContextGetter( 144 message_loop_.message_loop_proxy()); 145 flow_.reset(new MockApiCallFlow( 146 request_context_getter.get(), refresh_token, access_token, scopes)); 147 access_token_fetcher_.reset(new MockAccessTokenFetcher( 148 flow_.get(), request_context_getter.get(), refresh_token)); 149 } 150 151 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { 152 std::string body(CreateBody()); 153 GURL url(CreateApiUrl()); 154 EXPECT_CALL(*flow_, CreateApiCallBody()).WillOnce(Return(body)); 155 EXPECT_CALL(*flow_, CreateApiCallUrl()).WillOnce(Return(url)); 156 TestURLFetcher* url_fetcher = 157 CreateURLFetcher(url, succeeds, status, std::string()); 158 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) 159 .WillOnce(Return(url_fetcher)); 160 return url_fetcher; 161 } 162 163 MockUrlFetcherFactory factory_; 164 scoped_ptr<MockApiCallFlow> flow_; 165 scoped_ptr<MockAccessTokenFetcher> access_token_fetcher_; 166 base::MessageLoop message_loop_; 167 }; 168 169 TEST_F(OAuth2ApiCallFlowTest, FirstApiCallSucceeds) { 170 std::string rt = "refresh_token"; 171 std::string at = "access_token"; 172 std::vector<std::string> scopes(CreateTestScopes()); 173 174 CreateFlow(rt, at, scopes); 175 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); 176 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher)); 177 flow_->Start(); 178 flow_->OnURLFetchComplete(url_fetcher); 179 } 180 181 TEST_F(OAuth2ApiCallFlowTest, SecondApiCallSucceeds) { 182 std::string rt = "refresh_token"; 183 std::string at = "access_token"; 184 std::vector<std::string> scopes(CreateTestScopes()); 185 186 CreateFlow(rt, at, scopes); 187 TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED); 188 flow_->Start(); 189 SetupAccessTokenFetcher(scopes); 190 flow_->OnURLFetchComplete(url_fetcher1); 191 TestURLFetcher* url_fetcher2 = SetupApiCall(true, net::HTTP_OK); 192 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher2)); 193 flow_->OnGetTokenSuccess( 194 at, 195 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); 196 flow_->OnURLFetchComplete(url_fetcher2); 197 } 198 199 TEST_F(OAuth2ApiCallFlowTest, SecondApiCallFails) { 200 std::string rt = "refresh_token"; 201 std::string at = "access_token"; 202 std::vector<std::string> scopes(CreateTestScopes()); 203 204 CreateFlow(rt, at, scopes); 205 TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED); 206 flow_->Start(); 207 SetupAccessTokenFetcher(scopes); 208 flow_->OnURLFetchComplete(url_fetcher1); 209 TestURLFetcher* url_fetcher2 = SetupApiCall(false, net::HTTP_UNAUTHORIZED); 210 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher2)); 211 flow_->OnGetTokenSuccess( 212 at, 213 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); 214 flow_->OnURLFetchComplete(url_fetcher2); 215 } 216 217 TEST_F(OAuth2ApiCallFlowTest, NewTokenGenerationFails) { 218 std::string rt = "refresh_token"; 219 std::string at = "access_token"; 220 std::vector<std::string> scopes(CreateTestScopes()); 221 222 CreateFlow(rt, at, scopes); 223 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_UNAUTHORIZED); 224 flow_->Start(); 225 SetupAccessTokenFetcher(scopes); 226 flow_->OnURLFetchComplete(url_fetcher); 227 GoogleServiceAuthError error( 228 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 229 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); 230 flow_->OnGetTokenFailure(error); 231 } 232 233 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenFirstApiCallSucceeds) { 234 std::string rt = "refresh_token"; 235 std::string at = "access_token"; 236 std::vector<std::string> scopes(CreateTestScopes()); 237 238 CreateFlow(rt, std::string(), scopes); 239 SetupAccessTokenFetcher(scopes); 240 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); 241 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher)); 242 flow_->Start(); 243 flow_->OnGetTokenSuccess( 244 at, 245 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); 246 flow_->OnURLFetchComplete(url_fetcher); 247 } 248 249 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenApiCallFails) { 250 std::string rt = "refresh_token"; 251 std::string at = "access_token"; 252 std::vector<std::string> scopes(CreateTestScopes()); 253 254 CreateFlow(rt, std::string(), scopes); 255 SetupAccessTokenFetcher(scopes); 256 TestURLFetcher* url_fetcher = SetupApiCall(false, net::HTTP_BAD_GATEWAY); 257 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher)); 258 flow_->Start(); 259 flow_->OnGetTokenSuccess( 260 at, 261 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); 262 flow_->OnURLFetchComplete(url_fetcher); 263 } 264 265 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenNewTokenGenerationFails) { 266 std::string rt = "refresh_token"; 267 std::string at = "access_token"; 268 std::vector<std::string> scopes(CreateTestScopes()); 269 270 CreateFlow(rt, std::string(), scopes); 271 SetupAccessTokenFetcher(scopes); 272 GoogleServiceAuthError error( 273 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 274 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); 275 flow_->Start(); 276 flow_->OnGetTokenFailure(error); 277 } 278 279 TEST_F(OAuth2ApiCallFlowTest, CreateURLFetcher) { 280 std::string rt = "refresh_token"; 281 std::string at = "access_token"; 282 std::vector<std::string> scopes(CreateTestScopes()); 283 std::string body = CreateBody(); 284 GURL url(CreateApiUrl()); 285 286 CreateFlow(rt, at, scopes); 287 scoped_ptr<TestURLFetcher> url_fetcher(SetupApiCall(true, net::HTTP_OK)); 288 flow_->CreateURLFetcher(); 289 HttpRequestHeaders headers; 290 url_fetcher->GetExtraRequestHeaders(&headers); 291 std::string auth_header; 292 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header)); 293 EXPECT_EQ("Bearer access_token", auth_header); 294 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); 295 EXPECT_EQ(body, url_fetcher->upload_data()); 296 } 297