FIRSendVerificationCodeResponseTests.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FirebaseAuth/Sources/Public/FIRAuthErrors.h"
  18. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeResponse.h"
  21. #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredential.h"
  22. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  23. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  24. /** @var kTestAPIKey
  25. @brief Fake API key used for testing.
  26. */
  27. static NSString *const kTestAPIKey = @"APIKey";
  28. /** @var kTestPhoneNumber
  29. @brief Fake phone number used for testing.
  30. */
  31. static NSString *const kTestPhoneNumber = @"12345678";
  32. /** @var kTestInvalidPhoneNumber
  33. @brief An invalid testing phone number.
  34. */
  35. static NSString *const kTestInvalidPhoneNumber = @"555+!*55555";
  36. /** @var kVerificationIDKey
  37. @brief Fake key for the test verification ID.
  38. */
  39. static NSString *const kVerificationIDKey = @"sessionInfo";
  40. /** @var kFakeVerificationID
  41. @brief Fake verification ID for testing.
  42. */
  43. static NSString *const kFakeVerificationID = @"testVerificationID";
  44. /** @var kTestSecret
  45. @brief Fake secret used for testing.
  46. */
  47. static NSString *const kTestSecret = @"secret";
  48. /** @var kTestReceipt
  49. @brief Fake receipt used for testing.
  50. */
  51. static NSString *const kTestReceipt = @"receipt";
  52. /** @var kTestReCAPTCHAToken
  53. @brief Fake reCAPTCHA token used for testing.
  54. */
  55. static NSString *const kTestReCAPTCHAToken = @"reCAPTCHAToken";
  56. /** @var kInvalidPhoneNumberErrorMessage
  57. @brief This is the error message the server will respond with if an incorrectly formatted phone
  58. number is provided.
  59. */
  60. static NSString *const kInvalidPhoneNumberErrorMessage = @"INVALID_PHONE_NUMBER";
  61. /** @var kQuotaExceededErrorMessage
  62. @brief This is the error message the server will respond with if the quota for SMS text messages
  63. has been exceeded for the project.
  64. */
  65. static NSString *const kQuotaExceededErrorMessage = @"QUOTA_EXCEEDED";
  66. /** @var kAppNotVerifiedErrorMessage
  67. @brief This is the error message the server will respond with if Firebase could not verify the
  68. app during a phone authentication flow.
  69. */
  70. static NSString *const kAppNotVerifiedErrorMessage = @"APP_NOT_VERIFIED";
  71. /** @var kCaptchaCheckFailedErrorMessage
  72. @brief This is the error message the server will respond with if the reCAPTCHA token provided is
  73. invalid.
  74. */
  75. static NSString *const kCaptchaCheckFailedErrorMessage = @"CAPTCHA_CHECK_FAILED";
  76. /** @class FIRSendVerificationCodeResponseTests
  77. @brief Tests for @c FIRSendVerificationCodeResponseTests.
  78. */
  79. @interface FIRSendVerificationCodeResponseTests : XCTestCase
  80. @end
  81. @implementation FIRSendVerificationCodeResponseTests {
  82. /** @var _RPCIssuer
  83. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  84. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  85. */
  86. FIRFakeBackendRPCIssuer *_RPCIssuer;
  87. /** @var _requestConfiguration
  88. @brief This is the request configuration used for testing.
  89. */
  90. FIRAuthRequestConfiguration *_requestConfiguration;
  91. }
  92. - (void)setUp {
  93. [super setUp];
  94. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  95. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  96. _RPCIssuer = RPCIssuer;
  97. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  98. }
  99. - (void)tearDown {
  100. _requestConfiguration = nil;
  101. _RPCIssuer = nil;
  102. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  103. [super tearDown];
  104. }
  105. /** @fn testSendVerificationCodeResponseInvalidPhoneNumber
  106. @brief Tests a failed attempt to send a verification code with an invalid phone number.
  107. */
  108. - (void)testSendVerificationCodeResponseInvalidPhoneNumber {
  109. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  110. secret:kTestSecret];
  111. FIRSendVerificationCodeRequest *request =
  112. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestInvalidPhoneNumber
  113. appCredential:credential
  114. reCAPTCHAToken:nil
  115. requestConfiguration:_requestConfiguration];
  116. __block BOOL callbackInvoked;
  117. __block FIRSendVerificationCodeResponse *RPCResponse;
  118. __block NSError *RPCError;
  119. [FIRAuthBackend sendVerificationCode:request
  120. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  121. NSError *_Nullable error) {
  122. RPCResponse = response;
  123. RPCError = error;
  124. callbackInvoked = YES;
  125. }];
  126. [_RPCIssuer respondWithServerErrorMessage:kInvalidPhoneNumberErrorMessage];
  127. XCTAssert(callbackInvoked);
  128. XCTAssertNil(RPCResponse);
  129. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidPhoneNumber);
  130. }
  131. /** @fn testSendVerificationCodeResponseQuotaExceededError
  132. @brief Tests a failed attempt to send a verification code due to SMS quota having been exceeded.
  133. */
  134. - (void)testSendVerificationCodeResponseQuotaExceededError {
  135. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  136. secret:kTestSecret];
  137. FIRSendVerificationCodeRequest *request =
  138. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  139. appCredential:credential
  140. reCAPTCHAToken:nil
  141. requestConfiguration:_requestConfiguration];
  142. __block BOOL callbackInvoked;
  143. __block FIRSendVerificationCodeResponse *RPCResponse;
  144. __block NSError *RPCError;
  145. [FIRAuthBackend sendVerificationCode:request
  146. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  147. NSError *_Nullable error) {
  148. RPCResponse = response;
  149. RPCError = error;
  150. callbackInvoked = YES;
  151. }];
  152. [_RPCIssuer respondWithServerErrorMessage:kQuotaExceededErrorMessage];
  153. XCTAssert(callbackInvoked);
  154. XCTAssertNil(RPCResponse);
  155. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeQuotaExceeded);
  156. }
  157. /** @fn testSendVerificationCodeResponseAppNotVerifiedError
  158. @brief Tests a failed attempt to send a verification code due to Firebase not being able to
  159. verify the app.
  160. */
  161. - (void)testSendVerificationCodeResponseAppNotVerifiedError {
  162. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  163. secret:kTestSecret];
  164. FIRSendVerificationCodeRequest *request =
  165. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  166. appCredential:credential
  167. reCAPTCHAToken:nil
  168. requestConfiguration:_requestConfiguration];
  169. __block BOOL callbackInvoked;
  170. __block FIRSendVerificationCodeResponse *RPCResponse;
  171. __block NSError *RPCError;
  172. [FIRAuthBackend sendVerificationCode:request
  173. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  174. NSError *_Nullable error) {
  175. RPCResponse = response;
  176. RPCError = error;
  177. callbackInvoked = YES;
  178. }];
  179. [_RPCIssuer respondWithServerErrorMessage:kAppNotVerifiedErrorMessage];
  180. XCTAssert(callbackInvoked);
  181. XCTAssertNil(RPCResponse);
  182. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeAppNotVerified);
  183. }
  184. /** @fn testSendVerificationCodeResponseCaptchaCheckFailedError
  185. @brief Tests a failed attempt to send a verification code due to an invalid reCAPTCHA token
  186. being provided in the request.
  187. */
  188. - (void)testSendVerificationCodeResponseCaptchaCheckFailedError {
  189. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  190. secret:kTestSecret];
  191. FIRSendVerificationCodeRequest *request =
  192. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  193. appCredential:credential
  194. reCAPTCHAToken:kTestReCAPTCHAToken
  195. requestConfiguration:_requestConfiguration];
  196. __block BOOL callbackInvoked;
  197. __block FIRSendVerificationCodeResponse *RPCResponse;
  198. __block NSError *RPCError;
  199. [FIRAuthBackend sendVerificationCode:request
  200. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  201. NSError *_Nullable error) {
  202. RPCResponse = response;
  203. RPCError = error;
  204. callbackInvoked = YES;
  205. }];
  206. [_RPCIssuer respondWithServerErrorMessage:kCaptchaCheckFailedErrorMessage];
  207. XCTAssert(callbackInvoked);
  208. XCTAssertNil(RPCResponse);
  209. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeCaptchaCheckFailed);
  210. }
  211. /** @fn testSuccessfulSendVerificationCodeResponse
  212. @brief Tests a succesful to send a verification code.
  213. */
  214. - (void)testSuccessfulSendVerificationCodeResponse {
  215. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  216. secret:kTestSecret];
  217. FIRSendVerificationCodeRequest *request =
  218. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  219. appCredential:credential
  220. reCAPTCHAToken:nil
  221. requestConfiguration:_requestConfiguration];
  222. __block BOOL callbackInvoked;
  223. __block FIRSendVerificationCodeResponse *RPCResponse;
  224. __block NSError *RPCError;
  225. [FIRAuthBackend sendVerificationCode:request
  226. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  227. NSError *_Nullable error) {
  228. RPCResponse = response;
  229. RPCError = error;
  230. callbackInvoked = YES;
  231. }];
  232. [_RPCIssuer respondWithJSON:@{kVerificationIDKey : kFakeVerificationID}];
  233. XCTAssert(callbackInvoked);
  234. XCTAssertNotNil(RPCResponse);
  235. XCTAssertEqualObjects(RPCResponse.verificationID, kFakeVerificationID);
  236. }
  237. @end