FIRAuthBackendRPCImplementationTests.m 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. @import HeartbeatLoggingTestUtils;
  18. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  19. #import "FirebaseAuth/Sources/Backend/FIRAuthRPCRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/FIRAuthRPCResponse.h"
  21. #import "FirebaseAuth/Sources/Backend/FIRAuthRequestConfiguration.h"
  22. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  23. #import "FirebaseAuth/Sources/Utilities/FIRAuthInternalErrors.h"
  24. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  25. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  26. /** @var kFakeRequestURL
  27. @brief Used as a fake URL for a fake RPC request. We don't test this here, since it's tested
  28. for the specific RPC requests in their various unit tests.
  29. */
  30. static NSString *const kFakeRequestURL = @"https://www.google.com/";
  31. /** @var kFakeAPIkey
  32. @brief Used as a fake APIKey for a fake RPC request. We don't test this here.
  33. */
  34. static NSString *const kFakeAPIkey = @"FAKE_API_KEY";
  35. /** @var kFakeFirebaseAppID
  36. @brief Used as a fake Firebase app ID for a fake RPC request. We don't test this here.
  37. */
  38. static NSString *const kFakeFirebaseAppID = @"FAKE_APP_ID";
  39. /** @var kFakeErrorDomain
  40. @brief A value to use for fake @c NSErrors.
  41. */
  42. static NSString *const kFakeErrorDomain = @"fakeDomain";
  43. /** @var kFakeErrorCode
  44. @brief A value to use for fake @c NSErrors.
  45. */
  46. static const NSUInteger kFakeErrorCode = -1;
  47. /** @var kUnknownServerErrorMessage
  48. @brief A value to use for fake server errors with an unknown message.
  49. */
  50. static NSString *const kUnknownServerErrorMessage = @"UNKNOWN_MESSAGE";
  51. /** @var kErrorMessageCaptchaRequired
  52. @brief The error message in JSON responses from the server for CAPTCHA required.
  53. */
  54. static NSString *const kErrorMessageCaptchaRequired = @"CAPTCHA_REQUIRED";
  55. /** @var kErrorMessageCaptchaRequiredInvalidPassword
  56. @brief The error message in JSON responses from the server for CAPTCHA required with invalid
  57. password.
  58. */
  59. static NSString *const kErrorMessageCaptchaRequiredInvalidPassword =
  60. @"CAPTCHA_REQUIRED_INVALID_PASSWORD";
  61. /** @var kErrorMessageCaptchaCheckFailed
  62. @brief The error message in JSON responses from the server for CAPTCHA check failed.
  63. */
  64. static NSString *const kErrorMessageCaptchaCheckFailed = @"CAPTCHA_CHECK_FAILED";
  65. /** @var kErrorMessageEmailExists
  66. @brief The error message in JSON responses from the server for user's email already exists.
  67. */
  68. static NSString *const kErrorMessageEmailExists = @"EMAIL_EXISTS";
  69. /** @var kErrorMessageKey
  70. @brief The key for the error message in an error response.
  71. */
  72. static NSString *const kErrorMessageKey = @"message";
  73. /** @var kTestKey
  74. @brief A key to use for a successful response dictionary.
  75. */
  76. static NSString *const kTestKey = @"TestKey";
  77. /** @var kUserDisabledErrorMessage
  78. @brief This is the base error message the server will respond with if the user's account has
  79. been disabled.
  80. */
  81. static NSString *const kUserDisabledErrorMessage = @"USER_DISABLED";
  82. /** @var kFakeUserDisabledCustomErrorMessage
  83. @brief This is a fake custom error message the server can respond with if the user's account has
  84. been disabled.
  85. */
  86. static NSString *const kFakeUserDisabledCustomErrorMessage = @"The user has been disabled.";
  87. /** @var kServerErrorDetailMarker
  88. @brief This marker indicates that the server error message contains a detail error message which
  89. should be used instead of the hardcoded client error message.
  90. */
  91. static NSString *const kServerErrorDetailMarker = @" : ";
  92. /** @var kTestValue
  93. @brief A value to use for a successful response dictionary.
  94. */
  95. static NSString *const kTestValue = @"TestValue";
  96. #pragma mark - FIRAuthBackendRPCImplementation
  97. /** @class FIRAuthBackendRPCImplementation
  98. @brief Exposes an otherwise private class to these tests. See the real implementation for
  99. documentation.
  100. */
  101. @interface FIRAuthBackendRPCImplementation : NSObject <FIRAuthBackendImplementation>
  102. /** @fn postWithRequest:response:callback:
  103. @brief Calls the RPC using HTTP POST.
  104. @remarks Possible error responses:
  105. @see FIRAuthInternalErrorCodeRPCRequestEncodingError
  106. @see FIRAuthInternalErrorCodeJSONSerializationError
  107. @see FIRAuthInternalErrorCodeNetworkError
  108. @see FIRAuthInternalErrorCodeUnexpectedErrorResponse
  109. @see FIRAuthInternalErrorCodeUnexpectedResponse
  110. @see FIRAuthInternalErrorCodeRPCResponseDecodingError
  111. @param request The request.
  112. @param response The empty response to be filled.
  113. @param callback The callback for both success and failure.
  114. */
  115. - (void)postWithRequest:(id<FIRAuthRPCRequest>)request
  116. response:(id<FIRAuthRPCResponse>)response
  117. callback:(void (^)(NSError *error))callback;
  118. @end
  119. #pragma mark - FIRFakeHeartbeatLogger
  120. /// A fake heartbeat logger used for dependency injection during testing.
  121. @interface FIRFakeHeartbeatLogger : NSObject <FIRHeartbeatLoggerProtocol>
  122. @property(nonatomic, copy, nullable) FIRHeartbeatsPayload * (^onFlushHeartbeatsIntoPayloadHandler)
  123. (void);
  124. @property(nonatomic, copy, nullable) FIRDailyHeartbeatCode (^onHeartbeatCodeForTodayHandler)(void);
  125. @end
  126. @implementation FIRFakeHeartbeatLogger
  127. - (nonnull FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
  128. if (self.onFlushHeartbeatsIntoPayloadHandler) {
  129. return self.onFlushHeartbeatsIntoPayloadHandler();
  130. } else {
  131. return nil;
  132. }
  133. }
  134. - (FIRDailyHeartbeatCode)heartbeatCodeForToday {
  135. // This API should not be used by the below tests because the Auth
  136. // SDK uses only the V2 heartbeat API (`flushHeartbeatsIntoPayload`) for
  137. // getting heartbeats.
  138. [self doesNotRecognizeSelector:_cmd];
  139. return FIRDailyHeartbeatCodeNone;
  140. }
  141. - (void)log {
  142. // This API should not be used by the below tests because the Auth
  143. // SDK does not log heartbeats in it's networking context.
  144. [self doesNotRecognizeSelector:_cmd];
  145. }
  146. @end
  147. #pragma mark - FIRFakeRequest
  148. /** @class FIRFakeRequest
  149. @brief Allows us to fake a request with deterministic request bodies and encoding errors
  150. returned from the @c FIRAuthRPCRequest-specified @c unencodedHTTPRequestBodyWithError:
  151. method.
  152. */
  153. @interface FIRFakeRequest : NSObject <FIRAuthRPCRequest>
  154. /** @fn fakeRequest
  155. @brief A "normal" request which returns an encodable request object with no error.
  156. */
  157. + (nullable instancetype)fakeRequest;
  158. /** @fn fakeRequestWithEncodingError
  159. @brief A request which returns a fake error during the encoding process.
  160. */
  161. + (nullable instancetype)fakeRequestWithEncodingError:(NSError *)error;
  162. /** @fn fakeRequestWithUnserializableRequestBody
  163. @brief A request which returns a request object which can not be properly serialized by
  164. @c NSJSONSerialization.
  165. */
  166. + (nullable instancetype)fakeRequestWithUnserializableRequestBody;
  167. /** @fn fakeRequestWithNoBody
  168. @brief A request which returns a nil request body but no error.
  169. */
  170. + (nullable instancetype)fakeRequestWithNoBody;
  171. /** @fn init
  172. @brief Please use initWithRequestBody:encodingError:
  173. */
  174. - (nullable instancetype)init NS_UNAVAILABLE;
  175. /** @fn initWithRequestBody:encodingError:
  176. @brief Designated initializer.
  177. @param requestBody The fake request body to return when @c unencodedHTTPRequestBodyWithError: is
  178. invoked.
  179. @param encodingError The fake error to return when @c unencodedHTTPRequestBodyWithError is
  180. invoked.
  181. @param requestConfiguration The request configuration associated with the fake request.
  182. */
  183. - (nullable instancetype)initWithRequestBody:(nullable id)requestBody
  184. encodingError:(nullable NSError *)encodingError
  185. requestConfiguration:
  186. (nullable FIRAuthRequestConfiguration *)requestConfiguration
  187. NS_DESIGNATED_INITIALIZER;
  188. @end
  189. @implementation FIRFakeRequest {
  190. /** @var _requestBody
  191. @brief The fake request body object we will return when @c unencodedHTTPRequestBodyWithError:
  192. is invoked.
  193. */
  194. id _Nullable _requestBody;
  195. /** @var _requestEncodingError
  196. @brief The fake error object we will return when @c unencodedHTTPRequestBodyWithError:
  197. is invoked.
  198. */
  199. NSError *_Nullable _requestEncodingError;
  200. /** @var _requestConfiguration
  201. @brief The request configuration to return.
  202. */
  203. FIRAuthRequestConfiguration *_Nullable _requestConfiguration;
  204. }
  205. + (nullable instancetype)fakeRequest {
  206. return [[self alloc] initWithRequestBody:@{} encodingError:nil requestConfiguration:nil];
  207. }
  208. + (nullable instancetype)fakeRequestWithRequestConfiguration:
  209. (FIRAuthRequestConfiguration *)requestConfiguration {
  210. return [[self alloc] initWithRequestBody:@{}
  211. encodingError:nil
  212. requestConfiguration:requestConfiguration];
  213. }
  214. + (nullable instancetype)fakeRequestWithEncodingError:(NSError *)error {
  215. return [[self alloc] initWithRequestBody:nil encodingError:error requestConfiguration:nil];
  216. }
  217. + (nullable instancetype)fakeRequestWithUnserializableRequestBody {
  218. return [[self alloc] initWithRequestBody:@{@"unencodableValue" : self}
  219. encodingError:nil
  220. requestConfiguration:nil];
  221. }
  222. + (nullable instancetype)fakeRequestWithNoBody {
  223. return [[self alloc] initWithRequestBody:nil encodingError:nil requestConfiguration:nil];
  224. }
  225. - (nullable instancetype)initWithRequestBody:(nullable id)requestBody
  226. encodingError:(nullable NSError *)encodingError
  227. requestConfiguration:
  228. (nullable FIRAuthRequestConfiguration *)requestConfiguration {
  229. self = [super init];
  230. if (self) {
  231. _requestBody = requestBody;
  232. _requestEncodingError = encodingError;
  233. _requestConfiguration = requestConfiguration;
  234. }
  235. return self;
  236. }
  237. - (NSURL *)requestURL {
  238. return [NSURL URLWithString:kFakeRequestURL];
  239. }
  240. - (BOOL)containsPostBody {
  241. return YES;
  242. }
  243. - (FIRAuthRequestConfiguration *)requestConfiguration {
  244. if (!_requestConfiguration) {
  245. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kFakeAPIkey
  246. appID:kFakeFirebaseAppID];
  247. }
  248. return _requestConfiguration;
  249. }
  250. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  251. if (error) {
  252. *error = _requestEncodingError;
  253. }
  254. return _requestBody;
  255. }
  256. @end
  257. #pragma mark - FIRFakeResponse
  258. /** @class FIRFakeResponse
  259. @brief Allows us to inspect the dictionaries received by @c FIRAuthRPCResponse classes, and
  260. provide deterministic responses to the @c setWithDictionary:error:
  261. methods.
  262. */
  263. @interface FIRFakeResponse : NSObject <FIRAuthRPCResponse>
  264. /** @property receivedDictionary
  265. @brief The dictionary passed to the @c setWithDictionary:error: method.
  266. */
  267. @property(nonatomic, strong, readonly, nullable) NSDictionary *receivedDictionary;
  268. /** @fn fakeResponse
  269. @brief A "normal" sucessful response (no error, no expected kind.)
  270. */
  271. + (nullable instancetype)fakeResponse;
  272. /** @fn fakeResponseWithDecodingError
  273. @brief A response which returns a fake error during the decoding process.
  274. */
  275. + (nullable instancetype)fakeResponseWithDecodingError;
  276. /** @fn init
  277. @brief Please use initWithDecodingError:
  278. */
  279. - (nullable instancetype)init NS_UNAVAILABLE;
  280. - (nullable instancetype)initWithDecodingError:(nullable NSError *)decodingError
  281. NS_DESIGNATED_INITIALIZER;
  282. @end
  283. @implementation FIRFakeResponse {
  284. /** @var _responseDecodingError
  285. @brief The value to return for an error when the @c setWithDictionary:error: method is
  286. invoked.
  287. */
  288. NSError *_Nullable _responseDecodingError;
  289. }
  290. + (nullable instancetype)fakeResponse {
  291. return [[self alloc] initWithDecodingError:nil];
  292. }
  293. + (nullable instancetype)fakeResponseWithDecodingError {
  294. NSError *decodingError = [FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:self];
  295. return [[self alloc] initWithDecodingError:decodingError];
  296. }
  297. - (nullable instancetype)initWithDecodingError:(nullable NSError *)decodingError {
  298. self = [super init];
  299. if (self) {
  300. _responseDecodingError = decodingError;
  301. }
  302. return self;
  303. }
  304. - (BOOL)setWithDictionary:(NSDictionary *)dictionary error:(NSError *_Nullable *_Nullable)error {
  305. if (_responseDecodingError) {
  306. if (error) {
  307. *error = _responseDecodingError;
  308. }
  309. return NO;
  310. }
  311. _receivedDictionary = dictionary;
  312. return YES;
  313. }
  314. @end
  315. #pragma mark - FIRAuthBackendRPCImplementationTests
  316. /** @class FIRAuthBackendRPCImplementationTests
  317. @brief This set of unit tests is designed primarily to test the possible outcomes of the
  318. @c FIRAuthBackendRPCImplementation.postWithRequest:response:callback: method.
  319. */
  320. @interface FIRAuthBackendRPCImplementationTests : XCTestCase
  321. @end
  322. @implementation FIRAuthBackendRPCImplementationTests {
  323. /** @var _RPCIssuer
  324. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  325. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  326. */
  327. FIRFakeBackendRPCIssuer *_RPCIssuer;
  328. /** @var _RPCImplementation
  329. @brief This backend RPC implementation is used to make fake network requests for each test in
  330. the suite.
  331. */
  332. FIRAuthBackendRPCImplementation *_RPCImplementation;
  333. }
  334. - (void)setUp {
  335. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  336. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  337. _RPCIssuer = RPCIssuer;
  338. _RPCImplementation = [FIRAuthBackend implementation];
  339. }
  340. - (void)tearDown {
  341. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  342. _RPCIssuer = nil;
  343. _RPCImplementation = nil;
  344. }
  345. /** @fn testRequest_IncludesHeartbeatPayload_WhenHeartbeatsNeedSending
  346. @brief This test checks the behavior of @c postWithRequest:response:callback:
  347. to verify that a heartbeats payload is attached as a header to an
  348. outgoing request when there are stored heartbeats that need sending.
  349. */
  350. - (void)testRequest_IncludesHeartbeatPayload_WhenHeartbeatsNeedSending {
  351. // Given
  352. FIRFakeHeartbeatLogger *fakeHeartbeatLogger = [[FIRFakeHeartbeatLogger alloc] init];
  353. FIRAuthRequestConfiguration *requestConfiguration =
  354. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kFakeAPIkey
  355. appID:kFakeFirebaseAppID
  356. heartbeatLogger:fakeHeartbeatLogger];
  357. FIRFakeRequest *request =
  358. [FIRFakeRequest fakeRequestWithRequestConfiguration:requestConfiguration];
  359. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  360. // When
  361. FIRHeartbeatsPayload *nonEmptyHeartbeatsPayload =
  362. [FIRHeartbeatLoggingTestUtils nonEmptyHeartbeatsPayload];
  363. fakeHeartbeatLogger.onFlushHeartbeatsIntoPayloadHandler = ^FIRHeartbeatsPayload * {
  364. return nonEmptyHeartbeatsPayload;
  365. };
  366. __block NSError *callbackError;
  367. __block BOOL callbackInvoked;
  368. [_RPCImplementation postWithRequest:request
  369. response:response
  370. callback:^(NSError *error) {
  371. callbackInvoked = YES;
  372. callbackError = error;
  373. }];
  374. // Then
  375. NSString *expectedHeader = FIRHeaderValueFromHeartbeatsPayload(nonEmptyHeartbeatsPayload);
  376. XCTAssertEqualObjects([_RPCIssuer.completeRequest valueForHTTPHeaderField:@"X-Firebase-Client"],
  377. expectedHeader);
  378. }
  379. /** @fn testRequest_DoesNotIncludeAHeartbeatPayload_WhenNoHeartbeatsNeedSending
  380. @brief This test checks the behavior of @c postWithRequest:response:callback:
  381. to verify that a request header does not contain heartbeat data in the
  382. case that there are no stored heartbeats that need sending.
  383. */
  384. - (void)testRequest_DoesNotIncludeAHeartbeatPayload_WhenNoHeartbeatsNeedSending {
  385. // Given
  386. FIRFakeHeartbeatLogger *fakeHeartbeatLogger = [[FIRFakeHeartbeatLogger alloc] init];
  387. FIRAuthRequestConfiguration *requestConfiguration =
  388. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kFakeAPIkey
  389. appID:kFakeFirebaseAppID
  390. heartbeatLogger:fakeHeartbeatLogger];
  391. FIRFakeRequest *request =
  392. [FIRFakeRequest fakeRequestWithRequestConfiguration:requestConfiguration];
  393. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  394. // When
  395. FIRHeartbeatsPayload *emptyHeartbeatsPayload =
  396. [FIRHeartbeatLoggingTestUtils emptyHeartbeatsPayload];
  397. fakeHeartbeatLogger.onFlushHeartbeatsIntoPayloadHandler = ^FIRHeartbeatsPayload * {
  398. return emptyHeartbeatsPayload;
  399. };
  400. __block NSError *callbackError;
  401. __block BOOL callbackInvoked;
  402. [_RPCImplementation postWithRequest:request
  403. response:response
  404. callback:^(NSError *error) {
  405. callbackInvoked = YES;
  406. callbackError = error;
  407. }];
  408. // Then
  409. XCTAssertNil([_RPCIssuer.completeRequest valueForHTTPHeaderField:@"X-Firebase-Client"]);
  410. }
  411. /** @fn testRequestEncodingError
  412. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  413. request passed returns an error during it's unencodedHTTPRequestBodyWithError: method.
  414. The error returned should be delivered to the caller without any change.
  415. */
  416. - (void)testRequestEncodingError {
  417. NSError *encodingError = [NSError errorWithDomain:kFakeErrorDomain
  418. code:kFakeErrorCode
  419. userInfo:@{}];
  420. FIRFakeRequest *request = [FIRFakeRequest fakeRequestWithEncodingError:encodingError];
  421. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  422. __block NSError *callbackError;
  423. __block BOOL callbackInvoked;
  424. [_RPCImplementation postWithRequest:request
  425. response:response
  426. callback:^(NSError *error) {
  427. callbackInvoked = YES;
  428. callbackError = error;
  429. }];
  430. // There is no need to call [_RPCIssuer respondWithError:...] in this test because a request
  431. // should never have been tried - and we we know that's the case when we test @c callbackInvoked.
  432. XCTAssert(callbackInvoked);
  433. XCTAssertNotNil(callbackError);
  434. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  435. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  436. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  437. XCTAssertNotNil(underlyingError);
  438. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  439. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeRPCRequestEncodingError);
  440. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  441. XCTAssertNotNil(underlyingUnderlyingError);
  442. XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  443. XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
  444. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  445. XCTAssertNil(deserializedResponse);
  446. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  447. XCTAssertNil(dataResponse);
  448. }
  449. /** @fn testBodyDataSerializationError
  450. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  451. request returns an object which isn't serializable by @c NSJSONSerialization.
  452. The error from @c NSJSONSerialization should be returned as the underlyingError for an
  453. @c NSError with the code @c FIRAuthErrorCodeJSONSerializationError.
  454. */
  455. - (void)testBodyDataSerializationError {
  456. FIRFakeRequest *request = [FIRFakeRequest fakeRequestWithUnserializableRequestBody];
  457. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  458. __block NSError *callbackError;
  459. __block BOOL callbackInvoked;
  460. [_RPCImplementation postWithRequest:request
  461. response:response
  462. callback:^(NSError *error) {
  463. callbackInvoked = YES;
  464. callbackError = error;
  465. }];
  466. // There is no need to call [_RPCIssuer respondWithError:...] in this test because a request
  467. // should never have been tried - and we we know that's the case when we test @c callbackInvoked.
  468. XCTAssert(callbackInvoked);
  469. XCTAssertNotNil(callbackError);
  470. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  471. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  472. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  473. XCTAssertNotNil(underlyingError);
  474. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  475. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeJSONSerializationError);
  476. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  477. XCTAssertNil(underlyingUnderlyingError);
  478. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  479. XCTAssertNil(deserializedResponse);
  480. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  481. XCTAssertNil(dataResponse);
  482. }
  483. /** @fn testNetworkError
  484. @brief This test checks to make sure a network error is properly wrapped and forwarded with the
  485. correct code (FIRAuthErrorCodeNetworkError).
  486. */
  487. - (void)testNetworkError {
  488. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  489. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  490. __block NSError *callbackError;
  491. __block BOOL callbackInvoked;
  492. [_RPCImplementation postWithRequest:request
  493. response:response
  494. callback:^(NSError *error) {
  495. callbackInvoked = YES;
  496. callbackError = error;
  497. }];
  498. // It shouldn't matter what the error domain/code/userInfo are, any junk values are suitable. The
  499. // implementation should treat any error with no response data as a network error.
  500. NSError *responseError = [NSError errorWithDomain:kFakeErrorDomain
  501. code:kFakeErrorCode
  502. userInfo:nil];
  503. [_RPCIssuer respondWithError:responseError];
  504. XCTAssert(callbackInvoked);
  505. XCTAssertNotNil(callbackError);
  506. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  507. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeNetworkError);
  508. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  509. XCTAssertNotNil(underlyingError);
  510. XCTAssertEqualObjects(underlyingError.domain, kFakeErrorDomain);
  511. XCTAssertEqual(underlyingError.code, kFakeErrorCode);
  512. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  513. XCTAssertNil(underlyingUnderlyingError);
  514. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  515. XCTAssertNil(deserializedResponse);
  516. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  517. XCTAssertNil(dataResponse);
  518. }
  519. /** @fn testUnparsableErrorResponse
  520. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  521. response isn't deserializable by @c NSJSONSerialization and an error
  522. condition (with an associated error response message) was expected. We are expecting to
  523. receive the original network error wrapped in an @c NSError with the code
  524. @c FIRAuthErrorCodeUnexpectedHTTPResponse.
  525. */
  526. - (void)testUnparsableErrorResponse {
  527. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  528. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  529. __block NSError *callbackError;
  530. __block BOOL callbackInvoked;
  531. [_RPCImplementation postWithRequest:request
  532. response:response
  533. callback:^(NSError *error) {
  534. callbackInvoked = YES;
  535. callbackError = error;
  536. }];
  537. NSData *data =
  538. [@"<html><body>An error occurred.</body></html>" dataUsingEncoding:NSUTF8StringEncoding];
  539. NSError *error = [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{}];
  540. [_RPCIssuer respondWithData:data error:error];
  541. XCTAssert(callbackInvoked);
  542. XCTAssertNotNil(callbackError);
  543. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  544. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  545. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  546. XCTAssertNotNil(underlyingError);
  547. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  548. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  549. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  550. XCTAssertNotNil(underlyingUnderlyingError);
  551. XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  552. XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
  553. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  554. XCTAssertNil(deserializedResponse);
  555. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  556. XCTAssertNotNil(dataResponse);
  557. XCTAssertEqualObjects(dataResponse, data);
  558. }
  559. /** @fn testUnparsableSuccessResponse
  560. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  561. response isn't deserializable by @c NSJSONSerialization and no error
  562. condition was indicated. We are expecting to
  563. receive the @c NSJSONSerialization error wrapped in an @c NSError with the code
  564. @c FIRAuthErrorCodeUnexpectedServerResponse.
  565. */
  566. - (void)testUnparsableSuccessResponse {
  567. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  568. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  569. __block NSError *callbackError;
  570. __block BOOL callbackInvoked;
  571. [_RPCImplementation postWithRequest:request
  572. response:response
  573. callback:^(NSError *error) {
  574. callbackInvoked = YES;
  575. callbackError = error;
  576. }];
  577. NSData *data = [@"<xml>Some non-JSON value.</xml>" dataUsingEncoding:NSUTF8StringEncoding];
  578. [_RPCIssuer respondWithData:data error:nil];
  579. XCTAssert(callbackInvoked);
  580. XCTAssertNotNil(callbackError);
  581. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  582. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  583. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  584. XCTAssertNotNil(underlyingError);
  585. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  586. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedResponse);
  587. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  588. XCTAssertNotNil(underlyingUnderlyingError);
  589. XCTAssertEqualObjects(underlyingUnderlyingError.domain, NSCocoaErrorDomain);
  590. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  591. XCTAssertNil(deserializedResponse);
  592. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  593. XCTAssertNotNil(dataResponse);
  594. XCTAssertEqualObjects(dataResponse, data);
  595. }
  596. /** @fn testNonDictionaryErrorResponse
  597. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  598. response deserialized by @c NSJSONSerialization is not a dictionary, and an error was
  599. expected. We are expecting to receive the original network error wrapped in an @c NSError
  600. with the code @c FIRAuthInternalErrorCodeUnexpectedErrorResponse with the decoded response
  601. in the @c NSError.userInfo dictionary associated with the key
  602. @c FIRAuthErrorUserInfoDeserializedResponseKey.
  603. */
  604. - (void)testNonDictionaryErrorResponse {
  605. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  606. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  607. __block NSError *callbackError;
  608. __block BOOL callbackInvoked;
  609. [_RPCImplementation postWithRequest:request
  610. response:response
  611. callback:^(NSError *error) {
  612. callbackInvoked = YES;
  613. callbackError = error;
  614. }];
  615. // We are responding with a JSON-encoded string value representing an array - which is unexpected.
  616. // It should normally be a dictionary, and we need to check for this sort of thing. Because we can
  617. // successfully decode this value, however, we do return it in the error results. We check for
  618. // this array later in the test.
  619. NSData *data = [@"[]" dataUsingEncoding:NSUTF8StringEncoding];
  620. NSError *error = [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{}];
  621. [_RPCIssuer respondWithData:data error:error];
  622. XCTAssert(callbackInvoked);
  623. XCTAssertNotNil(callbackError);
  624. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  625. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  626. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  627. XCTAssertNotNil(underlyingError);
  628. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  629. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  630. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  631. XCTAssertNotNil(underlyingUnderlyingError);
  632. XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  633. XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
  634. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  635. XCTAssertNotNil(deserializedResponse);
  636. XCTAssert([deserializedResponse isKindOfClass:[NSArray class]]);
  637. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  638. XCTAssertNil(dataResponse);
  639. }
  640. /** @fn testNonDictionarySuccessResponse
  641. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  642. response deserialized by @c NSJSONSerialization is not a dictionary, and no error was
  643. expected. We are expecting to receive an @c NSError with the code
  644. @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded response in the
  645. @c NSError.userInfo dictionary associated with the key
  646. @c FIRAuthErrorUserInfoDecodedResponseKey.
  647. */
  648. - (void)testNonDictionarySuccessResponse {
  649. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  650. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  651. __block NSError *callbackError;
  652. __block BOOL callbackInvoked;
  653. [_RPCImplementation postWithRequest:request
  654. response:response
  655. callback:^(NSError *error) {
  656. callbackInvoked = YES;
  657. callbackError = error;
  658. }];
  659. // We are responding with a JSON-encoded string value representing an array - which is unexpected.
  660. // It should normally be a dictionary, and we need to check for this sort of thing. Because we can
  661. // successfully decode this value, however, we do return it in the error results. We check for
  662. // this array later in the test.
  663. NSData *data = [@"[]" dataUsingEncoding:NSUTF8StringEncoding];
  664. [_RPCIssuer respondWithData:data error:nil];
  665. XCTAssert(callbackInvoked);
  666. XCTAssertNotNil(callbackError);
  667. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  668. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  669. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  670. XCTAssertNotNil(underlyingError);
  671. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  672. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedResponse);
  673. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  674. XCTAssertNil(underlyingUnderlyingError);
  675. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  676. XCTAssertNotNil(deserializedResponse);
  677. XCTAssert([deserializedResponse isKindOfClass:[NSArray class]]);
  678. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  679. XCTAssertNil(dataResponse);
  680. }
  681. /** @fn testCaptchaRequiredResponse
  682. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  683. we get an error message indicating captcha is required. The backend should not be returning
  684. this error to mobile clients. If it does, we should wrap it in an @c NSError with the code
  685. @c FIRAuthInternalErrorCodeUnexpectedErrorResponse with the decoded error message in the
  686. @c NSError.userInfo dictionary associated with the key
  687. @c FIRAuthErrorUserInfoDeserializedResponseKey.
  688. */
  689. - (void)testCaptchaRequiredResponse {
  690. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  691. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  692. __block NSError *callbackError;
  693. __block BOOL callbackInvoked;
  694. [_RPCImplementation postWithRequest:request
  695. response:response
  696. callback:^(NSError *error) {
  697. callbackInvoked = YES;
  698. callbackError = error;
  699. }];
  700. NSError *error = [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{}];
  701. [_RPCIssuer respondWithServerErrorMessage:kErrorMessageCaptchaRequired error:error];
  702. XCTAssert(callbackInvoked);
  703. XCTAssertNotNil(callbackError);
  704. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  705. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  706. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  707. XCTAssertNotNil(underlyingError);
  708. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  709. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  710. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  711. XCTAssertNotNil(underlyingUnderlyingError);
  712. XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  713. XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
  714. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  715. XCTAssertNotNil(deserializedResponse);
  716. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  717. XCTAssertNotNil(deserializedResponse[@"message"]);
  718. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  719. XCTAssertNil(dataResponse);
  720. }
  721. /** @fn testCaptchaCheckFailedResponse
  722. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  723. we get an error message indicating captcha check failed. The backend should not be returning
  724. this error to mobile clients. If it does, we should wrap it in an @c NSError with the code
  725. @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded error message in the
  726. @c NSError.userInfo dictionary associated with the key
  727. @c FIRAuthErrorUserInfoDecodedErrorResponseKey.
  728. */
  729. - (void)testCaptchaCheckFailedResponse {
  730. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  731. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  732. __block NSError *callbackError;
  733. __block BOOL callbackInvoked;
  734. [_RPCImplementation postWithRequest:request
  735. response:response
  736. callback:^(NSError *error) {
  737. callbackInvoked = YES;
  738. callbackError = error;
  739. }];
  740. NSError *error = [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{}];
  741. [_RPCIssuer respondWithServerErrorMessage:kErrorMessageCaptchaCheckFailed error:error];
  742. XCTAssert(callbackInvoked);
  743. XCTAssertNotNil(callbackError);
  744. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  745. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeCaptchaCheckFailed);
  746. }
  747. /** @fn testCaptchaRequiredInvalidPasswordResponse
  748. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  749. we get an error message indicating captcha is required and an invalid password was entered.
  750. The backend should not be returning this error to mobile clients. If it does, we should wrap
  751. it in an @c NSError with the code
  752. @c FIRAuthInternalErrorCodeUnexpectedErrorResponse with the decoded error message in the
  753. @c NSError.userInfo dictionary associated with the key
  754. @c FIRAuthErrorUserInfoDeserializedResponseKey.
  755. */
  756. - (void)testCaptchaRequiredInvalidPasswordResponse {
  757. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  758. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  759. __block NSError *callbackError;
  760. __block BOOL callbackInvoked;
  761. [_RPCImplementation postWithRequest:request
  762. response:response
  763. callback:^(NSError *error) {
  764. callbackInvoked = YES;
  765. callbackError = error;
  766. }];
  767. NSError *error = [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{}];
  768. [_RPCIssuer respondWithServerErrorMessage:kErrorMessageCaptchaRequiredInvalidPassword
  769. error:error];
  770. XCTAssert(callbackInvoked);
  771. XCTAssertNotNil(callbackError);
  772. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  773. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  774. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  775. XCTAssertNotNil(underlyingError);
  776. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  777. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  778. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  779. XCTAssertNotNil(underlyingUnderlyingError);
  780. XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  781. XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
  782. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  783. XCTAssertNotNil(deserializedResponse);
  784. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  785. XCTAssertNotNil(deserializedResponse[@"message"]);
  786. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  787. XCTAssertNil(dataResponse);
  788. }
  789. /** @fn testDecodableErrorResponseWithUnknownMessage
  790. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  791. response deserialized by @c NSJSONSerialization represents a valid error response (and an
  792. error was indicated) but we didn't receive an error message we know about. We are expecting
  793. to receive the original network error wrapped in an @c NSError with the code
  794. @c FIRAuthInternalErrorCodeUnexpectedErrorResponse with the decoded
  795. error message in the @c NSError.userInfo dictionary associated with the key
  796. @c FIRAuthErrorUserInfoDeserializedResponseKey.
  797. */
  798. - (void)testDecodableErrorResponseWithUnknownMessage {
  799. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  800. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  801. __block NSError *callbackError;
  802. __block BOOL callbackInvoked;
  803. [_RPCImplementation postWithRequest:request
  804. response:response
  805. callback:^(NSError *error) {
  806. callbackInvoked = YES;
  807. callbackError = error;
  808. }];
  809. // We need to return a valid "error" response here, but we are going to intentionally use a bogus
  810. // error message.
  811. NSError *error = [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{}];
  812. [_RPCIssuer respondWithServerErrorMessage:kUnknownServerErrorMessage error:error];
  813. XCTAssert(callbackInvoked);
  814. XCTAssertNotNil(callbackError);
  815. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  816. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  817. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  818. XCTAssertNotNil(underlyingError);
  819. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  820. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  821. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  822. XCTAssertNotNil(underlyingUnderlyingError);
  823. XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  824. XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
  825. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  826. XCTAssertNotNil(deserializedResponse);
  827. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  828. XCTAssertNotNil(deserializedResponse[@"message"]);
  829. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  830. XCTAssertNil(dataResponse);
  831. }
  832. /** @fn testErrorResponseWithNoErrorMessage
  833. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  834. response deserialized by @c NSJSONSerialization is a dictionary, and an error was indicated,
  835. but no error information was present in the decoded response. We are expecting to receive
  836. the original network error wrapped in an @c NSError with the code
  837. @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded
  838. response message in the @c NSError.userInfo dictionary associated with the key
  839. @c FIRAuthErrorUserInfoDeserializedResponseKey.
  840. */
  841. - (void)testErrorResponseWithNoErrorMessage {
  842. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  843. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  844. __block NSError *callbackError;
  845. __block BOOL callbackInvoked;
  846. [_RPCImplementation postWithRequest:request
  847. response:response
  848. callback:^(NSError *error) {
  849. callbackInvoked = YES;
  850. callbackError = error;
  851. }];
  852. NSError *error = [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{}];
  853. [_RPCIssuer respondWithJSON:@{} error:error];
  854. XCTAssert(callbackInvoked);
  855. XCTAssertNotNil(callbackError);
  856. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  857. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  858. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  859. XCTAssertNotNil(underlyingError);
  860. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  861. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  862. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  863. XCTAssertNotNil(underlyingUnderlyingError);
  864. XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  865. XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
  866. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  867. XCTAssertNotNil(deserializedResponse);
  868. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  869. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  870. XCTAssertNil(dataResponse);
  871. }
  872. /** @fn testClientErrorResponse
  873. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  874. response contains a client error specified by an error messsage sent from the backend.
  875. */
  876. - (void)testClientErrorResponse {
  877. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  878. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  879. __block NSError *callbackerror;
  880. __block BOOL callBackInvoked;
  881. [_RPCImplementation postWithRequest:request
  882. response:response
  883. callback:^(NSError *error) {
  884. callBackInvoked = YES;
  885. callbackerror = error;
  886. }];
  887. NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
  888. NSString *customErrorMessage =
  889. [NSString stringWithFormat:@"%@%@%@", kUserDisabledErrorMessage, kServerErrorDetailMarker,
  890. kFakeUserDisabledCustomErrorMessage];
  891. [_RPCIssuer respondWithServerErrorMessage:customErrorMessage error:error];
  892. XCTAssertNotNil(callbackerror, @"An error should be returned from callback.");
  893. XCTAssert(callBackInvoked);
  894. XCTAssertEqual(callbackerror.code, FIRAuthErrorCodeUserDisabled);
  895. NSString *customMessage = callbackerror.userInfo[NSLocalizedDescriptionKey];
  896. XCTAssertEqualObjects(customMessage, kFakeUserDisabledCustomErrorMessage);
  897. }
  898. /** @fn testUndecodableSuccessResponse
  899. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  900. response isn't decodable by the response class but no error condition was expected. We are
  901. expecting to receive an @c NSError with the code
  902. @c FIRAuthErrorCodeUnexpectedServerResponse and the error from @c setWithDictionary:error:
  903. as the value of the underlyingError.
  904. */
  905. - (void)testUndecodableSuccessResponse {
  906. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  907. FIRFakeResponse *response = [FIRFakeResponse fakeResponseWithDecodingError];
  908. __block NSError *callbackError;
  909. __block BOOL callbackInvoked;
  910. [_RPCImplementation postWithRequest:request
  911. response:response
  912. callback:^(NSError *error) {
  913. callbackInvoked = YES;
  914. callbackError = error;
  915. }];
  916. // It doesn't matter what we respond with here, as long as it's not an error response. The fake
  917. // response will deterministicly simulate a decoding error regardless of the response value it was
  918. // given.
  919. [_RPCIssuer respondWithJSON:@{}];
  920. XCTAssert(callbackInvoked);
  921. XCTAssertNotNil(callbackError);
  922. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  923. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  924. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  925. XCTAssertNotNil(underlyingError);
  926. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  927. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeRPCResponseDecodingError);
  928. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  929. XCTAssertNotNil(deserializedResponse);
  930. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  931. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  932. XCTAssertNil(dataResponse);
  933. }
  934. /** @fn testSuccessfulResponse
  935. @brief Tests that a decoded dictionary is handed to the response instance.
  936. */
  937. - (void)testSuccessfulResponse {
  938. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  939. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  940. __block NSError *callbackError;
  941. __block BOOL callbackInvoked;
  942. [_RPCImplementation postWithRequest:request
  943. response:response
  944. callback:^(NSError *error) {
  945. callbackInvoked = YES;
  946. callbackError = error;
  947. }];
  948. [_RPCIssuer respondWithJSON:@{kTestKey : kTestValue}];
  949. XCTAssert(callbackInvoked);
  950. XCTAssertNil(callbackError);
  951. XCTAssertNotNil(response.receivedDictionary);
  952. XCTAssertEqualObjects(response.receivedDictionary[kTestKey], kTestValue);
  953. }
  954. @end