Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / remoting / test / access_token_fetcher_unittest.cc
blobb06151209ed7894b435dc2760010b23a0e1bdf24
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"
7 #include <string>
9 #include "base/bind.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"
16 namespace {
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[] =
21 "{"
22 " \"refresh_token\": \"test_refresh_token_value\","
23 " \"access_token\": \"test_access_token_value\","
24 " \"expires_in\": 3600,"
25 " \"token_type\": \"Bearer\""
26 "}";
27 const char kAuthCodeExchangeEmptyResponse[] = "{}";
28 const char kRefreshTokenExchangeValidResponse[] =
29 "{"
30 " \"access_token\": \"test_access_token_value\","
31 " \"expires_in\": 3600,"
32 " \"token_type\": \"Bearer\""
33 "}";
34 const char kRefreshTokenExchangeEmptyResponse[] = "{}";
35 const char kValidTokenInfoResponse[] =
36 "{"
37 " \"audience\": \"blah.apps.googleusercontent.blah.com\","
38 " \"used_id\": \"1234567890\","
39 " \"scope\": \"all the things\","
40 " \"expires_in\": \"1800\","
41 " \"token_type\": \"Bearer\""
42 "}";
43 const char kInvalidTokenInfoResponse[] =
44 "{"
45 " \"error\": \"invalid_token\""
46 "}";
47 } // namespace
49 namespace remoting {
50 namespace test {
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 {
58 public:
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;
70 done_closure.Run();
73 protected:
74 // Test interface.
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_;
94 private:
95 net::FakeURLFetcherFactory url_fetcher_factory_;
96 scoped_ptr<base::MessageLoopForIO> message_loop_;
98 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherTest);
101 TEST_F(AccessTokenFetcherTest, ExchangeAuthCodeForAccessToken) {
102 SetFakeResponse(
103 GaiaUrls::GetInstance()->oauth2_token_url(),
104 kAuthCodeExchangeValidResponse,
105 net::HTTP_OK,
106 net::URLRequestStatus::SUCCESS);
108 SetFakeResponse(
109 GaiaUrls::GetInstance()->oauth2_token_info_url(),
110 kValidTokenInfoResponse,
111 net::HTTP_OK,
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(
122 kAuthCodeValue,
123 access_token_callback);
125 run_loop.Run();
127 EXPECT_EQ(access_token_retrieved_.compare(kAccessTokenValue), 0);
128 EXPECT_EQ(refresh_token_retrieved_.compare(kRefreshTokenValue), 0);
131 TEST_F(AccessTokenFetcherTest, ExchangeRefreshTokenForAccessToken) {
132 SetFakeResponse(
133 GaiaUrls::GetInstance()->oauth2_token_url(),
134 kRefreshTokenExchangeValidResponse,
135 net::HTTP_OK,
136 net::URLRequestStatus::SUCCESS);
138 SetFakeResponse(
139 GaiaUrls::GetInstance()->oauth2_token_info_url(),
140 kValidTokenInfoResponse,
141 net::HTTP_OK,
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(
152 kRefreshTokenValue,
153 access_token_callback);
155 run_loop.Run();
157 EXPECT_EQ(access_token_retrieved_.compare(kAccessTokenValue), 0);
158 EXPECT_EQ(refresh_token_retrieved_.compare(kRefreshTokenValue), 0);
161 TEST_F(AccessTokenFetcherTest, MultipleAccessTokenCalls) {
162 SetFakeResponse(
163 GaiaUrls::GetInstance()->oauth2_token_url(),
164 kAuthCodeExchangeValidResponse,
165 net::HTTP_OK,
166 net::URLRequestStatus::SUCCESS);
168 SetFakeResponse(
169 GaiaUrls::GetInstance()->oauth2_token_info_url(),
170 kValidTokenInfoResponse,
171 net::HTTP_OK,
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(
183 kAuthCodeValue,
184 access_token_callback);
186 run_loop->Run();
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.
196 SetFakeResponse(
197 GaiaUrls::GetInstance()->oauth2_token_url(),
198 kRefreshTokenExchangeValidResponse,
199 net::HTTP_OK,
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(
209 kRefreshTokenValue,
210 access_token_callback);
212 run_loop->Run();
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(
228 kRefreshTokenValue,
229 access_token_callback);
231 run_loop->Run();
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) {
238 SetFakeResponse(
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(
252 kAuthCodeValue,
253 access_token_callback);
255 run_loop.Run();
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) {
263 SetFakeResponse(
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(
277 kRefreshTokenValue,
278 access_token_callback);
280 run_loop.Run();
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) {
288 SetFakeResponse(
289 GaiaUrls::GetInstance()->oauth2_token_url(),
290 kAuthCodeExchangeValidResponse,
291 net::HTTP_NOT_FOUND,
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(
302 kAuthCodeValue,
303 access_token_callback);
305 run_loop.Run();
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) {
313 SetFakeResponse(
314 GaiaUrls::GetInstance()->oauth2_token_url(),
315 kRefreshTokenExchangeValidResponse,
316 net::HTTP_NOT_FOUND,
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(
327 kRefreshTokenValue,
328 access_token_callback);
330 run_loop.Run();
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) {
338 SetFakeResponse(
339 GaiaUrls::GetInstance()->oauth2_token_url(),
340 kAuthCodeExchangeValidResponse,
341 net::HTTP_OK,
342 net::URLRequestStatus::SUCCESS);
344 SetFakeResponse(
345 GaiaUrls::GetInstance()->oauth2_token_info_url(),
346 kInvalidTokenInfoResponse,
347 net::HTTP_OK,
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(
358 kAuthCodeValue,
359 access_token_callback);
361 run_loop.Run();
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) {
369 SetFakeResponse(
370 GaiaUrls::GetInstance()->oauth2_token_url(),
371 kAuthCodeExchangeEmptyResponse,
372 net::HTTP_OK,
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(
383 kAuthCodeValue,
384 access_token_callback);
386 run_loop.Run();
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) {
394 SetFakeResponse(
395 GaiaUrls::GetInstance()->oauth2_token_url(),
396 kRefreshTokenExchangeValidResponse,
397 net::HTTP_OK,
398 net::URLRequestStatus::SUCCESS);
400 SetFakeResponse(
401 GaiaUrls::GetInstance()->oauth2_token_info_url(),
402 kInvalidTokenInfoResponse,
403 net::HTTP_OK,
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(
414 kRefreshTokenValue,
415 access_token_callback);
417 run_loop.Run();
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) {
425 SetFakeResponse(
426 GaiaUrls::GetInstance()->oauth2_token_url(),
427 kRefreshTokenExchangeEmptyResponse,
428 net::HTTP_OK,
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(
439 kRefreshTokenValue,
440 access_token_callback);
442 run_loop.Run();
444 // Our callback should have been called with empty strings.
445 EXPECT_TRUE(access_token_retrieved_.empty());
446 EXPECT_TRUE(refresh_token_retrieved_.empty());
449 } // namespace test
450 } // namespace remoting