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.
5 // A complete set of unit tests for OAuth2MintTokenFlow.
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"
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
;
38 using testing::Return
;
42 static std::string
CreateBody() {
46 static GURL
CreateApiUrl() {
47 return GURL("https://www.googleapis.com/someapi");
50 static std::vector
<std::string
> CreateTestScopes() {
51 std::vector
<std::string
> scopes
;
52 scopes
.push_back("scope1");
53 scopes
.push_back("scope2");
57 class MockUrlFetcherFactory
: public ScopedURLFetcherFactory
,
58 public URLFetcherFactory
{
60 MockUrlFetcherFactory()
61 : ScopedURLFetcherFactory(this) {
63 virtual ~MockUrlFetcherFactory() {}
69 URLFetcher::RequestType request_type
,
70 URLFetcherDelegate
* d
));
73 class MockAccessTokenFetcher
: public OAuth2AccessTokenFetcherImpl
{
75 MockAccessTokenFetcher(OAuth2AccessTokenConsumer
* consumer
,
76 net::URLRequestContextGetter
* getter
,
77 const std::string
& refresh_token
)
78 : OAuth2AccessTokenFetcherImpl(consumer
, getter
, refresh_token
) {}
79 ~MockAccessTokenFetcher() {}
82 void(const std::string
& client_id
,
83 const std::string
& client_secret
,
84 const std::vector
<std::string
>& scopes
));
87 class MockApiCallFlow
: public OAuth2ApiCallFlow
{
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
) {}
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
* ());
111 class OAuth2ApiCallFlowTest
: public testing::Test
{
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(),
118 EXPECT_CALL(*flow_
, CreateAccessTokenFetcher())
119 .WillOnce(Return(access_token_fetcher_
.release()));
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));
130 if (response_code
!= 0)
131 url_fetcher
->set_response_code(response_code
);
134 url_fetcher
->SetResponseString(body
);
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
));
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
));
163 MockUrlFetcherFactory factory_
;
164 scoped_ptr
<MockApiCallFlow
> flow_
;
165 scoped_ptr
<MockAccessTokenFetcher
> access_token_fetcher_
;
166 base::MessageLoop message_loop_
;
169 TEST_F(OAuth2ApiCallFlowTest
, FirstApiCallSucceeds
) {
170 std::string rt
= "refresh_token";
171 std::string at
= "access_token";
172 std::vector
<std::string
> scopes(CreateTestScopes());
174 CreateFlow(rt
, at
, scopes
);
175 TestURLFetcher
* url_fetcher
= SetupApiCall(true, net::HTTP_OK
);
176 EXPECT_CALL(*flow_
, ProcessApiCallSuccess(url_fetcher
));
178 flow_
->OnURLFetchComplete(url_fetcher
);
181 TEST_F(OAuth2ApiCallFlowTest
, SecondApiCallSucceeds
) {
182 std::string rt
= "refresh_token";
183 std::string at
= "access_token";
184 std::vector
<std::string
> scopes(CreateTestScopes());
186 CreateFlow(rt
, at
, scopes
);
187 TestURLFetcher
* url_fetcher1
= SetupApiCall(true, net::HTTP_UNAUTHORIZED
);
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(
195 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
196 flow_
->OnURLFetchComplete(url_fetcher2
);
199 TEST_F(OAuth2ApiCallFlowTest
, SecondApiCallFails
) {
200 std::string rt
= "refresh_token";
201 std::string at
= "access_token";
202 std::vector
<std::string
> scopes(CreateTestScopes());
204 CreateFlow(rt
, at
, scopes
);
205 TestURLFetcher
* url_fetcher1
= SetupApiCall(true, net::HTTP_UNAUTHORIZED
);
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(
213 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
214 flow_
->OnURLFetchComplete(url_fetcher2
);
217 TEST_F(OAuth2ApiCallFlowTest
, NewTokenGenerationFails
) {
218 std::string rt
= "refresh_token";
219 std::string at
= "access_token";
220 std::vector
<std::string
> scopes(CreateTestScopes());
222 CreateFlow(rt
, at
, scopes
);
223 TestURLFetcher
* url_fetcher
= SetupApiCall(true, net::HTTP_UNAUTHORIZED
);
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
);
233 TEST_F(OAuth2ApiCallFlowTest
, EmptyAccessTokenFirstApiCallSucceeds
) {
234 std::string rt
= "refresh_token";
235 std::string at
= "access_token";
236 std::vector
<std::string
> scopes(CreateTestScopes());
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
));
243 flow_
->OnGetTokenSuccess(
245 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
246 flow_
->OnURLFetchComplete(url_fetcher
);
249 TEST_F(OAuth2ApiCallFlowTest
, EmptyAccessTokenApiCallFails
) {
250 std::string rt
= "refresh_token";
251 std::string at
= "access_token";
252 std::vector
<std::string
> scopes(CreateTestScopes());
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
));
259 flow_
->OnGetTokenSuccess(
261 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
262 flow_
->OnURLFetchComplete(url_fetcher
);
265 TEST_F(OAuth2ApiCallFlowTest
, EmptyAccessTokenNewTokenGenerationFails
) {
266 std::string rt
= "refresh_token";
267 std::string at
= "access_token";
268 std::vector
<std::string
> scopes(CreateTestScopes());
270 CreateFlow(rt
, std::string(), scopes
);
271 SetupAccessTokenFetcher(scopes
);
272 GoogleServiceAuthError
error(
273 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
274 EXPECT_CALL(*flow_
, ProcessMintAccessTokenFailure(error
));
276 flow_
->OnGetTokenFailure(error
);
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());
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());