oauth_http_fetcher_unittest.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2022 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.
  4. #include "ash/quick_pair/repository/oauth_http_fetcher.h"
  5. #include "ash/quick_pair/common/mock_quick_pair_browser_delegate.h"
  6. #include "base/memory/scoped_refptr.h"
  7. #include "base/test/task_environment.h"
  8. #include "components/signin/public/identity_manager/identity_test_environment.h"
  9. #include "components/signin/public/identity_manager/identity_test_utils.h"
  10. #include "net/http/http_util.h"
  11. #include "net/traffic_annotation/network_traffic_annotation.h"
  12. #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
  13. #include "services/network/test/test_url_loader_factory.h"
  14. #include "testing/gmock/include/gmock/gmock.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace {
  17. constexpr char kBody[] = "body";
  18. constexpr char kTestUrl[] = "http://www.test.com/";
  19. constexpr char kTestScope[] = "http://www.test.com/scope";
  20. const net::PartialNetworkTrafficAnnotationTag kTrafficAnnotation =
  21. net::DefinePartialNetworkTrafficAnnotation("test_request",
  22. "oauth2_api_call_flow",
  23. R"(
  24. semantics {
  25. sender: "Test Request"
  26. description:
  27. "Test request."
  28. trigger:
  29. "Test request."
  30. data: "Test Request."
  31. destination: GOOGLE_OWNED_SERVICE
  32. }
  33. policy {
  34. cookies_allowed: NO
  35. setting:
  36. "Test Request."
  37. })");
  38. } // namespace
  39. namespace ash {
  40. namespace quick_pair {
  41. class OAuthHttpFetcherTest : public testing::Test {
  42. public:
  43. OAuthHttpFetcherTest() : identity_test_env_(&url_loader_factory_) {
  44. identity_test_env_.MakePrimaryAccountAvailable("1@mail.com",
  45. signin::ConsentLevel::kSync);
  46. }
  47. void SetUp() override {
  48. http_fetcher_ =
  49. std::make_unique<OAuthHttpFetcher>(kTrafficAnnotation, kTestScope);
  50. browser_delegate_ = std::make_unique<MockQuickPairBrowserDelegate>();
  51. ON_CALL(*browser_delegate_, GetURLLoaderFactory())
  52. .WillByDefault(
  53. testing::Return(url_loader_factory_.GetSafeWeakWrapper()));
  54. ON_CALL(*browser_delegate_, GetIdentityManager())
  55. .WillByDefault(testing::Return(identity_test_env_.identity_manager()));
  56. identity_test_env_.SetAutomaticIssueOfAccessTokens(true);
  57. }
  58. void TearDown() override { url_loader_factory_.ClearResponses(); }
  59. protected:
  60. base::test::TaskEnvironment task_environment_;
  61. std::unique_ptr<OAuthHttpFetcher> http_fetcher_;
  62. std::unique_ptr<MockQuickPairBrowserDelegate> browser_delegate_;
  63. network::TestURLLoaderFactory url_loader_factory_;
  64. signin::IdentityTestEnvironment identity_test_env_;
  65. };
  66. TEST_F(OAuthHttpFetcherTest, ExecuteGetRequest_Success) {
  67. GURL url(kTestUrl);
  68. std::string body(kBody);
  69. auto head = network::mojom::URLResponseHead::New();
  70. head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
  71. net::HttpUtil::AssembleRawHeaders(""));
  72. head->headers->GetMimeType(&head->mime_type);
  73. network::URLLoaderCompletionStatus status(net::Error::OK);
  74. status.decoded_body_length = body.size();
  75. url_loader_factory_.AddResponse(url, std::move(head), body, status);
  76. http_fetcher_->ExecuteGetRequest(
  77. url, base::BindOnce([](std::unique_ptr<std::string> response,
  78. std::unique_ptr<FastPairHttpResult> result) {
  79. ASSERT_EQ(kBody, *response);
  80. ASSERT_TRUE(result->IsSuccess());
  81. }));
  82. task_environment_.RunUntilIdle();
  83. }
  84. TEST_F(OAuthHttpFetcherTest, ExecuteGetRequest_Failure) {
  85. url_loader_factory_.AddResponse(kTestUrl, "",
  86. net::HTTP_INTERNAL_SERVER_ERROR);
  87. http_fetcher_->ExecuteGetRequest(
  88. GURL(kTestUrl),
  89. base::BindOnce([](std::unique_ptr<std::string> response,
  90. std::unique_ptr<FastPairHttpResult> result) {
  91. ASSERT_EQ(nullptr, response);
  92. ASSERT_FALSE(result->IsSuccess());
  93. ASSERT_EQ(result->http_response_error(),
  94. net::HTTP_INTERNAL_SERVER_ERROR);
  95. }));
  96. task_environment_.RunUntilIdle();
  97. }
  98. TEST_F(OAuthHttpFetcherTest, ExecuteGetRequest_MultipleCalls) {
  99. url_loader_factory_.AddResponse(kTestUrl, "",
  100. net::HTTP_INTERNAL_SERVER_ERROR);
  101. http_fetcher_->ExecuteGetRequest(
  102. GURL(kTestUrl),
  103. base::BindOnce([](std::unique_ptr<std::string> response,
  104. std::unique_ptr<FastPairHttpResult> result) {
  105. ASSERT_EQ(nullptr, response);
  106. ASSERT_FALSE(result->IsSuccess());
  107. ASSERT_EQ(result->http_response_error(),
  108. net::HTTP_INTERNAL_SERVER_ERROR);
  109. }));
  110. #if DCHECK_IS_ON()
  111. EXPECT_DEATH(
  112. http_fetcher_->ExecuteGetRequest(GURL(kTestUrl), base::DoNothing()), "");
  113. #else
  114. http_fetcher_->ExecuteGetRequest(
  115. GURL(kTestUrl),
  116. base::BindOnce(
  117. [](std::unique_ptr<std::string> response,
  118. std::unique_ptr<FastPairHttpResult> result) { FAIL(); }));
  119. #endif
  120. task_environment_.RunUntilIdle();
  121. }
  122. TEST_F(OAuthHttpFetcherTest, ExecuteGetRequest_NoToken) {
  123. identity_test_env_.SetAutomaticIssueOfAccessTokens(false);
  124. url_loader_factory_.AddResponse(kTestUrl, "",
  125. net::HTTP_INTERNAL_SERVER_ERROR);
  126. http_fetcher_->ExecuteGetRequest(
  127. GURL(kTestUrl),
  128. base::BindOnce([](std::unique_ptr<std::string> response,
  129. std::unique_ptr<FastPairHttpResult> result) {
  130. ASSERT_EQ(nullptr, response);
  131. ASSERT_EQ(nullptr, result);
  132. }));
  133. task_environment_.RunUntilIdle();
  134. }
  135. TEST_F(OAuthHttpFetcherTest, ExecuteGetRequest_NoUrlFactory) {
  136. ON_CALL(*browser_delegate_, GetURLLoaderFactory())
  137. .WillByDefault(testing::Return(nullptr));
  138. url_loader_factory_.AddResponse(kTestUrl, "",
  139. net::HTTP_INTERNAL_SERVER_ERROR);
  140. http_fetcher_->ExecuteGetRequest(
  141. GURL(kTestUrl),
  142. base::BindOnce([](std::unique_ptr<std::string> response,
  143. std::unique_ptr<FastPairHttpResult> result) {
  144. ASSERT_EQ(nullptr, response);
  145. ASSERT_EQ(nullptr, result);
  146. }));
  147. task_environment_.RunUntilIdle();
  148. }
  149. TEST_F(OAuthHttpFetcherTest, ExecuteGetRequest_NoIdentityManager) {
  150. ON_CALL(*browser_delegate_, GetIdentityManager())
  151. .WillByDefault(testing::Return(nullptr));
  152. #if DCHECK_IS_ON()
  153. EXPECT_DEATH(
  154. http_fetcher_->ExecuteGetRequest(GURL(kTestUrl), base::DoNothing()), "");
  155. #else
  156. http_fetcher_->ExecuteGetRequest(
  157. GURL(kTestUrl),
  158. base::BindOnce(
  159. [](std::unique_ptr<std::string> response,
  160. std::unique_ptr<FastPairHttpResult> result) { FAIL(); }));
  161. #endif
  162. task_environment_.RunUntilIdle();
  163. }
  164. TEST_F(OAuthHttpFetcherTest, ExecuteGetRequest_MultipleRaceCondition) {
  165. http_fetcher_->ExecuteGetRequest(GURL(kTestUrl), base::DoNothing());
  166. #if DCHECK_IS_ON()
  167. EXPECT_DEATH(
  168. http_fetcher_->ExecuteGetRequest(GURL(kTestUrl), base::DoNothing()), "");
  169. #else
  170. http_fetcher_->ExecuteGetRequest(
  171. GURL(kTestUrl),
  172. base::BindOnce(
  173. [](std::unique_ptr<std::string> response,
  174. std::unique_ptr<FastPairHttpResult> result) { FAIL(); }));
  175. #endif
  176. task_environment_.RunUntilIdle();
  177. }
  178. TEST_F(OAuthHttpFetcherTest, ExecutePostRequest_Success) {
  179. GURL url(kTestUrl);
  180. std::string body(kBody);
  181. auto head = network::mojom::URLResponseHead::New();
  182. head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
  183. net::HttpUtil::AssembleRawHeaders(""));
  184. head->headers->GetMimeType(&head->mime_type);
  185. network::URLLoaderCompletionStatus status(net::Error::OK);
  186. status.decoded_body_length = body.size();
  187. url_loader_factory_.AddResponse(url, std::move(head), body, status);
  188. http_fetcher_->ExecutePostRequest(
  189. url, kBody,
  190. base::BindOnce([](std::unique_ptr<std::string> response,
  191. std::unique_ptr<FastPairHttpResult> result) {
  192. ASSERT_TRUE(result->IsSuccess());
  193. }));
  194. task_environment_.RunUntilIdle();
  195. }
  196. TEST_F(OAuthHttpFetcherTest, ExecuteDeleteRequest_Success) {
  197. GURL url(kTestUrl);
  198. std::string body(kBody);
  199. auto head = network::mojom::URLResponseHead::New();
  200. head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
  201. net::HttpUtil::AssembleRawHeaders(""));
  202. head->headers->GetMimeType(&head->mime_type);
  203. network::URLLoaderCompletionStatus status(net::Error::OK);
  204. status.decoded_body_length = body.size();
  205. url_loader_factory_.AddResponse(url, std::move(head), body, status);
  206. http_fetcher_->ExecuteDeleteRequest(
  207. url, base::BindOnce([](std::unique_ptr<std::string> response,
  208. std::unique_ptr<FastPairHttpResult> result) {
  209. ASSERT_TRUE(result->IsSuccess());
  210. }));
  211. task_environment_.RunUntilIdle();
  212. }
  213. } // namespace quick_pair
  214. } // namespace ash