Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: google_apis/gaia/oauth2_api_call_flow_unittest.cc

Issue 182573003: Extract OAuth2AccessTokenFetcher interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile errors in chrome Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "google_apis/gaia/gaia_urls.h" 13 #include "google_apis/gaia/gaia_urls.h"
14 #include "google_apis/gaia/google_service_auth_error.h" 14 #include "google_apis/gaia/google_service_auth_error.h"
15 #include "google_apis/gaia/oauth2_access_token_consumer.h" 15 #include "google_apis/gaia/oauth2_access_token_consumer.h"
16 #include "google_apis/gaia/oauth2_access_token_fetcher.h" 16 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
17 #include "google_apis/gaia/oauth2_api_call_flow.h" 17 #include "google_apis/gaia/oauth2_api_call_flow.h"
18 #include "net/http/http_request_headers.h" 18 #include "net/http/http_request_headers.h"
19 #include "net/http/http_status_code.h" 19 #include "net/http/http_status_code.h"
20 #include "net/url_request/test_url_fetcher_factory.h" 20 #include "net/url_request/test_url_fetcher_factory.h"
21 #include "net/url_request/url_fetcher.h" 21 #include "net/url_request/url_fetcher.h"
22 #include "net/url_request/url_fetcher_delegate.h" 22 #include "net/url_request/url_fetcher_delegate.h"
23 #include "net/url_request/url_fetcher_factory.h" 23 #include "net/url_request/url_fetcher_factory.h"
24 #include "net/url_request/url_request.h" 24 #include "net/url_request/url_request.h"
25 #include "net/url_request/url_request_status.h" 25 #include "net/url_request/url_request_status.h"
26 #include "net/url_request/url_request_test_util.h" 26 #include "net/url_request/url_request_test_util.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 virtual ~MockUrlFetcherFactory() {} 63 virtual ~MockUrlFetcherFactory() {}
64 64
65 MOCK_METHOD4( 65 MOCK_METHOD4(
66 CreateURLFetcher, 66 CreateURLFetcher,
67 URLFetcher* (int id, 67 URLFetcher* (int id,
68 const GURL& url, 68 const GURL& url,
69 URLFetcher::RequestType request_type, 69 URLFetcher::RequestType request_type,
70 URLFetcherDelegate* d)); 70 URLFetcherDelegate* d));
71 }; 71 };
72 72
73 class MockAccessTokenFetcher : public OAuth2AccessTokenFetcher { 73 class MockAccessTokenFetcher : public OAuth2AccessTokenFetcherImpl {
74 public: 74 public:
75 MockAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, 75 MockAccessTokenFetcher(net::URLRequestContextGetter* getter)
76 net::URLRequestContextGetter* getter) 76 : OAuth2AccessTokenFetcherImpl(getter) {}
77 : OAuth2AccessTokenFetcher(consumer, getter) {}
78 ~MockAccessTokenFetcher() {} 77 ~MockAccessTokenFetcher() {}
79 78
80 MOCK_METHOD4(Start, 79 MOCK_METHOD5(Start,
81 void (const std::string& client_id, 80 void(const std::string& client_id,
82 const std::string& client_secret, 81 const std::string& client_secret,
83 const std::string& refresh_token, 82 const std::string& refresh_token,
84 const std::vector<std::string>& scopes)); 83 const std::vector<std::string>& scopes,
84 OAuth2AccessTokenConsumer* consumer));
85 }; 85 };
86 86
87 class MockApiCallFlow : public OAuth2ApiCallFlow { 87 class MockApiCallFlow : public OAuth2ApiCallFlow {
88 public: 88 public:
89 MockApiCallFlow(net::URLRequestContextGetter* context, 89 MockApiCallFlow(net::URLRequestContextGetter* context,
90 const std::string& refresh_token, 90 const std::string& refresh_token,
91 const std::string& access_token, 91 const std::string& access_token,
92 const std::vector<std::string>& scopes) 92 const std::vector<std::string>& scopes)
93 : OAuth2ApiCallFlow(context, refresh_token, access_token, scopes) {} 93 : OAuth2ApiCallFlow(context, refresh_token, access_token, scopes) {}
94 ~MockApiCallFlow() {} 94 ~MockApiCallFlow() {}
95 95
96 MOCK_METHOD0(CreateApiCallUrl, GURL ()); 96 MOCK_METHOD0(CreateApiCallUrl, GURL ());
97 MOCK_METHOD0(CreateApiCallBody, std::string ()); 97 MOCK_METHOD0(CreateApiCallBody, std::string ());
98 MOCK_METHOD1(ProcessApiCallSuccess, 98 MOCK_METHOD1(ProcessApiCallSuccess,
99 void (const URLFetcher* source)); 99 void (const URLFetcher* source));
100 MOCK_METHOD1(ProcessApiCallFailure, 100 MOCK_METHOD1(ProcessApiCallFailure,
101 void (const URLFetcher* source)); 101 void (const URLFetcher* source));
102 MOCK_METHOD1(ProcessNewAccessToken, 102 MOCK_METHOD1(ProcessNewAccessToken,
103 void (const std::string& access_token)); 103 void (const std::string& access_token));
104 MOCK_METHOD1(ProcessMintAccessTokenFailure, 104 MOCK_METHOD1(ProcessMintAccessTokenFailure,
105 void (const GoogleServiceAuthError& error)); 105 void (const GoogleServiceAuthError& error));
106 MOCK_METHOD0(CreateAccessTokenFetcher, OAuth2AccessTokenFetcher* ()); 106 MOCK_METHOD0(CreateAccessTokenFetcher, OAuth2AccessTokenFetcher* ());
107 }; 107 };
108 108
109 } // namespace 109 } // namespace
110 110
111 class OAuth2ApiCallFlowTest : public testing::Test { 111 class OAuth2ApiCallFlowTest : public testing::Test {
112 protected: 112 protected:
113 void SetupAccessTokenFetcher( 113 void SetupAccessTokenFetcher(const std::string& rt,
114 const std::string& rt, const std::vector<std::string>& scopes) { 114 const std::vector<std::string>& scopes,
115 OAuth2AccessTokenConsumer* consumer) {
115 EXPECT_CALL(*access_token_fetcher_, 116 EXPECT_CALL(*access_token_fetcher_,
116 Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), 117 Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(),
117 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), 118 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(),
118 rt, scopes)) 119 rt,
119 .Times(1); 120 scopes,
121 consumer)).Times(1);
120 EXPECT_CALL(*flow_, CreateAccessTokenFetcher()) 122 EXPECT_CALL(*flow_, CreateAccessTokenFetcher())
121 .WillOnce(Return(access_token_fetcher_.release())); 123 .WillOnce(Return(access_token_fetcher_.release()));
122 } 124 }
123 125
124 TestURLFetcher* CreateURLFetcher( 126 TestURLFetcher* CreateURLFetcher(
125 const GURL& url, bool fetch_succeeds, 127 const GURL& url, bool fetch_succeeds,
126 int response_code, const std::string& body) { 128 int response_code, const std::string& body) {
127 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, flow_.get()); 129 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, flow_.get());
128 URLRequestStatus::Status status = 130 URLRequestStatus::Status status =
129 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED; 131 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED;
(...skipping 10 matching lines...) Expand all
140 142
141 void CreateFlow(const std::string& refresh_token, 143 void CreateFlow(const std::string& refresh_token,
142 const std::string& access_token, 144 const std::string& access_token,
143 const std::vector<std::string>& scopes) { 145 const std::vector<std::string>& scopes) {
144 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = 146 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
145 new net::TestURLRequestContextGetter( 147 new net::TestURLRequestContextGetter(
146 message_loop_.message_loop_proxy()); 148 message_loop_.message_loop_proxy());
147 flow_.reset(new MockApiCallFlow( 149 flow_.reset(new MockApiCallFlow(
148 request_context_getter, refresh_token, access_token, scopes)); 150 request_context_getter, refresh_token, access_token, scopes));
149 access_token_fetcher_.reset( 151 access_token_fetcher_.reset(
150 new MockAccessTokenFetcher(flow_.get(), request_context_getter)); 152 new MockAccessTokenFetcher(request_context_getter));
151 } 153 }
152 154
153 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { 155 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) {
154 std::string body(CreateBody()); 156 std::string body(CreateBody());
155 GURL url(CreateApiUrl()); 157 GURL url(CreateApiUrl());
156 EXPECT_CALL(*flow_, CreateApiCallBody()).WillOnce(Return(body)); 158 EXPECT_CALL(*flow_, CreateApiCallBody()).WillOnce(Return(body));
157 EXPECT_CALL(*flow_, CreateApiCallUrl()).WillOnce(Return(url)); 159 EXPECT_CALL(*flow_, CreateApiCallUrl()).WillOnce(Return(url));
158 TestURLFetcher* url_fetcher = 160 TestURLFetcher* url_fetcher =
159 CreateURLFetcher(url, succeeds, status, std::string()); 161 CreateURLFetcher(url, succeeds, status, std::string());
160 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) 162 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _))
(...skipping 20 matching lines...) Expand all
181 } 183 }
182 184
183 TEST_F(OAuth2ApiCallFlowTest, SecondApiCallSucceeds) { 185 TEST_F(OAuth2ApiCallFlowTest, SecondApiCallSucceeds) {
184 std::string rt = "refresh_token"; 186 std::string rt = "refresh_token";
185 std::string at = "access_token"; 187 std::string at = "access_token";
186 std::vector<std::string> scopes(CreateTestScopes()); 188 std::vector<std::string> scopes(CreateTestScopes());
187 189
188 CreateFlow(rt, at, scopes); 190 CreateFlow(rt, at, scopes);
189 TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED); 191 TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED);
190 flow_->Start(); 192 flow_->Start();
191 SetupAccessTokenFetcher(rt, scopes); 193 SetupAccessTokenFetcher(rt, scopes, flow_.get());
192 flow_->OnURLFetchComplete(url_fetcher1); 194 flow_->OnURLFetchComplete(url_fetcher1);
193 TestURLFetcher* url_fetcher2 = SetupApiCall(true, net::HTTP_OK); 195 TestURLFetcher* url_fetcher2 = SetupApiCall(true, net::HTTP_OK);
194 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher2)); 196 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher2));
195 flow_->OnGetTokenSuccess( 197 flow_->OnGetTokenSuccess(
196 at, 198 at,
197 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); 199 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
198 flow_->OnURLFetchComplete(url_fetcher2); 200 flow_->OnURLFetchComplete(url_fetcher2);
199 } 201 }
200 202
201 TEST_F(OAuth2ApiCallFlowTest, SecondApiCallFails) { 203 TEST_F(OAuth2ApiCallFlowTest, SecondApiCallFails) {
202 std::string rt = "refresh_token"; 204 std::string rt = "refresh_token";
203 std::string at = "access_token"; 205 std::string at = "access_token";
204 std::vector<std::string> scopes(CreateTestScopes()); 206 std::vector<std::string> scopes(CreateTestScopes());
205 207
206 CreateFlow(rt, at, scopes); 208 CreateFlow(rt, at, scopes);
207 TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED); 209 TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED);
208 flow_->Start(); 210 flow_->Start();
209 SetupAccessTokenFetcher(rt, scopes); 211 SetupAccessTokenFetcher(rt, scopes, flow_.get());
210 flow_->OnURLFetchComplete(url_fetcher1); 212 flow_->OnURLFetchComplete(url_fetcher1);
211 TestURLFetcher* url_fetcher2 = SetupApiCall(false, net::HTTP_UNAUTHORIZED); 213 TestURLFetcher* url_fetcher2 = SetupApiCall(false, net::HTTP_UNAUTHORIZED);
212 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher2)); 214 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher2));
213 flow_->OnGetTokenSuccess( 215 flow_->OnGetTokenSuccess(
214 at, 216 at,
215 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); 217 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
216 flow_->OnURLFetchComplete(url_fetcher2); 218 flow_->OnURLFetchComplete(url_fetcher2);
217 } 219 }
218 220
219 TEST_F(OAuth2ApiCallFlowTest, NewTokenGenerationFails) { 221 TEST_F(OAuth2ApiCallFlowTest, NewTokenGenerationFails) {
220 std::string rt = "refresh_token"; 222 std::string rt = "refresh_token";
221 std::string at = "access_token"; 223 std::string at = "access_token";
222 std::vector<std::string> scopes(CreateTestScopes()); 224 std::vector<std::string> scopes(CreateTestScopes());
223 225
224 CreateFlow(rt, at, scopes); 226 CreateFlow(rt, at, scopes);
225 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_UNAUTHORIZED); 227 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_UNAUTHORIZED);
226 flow_->Start(); 228 flow_->Start();
227 SetupAccessTokenFetcher(rt, scopes); 229 SetupAccessTokenFetcher(rt, scopes, flow_.get());
228 flow_->OnURLFetchComplete(url_fetcher); 230 flow_->OnURLFetchComplete(url_fetcher);
229 GoogleServiceAuthError error( 231 GoogleServiceAuthError error(
230 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 232 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
231 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); 233 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error));
232 flow_->OnGetTokenFailure(error); 234 flow_->OnGetTokenFailure(error);
233 } 235 }
234 236
235 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenFirstApiCallSucceeds) { 237 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenFirstApiCallSucceeds) {
236 std::string rt = "refresh_token"; 238 std::string rt = "refresh_token";
237 std::string at = "access_token"; 239 std::string at = "access_token";
238 std::vector<std::string> scopes(CreateTestScopes()); 240 std::vector<std::string> scopes(CreateTestScopes());
239 241
240 CreateFlow(rt, std::string(), scopes); 242 CreateFlow(rt, std::string(), scopes);
241 SetupAccessTokenFetcher(rt, scopes); 243 SetupAccessTokenFetcher(rt, scopes, flow_.get());
242 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); 244 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK);
243 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher)); 245 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher));
244 flow_->Start(); 246 flow_->Start();
245 flow_->OnGetTokenSuccess( 247 flow_->OnGetTokenSuccess(
246 at, 248 at,
247 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); 249 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
248 flow_->OnURLFetchComplete(url_fetcher); 250 flow_->OnURLFetchComplete(url_fetcher);
249 } 251 }
250 252
251 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenApiCallFails) { 253 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenApiCallFails) {
252 std::string rt = "refresh_token"; 254 std::string rt = "refresh_token";
253 std::string at = "access_token"; 255 std::string at = "access_token";
254 std::vector<std::string> scopes(CreateTestScopes()); 256 std::vector<std::string> scopes(CreateTestScopes());
255 257
256 CreateFlow(rt, std::string(), scopes); 258 CreateFlow(rt, std::string(), scopes);
257 SetupAccessTokenFetcher(rt, scopes); 259 SetupAccessTokenFetcher(rt, scopes, flow_.get());
258 TestURLFetcher* url_fetcher = SetupApiCall(false, net::HTTP_BAD_GATEWAY); 260 TestURLFetcher* url_fetcher = SetupApiCall(false, net::HTTP_BAD_GATEWAY);
259 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher)); 261 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher));
260 flow_->Start(); 262 flow_->Start();
261 flow_->OnGetTokenSuccess( 263 flow_->OnGetTokenSuccess(
262 at, 264 at,
263 base::Time::Now() + base::TimeDelta::FromMinutes(3600)); 265 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
264 flow_->OnURLFetchComplete(url_fetcher); 266 flow_->OnURLFetchComplete(url_fetcher);
265 } 267 }
266 268
267 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenNewTokenGenerationFails) { 269 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenNewTokenGenerationFails) {
268 std::string rt = "refresh_token"; 270 std::string rt = "refresh_token";
269 std::string at = "access_token"; 271 std::string at = "access_token";
270 std::vector<std::string> scopes(CreateTestScopes()); 272 std::vector<std::string> scopes(CreateTestScopes());
271 273
272 CreateFlow(rt, std::string(), scopes); 274 CreateFlow(rt, std::string(), scopes);
273 SetupAccessTokenFetcher(rt, scopes); 275 SetupAccessTokenFetcher(rt, scopes, flow_.get());
274 GoogleServiceAuthError error( 276 GoogleServiceAuthError error(
275 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 277 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
276 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); 278 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error));
277 flow_->Start(); 279 flow_->Start();
278 flow_->OnGetTokenFailure(error); 280 flow_->OnGetTokenFailure(error);
279 } 281 }
280 282
281 TEST_F(OAuth2ApiCallFlowTest, CreateURLFetcher) { 283 TEST_F(OAuth2ApiCallFlowTest, CreateURLFetcher) {
282 std::string rt = "refresh_token"; 284 std::string rt = "refresh_token";
283 std::string at = "access_token"; 285 std::string at = "access_token";
284 std::vector<std::string> scopes(CreateTestScopes()); 286 std::vector<std::string> scopes(CreateTestScopes());
285 std::string body = CreateBody(); 287 std::string body = CreateBody();
286 GURL url(CreateApiUrl()); 288 GURL url(CreateApiUrl());
287 289
288 CreateFlow(rt, at, scopes); 290 CreateFlow(rt, at, scopes);
289 scoped_ptr<TestURLFetcher> url_fetcher(SetupApiCall(true, net::HTTP_OK)); 291 scoped_ptr<TestURLFetcher> url_fetcher(SetupApiCall(true, net::HTTP_OK));
290 flow_->CreateURLFetcher(); 292 flow_->CreateURLFetcher();
291 HttpRequestHeaders headers; 293 HttpRequestHeaders headers;
292 url_fetcher->GetExtraRequestHeaders(&headers); 294 url_fetcher->GetExtraRequestHeaders(&headers);
293 std::string auth_header; 295 std::string auth_header;
294 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header)); 296 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header));
295 EXPECT_EQ("Bearer access_token", auth_header); 297 EXPECT_EQ("Bearer access_token", auth_header);
296 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); 298 EXPECT_EQ(url, url_fetcher->GetOriginalURL());
297 EXPECT_EQ(body, url_fetcher->upload_data()); 299 EXPECT_EQ(body, url_fetcher->upload_data());
298 } 300 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698