FIRAuthBackendRPCImplementationTests.m 51 KB

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