Chromium Code Reviews| Index: remoting/test/access_token_fetcher_unittest.cc |
| diff --git a/remoting/test/access_token_fetcher_unittest.cc b/remoting/test/access_token_fetcher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3034942ab0b0f42d62c4a268437f57728694ae88 |
| --- /dev/null |
| +++ b/remoting/test/access_token_fetcher_unittest.cc |
| @@ -0,0 +1,450 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/test/access_token_fetcher.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "google_apis/gaia/gaia_urls.h" |
| +#include "net/url_request/test_url_fetcher_factory.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + const char kAuthCodeValue[] = "test_auth_code_value"; |
| + const char kAccessTokenValue[] = "test_access_token_value"; |
| + const char kRefreshTokenValue[] = "test_refresh_token_value"; |
| + const char kAuthCodeExchangeValidResponse[] = |
| + "{" |
| + " \"refresh_token\": \"test_refresh_token_value\"," |
| + " \"access_token\": \"test_access_token_value\"," |
| + " \"expires_in\": 3600," |
| + " \"token_type\": \"Bearer\"" |
| + "}"; |
| + const char kAuthCodeExchangeEmptyResponse[] = "{}"; |
| + const char kRefreshTokenExchangeValidResponse[] = |
| + "{" |
| + " \"access_token\": \"test_access_token_value\"," |
| + " \"expires_in\": 3600," |
| + " \"token_type\": \"Bearer\"" |
| + "}"; |
| + const char kRefreshTokenExchangeEmptyResponse[] = "{}"; |
| + const char kValidTokenInfoResponse[] = |
| + "{" |
| + " \"audience\": \"blah.apps.googleusercontent.blah.com\"," |
| + " \"used_id\": \"1234567890\"," |
| + " \"scope\": \"all the things\"," |
| + " \"expires_in\": \"1800\"," |
| + " \"token_type\": \"Bearer\"" |
| + "}"; |
| + const char kInvalidTokenInfoResponse[] = |
| + "{" |
| + " \"error\": \"invalid_token\"" |
| + "}"; |
| +} |
| + |
| +namespace remoting { |
| +namespace test { |
| + |
| +// Provides base functionality for the AccessTokenFetcher Tests below. The |
| +// FakeURLFetcherFactory allows us to override the response data and payload for |
| +// specified URLs. We use this to stub out network calls made by the |
| +// GaiaOAuthClient used by the AccessTokenFetcher. The other service provided |
| +// by this test fixture is to ensure that a message loop is running on the |
| +// current thread. This is needed to create a resource context getter which is |
| +// passed to the GaiaOAuthClient instance on construction. |
|
Wez
2015/02/19 22:00:23
This seems to be an irrelevant implementation deta
joedow
2015/02/20 02:58:35
Done.
|
| +class AccessTokenFetcherTest : public ::testing::Test { |
| + public: |
| + AccessTokenFetcherTest() : |
| + url_fetcher_factory_(nullptr) {} |
| + ~AccessTokenFetcherTest() override {} |
| + |
| + void OnAccessTokenRetrieved( |
| + base::Closure run_loop_quit_closure, |
|
Wez
2015/02/19 22:00:23
Suggest calling this done_closure, since in princi
joedow
2015/02/20 02:58:35
Done.
|
| + const std::string& access_token, |
| + const std::string& refresh_token) { |
| + access_token_retrieved = access_token; |
| + refresh_token_retrieved = refresh_token; |
| + |
| + run_loop_quit_closure.Run(); |
| + } |
| + |
| + protected: |
| + // Test interface. |
| + void SetUp() override { |
| + if (!base::MessageLoop::current()) { |
| + // Create a temporary message loop if the current thread does not already |
| + // have one so we can use its task runner to create a request object. |
| + message_loop_.reset(new base::MessageLoopForIO); |
| + } |
| + } |
| + |
| + void SetFakeResponse(const GURL& url, |
| + const std::string& data, |
| + net::HttpStatusCode code, |
| + net::URLRequestStatus::Status status) { |
| + url_fetcher_factory_.SetFakeResponse(url, data, code, status); |
| + } |
| + |
| + // Used for result verification |
| + std::string access_token_retrieved; |
|
Wez
2015/02/19 22:00:23
Members must end in _
joedow
2015/02/20 02:58:35
Done.
|
| + std::string refresh_token_retrieved; |
| + |
| + private: |
| + net::FakeURLFetcherFactory url_fetcher_factory_; |
| + scoped_ptr<base::MessageLoopForIO> message_loop_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherTest); |
| +}; |
| + |
| +TEST_F(AccessTokenFetcherTest, ExchangeAuthCodeForAccessToken) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kAuthCodeExchangeValidResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_info_url(), |
| + kValidTokenInfoResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + base::RunLoop network_request_run_loop; |
|
Wez
2015/02/19 22:00:23
Why not just |run_loop|?
joedow
2015/02/20 02:58:35
Done.
|
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromAuthCode( |
| + kAuthCodeValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + EXPECT_STREQ(kAccessTokenValue, access_token_retrieved.c_str()); |
| + EXPECT_STREQ(kRefreshTokenValue, refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, ExchangeRefreshTokenForAccessToken) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kRefreshTokenExchangeValidResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_info_url(), |
| + kValidTokenInfoResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + base::RunLoop network_request_run_loop; |
|
Wez
2015/02/19 22:00:23
As above
joedow
2015/02/20 02:58:35
Done.
|
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromRefreshToken( |
| + kRefreshTokenValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + EXPECT_STREQ(kAccessTokenValue, access_token_retrieved.c_str()); |
| + EXPECT_STREQ(kRefreshTokenValue, refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, MultipleAccessTokenCalls) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kAuthCodeExchangeValidResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_info_url(), |
| + kValidTokenInfoResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + scoped_ptr<base::RunLoop> network_request_run_loop; |
|
Wez
2015/02/19 22:00:23
As above
joedow
2015/02/20 02:58:35
Done.
|
| + network_request_run_loop.reset(new base::RunLoop()); |
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop->QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromAuthCode( |
| + kAuthCodeValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop->Run(); |
| + |
| + EXPECT_STREQ(kAccessTokenValue, access_token_retrieved.c_str()); |
| + EXPECT_STREQ(kRefreshTokenValue, refresh_token_retrieved.c_str()); |
| + |
| + // Reset our token data for the next iteration. |
| + access_token_retrieved.clear(); |
| + refresh_token_retrieved.clear(); |
| + |
| + // Update the response since we will call the refresh token method next. |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kRefreshTokenExchangeValidResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + network_request_run_loop.reset(new base::RunLoop()); |
| + access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop->QuitClosure()); |
| + |
| + access_token_fetcher.GetAccessTokenFromRefreshToken( |
| + kRefreshTokenValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop->Run(); |
| + |
| + EXPECT_STREQ(kAccessTokenValue, |
| + access_token_retrieved.c_str()); |
| + EXPECT_STREQ(kRefreshTokenValue, |
| + refresh_token_retrieved.c_str()); |
| + |
| + network_request_run_loop.reset(new base::RunLoop()); |
| + access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop->QuitClosure()); |
| + |
| + // Reset our token data for the next iteration. |
| + access_token_retrieved.clear(); |
| + refresh_token_retrieved.clear(); |
| + |
| + access_token_fetcher.GetAccessTokenFromRefreshToken( |
| + kRefreshTokenValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop->Run(); |
| + |
| + EXPECT_STREQ(kAccessTokenValue, |
| + access_token_retrieved.c_str()); |
| + EXPECT_STREQ(kRefreshTokenValue, |
| + refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, ExchangeAuthCode_Unauthorized_Error) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kAuthCodeExchangeValidResponse, |
| + net::HTTP_UNAUTHORIZED, |
| + net::URLRequestStatus::FAILED); |
| + |
| + base::RunLoop network_request_run_loop; |
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromAuthCode( |
| + kAuthCodeValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + EXPECT_STREQ("", access_token_retrieved.c_str()); |
| + EXPECT_STREQ("", refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, ExchangeRefreshToken_Unauthorized_Error) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kRefreshTokenExchangeValidResponse, |
| + net::HTTP_UNAUTHORIZED, |
| + net::URLRequestStatus::FAILED); |
| + |
| + base::RunLoop network_request_run_loop; |
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromRefreshToken( |
| + kRefreshTokenValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + EXPECT_STREQ("", access_token_retrieved.c_str()); |
| + EXPECT_STREQ("", refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, ExchangeAuthCode_NetworkError) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kAuthCodeExchangeValidResponse, |
| + net::HTTP_NOT_FOUND, |
| + net::URLRequestStatus::FAILED); |
| + |
| + base::RunLoop network_request_run_loop; |
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromAuthCode( |
| + kAuthCodeValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + EXPECT_STREQ("", access_token_retrieved.c_str()); |
| + EXPECT_STREQ("", refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, ExchangeRefreshToken_NetworkError) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kRefreshTokenExchangeValidResponse, |
| + net::HTTP_NOT_FOUND, |
| + net::URLRequestStatus::FAILED); |
| + |
| + base::RunLoop network_request_run_loop; |
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromRefreshToken( |
| + kRefreshTokenValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + EXPECT_STREQ("", access_token_retrieved.c_str()); |
| + EXPECT_STREQ("", refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, AuthCode_GetTokenInfoResponse_InvalidToken) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kAuthCodeExchangeValidResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_info_url(), |
| + kInvalidTokenInfoResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + base::RunLoop network_request_run_loop; |
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromAuthCode( |
| + kAuthCodeValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + // Our callback should have been called with empty strings. |
| + EXPECT_STREQ("", access_token_retrieved.c_str()); |
| + EXPECT_STREQ("", refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, ExchangeAuthCodeForAccessToken_EmptyToken) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kAuthCodeExchangeEmptyResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + base::RunLoop network_request_run_loop; |
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromAuthCode( |
| + kAuthCodeValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + // Our callback should have been called with empty strings. |
| + EXPECT_STREQ("", access_token_retrieved.c_str()); |
| + EXPECT_STREQ("", refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, RefreshToken_GetTokenInfoResponse_InvalidToken) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kRefreshTokenExchangeValidResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_info_url(), |
| + kInvalidTokenInfoResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + base::RunLoop network_request_run_loop; |
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromRefreshToken( |
| + kRefreshTokenValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + // Our callback should have been called with empty strings. |
| + EXPECT_STREQ("", access_token_retrieved.c_str()); |
| + EXPECT_STREQ("", refresh_token_retrieved.c_str()); |
| +} |
| + |
| +TEST_F(AccessTokenFetcherTest, ExchangeRefreshTokenForAccessToken_EmptyToken) { |
| + SetFakeResponse( |
| + GaiaUrls::GetInstance()->oauth2_token_url(), |
| + kRefreshTokenExchangeEmptyResponse, |
| + net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + |
| + base::RunLoop network_request_run_loop; |
| + AccessTokenCallback access_token_callback = |
| + base::Bind(&AccessTokenFetcherTest::OnAccessTokenRetrieved, |
| + base::Unretained(this), |
| + network_request_run_loop.QuitClosure()); |
| + |
| + AccessTokenFetcher access_token_fetcher; |
| + access_token_fetcher.GetAccessTokenFromRefreshToken( |
| + kRefreshTokenValue, |
| + access_token_callback); |
| + |
| + network_request_run_loop.Run(); |
| + |
| + // Our callback should have been called with empty strings. |
| + EXPECT_STREQ("", access_token_retrieved.c_str()); |
| + EXPECT_STREQ("", refresh_token_retrieved.c_str()); |
| +} |
| + |
| +} // namespace test |
| +} // namespace remoting |