FIRGetOOBConfirmationCodeResponseTests.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "FIRAuthErrors.h"
  18. #import "FIRAuthBackend.h"
  19. #import "FIRGetOOBConfirmationCodeRequest.h"
  20. #import "FIRGetOOBConfirmationCodeResponse.h"
  21. #import "FIRFakeBackendRPCIssuer.h"
  22. /** @var kTestEmail
  23. @brief Testing user email adadress.
  24. */
  25. static NSString *const kTestEmail = @"test@gmail.com";
  26. /** @var kTestAccessToken
  27. @brief Testing access token.
  28. */
  29. static NSString *const kTestAccessToken = @"ACCESS_TOKEN";
  30. /** @var kTestAPIKey
  31. @brief Fake API key used for testing.
  32. */
  33. static NSString *const kTestAPIKey = @"APIKey";
  34. /** @var kOOBCodeKey
  35. @brief The name of the field in the response JSON for the OOB code.
  36. */
  37. static NSString *const kOOBCodeKey = @"oobCode";
  38. /** @var kTestOOBCode
  39. @brief Fake OOB Code used for testing.
  40. */
  41. static NSString *const kTestOOBCode = @"OOBCode";
  42. /** @var kEmailNotFoundMessage
  43. @brief The value of the "message" field returned for an "email not found" error.
  44. */
  45. static NSString *const kEmailNotFoundMessage = @"EMAIL_NOT_FOUND: fake custom message";
  46. /** @var kInvalidEmailErrorMessage
  47. @brief The error returned by the server if the email is invalid.
  48. */
  49. static NSString *const kInvalidEmailErrorMessage = @"INVALID_EMAIL:";
  50. /** @var kInvalidMessagePayloadErrorMessage
  51. @brief This is the prefix for the error message the server responds with if an invalid message
  52. payload was sent.
  53. */
  54. static NSString *const kInvalidMessagePayloadErrorMessage = @"INVALID_MESSAGE_PAYLOAD";
  55. /** @var kInvalidSenderErrorMessage
  56. @brief This is the prefix for the error message the server responds with if invalid sender is
  57. used to send the email for updating user's email address.
  58. */
  59. static NSString *const kInvalidSenderErrorMessage = @"INVALID_SENDER";
  60. /** @var kInvalidRecipientEmailErrorMessage
  61. @brief This is the prefix for the error message the server responds with if the recipient email
  62. is invalid.
  63. */
  64. static NSString *const kInvalidRecipientEmailErrorMessage = @"INVALID_RECIPIENT_EMAIL";
  65. /** @class FIRGetOOBConfirmationCodeResponseTests
  66. @brief Tests for @c FIRGetOOBConfirmationCodeResponse.
  67. */
  68. @interface FIRGetOOBConfirmationCodeResponseTests : XCTestCase
  69. @end
  70. @implementation FIRGetOOBConfirmationCodeResponseTests {
  71. /** @var _RPCIssuer
  72. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  73. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  74. */
  75. FIRFakeBackendRPCIssuer *_RPCIssuer;
  76. }
  77. - (void)setUp {
  78. [super setUp];
  79. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  80. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  81. _RPCIssuer = RPCIssuer;
  82. }
  83. - (void)tearDown {
  84. _RPCIssuer = nil;
  85. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  86. [super tearDown];
  87. }
  88. /** @fn testSuccessfulPasswordResetResponse
  89. @brief This test simulates a complete password reset response (with OOB Code) and makes sure
  90. it succeeds, and we get the OOB Code decoded correctly.
  91. */
  92. - (void)testSuccessfulPasswordResetResponse {
  93. FIRGetOOBConfirmationCodeRequest *request =
  94. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  95. APIKey:kTestAPIKey];
  96. __block BOOL callbackInvoked;
  97. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  98. __block NSError *RPCError;
  99. [FIRAuthBackend getOOBConfirmationCode:request
  100. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  101. NSError *_Nullable error) {
  102. callbackInvoked = YES;
  103. RPCResponse = response;
  104. RPCError = error;
  105. }];
  106. [_RPCIssuer respondWithJSON:@{
  107. kOOBCodeKey : kTestOOBCode
  108. }];
  109. XCTAssert(callbackInvoked);
  110. XCTAssertNil(RPCError);
  111. XCTAssertNotNil(RPCResponse);
  112. XCTAssertEqualObjects(RPCResponse.OOBCode, kTestOOBCode);
  113. }
  114. /** @fn testSuccessfulPasswordResetResponseWithoutOOBCode
  115. @brief This test simulates a password reset request where we don't receive the optional OOBCode
  116. response value. It should still succeed.
  117. */
  118. - (void)testSuccessfulPasswordResetResponseWithoutOOBCode {
  119. FIRGetOOBConfirmationCodeRequest *request =
  120. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  121. APIKey:kTestAPIKey];
  122. __block BOOL callbackInvoked;
  123. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  124. __block NSError *RPCError;
  125. [FIRAuthBackend getOOBConfirmationCode:request
  126. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  127. NSError *_Nullable error) {
  128. callbackInvoked = YES;
  129. RPCResponse = response;
  130. RPCError = error;
  131. }];
  132. [_RPCIssuer respondWithJSON:@{}];
  133. XCTAssert(callbackInvoked);
  134. XCTAssertNil(RPCError);
  135. XCTAssertNotNil(RPCResponse);
  136. XCTAssertNil(RPCResponse.OOBCode);
  137. }
  138. /** @fn testEmailNotFoundError
  139. @brief This test checks for email not found responses, and makes sure they are decoded to the
  140. correct error response.
  141. */
  142. - (void)testEmailNotFoundError {
  143. FIRGetOOBConfirmationCodeRequest *request =
  144. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  145. APIKey:kTestAPIKey];
  146. __block BOOL callbackInvoked;
  147. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  148. __block NSError *RPCError;
  149. [FIRAuthBackend getOOBConfirmationCode:request
  150. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  151. NSError *_Nullable error) {
  152. callbackInvoked = YES;
  153. RPCResponse = response;
  154. RPCError = error;
  155. }];
  156. [_RPCIssuer respondWithServerErrorMessage:kEmailNotFoundMessage];
  157. XCTAssert(callbackInvoked);
  158. XCTAssertNotNil(RPCError);
  159. XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain);
  160. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeUserNotFound);
  161. XCTAssertNil(RPCResponse);
  162. }
  163. /** @fn testInvalidEmailError
  164. @brief This test checks for the INVALID_EMAIL error message from the backend.
  165. */
  166. - (void)testInvalidEmailError {
  167. FIRGetOOBConfirmationCodeRequest *request =
  168. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  169. APIKey:kTestAPIKey];
  170. __block BOOL callbackInvoked;
  171. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  172. __block NSError *RPCError;
  173. [FIRAuthBackend getOOBConfirmationCode:request
  174. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  175. NSError *_Nullable error) {
  176. callbackInvoked = YES;
  177. RPCResponse = response;
  178. RPCError = error;
  179. }];
  180. [_RPCIssuer respondWithServerErrorMessage:kInvalidEmailErrorMessage];
  181. XCTAssert(callbackInvoked);
  182. XCTAssertNotNil(RPCError);
  183. XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain);
  184. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  185. XCTAssertNil(RPCResponse);
  186. }
  187. /** @fn testInvalidMessagePayloadError
  188. @brief Tests for @c FIRAuthErrorCodeInvalidMessagePayload.
  189. */
  190. - (void)testInvalidMessagePayloadError {
  191. FIRGetOOBConfirmationCodeRequest *request =
  192. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  193. APIKey:kTestAPIKey];
  194. __block BOOL callbackInvoked;
  195. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  196. __block NSError *RPCError;
  197. [FIRAuthBackend getOOBConfirmationCode:request
  198. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  199. NSError *_Nullable error) {
  200. callbackInvoked = YES;
  201. RPCResponse = response;
  202. RPCError = error;
  203. }];
  204. [_RPCIssuer respondWithServerErrorMessage:kInvalidMessagePayloadErrorMessage];
  205. XCTAssert(callbackInvoked);
  206. XCTAssertNil(RPCResponse);
  207. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidMessagePayload);
  208. }
  209. /** @fn testInvalidSenderError
  210. @brief Tests for @c FIRAuthErrorCodeInvalidSender.
  211. */
  212. - (void)testInvalidSenderError {
  213. FIRGetOOBConfirmationCodeRequest *request =
  214. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  215. APIKey:kTestAPIKey];
  216. __block BOOL callbackInvoked;
  217. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  218. __block NSError *RPCError;
  219. [FIRAuthBackend getOOBConfirmationCode:request
  220. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  221. NSError *_Nullable error) {
  222. callbackInvoked = YES;
  223. RPCResponse = response;
  224. RPCError = error;
  225. }];
  226. [_RPCIssuer respondWithServerErrorMessage:kInvalidSenderErrorMessage];
  227. XCTAssert(callbackInvoked);
  228. XCTAssertNil(RPCResponse);
  229. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidSender);
  230. }
  231. /** @fn testInvalidRecipientEmailError
  232. @brief Tests for @c FIRAuthErrorCodeInvalidRecipientEmail.
  233. */
  234. - (void)testInvalidRecipientEmailError {
  235. FIRGetOOBConfirmationCodeRequest *request =
  236. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  237. APIKey:kTestAPIKey];
  238. __block BOOL callbackInvoked;
  239. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  240. __block NSError *RPCError;
  241. [FIRAuthBackend getOOBConfirmationCode:request
  242. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  243. NSError *_Nullable error) {
  244. callbackInvoked = YES;
  245. RPCResponse = response;
  246. RPCError = error;
  247. }];
  248. [_RPCIssuer respondWithServerErrorMessage:kInvalidRecipientEmailErrorMessage];
  249. XCTAssert(callbackInvoked);
  250. XCTAssertNil(RPCResponse);
  251. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidRecipientEmail);
  252. }
  253. /** @fn testSuccessfulEmailVerificationResponse
  254. @brief This test is really not much different than the original test for password reset. But
  255. it's here for completeness sake.
  256. */
  257. - (void)testSuccessfulEmailVerificationResponse {
  258. FIRGetOOBConfirmationCodeRequest *request =
  259. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  260. APIKey:kTestAPIKey];
  261. __block BOOL callbackInvoked;
  262. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  263. __block NSError *RPCError;
  264. [FIRAuthBackend getOOBConfirmationCode:request
  265. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  266. NSError *_Nullable error) {
  267. callbackInvoked = YES;
  268. RPCResponse = response;
  269. RPCError = error;
  270. }];
  271. [_RPCIssuer respondWithJSON:@{
  272. kOOBCodeKey : kTestOOBCode
  273. }];
  274. XCTAssert(callbackInvoked);
  275. XCTAssertNil(RPCError);
  276. XCTAssertNotNil(RPCResponse);
  277. XCTAssertEqualObjects(RPCResponse.OOBCode, kTestOOBCode);
  278. }
  279. @end