FIRSendVerificationCodeResponseTests.m 12 KB

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