FIRAuthBackendRPCImplementationTests.m 50 KB

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