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 "google_apis/gaia/gaia_urls.h"
12 #include "google_apis/gaia/google_service_auth_error.h"
13 #include "google_apis/gaia/oauth2_access_token_consumer.h"
14 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
15 #include "net/http/http_status_code.h"
16 #include "net/url_request/test_url_fetcher_factory.h"
17 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19 #include "net/url_request/url_fetcher_factory.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_status.h"
22 #include "net/url_request/url_request_test_util.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
27 using net::ResponseCookies
;
28 using net::ScopedURLFetcherFactory
;
29 using net::TestURLFetcher
;
30 using net::URLFetcher
;
31 using net::URLFetcherDelegate
;
32 using net::URLFetcherFactory
;
33 using net::URLRequestStatus
;
35 using testing::Return
;
39 typedef std::vector
<std::string
> ScopeList
;
41 static const char kValidTokenResponse
[] =
43 " \"access_token\": \"at1\","
44 " \"expires_in\": 3600,"
45 " \"token_type\": \"Bearer\""
47 static const char kTokenResponseNoAccessToken
[] =
49 " \"expires_in\": 3600,"
50 " \"token_type\": \"Bearer\""
53 static const char kValidFailureTokenResponse
[] =
55 " \"error\": \"invalid_grant\""
58 class MockUrlFetcherFactory
: public ScopedURLFetcherFactory
,
59 public URLFetcherFactory
{
61 MockUrlFetcherFactory() : ScopedURLFetcherFactory(this) {}
62 virtual ~MockUrlFetcherFactory() {}
64 MOCK_METHOD4(CreateURLFetcherMock
,
67 URLFetcher::RequestType request_type
,
68 URLFetcherDelegate
* d
));
70 scoped_ptr
<URLFetcher
> CreateURLFetcher(int id
,
72 URLFetcher::RequestType request_type
,
73 URLFetcherDelegate
* d
) override
{
74 return scoped_ptr
<URLFetcher
>(
75 CreateURLFetcherMock(id
, url
, request_type
, d
));
79 class MockOAuth2AccessTokenConsumer
: public OAuth2AccessTokenConsumer
{
81 MockOAuth2AccessTokenConsumer() {}
82 ~MockOAuth2AccessTokenConsumer() {}
84 MOCK_METHOD2(OnGetTokenSuccess
,
85 void(const std::string
& access_token
,
86 const base::Time
& expiration_time
));
87 MOCK_METHOD1(OnGetTokenFailure
, void(const GoogleServiceAuthError
& error
));
92 class OAuth2AccessTokenFetcherImplTest
: public testing::Test
{
94 OAuth2AccessTokenFetcherImplTest()
95 : request_context_getter_(new net::TestURLRequestContextGetter(
96 base::MessageLoopProxy::current())),
97 fetcher_(&consumer_
, request_context_getter_
.get(), "refresh_token") {
98 base::RunLoop().RunUntilIdle();
101 ~OAuth2AccessTokenFetcherImplTest() override
{}
103 virtual TestURLFetcher
* SetupGetAccessToken(bool fetch_succeeds
,
105 const std::string
& body
) {
106 GURL
url(GaiaUrls::GetInstance()->oauth2_token_url());
107 TestURLFetcher
* url_fetcher
= new TestURLFetcher(0, url
, &fetcher_
);
108 URLRequestStatus::Status status
=
109 fetch_succeeds
? URLRequestStatus::SUCCESS
: URLRequestStatus::FAILED
;
110 url_fetcher
->set_status(URLRequestStatus(status
, 0));
112 if (response_code
!= 0)
113 url_fetcher
->set_response_code(response_code
);
116 url_fetcher
->SetResponseString(body
);
118 EXPECT_CALL(factory_
, CreateURLFetcherMock(_
, url
, _
, _
))
119 .WillOnce(Return(url_fetcher
));
124 base::MessageLoop message_loop_
;
125 MockUrlFetcherFactory factory_
;
126 MockOAuth2AccessTokenConsumer consumer_
;
127 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
128 OAuth2AccessTokenFetcherImpl fetcher_
;
131 // These four tests time out, see http://crbug.com/113446.
132 TEST_F(OAuth2AccessTokenFetcherImplTest
,
133 DISABLED_GetAccessTokenRequestFailure
) {
134 TestURLFetcher
* url_fetcher
= SetupGetAccessToken(false, 0, std::string());
135 EXPECT_CALL(consumer_
, OnGetTokenFailure(_
)).Times(1);
136 fetcher_
.Start("client_id", "client_secret", ScopeList());
137 fetcher_
.OnURLFetchComplete(url_fetcher
);
140 TEST_F(OAuth2AccessTokenFetcherImplTest
,
141 DISABLED_GetAccessTokenResponseCodeFailure
) {
142 TestURLFetcher
* url_fetcher
=
143 SetupGetAccessToken(true, net::HTTP_FORBIDDEN
, std::string());
144 EXPECT_CALL(consumer_
, OnGetTokenFailure(_
)).Times(1);
145 fetcher_
.Start("client_id", "client_secret", ScopeList());
146 fetcher_
.OnURLFetchComplete(url_fetcher
);
149 TEST_F(OAuth2AccessTokenFetcherImplTest
, DISABLED_Success
) {
150 TestURLFetcher
* url_fetcher
=
151 SetupGetAccessToken(true, net::HTTP_OK
, kValidTokenResponse
);
152 EXPECT_CALL(consumer_
, OnGetTokenSuccess("at1", _
)).Times(1);
153 fetcher_
.Start("client_id", "client_secret", ScopeList());
154 fetcher_
.OnURLFetchComplete(url_fetcher
);
157 TEST_F(OAuth2AccessTokenFetcherImplTest
, DISABLED_MakeGetAccessTokenBody
) {
162 "grant_type=refresh_token&"
165 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
166 "cid1", "cs1", "rt1", ScopeList()));
173 "grant_type=refresh_token&"
175 "scope=https://www.googleapis.com/foo";
177 scopes
.push_back("https://www.googleapis.com/foo");
179 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
180 "cid1", "cs1", "rt1", scopes
));
183 { // Multiple scopes.
187 "grant_type=refresh_token&"
189 "scope=https://www.googleapis.com/foo+"
190 "https://www.googleapis.com/bar+"
191 "https://www.googleapis.com/baz";
193 scopes
.push_back("https://www.googleapis.com/foo");
194 scopes
.push_back("https://www.googleapis.com/bar");
195 scopes
.push_back("https://www.googleapis.com/baz");
197 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
198 "cid1", "cs1", "rt1", scopes
));
202 // http://crbug.com/114215
204 #define MAYBE_ParseGetAccessTokenResponse DISABLED_ParseGetAccessTokenResponse
206 #define MAYBE_ParseGetAccessTokenResponse ParseGetAccessTokenResponse
207 #endif // defined(OS_WIN)
208 TEST_F(OAuth2AccessTokenFetcherImplTest
, MAYBE_ParseGetAccessTokenResponse
) {
210 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
215 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
216 &url_fetcher
, &at
, &expires_in
));
217 EXPECT_TRUE(at
.empty());
220 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
221 url_fetcher
.SetResponseString("foo");
226 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
227 &url_fetcher
, &at
, &expires_in
));
228 EXPECT_TRUE(at
.empty());
230 { // Valid json: access token missing.
231 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
232 url_fetcher
.SetResponseString(kTokenResponseNoAccessToken
);
237 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
238 &url_fetcher
, &at
, &expires_in
));
239 EXPECT_TRUE(at
.empty());
241 { // Valid json: all good.
242 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
243 url_fetcher
.SetResponseString(kValidTokenResponse
);
248 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
249 &url_fetcher
, &at
, &expires_in
));
250 EXPECT_EQ("at1", at
);
251 EXPECT_EQ(3600, expires_in
);
253 { // Valid json: invalid error response.
254 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
255 url_fetcher
.SetResponseString(kTokenResponseNoAccessToken
);
259 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
260 &url_fetcher
, &error
));
261 EXPECT_TRUE(error
.empty());
263 { // Valid json: error response.
264 TestURLFetcher
url_fetcher(0, GURL("http://www.google.com"), NULL
);
265 url_fetcher
.SetResponseString(kValidFailureTokenResponse
);
269 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
270 &url_fetcher
, &error
));
271 EXPECT_EQ("invalid_grant", error
);