File: plus_address_http_client_impl_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (877 lines) | stat: -rw-r--r-- 37,836 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/plus_addresses/plus_address_http_client_impl.h"

#include <optional>
#include <string>

#include "base/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/strings/string_util.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "components/plus_addresses/features.h"
#include "components/plus_addresses/metrics/plus_address_metrics.h"
#include "components/plus_addresses/plus_address_http_client.h"
#include "components/plus_addresses/plus_address_http_client_impl_test_api.h"
#include "components/plus_addresses/plus_address_test_utils.h"
#include "components/plus_addresses/plus_address_types.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/access_token_info.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/scope_set.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/base/net_errors.h"
#include "net/http/http_status_code.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace plus_addresses {
namespace {

using ::testing::SizeIs;

constexpr char kServerBaseUrl[] = "https://enterprise.foo/";
constexpr char kTestScope[] = "scope";
constexpr char kEmailAddress[] = "foo@plus.plus";

constexpr base::TimeDelta kLatency = base::Milliseconds(2400);

constexpr char kPlusAddressOauthErrorHistogram[] =
    "PlusAddresses.NetworkRequest.OauthError";

std::string LatencyHistogramFor(PlusAddressNetworkRequestType type) {
  return base::ReplaceStringPlaceholders(
      "PlusAddresses.NetworkRequest.$1.Latency",
      {metrics::PlusAddressNetworkRequestTypeToString(type)},
      /*offsets=*/nullptr);
}

std::string NetErrorCodeHistogramFor(PlusAddressNetworkRequestType type) {
  return base::ReplaceStringPlaceholders(
      "PlusAddresses.NetworkRequest.$1.NetErrorCode",
      {metrics::PlusAddressNetworkRequestTypeToString(type)},
      /*offsets=*/nullptr);
}

std::string ResponseCodeHistogramFor(PlusAddressNetworkRequestType type) {
  return base::ReplaceStringPlaceholders(
      "PlusAddresses.NetworkRequest.$1.ResponseCode",
      {metrics::PlusAddressNetworkRequestTypeToString(type)},
      /*offsets=*/nullptr);
}

std::string ResponseByteSizeHistogramFor(PlusAddressNetworkRequestType type) {
  return base::ReplaceStringPlaceholders(
      "PlusAddresses.NetworkRequest.$1.ResponseByteSize",
      {metrics::PlusAddressNetworkRequestTypeToString(type)},
      /*offsets=*/nullptr);
}

// Tests that use fake out the URL loading and issues requests to the enterprise
// provided server.
class PlusAddressHttpClientRequests : public ::testing::Test {
 public:
  PlusAddressHttpClientRequests()
      : test_shared_loader_factory_(
            base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
                &test_url_loader_factory_)) {
    features_.InitAndEnableFeatureWithParameters(
        features::kPlusAddressesEnabled,
        {{features::kEnterprisePlusAddressServerUrl.name, kServerBaseUrl},
         {features::kEnterprisePlusAddressOAuthScope.name, kTestScope}});
    identity_test_env_.MakePrimaryAccountAvailable(
        kEmailAddress, signin::ConsentLevel::kSignin);
    test_url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
        [&](const network::ResourceRequest& request) {
          last_request_ = request;
        }));
    InitClient();
  }

 protected:
  // Runtime constants:
  const std::string kFullProfileEndpoint =
      base::StrCat({kServerBaseUrl, kServerPlusProfileEndpoint});
  const std::string kFullReserveEndpoint =
      base::StrCat({kServerBaseUrl, kServerReservePlusAddressEndpoint});
  const std::string kFullCreateEndpoint =
      base::StrCat({kServerBaseUrl, kServerCreatePlusAddressEndpoint});
  const std::string kFullPreallocateEndpoint =
      base::StrCat({kServerBaseUrl, kServerPreallocatePlusAddressEndpoint});
  // This is a `std::string` to allow easier concatenation via `operator+`.
  const std::string kToken = "myToken";

  void FastForwardBy(base::TimeDelta delta) {
    task_environment_.FastForwardBy(delta);
  }

  void InitClient() {
    client_.emplace(identity_manager(), shared_loader_factory());
  }

  PlusAddressHttpClientImpl& client() { return *client_; }
  signin::IdentityTestEnvironment& identity_env() { return identity_test_env_; }
  signin::IdentityManager* identity_manager() {
    return identity_test_env_.identity_manager();
  }
  const network::ResourceRequest& last_request() const { return last_request_; }
  const scoped_refptr<network::SharedURLLoaderFactory>&
  shared_loader_factory() {
    return test_shared_loader_factory_;
  }
  network::TestURLLoaderFactory& url_loader_factory() {
    return test_url_loader_factory_;
  }

 private:
  base::test::ScopedFeatureList features_;
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  signin::IdentityTestEnvironment identity_test_env_;
  scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
  network::TestURLLoaderFactory test_url_loader_factory_;
  data_decoder::test::InProcessDataDecoder decoder_;

  // The last network request made.
  network::ResourceRequest last_request_;

  // `client_` is wrapped in an optional to defer creation until after features
  // are initialized. After fixture creation, this is never empty.
  std::optional<PlusAddressHttpClientImpl> client_;
};

// Ensures the request sent by Chrome matches what we intended.
TEST_F(PlusAddressHttpClientRequests, ReservePlusAddress_IssuesCorrectRequest) {
  const url::Origin origin = url::Origin::Create(GURL("https://foobar.com"));
  std::string facet = origin.Serialize();
  client().ReservePlusAddress(origin, /*refresh=*/false, base::DoNothing());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  // Validate that the V1 Create request uses the right url and requests method.
  EXPECT_EQ(last_request().url, kFullReserveEndpoint);
  EXPECT_EQ(last_request().method, net::HttpRequestHeaders::kPutMethod);
  // Validate the Authorization header includes "myToken".
  EXPECT_EQ(
      last_request().headers.GetHeader("Authorization").value_or(std::string()),
      "Bearer " + kToken);

  // Validate the request payload.
  ASSERT_NE(last_request().request_body, nullptr);
  ASSERT_EQ(last_request().request_body->elements()->size(), 1u);
  std::optional<base::Value> body =
      base::JSONReader::Read(last_request()
                                 .request_body->elements()
                                 ->at(0)
                                 .As<network::DataElementBytes>()
                                 .AsStringPiece());
  ASSERT_TRUE(body.has_value() && body->is_dict());
  std::string* facet_entry = body->GetDict().FindString("facet");
  ASSERT_NE(facet_entry, nullptr);
  EXPECT_EQ(*facet_entry, facet);
}

// Tests that the reserve request contains the expected data when the refresh
// flag is set.
TEST_F(PlusAddressHttpClientRequests,
       ReservePlusAddress_IssuesCorrectRequestWithRefreshFlag) {
  const url::Origin origin = url::Origin::Create(GURL("https://foobar.com"));
  std::string facet = origin.Serialize();
  client().ReservePlusAddress(origin, /*refresh=*/true, base::DoNothing());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  // Validate that the V1 Create request uses the right url and requests method.
  EXPECT_EQ(last_request().url, kFullReserveEndpoint);
  EXPECT_EQ(last_request().method, net::HttpRequestHeaders::kPutMethod);
  // Validate the Authorization header includes "myToken".
  EXPECT_EQ(
      last_request().headers.GetHeader("Authorization").value_or(std::string()),
      "Bearer " + kToken);

  // Validate the request payload.
  ASSERT_NE(last_request().request_body, nullptr);
  ASSERT_THAT(*last_request().request_body->elements(), SizeIs(1));
  std::optional<base::Value> body =
      base::JSONReader::Read(last_request()
                                 .request_body->elements()
                                 ->at(0)
                                 .As<network::DataElementBytes>()
                                 .AsStringPiece());
  ASSERT_TRUE(body.has_value() && body->is_dict());
  std::string* facet_entry = body->GetDict().FindString("facet");
  ASSERT_NE(facet_entry, nullptr);
  EXPECT_EQ(*facet_entry, facet);
  EXPECT_TRUE(
      body->GetDict().FindBool("refresh_email_address").value_or(false));
}

// Ensures the request sent by Chrome matches what we intended.
TEST_F(PlusAddressHttpClientRequests, ConfirmPlusAddress_IssuesCorrectRequest) {
  const url::Origin origin = url::Origin::Create(GURL("https://foobar.com"));
  std::string facet = origin.Serialize();
  client().ConfirmPlusAddress(origin, PlusAddress("plus@plus.plus"),
                              base::DoNothing());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  // Validate that the V1 Create request uses the right url and requests method.
  EXPECT_EQ(last_request().url, kFullCreateEndpoint);
  EXPECT_EQ(last_request().method, net::HttpRequestHeaders::kPutMethod);
  // Validate the Authorization header includes "myToken".
  EXPECT_EQ(
      last_request().headers.GetHeader("Authorization").value_or(std::string()),
      "Bearer " + kToken);

  // Validate the request payload.
  ASSERT_NE(last_request().request_body, nullptr);
  ASSERT_EQ(last_request().request_body->elements()->size(), 1u);
  std::optional<base::Value> body =
      base::JSONReader::Read(last_request()
                                 .request_body->elements()
                                 ->at(0)
                                 .As<network::DataElementBytes>()
                                 .AsStringPiece());
  ASSERT_TRUE(body.has_value() && body->is_dict());
  std::string* facet_entry = body->GetDict().FindString("facet");
  ASSERT_NE(facet_entry, nullptr);
  EXPECT_EQ(*facet_entry, facet);
}

// Tests the behavior of the PlusAddressCreationRequests (Reserve+Confirm) which
// have identical expectations outside of the method signature.
class PlusAddressCreationRequests
    : public PlusAddressHttpClientRequests,
      public testing::WithParamInterface<PlusAddressNetworkRequestType> {
 public:
  PlusAddressCreationRequests() = default;

 protected:
  std::string Endpoint() {
    if (GetParam() == PlusAddressNetworkRequestType::kReserve) {
      return kFullReserveEndpoint;
    }
    if (GetParam() == PlusAddressNetworkRequestType::kCreate) {
      return kFullCreateEndpoint;
    }
    NOTREACHED();
  }
  void MakeCreationRequest(const PlusProfile& profile,
                           PlusAddressRequestCallback callback) {
    url::Origin origin =
        url::Origin::Create(GURL(profile.facet.canonical_spec()));
    if (GetParam() == PlusAddressNetworkRequestType::kReserve) {
      client().ReservePlusAddress(origin, /*refresh=*/false,
                                  std::move(callback));
    } else if (GetParam() == PlusAddressNetworkRequestType::kCreate) {
      client().ConfirmPlusAddress(origin, profile.plus_address,
                                  std::move(callback));
    } else {
      NOTREACHED();
    }
  }
  std::string LatencyHistogram() { return LatencyHistogramFor(GetParam()); }
  std::string NetErrorCodeHistogram() {
    return NetErrorCodeHistogramFor(GetParam());
  }
  std::string ResponseCodeHistogram() {
    return ResponseCodeHistogramFor(GetParam());
  }
  std::string ResponseByteSizeHistogram() {
    return ResponseByteSizeHistogramFor(GetParam());
  }
};

// Verifies ability to support making multiple requests at once.
// Note: Create() is not idempotent, but that is ignored for this test.
TEST_P(PlusAddressCreationRequests, HandlesConcurrentRequests) {
  const PlusProfile profile = test::CreatePlusProfile();
  base::test::TestFuture<const PlusProfileOrError&> first_request;
  base::test::TestFuture<const PlusProfileOrError&> second_request;

  // Send two requests in quick succession.
  MakeCreationRequest(profile, first_request.GetCallback());
  MakeCreationRequest(profile, second_request.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  // The first callback should be run once the server responds to its request.
  url_loader_factory().SimulateResponseForPendingRequest(
      Endpoint(), test::MakeCreationResponse(profile));
  EXPECT_TRUE(first_request.Wait());

  // Same for the second callback.
  url_loader_factory().SimulateResponseForPendingRequest(
      Endpoint(), test::MakeCreationResponse(profile));
  EXPECT_TRUE(second_request.Wait());
}

TEST_P(PlusAddressCreationRequests, RequestsOauthToken) {
  const PlusProfile profile = test::CreatePlusProfile();
  // Make a request when the PlusAddressHttpClient has an expired OAuth token.
  base::test::TestFuture<const PlusProfileOrError&> future;
  MakeCreationRequest(profile, future.GetCallback());
  ASSERT_FALSE(future.IsReady());
  ASSERT_TRUE(identity_env().IsAccessTokenRequestPending());

  // Verify that ConfirmPlusAddress hasn't already sent the network request.
  ASSERT_EQ(url_loader_factory().NumPending(), 0);

  // ConfirmPlusAddress will run `callback` after an OAuth token is retrieved.
  identity_env()
      .WaitForAccessTokenRequestIfNecessaryAndRespondWithTokenForScopes(
          "token", base::Time::Max(), "id", {kTestScope});

  // Unblock the pending request.
  ASSERT_EQ(url_loader_factory().NumPending(), 1);
  ASSERT_FALSE(future.IsReady());
  url_loader_factory().SimulateResponseForPendingRequest(
      Endpoint(), test::MakeCreationResponse(profile));
  EXPECT_TRUE(future.Wait());
  EXPECT_EQ(future.Get()->plus_address, profile.plus_address);
}

TEST_P(PlusAddressCreationRequests, RunCallbackOnSuccess) {
  const PlusProfile profile = test::CreatePlusProfile();
  base::HistogramTester histogram_tester;
  base::test::TestFuture<const PlusProfileOrError&> future;
  MakeCreationRequest(profile, future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  // Fulfill the request and the appropriate callback should be run.
  FastForwardBy(kLatency);
  const std::string json = test::MakeCreationResponse(profile);
  url_loader_factory().SimulateResponseForPendingRequest(Endpoint(), json);

  ASSERT_TRUE(future.Wait());
  EXPECT_TRUE(future.Get().has_value());
  EXPECT_EQ(future.Get()->plus_address, profile.plus_address);

  // Verify expected metrics.
  histogram_tester.ExpectUniqueTimeSample(LatencyHistogram(), kLatency, 1);
  histogram_tester.ExpectUniqueSample(NetErrorCodeHistogram(), net::OK, 1);
  histogram_tester.ExpectUniqueSample(ResponseCodeHistogram(), 200, 1);
  histogram_tester.ExpectUniqueSample(ResponseByteSizeHistogram(), json.size(),
                                      1);
}

// Tests that if the client does not receive a server response within
// `kPlusAddressRequestTimeout`, then the request fails with a
// `PlusAddressRequestErrorType::kClientTimeout` error.
TEST_P(PlusAddressCreationRequests, ClientTimeout) {
  const PlusProfile profile = test::CreatePlusProfile();
  base::HistogramTester histogram_tester;
  base::test::TestFuture<const PlusProfileOrError&> future;
  MakeCreationRequest(profile, future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  FastForwardBy(features::kPlusAddressRequestTimeout.Get() + base::Seconds(1));

  ASSERT_TRUE(future.Wait());
  const PlusProfileOrError& response = future.Get();
  ASSERT_FALSE(response.has_value());
  EXPECT_EQ(response.error().type(),
            PlusAddressRequestErrorType::kClientTimeout);
  EXPECT_TRUE(response.error().IsTimeoutError());

  // Verify expected metrics.
  histogram_tester.ExpectTotalCount(LatencyHistogram(), 1);
  histogram_tester.ExpectUniqueSample(NetErrorCodeHistogram(),
                                      net::ERR_TIMED_OUT, 1);
  histogram_tester.ExpectTotalCount(ResponseCodeHistogram(), 0);
  histogram_tester.ExpectTotalCount(ResponseByteSizeHistogram(), 0);
}

// Tests that calls to the `Reserve` and `Create` endpoints run the callback
// with an error if the network request experienced an error. Also checks that
// the error includes the HTTP response code.
TEST_P(PlusAddressCreationRequests, RunCallbackOnNetworkError) {
  base::HistogramTester histogram_tester;
  base::test::TestFuture<const PlusProfileOrError&> future;
  MakeCreationRequest(test::CreatePlusProfile(), future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  FastForwardBy(kLatency);
  //  TODO (kaklilu): Checks behavior when response body isn't null.
  EXPECT_TRUE(url_loader_factory().SimulateResponseForPendingRequest(
      Endpoint(), "", net::HTTP_NOT_FOUND));

  // The request fails and the appropriate callback is run.
  ASSERT_TRUE(future.Wait());
  EXPECT_FALSE(future.Get().has_value());
  EXPECT_EQ(future.Get().error(),
            PlusAddressRequestError::AsNetworkError(net::HTTP_NOT_FOUND));

  // Verify expected metrics.
  histogram_tester.ExpectUniqueTimeSample(LatencyHistogram(), kLatency, 1);
  histogram_tester.ExpectUniqueSample(ResponseCodeHistogram(),
                                      net::HTTP_NOT_FOUND, 1);
  histogram_tester.ExpectTotalCount(ResponseByteSizeHistogram(), 0);
}

TEST_P(PlusAddressCreationRequests, RunCallbackOnClientError) {
  base::HistogramTester histogram_tester;
  base::test::TestFuture<const PlusProfileOrError&> future;
  MakeCreationRequest(test::CreatePlusProfile(), future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  // Return a response missing all of the expected fields.
  FastForwardBy(kLatency);
  const std::string json = "{}";
  url_loader_factory().SimulateResponseForPendingRequest(Endpoint(), json);

  // The request fails and the appropriate callback is run.
  ASSERT_TRUE(future.Wait());
  EXPECT_FALSE(future.Get().has_value());
  EXPECT_EQ(future.Get().error().type(),
            PlusAddressRequestErrorType::kParsingError);

  // Verify expected metrics.
  histogram_tester.ExpectUniqueTimeSample(LatencyHistogram(), kLatency, 1);
  histogram_tester.ExpectUniqueSample(ResponseCodeHistogram(), net::HTTP_OK, 1);
  histogram_tester.ExpectUniqueSample(ResponseByteSizeHistogram(), json.size(),
                                      1);
}

TEST_P(PlusAddressCreationRequests, RunCallbackOnOauthError) {
  base::HistogramTester histogram_tester;
  base::test::TestFuture<const PlusProfileOrError&> future;
  MakeCreationRequest(test::CreatePlusProfile(), future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
      GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));

  // Verify that no network requests are made.
  EXPECT_EQ(url_loader_factory().NumPending(), 0);

  // The callback is still run with an OAuth error.
  ASSERT_TRUE(future.Wait());
  EXPECT_FALSE(future.Get().has_value());
  EXPECT_EQ(future.Get().error().type(),
            PlusAddressRequestErrorType::kOAuthError);

  // Verify expected metrics.
  EXPECT_THAT(histogram_tester.GetAllSamples(kPlusAddressOauthErrorHistogram),
              BucketsAre(base::Bucket(
                  GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS, 1)));
  histogram_tester.ExpectTotalCount(LatencyHistogram(), 0);
  histogram_tester.ExpectTotalCount(ResponseCodeHistogram(), 0);
  histogram_tester.ExpectTotalCount(ResponseByteSizeHistogram(), 0);
}
INSTANTIATE_TEST_SUITE_P(
    All,
    PlusAddressCreationRequests,
    ::testing::Values(PlusAddressNetworkRequestType::kReserve,
                      PlusAddressNetworkRequestType::kCreate),
    [](const testing::TestParamInfo<PlusAddressCreationRequests::ParamType>&
           info) {
      return metrics::PlusAddressNetworkRequestTypeToString(info.param);
    });

TEST_F(PlusAddressHttpClientRequests,
       PreallocatePlusAddresses_IssuesCorrectRequest) {
  client().PreallocatePlusAddresses(base::DoNothing());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  EXPECT_EQ(last_request().url, kFullPreallocateEndpoint);
  EXPECT_EQ(last_request().method, net::HttpRequestHeaders::kPostMethod);
  EXPECT_EQ(
      last_request().headers.GetHeader("Authorization").value_or(std::string()),
      "Bearer " + kToken);

  // Validate the request payload.
  ASSERT_EQ(last_request().request_body, nullptr);
}

// Tests that a successful server response from the preallocation endpoint is
// received, parsed, and triggers the callback properly.
TEST_F(PlusAddressHttpClientRequests,
       PreallocatePlusAddresses_SuccessResponse) {
  base::HistogramTester histogram_tester;
  base::test::TestFuture<PlusAddressHttpClient::PreallocatePlusAddressesResult>
      future;
  client().PreallocatePlusAddresses(future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  const auto address1 = PreallocatedPlusAddress(PlusAddress("plus1@plus.com"),
                                                /*lifetime=*/base::Days(10));
  const auto address2 = PreallocatedPlusAddress(PlusAddress("plus2@foo.com"),
                                                /*lifetime=*/base::Days(50));

  const std::string json = test::MakePreallocateResponse({address1, address2});
  EXPECT_TRUE(url_loader_factory().SimulateResponseForPendingRequest(
      kFullPreallocateEndpoint, json));
  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(future.Get(), std::vector({address1, address2}));

  histogram_tester.ExpectTotalCount(
      LatencyHistogramFor(PlusAddressNetworkRequestType::kPreallocate), 1);
  histogram_tester.ExpectUniqueSample(
      NetErrorCodeHistogramFor(PlusAddressNetworkRequestType::kPreallocate),
      net::OK, 1);
  histogram_tester.ExpectUniqueSample(
      ResponseCodeHistogramFor(PlusAddressNetworkRequestType::kPreallocate),
      net::HTTP_OK, 1);
  histogram_tester.ExpectTotalCount(
      ResponseByteSizeHistogramFor(PlusAddressNetworkRequestType::kPreallocate),
      1);
}

// Tests that if the client does not receive a server response within
// to a preallocation call `kPlusAddressRequestTimeout`, then the request fails
// with a `PlusAddressRequestErrorType::kClientTimeout` error.
TEST_F(PlusAddressHttpClientRequests, PreallocatePlusAddresses_ClientTimeout) {
  using Result = PlusAddressHttpClient::PreallocatePlusAddressesResult;
  const PlusProfile profile = test::CreatePlusProfile();
  base::HistogramTester histogram_tester;
  base::test::TestFuture<Result> future;

  client().PreallocatePlusAddresses(future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  FastForwardBy(features::kPlusAddressRequestTimeout.Get() + base::Seconds(1));

  ASSERT_TRUE(future.Wait());
  const Result& response = future.Get();
  ASSERT_FALSE(response.has_value());
  EXPECT_EQ(response.error().type(),
            PlusAddressRequestErrorType::kClientTimeout);
  EXPECT_TRUE(response.error().IsTimeoutError());

  // Verify expected metrics.
  histogram_tester.ExpectTotalCount(
      LatencyHistogramFor(PlusAddressNetworkRequestType::kPreallocate), 1);
  histogram_tester.ExpectUniqueSample(
      NetErrorCodeHistogramFor(PlusAddressNetworkRequestType::kPreallocate),
      net::ERR_TIMED_OUT, 1);
  histogram_tester.ExpectTotalCount(
      ResponseCodeHistogramFor(PlusAddressNetworkRequestType::kPreallocate), 0);
  histogram_tester.ExpectTotalCount(
      ResponseByteSizeHistogramFor(PlusAddressNetworkRequestType::kPreallocate),
      0);
}

// Tests that a malformed server response from the preallocation endpoint is
// received, parsed, and passes a parsing error to the callback.
TEST_F(PlusAddressHttpClientRequests, PreallocatePlusAddresses_ParsingError) {
  base::test::TestFuture<PlusAddressHttpClient::PreallocatePlusAddressesResult>
      future;
  client().PreallocatePlusAddresses(future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  EXPECT_TRUE(url_loader_factory().SimulateResponseForPendingRequest(
      kFullPreallocateEndpoint, "[]"));
  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(future.Get(), base::unexpected(PlusAddressRequestError(
                              PlusAddressRequestErrorType::kParsingError)));
}

// Tests that a network error response from the preallocation endpoint is
// received and passed on to the callback.
TEST_F(PlusAddressHttpClientRequests, PreallocatePlusAddresses_NetworkError) {
  base::test::TestFuture<PlusAddressHttpClient::PreallocatePlusAddressesResult>
      future;
  client().PreallocatePlusAddresses(future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  EXPECT_TRUE(url_loader_factory().SimulateResponseForPendingRequest(
      kFullPreallocateEndpoint, "", net::HTTP_NOT_FOUND));
  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(future.Get(),
            base::unexpected(
                PlusAddressRequestError::AsNetworkError(net::HTTP_NOT_FOUND)));
}

// Tests that resetting the http client while a pre-allocate request is ongoing
// results in a `kUserSignedOut` error.
TEST_F(PlusAddressHttpClientRequests, PreallocatePlusAddresses_SignoutError) {
  base::test::TestFuture<PlusAddressHttpClient::PreallocatePlusAddressesResult>
      future;
  client().PreallocatePlusAddresses(future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  EXPECT_EQ(url_loader_factory().NumPending(), 1);
  client().Reset();
  EXPECT_EQ(url_loader_factory().NumPending(), 0);
  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(future.Get(), base::unexpected(PlusAddressRequestError(
                              PlusAddressRequestErrorType::kUserSignedOut)));
}

// Tests that calling reset cancels ongoing network requests and runs pending
// callbacks with a `PlusAddressRequestErrorType::kUserSignedOut`.
TEST_F(PlusAddressHttpClientRequests, ResetWhileWaitingForNetwork) {
  const url::Origin origin = url::Origin::Create(GURL("https://foobar.com"));
  std::string facet = origin.Serialize();
  base::test::TestFuture<const PlusProfileOrError&> future;

  client().ReservePlusAddress(origin, /*refresh=*/false, future.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      kToken, base::Time::Max());

  EXPECT_EQ(url_loader_factory().NumPending(), 1);
  client().Reset();
  EXPECT_EQ(url_loader_factory().NumPending(), 0);
  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(future.Get(), base::unexpected(PlusAddressRequestError(
                              PlusAddressRequestErrorType::kUserSignedOut)));
}

// Tests that calling reset cancels ongoing OAuth requests and runs pending
// callbacks with a `PlusAddressRequestErrorType::kUserSignedOut`.
TEST_F(PlusAddressHttpClientRequests, ResetWhileWaitingForOAuth) {
  const url::Origin origin = url::Origin::Create(GURL("https://foobar.com"));
  std::string facet = origin.Serialize();
  base::test::TestFuture<const PlusProfileOrError&> future;

  client().ReservePlusAddress(origin, /*refresh=*/false, future.GetCallback());
  EXPECT_EQ(url_loader_factory().NumPending(), 0);
  client().Reset();
  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(future.Get(), base::unexpected(PlusAddressRequestError(
                              PlusAddressRequestErrorType::kUserSignedOut)));
}

TEST(PlusAddressHttpClient, ChecksUrlParamIsValidGurl) {
  base::test::TaskEnvironment task_environment;
  signin::IdentityTestEnvironment identity_test_env;
  std::string server_url = "https://foo.com/";
  base::test::ScopedFeatureList feature;
  feature.InitAndEnableFeatureWithParameters(
      features::kPlusAddressesEnabled,
      {{features::kEnterprisePlusAddressServerUrl.name, server_url}});
  PlusAddressHttpClientImpl client(
      identity_test_env.identity_manager(),
      base::MakeRefCounted<network::TestSharedURLLoaderFactory>());
  EXPECT_EQ(test_api(client).GetServerUrlForTesting().value_or(GURL()),
            server_url);
}

TEST(PlusAddressHttpClient, RejectsNonUrlStrings) {
  base::test::TaskEnvironment task_environment;
  signin::IdentityTestEnvironment identity_test_env;
  base::test::ScopedFeatureList feature;
  feature.InitAndEnableFeatureWithParameters(
      features::kPlusAddressesEnabled,
      {{features::kEnterprisePlusAddressServerUrl.name, "kirubeldotcom"}});
  PlusAddressHttpClientImpl client(
      identity_test_env.identity_manager(),
      base::MakeRefCounted<network::TestSharedURLLoaderFactory>());
  EXPECT_FALSE(test_api(client).GetServerUrlForTesting().has_value());
}

class PlusAddressAuthToken : public ::testing::Test {
 public:
  PlusAddressAuthToken() {
    // Init the feature param to add `kTestScope_` to GetUnconsentedOAuth2Scopes
    features_.InitAndEnableFeatureWithParameters(
        features::kPlusAddressesEnabled,
        {{features::kEnterprisePlusAddressOAuthScope.name, kTestScope}});
    InitClient();
  }

  static constexpr base::TimeDelta kTestTokenLifetime = base::Seconds(1000);
  static constexpr char kTestToken[] = "access_token";
  static constexpr char kTestScope[] = "https://googleapis.com/test.scope";

 protected:
  PlusAddressHttpClientImpl& client() { return *client_; }
  signin::IdentityTestEnvironment& identity_env() { return identity_test_env_; }
  signin::IdentityManager* identity_manager() {
    return identity_env().identity_manager();
  }
  base::test::TaskEnvironment& task_environment() { return task_environment_; }

  void InitClient() {
    client_.emplace(identity_manager(), /*url_loader_factory=*/nullptr);
  }

  AccountInfo SignIn() {
    return identity_env().MakePrimaryAccountAvailable(
        "foo@gmail.com", signin::ConsentLevel::kSignin);
  }

  void WaitAndRespondToTokenRequest(base::Time expiration) {
    identity_env()
        .WaitForAccessTokenRequestIfNecessaryAndRespondWithTokenForScopes(
            kTestToken, expiration, "unused", {kTestScope});
  }

 private:
  base::test::ScopedFeatureList features_;
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  signin::IdentityTestEnvironment identity_test_env_;

  std::optional<PlusAddressHttpClientImpl> client_;
};

TEST_F(PlusAddressAuthToken, RequestedBeforeSignin) {
  base::HistogramTester histogram_tester;
  base::test::TestFuture<std::optional<std::string>> callback;
  test_api(client()).GetAuthToken(callback.GetCallback());

  // The callback is run only after signin.
  EXPECT_FALSE(callback.IsReady());
  SignIn();
  WaitAndRespondToTokenRequest(base::Time::Now() + kTestTokenLifetime);

  EXPECT_TRUE(callback.Wait());
  EXPECT_THAT(histogram_tester.GetAllSamples(kPlusAddressOauthErrorHistogram),
              BucketsAre(base::Bucket(GoogleServiceAuthError::State::NONE, 1)));
}

TEST_F(PlusAddressAuthToken, RequestedUserNeverSignsIn) {
  base::HistogramTester histogram_tester;
  base::test::TestFuture<std::optional<std::string>> callback;
  test_api(client()).GetAuthToken(callback.GetCallback());
  EXPECT_FALSE(callback.IsReady());
  histogram_tester.ExpectTotalCount(kPlusAddressOauthErrorHistogram, 0);
}

TEST_F(PlusAddressAuthToken, RequestedAfterExpiration) {
  base::HistogramTester histogram_tester;
  // Make an initial OAuth token request.
  base::test::TestFuture<std::optional<std::string>> first_callback;
  test_api(client()).GetAuthToken(first_callback.GetCallback());

  // Sign in, get a token, and fast-forward to after it is expired.
  SignIn();
  WaitAndRespondToTokenRequest(base::Time::Now() + kTestTokenLifetime);
  EXPECT_TRUE(first_callback.Wait());
  EXPECT_THAT(histogram_tester.GetAllSamples(kPlusAddressOauthErrorHistogram),
              BucketsAre(base::Bucket(GoogleServiceAuthError::State::NONE, 1)));
  task_environment().FastForwardBy(kTestTokenLifetime + base::Seconds(1));

  // Issue another request for an OAuth token.
  base::test::TestFuture<std::optional<std::string>> second_callback;
  test_api(client()).GetAuthToken(second_callback.GetCallback());

  // Callback is only run once the new OAuth token request has completed.
  EXPECT_FALSE(second_callback.IsReady());
  WaitAndRespondToTokenRequest(base::Time::Now() + kTestTokenLifetime);
  EXPECT_TRUE(second_callback.Wait());
  EXPECT_THAT(histogram_tester.GetAllSamples(kPlusAddressOauthErrorHistogram),
              BucketsAre(base::Bucket(GoogleServiceAuthError::State::NONE, 2)));
}

TEST_F(PlusAddressAuthToken, AuthErrorWithMultipleAccounts) {
  // GetAuthToken() is only concerned with the primary token auth state.
  AccountInfo primary = SignIn();
  AccountInfo secondary =
      identity_env().MakeAccountAvailable("secondary@foo.com");
  identity_env().UpdatePersistentErrorOfRefreshTokenForAccount(
      secondary.account_id,
      GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
  InitClient();

  base::test::TestFuture<std::optional<std::string>> callback;
  test_api(client()).GetAuthToken(callback.GetCallback());
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
      primary.account_id, kTestToken, base::Time::Max());
  EXPECT_EQ(callback.Get(), kTestToken);
}

TEST_F(PlusAddressAuthToken, RequestWorks_ManyCallers) {
  SignIn();

  // Issue several requests for an OAuth token.
  base::test::TestFuture<std::optional<std::string>> first;
  base::test::TestFuture<std::optional<std::string>> second;
  base::test::TestFuture<std::optional<std::string>> third;
  test_api(client()).GetAuthToken(first.GetCallback());
  test_api(client()).GetAuthToken(second.GetCallback());
  test_api(client()).GetAuthToken(third.GetCallback());

  // Although we failed to get a token, each callback should still be run.
  WaitAndRespondToTokenRequest(base::Time::Max());
  EXPECT_EQ(first.Get().value(), kTestToken);
  EXPECT_EQ(second.Get().value(), kTestToken);
  EXPECT_EQ(third.Get().value(), kTestToken);
}

TEST_F(PlusAddressAuthToken, RequestFails_ManyCallers) {
  SignIn();

  // Issue several requests for an OAuth token.
  base::test::TestFuture<std::optional<std::string>> first;
  base::test::TestFuture<std::optional<std::string>> second;
  base::test::TestFuture<std::optional<std::string>> third;
  test_api(client()).GetAuthToken(first.GetCallback());
  test_api(client()).GetAuthToken(second.GetCallback());
  test_api(client()).GetAuthToken(third.GetCallback());

  // Although we failed to get a token, each callback should still be run.
  identity_env().WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
      GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
  EXPECT_FALSE(first.Get().has_value());
  EXPECT_FALSE(second.Get().has_value());
  EXPECT_FALSE(third.Get().has_value());
}

class PlusAddressHttpClientNullServerUrl : public PlusAddressHttpClientRequests {
 public:
  PlusAddressHttpClientNullServerUrl() {
    // Disable feature plus_addresses, which should also set `server_url_` to
    // `nullopt`.
    scoped_feature_list_.InitAndDisableFeature(features::kPlusAddressesEnabled);
    InitClient();
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(PlusAddressHttpClientNullServerUrl, ReservePlusAddress_SendsNoRequest) {
  const url::Origin origin = url::Origin::Create(GURL("https://foobar.com"));
  base::test::TestFuture<const PlusProfileOrError&> callback;

  EXPECT_FALSE(test_api(client()).GetServerUrlForTesting().has_value());
  // ReservePlusAddress should return without making any request when no valid
  // `server_ur_` is provided.
  client().ReservePlusAddress(origin, /*refresh=*/false,
                              callback.GetCallback());
  EXPECT_EQ(url_loader_factory().NumPending(), 0);
  EXPECT_FALSE(callback.IsReady());
}

TEST_F(PlusAddressHttpClientNullServerUrl, ConfirmPlusAddress_SendsNoRequest) {
  const url::Origin origin = url::Origin::Create(GURL("https://foobar.com"));
  base::test::TestFuture<const PlusProfileOrError&> callback;

  EXPECT_FALSE(test_api(client()).GetServerUrlForTesting().has_value());
  // ConfirmPlusAddress should return without making any request when no valid
  // `server_ur_` is provided.
  client().ConfirmPlusAddress(origin, PlusAddress("foo@moo.com"),
                              callback.GetCallback());
  EXPECT_EQ(url_loader_factory().NumPending(), 0);
  EXPECT_FALSE(callback.IsReady());
}

}  // namespace
}  // namespace plus_addresses