1 // Copyright 2015 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 #include "remoting/test/access_token_fetcher.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "google_apis/gaia/gaia_urls.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 const char kAuthCodeValue
[] = "test_auth_code_value";
18 const char kAccessTokenValue
[] = "test_access_token_value";
19 const char kRefreshTokenValue
[] = "test_refresh_token_value";
20 const char kAuthCodeExchangeValidResponse
[] =
22 " \"refresh_token\": \"test_refresh_token_value\","
23 " \"access_token\": \"test_access_token_value\","
24 " \"expires_in\": 3600,"
25 " \"token_type\": \"Bearer\""
27 const char kAuthCodeExchangeEmptyResponse
[] = "{}";
28 const char kRefreshTokenExchangeValidResponse
[] =
30 " \"access_token\": \"test_access_token_value\","
31 " \"expires_in\": 3600,"
32 " \"token_type\": \"Bearer\""
34 const char kRefreshTokenExchangeEmptyResponse
[] = "{}";
35 const char kValidTokenInfoResponse
[] =
37 " \"audience\": \"blah.apps.googleusercontent.blah.com\","
38 " \"used_id\": \"1234567890\","
39 " \"scope\": \"all the things\","
40 " \"expires_in\": \"1800\","
41 " \"token_type\": \"Bearer\""
43 const char kInvalidTokenInfoResponse
[] =
45 " \"error\": \"invalid_token\""
52 // Provides base functionality for the AccessTokenFetcher Tests below. The
53 // FakeURLFetcherFactory allows us to override the response data and payload for
54 // specified URLs. We use this to stub out network calls made by the
55 // AccessTokenFetcher. This fixture also creates an IO MessageLoop, if
56 // necessary, for use by the AccessTokenFetcher.
57 class AccessTokenFetcherTest
: public ::testing::Test
{
59 AccessTokenFetcherTest() :
60 url_fetcher_factory_(nullptr) {}
61 ~AccessTokenFetcherTest() override
{}
63 void OnAccessTokenRetrieved(
64 base::Closure done_closure
,
65 const std::string
& access_token
,
66 const std::string
& refresh_token
) {
67 access_token_retrieved_
= access_token
;
68 refresh_token_retrieved_
= refresh_token
;
75 void SetUp() override
{
76 if (!base::MessageLoop::current()) {
77 // Create a temporary message loop if the current thread does not already
78 // have one so we can use its task runner to create a request object.
79 message_loop_
.reset(new base::MessageLoopForIO
);
83 void SetFakeResponse(const GURL
& url
,
84 const std::string
& data
,
85 net::HttpStatusCode code
,
86 net::URLRequestStatus::Status status
) {
87 url_fetcher_factory_
.SetFakeResponse(url
, data
, code
, status
);
90 // Used for result verification
91 std::string access_token_retrieved_
;
92 std::string refresh_token_retrieved_
;
95 net::FakeURLFetcherFactory url_fetcher_factory_
;
96 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
98 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherTest
);
101 TEST_F(AccessTokenFetcherTest
, ExchangeAuthCodeForAccessToken
) {
103 GaiaUrls::GetInstance()->oauth2_token_url(),
104 kAuthCodeExchangeValidResponse
,
106 net::URLRequestStatus::SUCCESS
);
109 GaiaUrls::GetInstance()->oauth2_token_info_url(),
110 kValidTokenInfoResponse
,
112 net::URLRequestStatus::SUCCESS
);
114 base::RunLoop run_loop
;
115 AccessTokenCallback access_token_callback
=
116 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
117 base::Unretained(this),
118 run_loop
.QuitClosure());
120 AccessTokenFetcher access_token_fetcher
;
121 access_token_fetcher
.GetAccessTokenFromAuthCode(
123 access_token_callback
);
127 EXPECT_EQ(access_token_retrieved_
.compare(kAccessTokenValue
), 0);
128 EXPECT_EQ(refresh_token_retrieved_
.compare(kRefreshTokenValue
), 0);
131 TEST_F(AccessTokenFetcherTest
, ExchangeRefreshTokenForAccessToken
) {
133 GaiaUrls::GetInstance()->oauth2_token_url(),
134 kRefreshTokenExchangeValidResponse
,
136 net::URLRequestStatus::SUCCESS
);
139 GaiaUrls::GetInstance()->oauth2_token_info_url(),
140 kValidTokenInfoResponse
,
142 net::URLRequestStatus::SUCCESS
);
144 base::RunLoop run_loop
;
145 AccessTokenCallback access_token_callback
=
146 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
147 base::Unretained(this),
148 run_loop
.QuitClosure());
150 AccessTokenFetcher access_token_fetcher
;
151 access_token_fetcher
.GetAccessTokenFromRefreshToken(
153 access_token_callback
);
157 EXPECT_EQ(access_token_retrieved_
.compare(kAccessTokenValue
), 0);
158 EXPECT_EQ(refresh_token_retrieved_
.compare(kRefreshTokenValue
), 0);
161 TEST_F(AccessTokenFetcherTest
, MultipleAccessTokenCalls
) {
163 GaiaUrls::GetInstance()->oauth2_token_url(),
164 kAuthCodeExchangeValidResponse
,
166 net::URLRequestStatus::SUCCESS
);
169 GaiaUrls::GetInstance()->oauth2_token_info_url(),
170 kValidTokenInfoResponse
,
172 net::URLRequestStatus::SUCCESS
);
174 scoped_ptr
<base::RunLoop
> run_loop
;
175 run_loop
.reset(new base::RunLoop());
176 AccessTokenCallback access_token_callback
=
177 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
178 base::Unretained(this),
179 run_loop
->QuitClosure());
181 AccessTokenFetcher access_token_fetcher
;
182 access_token_fetcher
.GetAccessTokenFromAuthCode(
184 access_token_callback
);
188 EXPECT_EQ(access_token_retrieved_
.compare(kAccessTokenValue
), 0);
189 EXPECT_EQ(refresh_token_retrieved_
.compare(kRefreshTokenValue
), 0);
191 // Reset our token data for the next iteration.
192 access_token_retrieved_
.clear();
193 refresh_token_retrieved_
.clear();
195 // Update the response since we will call the refresh token method next.
197 GaiaUrls::GetInstance()->oauth2_token_url(),
198 kRefreshTokenExchangeValidResponse
,
200 net::URLRequestStatus::SUCCESS
);
202 run_loop
.reset(new base::RunLoop());
203 access_token_callback
=
204 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
205 base::Unretained(this),
206 run_loop
->QuitClosure());
208 access_token_fetcher
.GetAccessTokenFromRefreshToken(
210 access_token_callback
);
214 EXPECT_EQ(access_token_retrieved_
.compare(kAccessTokenValue
), 0);
215 EXPECT_EQ(refresh_token_retrieved_
.compare(kRefreshTokenValue
), 0);
217 run_loop
.reset(new base::RunLoop());
218 access_token_callback
=
219 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
220 base::Unretained(this),
221 run_loop
->QuitClosure());
223 // Reset our token data for the next iteration.
224 access_token_retrieved_
.clear();
225 refresh_token_retrieved_
.clear();
227 access_token_fetcher
.GetAccessTokenFromRefreshToken(
229 access_token_callback
);
233 EXPECT_EQ(access_token_retrieved_
.compare(kAccessTokenValue
), 0);
234 EXPECT_EQ(refresh_token_retrieved_
.compare(kRefreshTokenValue
), 0);
237 TEST_F(AccessTokenFetcherTest
, ExchangeAuthCode_Unauthorized_Error
) {
239 GaiaUrls::GetInstance()->oauth2_token_url(),
240 kAuthCodeExchangeValidResponse
,
241 net::HTTP_UNAUTHORIZED
,
242 net::URLRequestStatus::FAILED
);
244 base::RunLoop run_loop
;
245 AccessTokenCallback access_token_callback
=
246 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
247 base::Unretained(this),
248 run_loop
.QuitClosure());
250 AccessTokenFetcher access_token_fetcher
;
251 access_token_fetcher
.GetAccessTokenFromAuthCode(
253 access_token_callback
);
257 // Our callback should have been called with empty strings.
258 EXPECT_TRUE(access_token_retrieved_
.empty());
259 EXPECT_TRUE(refresh_token_retrieved_
.empty());
262 TEST_F(AccessTokenFetcherTest
, ExchangeRefreshToken_Unauthorized_Error
) {
264 GaiaUrls::GetInstance()->oauth2_token_url(),
265 kRefreshTokenExchangeValidResponse
,
266 net::HTTP_UNAUTHORIZED
,
267 net::URLRequestStatus::FAILED
);
269 base::RunLoop run_loop
;
270 AccessTokenCallback access_token_callback
=
271 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
272 base::Unretained(this),
273 run_loop
.QuitClosure());
275 AccessTokenFetcher access_token_fetcher
;
276 access_token_fetcher
.GetAccessTokenFromRefreshToken(
278 access_token_callback
);
282 // Our callback should have been called with empty strings.
283 EXPECT_TRUE(access_token_retrieved_
.empty());
284 EXPECT_TRUE(refresh_token_retrieved_
.empty());
287 TEST_F(AccessTokenFetcherTest
, ExchangeAuthCode_NetworkError
) {
289 GaiaUrls::GetInstance()->oauth2_token_url(),
290 kAuthCodeExchangeValidResponse
,
292 net::URLRequestStatus::FAILED
);
294 base::RunLoop run_loop
;
295 AccessTokenCallback access_token_callback
=
296 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
297 base::Unretained(this),
298 run_loop
.QuitClosure());
300 AccessTokenFetcher access_token_fetcher
;
301 access_token_fetcher
.GetAccessTokenFromAuthCode(
303 access_token_callback
);
307 // Our callback should have been called with empty strings.
308 EXPECT_TRUE(access_token_retrieved_
.empty());
309 EXPECT_TRUE(refresh_token_retrieved_
.empty());
312 TEST_F(AccessTokenFetcherTest
, ExchangeRefreshToken_NetworkError
) {
314 GaiaUrls::GetInstance()->oauth2_token_url(),
315 kRefreshTokenExchangeValidResponse
,
317 net::URLRequestStatus::FAILED
);
319 base::RunLoop run_loop
;
320 AccessTokenCallback access_token_callback
=
321 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
322 base::Unretained(this),
323 run_loop
.QuitClosure());
325 AccessTokenFetcher access_token_fetcher
;
326 access_token_fetcher
.GetAccessTokenFromRefreshToken(
328 access_token_callback
);
332 // Our callback should have been called with empty strings.
333 EXPECT_TRUE(access_token_retrieved_
.empty());
334 EXPECT_TRUE(refresh_token_retrieved_
.empty());
337 TEST_F(AccessTokenFetcherTest
, AuthCode_GetTokenInfoResponse_InvalidToken
) {
339 GaiaUrls::GetInstance()->oauth2_token_url(),
340 kAuthCodeExchangeValidResponse
,
342 net::URLRequestStatus::SUCCESS
);
345 GaiaUrls::GetInstance()->oauth2_token_info_url(),
346 kInvalidTokenInfoResponse
,
348 net::URLRequestStatus::SUCCESS
);
350 base::RunLoop run_loop
;
351 AccessTokenCallback access_token_callback
=
352 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
353 base::Unretained(this),
354 run_loop
.QuitClosure());
356 AccessTokenFetcher access_token_fetcher
;
357 access_token_fetcher
.GetAccessTokenFromAuthCode(
359 access_token_callback
);
363 // Our callback should have been called with empty strings.
364 EXPECT_TRUE(access_token_retrieved_
.empty());
365 EXPECT_TRUE(refresh_token_retrieved_
.empty());
368 TEST_F(AccessTokenFetcherTest
, ExchangeAuthCodeForAccessToken_EmptyToken
) {
370 GaiaUrls::GetInstance()->oauth2_token_url(),
371 kAuthCodeExchangeEmptyResponse
,
373 net::URLRequestStatus::SUCCESS
);
375 base::RunLoop run_loop
;
376 AccessTokenCallback access_token_callback
=
377 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
378 base::Unretained(this),
379 run_loop
.QuitClosure());
381 AccessTokenFetcher access_token_fetcher
;
382 access_token_fetcher
.GetAccessTokenFromAuthCode(
384 access_token_callback
);
388 // Our callback should have been called with empty strings.
389 EXPECT_TRUE(access_token_retrieved_
.empty());
390 EXPECT_TRUE(refresh_token_retrieved_
.empty());
393 TEST_F(AccessTokenFetcherTest
, RefreshToken_GetTokenInfoResponse_InvalidToken
) {
395 GaiaUrls::GetInstance()->oauth2_token_url(),
396 kRefreshTokenExchangeValidResponse
,
398 net::URLRequestStatus::SUCCESS
);
401 GaiaUrls::GetInstance()->oauth2_token_info_url(),
402 kInvalidTokenInfoResponse
,
404 net::URLRequestStatus::SUCCESS
);
406 base::RunLoop run_loop
;
407 AccessTokenCallback access_token_callback
=
408 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
409 base::Unretained(this),
410 run_loop
.QuitClosure());
412 AccessTokenFetcher access_token_fetcher
;
413 access_token_fetcher
.GetAccessTokenFromRefreshToken(
415 access_token_callback
);
419 // Our callback should have been called with empty strings.
420 EXPECT_TRUE(access_token_retrieved_
.empty());
421 EXPECT_TRUE(refresh_token_retrieved_
.empty());
424 TEST_F(AccessTokenFetcherTest
, ExchangeRefreshTokenForAccessToken_EmptyToken
) {
426 GaiaUrls::GetInstance()->oauth2_token_url(),
427 kRefreshTokenExchangeEmptyResponse
,
429 net::URLRequestStatus::SUCCESS
);
431 base::RunLoop run_loop
;
432 AccessTokenCallback access_token_callback
=
433 base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved
,
434 base::Unretained(this),
435 run_loop
.QuitClosure());
437 AccessTokenFetcher access_token_fetcher
;
438 access_token_fetcher
.GetAccessTokenFromRefreshToken(
440 access_token_callback
);
444 // Our callback should have been called with empty strings.
445 EXPECT_TRUE(access_token_retrieved_
.empty());
446 EXPECT_TRUE(refresh_token_retrieved_
.empty());
450 } // namespace remoting