FIRAuthBackendRPCImplementationTests.m 39 KB

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