FIRAuthBackendRPCImplementationTests.m 40 KB

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