1 // Copyright 2014 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 OAuth2AccessTokenFetcherImpl.
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "google_apis/gaia/gaia_urls.h"
13 #include "google_apis/gaia/google_service_auth_error.h"
14 #include "google_apis/gaia/oauth2_access_token_consumer.h"
15 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
16 #include "net/base/net_errors.h"
17 #include "net/http/http_status_code.h"
18 #include "net/url_request/test_url_fetcher_factory.h"
19 #include "net/url_request/url_fetcher.h"
20 #include "net/url_request/url_fetcher_delegate.h"
21 #include "net/url_request/url_fetcher_factory.h"
22 #include "net/url_request/url_request.h"
23 #include "net/url_request/url_request_status.h"
24 #include "net/url_request/url_request_test_util.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
29 using net::ResponseCookies
;
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 typedef std::vector
<std::string
> ScopeList
;
43 static const char kValidTokenResponse
[] =
45 " \"access_token\": \"at1\","
46 " \"expires_in\": 3600,"
47 " \"token_type\": \"Bearer\""
49 static const char kTokenResponseNoAccessToken
[] =
51 " \"expires_in\": 3600,"
52 " \"token_type\": \"Bearer\""
55 static const char kValidFailureTokenResponse
[] =
57 " \"error\": \"invalid_grant\""
60 class MockUrlFetcherFactory
: public ScopedURLFetcherFactory
,
61 public URLFetcherFactory
{
63 MockUrlFetcherFactory() : ScopedURLFetcherFactory(this) {}
64 virtual ~MockUrlFetcherFactory() {}
66 MOCK_METHOD4(CreateURLFetcherMock
,
69 URLFetcher::RequestType request_type
,
70 URLFetcherDelegate
* d
));
72 scoped_ptr
<URLFetcher
> CreateURLFetcher(int id
,
74 URLFetcher::RequestType request_type
,
75 URLFetcherDelegate
* d
) override
{
76 return scoped_ptr
<URLFetcher
>(
77 CreateURLFetcherMock(id
, url
, request_type
, d
));
81 class MockOAuth2AccessTokenConsumer
: public OAuth2AccessTokenConsumer
{
83 MockOAuth2AccessTokenConsumer() {}
84 ~MockOAuth2AccessTokenConsumer() {}
86 MOCK_METHOD2(OnGetTokenSuccess
,
87 void(const std::string
& access_token
,
88 const base::Time
& expiration_time
));
89 MOCK_METHOD1(OnGetTokenFailure
, void(const GoogleServiceAuthError
& error
));
94 class OAuth2AccessTokenFetcherImplTest
: public testing::Test
{
96 OAuth2AccessTokenFetcherImplTest()
97 : request_context_getter_(new net::TestURLRequestContextGetter(
98 base::ThreadTaskRunnerHandle::Get())),
99 fetcher_(&consumer_
, request_context_getter_
.get(), "refresh_token") {
100 base::RunLoop().RunUntilIdle();
103 ~OAuth2AccessTokenFetcherImplTest() override
{}
105 virtual TestURLFetcher
* SetupGetAccessToken(bool fetch_succeeds
,
107 const std::string
& body
) {
108 GURL
url(GaiaUrls::GetInstance()->oauth2_token_url());
109 TestURLFetcher
* url_fetcher
= new TestURLFetcher(0, url
, &fetcher_
);
110 net::Error error
= fetch_succeeds
? net::OK
: net::ERR_FAILED
;
111 url_fetcher
->set_status(URLRequestStatus::FromError(error
));
113 if (response_code
!= 0)
114 url_fetcher
->set_response_code(response_code
);
117 url_fetcher
->SetResponseString(body
);
119 EXPECT_CALL(factory_
, CreateURLFetcherMock(_
, url
, _
, _
))
120 .WillOnce(Return(url_fetcher
));
125 base::MessageLoop message_loop_
;
126 MockUrlFetcherFactory factory_
;
127 MockOAuth2AccessTokenConsumer consumer_
;
128 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
129 OAuth2AccessTokenFetcherImpl fetcher_
;
132 // These four tests time out, see http://crbug.com/113446.
133 TEST_F(OAuth2AccessTokenFetcherImplTest
,
134 DISABLED_GetAccessTokenRequestFailure
) {
135 TestURLFetcher
* url_fetcher
= SetupGetAccessToken(false, 0, std::string());
136 EXPECT_CALL(consumer_
, OnGetTokenFailure(_
)).Times(1);
137 fetcher_
.Start("client_id", "client_secret", ScopeList());
138 fetcher_
.OnURLFetchComplete(url_fetcher
);
141 TEST_F(OAuth2AccessTokenFetcherImplTest
,
142 DISABLED_GetAccessTokenResponseCodeFailure
) {
143 TestURLFetcher
* url_fetcher
=
144 SetupGetAccessToken(true, net::HTTP_FORBIDDEN
, std::string());
145 EXPECT_CALL(consumer_
, OnGetTokenFailure(_
)).Times(1);
146 fetcher_
.Start("client_id", "client_secret", ScopeList());
147 fetcher_
.OnURLFetchComplete(url_fetcher
);
150 TEST_F(OAuth2AccessTokenFetcherImplTest
, DISABLED_Success
) {
151 TestURLFetcher
* url_fetcher
=
152 SetupGetAccessToken(true, net::HTTP_OK
, kValidTokenResponse
);
153 EXPECT_CALL(consumer_
, OnGetTokenSuccess("at1", _
)).Times(1);
154 fetcher_
.Start("client_id", "client_secret", ScopeList());
155 fetcher_
.OnURLFetchComplete(url_fetcher
);
158 TEST_F(OAuth2AccessTokenFetcherImplTest
, DISABLED_MakeGetAccessTokenBody
) {
163 "grant_type=refresh_token&"
166 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
167 "cid1", "cs1", "rt1", ScopeList()));
174 "grant_type=refresh_token&"
176 "scope=https://www.googleapis.com/foo";
178 scopes
.push_back("https://www.googleapis.com/foo");
180 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
181 "cid1", "cs1", "rt1", scopes
));
184 { // Multiple scopes.
188 "grant_type=refresh_token&"
190 "scope=https://www.googleapis.com/foo+"
191 "https://www.googleapis.com/bar+"
192 "https://www.googleapis.com/baz";
194 scopes
.push_back("https://www.googleapis.com/foo");
195 scopes
.push_back("https://www.googleapis.com/bar");
196 scopes
.push_back("https://www.googleapis.com/baz");
198 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
199 "cid1", "cs1", "rt1", scopes
));
203 // http://crbug.com/114215
205 #define MAYBE_ParseGetAccessTokenResponse DISABLED_ParseGetAccessTokenResponse
207 #define MAYBE_ParseGetAccessTokenResponse ParseGetAccessTokenResponse
208 #endif // defined(OS_WIN)
209 TEST_F(OAuth2AccessTokenFetcherImplTest
, MAYBE_ParseGetAccessTokenResponse
) {
211 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
216 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
217 &url_fetcher
, &at
, &expires_in
));
218 EXPECT_TRUE(at
.empty());
221 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
222 url_fetcher
.SetResponseString("foo");
227 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
228 &url_fetcher
, &at
, &expires_in
));
229 EXPECT_TRUE(at
.empty());
231 { // Valid json: access token missing.
232 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
233 url_fetcher
.SetResponseString(kTokenResponseNoAccessToken
);
238 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
239 &url_fetcher
, &at
, &expires_in
));
240 EXPECT_TRUE(at
.empty());
242 { // Valid json: all good.
243 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
244 url_fetcher
.SetResponseString(kValidTokenResponse
);
249 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
250 &url_fetcher
, &at
, &expires_in
));
251 EXPECT_EQ("at1", at
);
252 EXPECT_EQ(3600, expires_in
);
254 { // Valid json: invalid error response.
255 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
256 url_fetcher
.SetResponseString(kTokenResponseNoAccessToken
);
260 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
261 &url_fetcher
, &error
));
262 EXPECT_TRUE(error
.empty());
264 { // Valid json: error response.
265 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
266 url_fetcher
.SetResponseString(kValidFailureTokenResponse
);
270 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
271 &url_fetcher
, &error
));
272 EXPECT_EQ("invalid_grant", error
);