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/time.h"
12 #include "chrome/test/base/testing_profile.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.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 "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 using net::HttpRequestHeaders
;
30 using net::ScopedURLFetcherFactory
;
31 using net::TestURLFetcher
;
32 using net::URLFetcher
;
33 using net::URLFetcherDelegate
;
34 using net::URLFetcherFactory
;
35 using net::URLRequestStatus
;
37 using testing::Return
;
41 static std::string
CreateBody() {
45 static GURL
CreateApiUrl() {
46 return GURL("https://www.googleapis.com/someapi");
49 static std::vector
<std::string
> CreateTestScopes() {
50 std::vector
<std::string
> scopes
;
51 scopes
.push_back("scope1");
52 scopes
.push_back("scope2");
56 class MockUrlFetcherFactory
: public ScopedURLFetcherFactory
,
57 public URLFetcherFactory
{
59 MockUrlFetcherFactory()
60 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
62 virtual ~MockUrlFetcherFactory() {}
68 URLFetcher::RequestType request_type
,
69 URLFetcherDelegate
* d
));
72 class MockAccessTokenFetcher
: public OAuth2AccessTokenFetcher
{
74 MockAccessTokenFetcher(OAuth2AccessTokenConsumer
* consumer
,
75 net::URLRequestContextGetter
* getter
)
76 : OAuth2AccessTokenFetcher(consumer
, getter
) {}
77 ~MockAccessTokenFetcher() {}
80 void (const std::string
& client_id
,
81 const std::string
& client_secret
,
82 const std::string
& refresh_token
,
83 const std::vector
<std::string
>& scopes
));
86 class MockApiCallFlow
: public OAuth2ApiCallFlow
{
88 MockApiCallFlow(net::URLRequestContextGetter
* context
,
89 const std::string
& refresh_token
,
90 const std::string
& access_token
,
91 const std::vector
<std::string
>& scopes
)
92 : OAuth2ApiCallFlow(context
, refresh_token
, access_token
, scopes
) {}
95 MOCK_METHOD0(CreateApiCallUrl
, GURL ());
96 MOCK_METHOD0(CreateApiCallBody
, std::string ());
97 MOCK_METHOD1(ProcessApiCallSuccess
,
98 void (const URLFetcher
* source
));
99 MOCK_METHOD1(ProcessApiCallFailure
,
100 void (const URLFetcher
* source
));
101 MOCK_METHOD1(ProcessNewAccessToken
,
102 void (const std::string
& access_token
));
103 MOCK_METHOD1(ProcessMintAccessTokenFailure
,
104 void (const GoogleServiceAuthError
& error
));
105 MOCK_METHOD0(CreateAccessTokenFetcher
, OAuth2AccessTokenFetcher
* ());
110 class OAuth2ApiCallFlowTest
: public testing::Test
{
112 OAuth2ApiCallFlowTest() {}
113 virtual ~OAuth2ApiCallFlowTest() {}
116 void SetupAccessTokenFetcher(
117 const std::string
& rt
, const std::vector
<std::string
>& scopes
) {
118 EXPECT_CALL(*access_token_fetcher_
,
119 Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(),
120 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(),
123 EXPECT_CALL(*flow_
, CreateAccessTokenFetcher())
124 .WillOnce(Return(access_token_fetcher_
.release()));
127 TestURLFetcher
* CreateURLFetcher(
128 const GURL
& url
, bool fetch_succeeds
,
129 int response_code
, const std::string
& body
) {
130 TestURLFetcher
* url_fetcher
= new TestURLFetcher(0, url
, flow_
.get());
131 URLRequestStatus::Status status
=
132 fetch_succeeds
? URLRequestStatus::SUCCESS
: URLRequestStatus::FAILED
;
133 url_fetcher
->set_status(URLRequestStatus(status
, 0));
135 if (response_code
!= 0)
136 url_fetcher
->set_response_code(response_code
);
139 url_fetcher
->SetResponseString(body
);
144 void CreateFlow(const std::string
& refresh_token
,
145 const std::string
& access_token
,
146 const std::vector
<std::string
>& scopes
) {
147 flow_
.reset(new MockApiCallFlow(
148 profile_
.GetRequestContext(),
152 access_token_fetcher_
.reset(new MockAccessTokenFetcher(
153 flow_
.get(), profile_
.GetRequestContext()));
156 TestURLFetcher
* SetupApiCall(bool succeeds
, net::HttpStatusCode status
) {
157 std::string
body(CreateBody());
158 GURL
url(CreateApiUrl());
159 EXPECT_CALL(*flow_
, CreateApiCallBody()).WillOnce(Return(body
));
160 EXPECT_CALL(*flow_
, CreateApiCallUrl()).WillOnce(Return(url
));
161 TestURLFetcher
* url_fetcher
=
162 CreateURLFetcher(url
, succeeds
, status
, std::string());
163 EXPECT_CALL(factory_
, CreateURLFetcher(_
, url
, _
, _
))
164 .WillOnce(Return(url_fetcher
));
168 MockUrlFetcherFactory factory_
;
169 scoped_ptr
<MockApiCallFlow
> flow_
;
170 scoped_ptr
<MockAccessTokenFetcher
> access_token_fetcher_
;
171 TestingProfile profile_
;
174 TEST_F(OAuth2ApiCallFlowTest
, FirstApiCallSucceeds
) {
175 std::string rt
= "refresh_token";
176 std::string at
= "access_token";
177 std::vector
<std::string
> scopes(CreateTestScopes());
179 CreateFlow(rt
, at
, scopes
);
180 TestURLFetcher
* url_fetcher
= SetupApiCall(true, net::HTTP_OK
);
181 EXPECT_CALL(*flow_
, ProcessApiCallSuccess(url_fetcher
));
183 flow_
->OnURLFetchComplete(url_fetcher
);
186 TEST_F(OAuth2ApiCallFlowTest
, SecondApiCallSucceeds
) {
187 std::string rt
= "refresh_token";
188 std::string at
= "access_token";
189 std::vector
<std::string
> scopes(CreateTestScopes());
191 CreateFlow(rt
, at
, scopes
);
192 TestURLFetcher
* url_fetcher1
= SetupApiCall(true, net::HTTP_UNAUTHORIZED
);
194 SetupAccessTokenFetcher(rt
, scopes
);
195 flow_
->OnURLFetchComplete(url_fetcher1
);
196 TestURLFetcher
* url_fetcher2
= SetupApiCall(true, net::HTTP_OK
);
197 EXPECT_CALL(*flow_
, ProcessApiCallSuccess(url_fetcher2
));
198 flow_
->OnGetTokenSuccess(
200 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
201 flow_
->OnURLFetchComplete(url_fetcher2
);
204 TEST_F(OAuth2ApiCallFlowTest
, SecondApiCallFails
) {
205 std::string rt
= "refresh_token";
206 std::string at
= "access_token";
207 std::vector
<std::string
> scopes(CreateTestScopes());
209 CreateFlow(rt
, at
, scopes
);
210 TestURLFetcher
* url_fetcher1
= SetupApiCall(true, net::HTTP_UNAUTHORIZED
);
212 SetupAccessTokenFetcher(rt
, scopes
);
213 flow_
->OnURLFetchComplete(url_fetcher1
);
214 TestURLFetcher
* url_fetcher2
= SetupApiCall(false, net::HTTP_UNAUTHORIZED
);
215 EXPECT_CALL(*flow_
, ProcessApiCallFailure(url_fetcher2
));
216 flow_
->OnGetTokenSuccess(
218 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
219 flow_
->OnURLFetchComplete(url_fetcher2
);
222 TEST_F(OAuth2ApiCallFlowTest
, NewTokenGenerationFails
) {
223 std::string rt
= "refresh_token";
224 std::string at
= "access_token";
225 std::vector
<std::string
> scopes(CreateTestScopes());
227 CreateFlow(rt
, at
, scopes
);
228 TestURLFetcher
* url_fetcher
= SetupApiCall(true, net::HTTP_UNAUTHORIZED
);
230 SetupAccessTokenFetcher(rt
, scopes
);
231 flow_
->OnURLFetchComplete(url_fetcher
);
232 GoogleServiceAuthError
error(
233 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
234 EXPECT_CALL(*flow_
, ProcessMintAccessTokenFailure(error
));
235 flow_
->OnGetTokenFailure(error
);
238 TEST_F(OAuth2ApiCallFlowTest
, EmptyAccessTokenFirstApiCallSucceeds
) {
239 std::string rt
= "refresh_token";
240 std::string at
= "access_token";
241 std::vector
<std::string
> scopes(CreateTestScopes());
243 CreateFlow(rt
, std::string(), scopes
);
244 SetupAccessTokenFetcher(rt
, scopes
);
245 TestURLFetcher
* url_fetcher
= SetupApiCall(true, net::HTTP_OK
);
246 EXPECT_CALL(*flow_
, ProcessApiCallSuccess(url_fetcher
));
248 flow_
->OnGetTokenSuccess(
250 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
251 flow_
->OnURLFetchComplete(url_fetcher
);
254 TEST_F(OAuth2ApiCallFlowTest
, EmptyAccessTokenApiCallFails
) {
255 std::string rt
= "refresh_token";
256 std::string at
= "access_token";
257 std::vector
<std::string
> scopes(CreateTestScopes());
259 CreateFlow(rt
, std::string(), scopes
);
260 SetupAccessTokenFetcher(rt
, scopes
);
261 TestURLFetcher
* url_fetcher
= SetupApiCall(false, net::HTTP_BAD_GATEWAY
);
262 EXPECT_CALL(*flow_
, ProcessApiCallFailure(url_fetcher
));
264 flow_
->OnGetTokenSuccess(
266 base::Time::Now() + base::TimeDelta::FromMinutes(3600));
267 flow_
->OnURLFetchComplete(url_fetcher
);
270 TEST_F(OAuth2ApiCallFlowTest
, EmptyAccessTokenNewTokenGenerationFails
) {
271 std::string rt
= "refresh_token";
272 std::string at
= "access_token";
273 std::vector
<std::string
> scopes(CreateTestScopes());
275 CreateFlow(rt
, std::string(), scopes
);
276 SetupAccessTokenFetcher(rt
, scopes
);
277 GoogleServiceAuthError
error(
278 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
279 EXPECT_CALL(*flow_
, ProcessMintAccessTokenFailure(error
));
281 flow_
->OnGetTokenFailure(error
);
284 TEST_F(OAuth2ApiCallFlowTest
, CreateURLFetcher
) {
285 std::string rt
= "refresh_token";
286 std::string at
= "access_token";
287 std::vector
<std::string
> scopes(CreateTestScopes());
288 std::string body
= CreateBody();
289 GURL
url(CreateApiUrl());
291 CreateFlow(rt
, at
, scopes
);
292 scoped_ptr
<TestURLFetcher
> url_fetcher(SetupApiCall(true, net::HTTP_OK
));
293 flow_
->CreateURLFetcher();
294 HttpRequestHeaders headers
;
295 url_fetcher
->GetExtraRequestHeaders(&headers
);
296 std::string auth_header
;
297 EXPECT_TRUE(headers
.GetHeader("Authorization", &auth_header
));
298 EXPECT_EQ("Bearer access_token", auth_header
);
299 EXPECT_EQ(url
, url_fetcher
->GetOriginalURL());
300 EXPECT_EQ(body
, url_fetcher
->upload_data());