aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Auth/Tests/FIRAuthBackendRPCImplementationTests.m
blob: 5930e1303038a0a6ad2bea9998b4b423e1a0343d (plain)
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <XCTest/XCTest.h>

#import "FIRAuthErrorUtils.h"
#import "FIRAuthInternalErrors.h"
#import "FIRAuthBackend.h"
#import "FIRAuthRPCRequest.h"
#import "FIRAuthRPCResponse.h"
#import "FIRFakeBackendRPCIssuer.h"

/** @var kFakeRequestURL
    @brief Used as a fake URL for a fake RPC request. We don't test this here, since it's tested
        for the specific RPC requests in their various unit tests.
 */
static NSString *const kFakeRequestURL = @"https://www.google.com/";

/** @var kFakeErrorDomain
    @brief A value to use for fake @c NSErrors.
 */
static NSString *const kFakeErrorDomain = @"fakeDomain";

/** @var kFakeErrorCode
    @brief A value to use for fake @c NSErrors.
 */
static const NSUInteger kFakeErrorCode = -1;

/** @var kUnknownServerErrorMessage
    @brief A value to use for fake server errors with an unknown message.
 */
static NSString *const kUnknownServerErrorMessage = @"UNKNOWN_MESSAGE";

/** @var kErrorMessageCaptchaRequired
    @brief The error message in JSON responses from the server for CAPTCHA required.
 */
static NSString *const kErrorMessageCaptchaRequired = @"CAPTCHA_REQUIRED";

/** @var kErrorMessageCaptchaRequiredInvalidPassword
    @brief The error message in JSON responses from the server for CAPTCHA required with invalid
        password.
 */
static NSString *const kErrorMessageCaptchaRequiredInvalidPassword =
    @"CAPTCHA_REQUIRED_INVALID_PASSWORD";

/** @var kErrorMessageCaptchaCheckFailed
    @brief The error message in JSON responses from the server for CAPTCHA check failed.
 */
static NSString *const kErrorMessageCaptchaCheckFailed = @"CAPTCHA_CHECK_FAILED";

/** @var kErrorMessageEmailExists
    @brief The error message in JSON responses from the server for user's email already exists.
 */
static NSString *const kErrorMessageEmailExists = @"EMAIL_EXISTS";

/** @var kErrorMessageKey
    @brief The key for the error message in an error response.
 */
static NSString *const kErrorMessageKey = @"message";

/** @var kTestKey
    @brief A key to use for a successful response dictionary.
 */
static NSString *const kTestKey = @"TestKey";

/** @var kUserDisabledErrorMessage
    @brief This is the base error message the server will respond with if the user's account has
        been disabled.
 */
static NSString *const kUserDisabledErrorMessage = @"USER_DISABLED";

/** @var kFakeUserDisabledCustomErrorMessage
    @brief This is a fake custom error message the server can respond with if the user's account has
        been disabled.
 */
static NSString *const kFakeUserDisabledCustomErrorMessage = @"The user has been disabled.";

/** @var kServerErrorDetailMarker
    @brief This marker indicates that the server error message contains a detail error message which
        should be used instead of the hardcoded client error message.
 */
static NSString *const kServerErrorDetailMarker = @" : ";

/** @var kTestValue
    @brief A value to use for a successful response dictionary.
 */
static NSString *const kTestValue = @"TestValue";

/** @class FIRAuthBackendRPCImplementation
    @brief Exposes an otherwise private class to these tests. See the real implementation for
        documentation.
 */
@interface FIRAuthBackendRPCImplementation : NSObject <FIRAuthBackendImplementation>

/** @fn postWithRequest:response:callback:
    @brief Calls the RPC using HTTP POST.
    @remarks Possible error responses:
        @see FIRAuthInternalErrorCodeRPCRequestEncodingError
        @see FIRAuthInternalErrorCodeJSONSerializationError
        @see FIRAuthInternalErrorCodeNetworkError
        @see FIRAuthInternalErrorCodeUnexpectedErrorResponse
        @see FIRAuthInternalErrorCodeUnexpectedResponse
        @see FIRAuthInternalErrorCodeRPCResponseDecodingError
    @param request The request.
    @param response The empty response to be filled.
    @param callback The callback for both success and failure.
 */
- (void)postWithRequest:(id<FIRAuthRPCRequest>)request
               response:(id<FIRAuthRPCResponse>)response
               callback:(void (^)(NSError *error))callback;

@end

/** @extension FIRAuthBackend
    @brief This class extension exposes the otherwise private @c implementation method. We use this
        here to directly call the @c postWithRequest:response:callback: method of
        @c FIRAuthBackendRPCImplementation in some of the tests.
 */
@interface FIRAuthBackend ()

/** @fn implementation
    @brief Exposes the otherwise private @c implementation method. We use this here to directly call
        the @c postWithRequest:response:callback: method of @c FIRAuthBackendRPCImplementation in
        some of the tests.
 */
+ (FIRAuthBackendRPCImplementation *)implementation;

@end

/** @class FIRFakeRequest
    @brief Allows us to fake a request with deterministic request bodies and encoding errors
        returned from the @c FIRAuthRPCRequest-specified @c unencodedHTTPRequestBodyWithError:
        method.
 */
@interface FIRFakeRequest : NSObject <FIRAuthRPCRequest>

/** @fn fakeRequest
    @brief A "normal" request which returns an encodable request object with no error.
 */
+ (nullable instancetype)fakeRequest;

/** @fn fakeRequestWithEncodingError
    @brief A request which returns a fake error during the encoding process.
 */
+ (nullable instancetype)fakeRequestWithEncodingError:(NSError *)error;

/** @fn fakeRequestWithUnserializableRequestBody
    @brief A request which returns a request object which can not be properly serialized by
        @c NSJSONSerialization.
 */
+ (nullable instancetype)fakeRequestWithUnserializableRequestBody;

/** @fn fakeRequestWithNoBody
    @brief A request which returns a nil request body but no error.
 */
+ (nullable instancetype)fakeRequestWithNoBody;

/** @fn init
    @brief Please use initWithRequestBody:encodingError:
 */
- (nullable instancetype)init NS_UNAVAILABLE;

/** @fn initWithRequestBody:encodingError:
    @brief Designated initializer.
    @param requestBody The fake request body to return when @c unencodedHTTPRequestBodyWithError: is
        invoked.
    @param encodingError The fake error to return when @c unencodedHTTPRequestBodyWithError is
        invoked.
 */
- (nullable instancetype)initWithRequestBody:(nullable id)requestBody
                               encodingError:(nullable NSError *)encodingError
    NS_DESIGNATED_INITIALIZER;

@end

@implementation FIRFakeRequest {
  /** @var _requestBody
      @brief The fake request body object we will return when @c unencodedHTTPRequestBodyWithError:
          is invoked.
   */
  id _Nullable _requestBody;

  /** @var _requestEncodingError
      @brief The fake error object we will return when @c unencodedHTTPRequestBodyWithError:
          is invoked.
   */
  NSError *_Nullable _requestEncodingError;
}

+ (nullable instancetype)fakeRequest {
  return [[self alloc] initWithRequestBody:@{ } encodingError:nil];
}

+ (nullable instancetype)fakeRequestWithEncodingError:(NSError *)error {
  return [[self alloc] initWithRequestBody:nil encodingError:error];
}

+ (nullable instancetype)fakeRequestWithUnserializableRequestBody {
  return [[self alloc] initWithRequestBody:@{ @"unencodableValue" : self } encodingError:nil];
}

+ (nullable instancetype)fakeRequestWithNoBody {
  return [[self alloc] initWithRequestBody:nil encodingError:nil];
}

- (nullable instancetype)initWithRequestBody:(nullable id)requestBody
                               encodingError:(nullable NSError *)encodingError {
  self = [super init];
  if (self) {
    _requestBody = requestBody;
    _requestEncodingError = encodingError;
  }
  return self;
}

- (NSURL *)requestURL {
  return [NSURL URLWithString:kFakeRequestURL];
}

- (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  if (error) {
    *error = _requestEncodingError;
  }
  return _requestBody;
}

@end

/** @class FIRFakeResponse
    @brief Allows us to inspect the dictionaries received by @c FIRAuthRPCResponse classes, and
        provide deterministic responses to the @c setWithDictionary:error:
        methods.
 */
@interface FIRFakeResponse : NSObject <FIRAuthRPCResponse>

/** @property receivedDictionary
    @brief The dictionary passed to the @c setWithDictionary:error: method.
 */
@property(nonatomic, strong, readonly, nullable) NSDictionary *receivedDictionary;

/** @fn fakeResponse
    @brief A "normal" sucessful response (no error, no expected kind.)
 */
+ (nullable instancetype)fakeResponse;

/** @fn fakeResponseWithDecodingError
    @brief A response which returns a fake error during the decoding process.
 */
+ (nullable instancetype)fakeResponseWithDecodingError;

/** @fn init
    @brief Please use initWithDecodingError:
 */
- (nullable instancetype)init NS_UNAVAILABLE;

- (nullable instancetype)initWithDecodingError:(nullable NSError *)decodingError
     NS_DESIGNATED_INITIALIZER;

@end

@implementation FIRFakeResponse {
  /** @var _responseDecodingError
      @brief The value to return for an error when the @c setWithDictionary:error: method is
          invoked.
   */
  NSError *_Nullable _responseDecodingError;
}

+ (nullable instancetype)fakeResponse {
  return [[self alloc] initWithDecodingError:nil];
}

+ (nullable instancetype)fakeResponseWithDecodingError {
  NSError *decodingError = [FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:self];
  return [[self alloc] initWithDecodingError:decodingError];
}

- (nullable instancetype)initWithDecodingError:(nullable NSError *)decodingError {
  self = [super init];
  if (self) {
    _responseDecodingError = decodingError;
  }
  return self;
}

- (BOOL)setWithDictionary:(NSDictionary *)dictionary
                    error:(NSError *_Nullable *_Nullable)error {
  if (_responseDecodingError) {
    if (error) {
      *error = _responseDecodingError;
    }
    return NO;
  }
  _receivedDictionary = dictionary;
  return YES;
}

@end

/** @class FIRAuthBackendRPCImplementationTests
    @brief This set of unit tests is designed primarily to test the possible outcomes of the
        @c FIRAuthBackendRPCImplementation.postWithRequest:response:callback: method.
 */
@interface FIRAuthBackendRPCImplementationTests : XCTestCase
@end
@implementation FIRAuthBackendRPCImplementationTests {
  /** @var _RPCIssuer
      @brief This backend RPC issuer is used to fake network responses for each test in the suite.
          In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
   */
  FIRFakeBackendRPCIssuer *_RPCIssuer;

  /** @var _RPCImplementation
      @brief This backend RPC implementation is used to make fake network requests for each test in
          the suite.
   */
  FIRAuthBackendRPCImplementation *_RPCImplementation;
}

- (void)setUp {
  FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  _RPCIssuer = RPCIssuer;
  _RPCImplementation = [FIRAuthBackend implementation];
}

- (void)tearDown {
  [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  _RPCIssuer = nil;
  _RPCImplementation = nil;
}

/** @fn testRequestEncodingError
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        request passed returns an error during it's unencodedHTTPRequestBodyWithError: method.
        The error returned should be delivered to the caller without any change.
 */
- (void)testRequestEncodingError {
  NSError *encodingError =
      [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  FIRFakeRequest *request = [FIRFakeRequest fakeRequestWithEncodingError:encodingError];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  // There is no need to call [_RPCIssuer respondWithError:...] in this test because a request
  // should never have been tried - and we we know that's the case when we test @c callbackInvoked.

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeRPCRequestEncodingError);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingUnderlyingError);
  XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNil(deserializedResponse);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testBodyDataSerializationError
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        request returns an object which isn't serializable by @c NSJSONSerialization.
        The error from @c NSJSONSerialization should be returned as the underlyingError for an
        @c NSError with the code @c FIRAuthErrorCodeJSONSerializationError.
 */
- (void)testBodyDataSerializationError {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequestWithUnserializableRequestBody];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  // There is no need to call [_RPCIssuer respondWithError:...] in this test because a request
  // should never have been tried - and we we know that's the case when we test @c callbackInvoked.

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeJSONSerializationError);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNil(underlyingUnderlyingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNil(deserializedResponse);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testNetworkError
    @brief This test checks to make sure a network error is properly wrapped and forwarded with the
        correct code (FIRAuthErrorCodeNetworkError).
 */
- (void)testNetworkError {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  // It shouldn't matter what the error domain/code/userInfo are, any junk values are suitable. The
  // implementation should treat any error with no response data as a network error.
  NSError *responseError = [NSError errorWithDomain:kFakeErrorDomain
                                               code:kFakeErrorCode
                                           userInfo:nil];
  [_RPCIssuer respondWithError:responseError];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeNetworkError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, kFakeErrorDomain);
  XCTAssertEqual(underlyingError.code, kFakeErrorCode);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNil(underlyingUnderlyingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNil(deserializedResponse);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testUnparsableErrorResponse
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        response isn't deserializable by @c NSJSONSerialization and an error
        condition (with an associated error response message) was expected. We are expecting to
        receive the original network error wrapped in an @c NSError with the code
        @c FIRAuthErrorCodeUnexpectedHTTPResponse.
 */
- (void)testUnparsableErrorResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  NSData *data =
      [@"<html><body>An error occurred.</body></html>" dataUsingEncoding:NSUTF8StringEncoding];
  NSError *error =
      [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  [_RPCIssuer respondWithData:data error:error];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingUnderlyingError);
  XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNil(deserializedResponse);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNotNil(dataResponse);
  XCTAssertEqualObjects(dataResponse, data);
}

/** @fn testUnparsableSuccessResponse
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        response isn't deserializable by @c NSJSONSerialization and no error
        condition was indicated. We are expecting to
        receive the @c NSJSONSerialization error wrapped in an @c NSError with the code
        @c FIRAuthErrorCodeUnexpectedServerResponse.
 */
- (void)testUnparsableSuccessResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  NSData *data =
      [@"<xml>Some non-JSON value.</xml>" dataUsingEncoding:NSUTF8StringEncoding];
  [_RPCIssuer respondWithData:data error:nil];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedResponse);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingUnderlyingError);
  XCTAssertEqualObjects(underlyingUnderlyingError.domain, NSCocoaErrorDomain);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNil(deserializedResponse);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNotNil(dataResponse);
  XCTAssertEqualObjects(dataResponse, data);
}

/** @fn testNonDictionaryErrorResponse
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        response deserialized by @c NSJSONSerialization is not a dictionary, and an error was
        expected. We are expecting to receive an @c NSError with the code
        @c FIRAuthErrorCodeUnexpectedErrorServerResponse with the decoded response in the
        @c NSError.userInfo dictionary associated with the key
        @c FIRAuthErrorUserInfoDecodedResponseKey.
 */
- (void)testNonDictionaryErrorResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  // We are responding with a JSON-encoded string value representing an array - which is unexpected.
  // It should normally be a dictionary, and we need to check for this sort of thing. Because we can
  // successfully decode this value, however, we do return it in the error results. We check for
  // this array later in the test.
  NSData *data = [@"[]" dataUsingEncoding:NSUTF8StringEncoding];
  NSError *error =
      [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  [_RPCIssuer respondWithData:data error:error];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNil(underlyingUnderlyingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNotNil(deserializedResponse);
  XCTAssert([deserializedResponse isKindOfClass:[NSArray class]]);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testNonDictionarySuccessResponse
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        response deserialized by @c NSJSONSerialization is not a dictionary, and no error was
        expected. We are expecting to receive an @c NSError with the code
        @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded response in the
        @c NSError.userInfo dictionary associated with the key
        @c FIRAuthErrorUserInfoDecodedResponseKey.
 */
- (void)testNonDictionarySuccessResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  // We are responding with a JSON-encoded string value representing an array - which is unexpected.
  // It should normally be a dictionary, and we need to check for this sort of thing. Because we can
  // successfully decode this value, however, we do return it in the error results. We check for
  // this array later in the test.
  NSData *data = [@"[]" dataUsingEncoding:NSUTF8StringEncoding];
  [_RPCIssuer respondWithData:data error:nil];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedResponse);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNil(underlyingUnderlyingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNotNil(deserializedResponse);
  XCTAssert([deserializedResponse isKindOfClass:[NSArray class]]);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testCaptchaRequiredResponse
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        we get an error message indicating captcha is required. The backend should not be returning
        this error to mobile clients. If it does, we should wrap it in an @c NSError with the code
        @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded error message in the
        @c NSError.userInfo dictionary associated with the key
        @c FIRAuthErrorUserInfoDecodedErrorResponseKey.
 */
- (void)testCaptchaRequiredResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  NSError *error =
      [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  [_RPCIssuer respondWithServerErrorMessage:kErrorMessageCaptchaRequired error:error];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNil(underlyingUnderlyingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNotNil(deserializedResponse);
  XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  XCTAssertNotNil(deserializedResponse[@"message"]);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testCaptchaCheckFailedResponse
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        we get an error message indicating captcha check failed. The backend should not be returning
        this error to mobile clients. If it does, we should wrap it in an @c NSError with the code
        @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded error message in the
        @c NSError.userInfo dictionary associated with the key
        @c FIRAuthErrorUserInfoDecodedErrorResponseKey.
 */
- (void)testCaptchaCheckFailedResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  NSError *error =
      [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  [_RPCIssuer respondWithServerErrorMessage:kErrorMessageCaptchaCheckFailed error:error];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNil(underlyingUnderlyingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNotNil(deserializedResponse);
  XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  XCTAssertNotNil(deserializedResponse[@"message"]);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testCaptchaRequiredInvalidPasswordResponse
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        we get an error message indicating captcha is required and an invalid password was entered.
        The backend should not be returning this error to mobile clients. If it does, we should wrap
        it in an @c NSError with the code
        @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded error message in the
        @c NSError.userInfo dictionary associated with the key
        @c FIRAuthErrorUserInfoDecodedErrorResponseKey.
 */
- (void)testCaptchaRequiredInvalidPasswordResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  NSError *error =
      [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  [_RPCIssuer respondWithServerErrorMessage:kErrorMessageCaptchaRequiredInvalidPassword
                                      error:error];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNil(underlyingUnderlyingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNotNil(deserializedResponse);
  XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  XCTAssertNotNil(deserializedResponse[@"message"]);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testDecodableErrorResponseWithUnknownMessage
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        response deserialized by @c NSJSONSerialization represents a valid error response (and an
        error was indicated) but we didn't receive an error message we know about. We are expecting
        an @c NSError with the code @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded
        error message in the @c NSError.userInfo dictionary associated with the key
        @c FIRAuthErrorUserInfoDecodedErrorResponseKey.
 */
- (void)testDecodableErrorResponseWithUnknownMessage {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  // We need to return a valid "error" response here, but we are going to intentionally use a bogus
  // error message.
  NSError *error =
      [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  [_RPCIssuer respondWithServerErrorMessage:kUnknownServerErrorMessage error:error];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNil(underlyingUnderlyingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNotNil(deserializedResponse);
  XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  XCTAssertNotNil(deserializedResponse[@"message"]);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testErrorResponseWithNoErrorMessage
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        response deserialized by @c NSJSONSerialization is a dictionary, and an error was indicated,
        but no error information was present in the decoded response. We are expecting an @c NSError
        with the code @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded
        response message in the @c NSError.userInfo dictionary associated with the key
        @c FIRAuthErrorUserInfoDecodedResponseKey.
 */
- (void)testErrorResponseWithNoErrorMessage {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  NSError *error =
      [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  [_RPCIssuer respondWithJSON:@{ } error:error];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);

  NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNil(underlyingUnderlyingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNotNil(deserializedResponse);
  XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testClientErrorResponse
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        response contains a client error specified by an error messsage sent from the backend.
 */
- (void)testClientErrorResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackerror;
  __block BOOL callBackInvoked;
  [_RPCImplementation postWithRequest: request response:response callback:^(NSError *error) {
    callBackInvoked = YES;
    callbackerror = error;
  }];
  NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
  NSString *customErrorMessage =[NSString stringWithFormat:@"%@%@%@",
                                                           kUserDisabledErrorMessage,
                                                           kServerErrorDetailMarker,
                                                           kFakeUserDisabledCustomErrorMessage];
  [_RPCIssuer respondWithServerErrorMessage:customErrorMessage error:error];
  XCTAssertNotNil(callbackerror, @"An error should be returned from callback.");
  XCTAssert(callBackInvoked);
  XCTAssertEqual(callbackerror.code, FIRAuthErrorCodeUserDisabled);
  NSString *customMessage = callbackerror.userInfo[NSLocalizedDescriptionKey];
  XCTAssertEqualObjects(customMessage, kFakeUserDisabledCustomErrorMessage);
}

/** @fn testUndecodableSuccessResponse
    @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
        response isn't decodable by the response class but no error condition was expected. We are
        expecting to receive an @c NSError with the code
        @c FIRAuthErrorCodeUnexpectedServerResponse and the error from @c setWithDictionary:error:
        as the value of the underlyingError.
 */
- (void)testUndecodableSuccessResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponseWithDecodingError];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  // It doesn't matter what we respond with here, as long as it's not an error response. The fake
  // response will deterministicly simulate a decoding error regardless of the response value it was
  // given.
  [_RPCIssuer respondWithJSON:@{ }];

  XCTAssert(callbackInvoked);

  XCTAssertNotNil(callbackError);
  XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);

  NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  XCTAssertNotNil(underlyingError);
  XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeRPCResponseDecodingError);

  id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  XCTAssertNotNil(deserializedResponse);
  XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);

  id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  XCTAssertNil(dataResponse);
}

/** @fn testSuccessfulResponse
    @brief Tests that a decoded dictionary is handed to the response instance.
 */
- (void)testSuccessfulResponse {
  FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  FIRFakeResponse *response = [FIRFakeResponse fakeResponse];

  __block NSError *callbackError;
  __block BOOL callbackInvoked;
  [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
    callbackInvoked = YES;
    callbackError = error;
  }];

  [_RPCIssuer respondWithJSON:@{ kTestKey : kTestValue }];

  XCTAssert(callbackInvoked);
  XCTAssertNil(callbackError);
  XCTAssertNotNil(response.receivedDictionary);
  XCTAssertEqualObjects(response.receivedDictionary[kTestKey], kTestValue);
}

@end