FIRAuthBackendRPCImplementationTests.m 42 KB

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