FIRSendVerificationCodeResponseTests.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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;
  20. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  21. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  22. /** @var kTestAPIKey
  23. @brief Fake API key used for testing.
  24. */
  25. static NSString *const kTestAPIKey = @"APIKey";
  26. /** @var kTestFirebaseAppID
  27. @brief Fake Firebase app ID used for testing.
  28. */
  29. static NSString *const kTestFirebaseAppID = @"appID";
  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. appID:kTestFirebaseAppID];
  101. }
  102. - (void)tearDown {
  103. _requestConfiguration = nil;
  104. _RPCIssuer = nil;
  105. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  106. [super tearDown];
  107. }
  108. /** @fn testSendVerificationCodeResponseInvalidPhoneNumber
  109. @brief Tests a failed attempt to send a verification code with an invalid phone number.
  110. */
  111. - (void)testSendVerificationCodeResponseInvalidPhoneNumber {
  112. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  113. secret:kTestSecret];
  114. FIRSendVerificationCodeRequest *request =
  115. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestInvalidPhoneNumber
  116. appCredential:credential
  117. reCAPTCHAToken:nil
  118. requestConfiguration:_requestConfiguration];
  119. __block BOOL callbackInvoked;
  120. __block FIRSendVerificationCodeResponse *RPCResponse;
  121. __block NSError *RPCError;
  122. [FIRAuthBackend sendVerificationCode:request
  123. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  124. NSError *_Nullable error) {
  125. RPCResponse = response;
  126. RPCError = error;
  127. callbackInvoked = YES;
  128. }];
  129. [_RPCIssuer respondWithServerErrorMessage:kInvalidPhoneNumberErrorMessage];
  130. XCTAssert(callbackInvoked);
  131. XCTAssertNil(RPCResponse);
  132. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidPhoneNumber);
  133. }
  134. /** @fn testSendVerificationCodeResponseQuotaExceededError
  135. @brief Tests a failed attempt to send a verification code due to SMS quota having been exceeded.
  136. */
  137. - (void)testSendVerificationCodeResponseQuotaExceededError {
  138. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  139. secret:kTestSecret];
  140. FIRSendVerificationCodeRequest *request =
  141. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  142. appCredential:credential
  143. reCAPTCHAToken:nil
  144. requestConfiguration:_requestConfiguration];
  145. __block BOOL callbackInvoked;
  146. __block FIRSendVerificationCodeResponse *RPCResponse;
  147. __block NSError *RPCError;
  148. [FIRAuthBackend sendVerificationCode:request
  149. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  150. NSError *_Nullable error) {
  151. RPCResponse = response;
  152. RPCError = error;
  153. callbackInvoked = YES;
  154. }];
  155. [_RPCIssuer respondWithServerErrorMessage:kQuotaExceededErrorMessage];
  156. XCTAssert(callbackInvoked);
  157. XCTAssertNil(RPCResponse);
  158. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeQuotaExceeded);
  159. }
  160. /** @fn testSendVerificationCodeResponseAppNotVerifiedError
  161. @brief Tests a failed attempt to send a verification code due to Firebase not being able to
  162. verify the app.
  163. */
  164. - (void)testSendVerificationCodeResponseAppNotVerifiedError {
  165. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  166. secret:kTestSecret];
  167. FIRSendVerificationCodeRequest *request =
  168. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  169. appCredential:credential
  170. reCAPTCHAToken:nil
  171. requestConfiguration:_requestConfiguration];
  172. __block BOOL callbackInvoked;
  173. __block FIRSendVerificationCodeResponse *RPCResponse;
  174. __block NSError *RPCError;
  175. [FIRAuthBackend sendVerificationCode:request
  176. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  177. NSError *_Nullable error) {
  178. RPCResponse = response;
  179. RPCError = error;
  180. callbackInvoked = YES;
  181. }];
  182. [_RPCIssuer respondWithServerErrorMessage:kAppNotVerifiedErrorMessage];
  183. XCTAssert(callbackInvoked);
  184. XCTAssertNil(RPCResponse);
  185. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeAppNotVerified);
  186. }
  187. /** @fn testSendVerificationCodeResponseCaptchaCheckFailedError
  188. @brief Tests a failed attempt to send a verification code due to an invalid reCAPTCHA token
  189. being provided in the request.
  190. */
  191. - (void)testSendVerificationCodeResponseCaptchaCheckFailedError {
  192. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  193. secret:kTestSecret];
  194. FIRSendVerificationCodeRequest *request =
  195. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  196. appCredential:credential
  197. reCAPTCHAToken:kTestReCAPTCHAToken
  198. requestConfiguration:_requestConfiguration];
  199. __block BOOL callbackInvoked;
  200. __block FIRSendVerificationCodeResponse *RPCResponse;
  201. __block NSError *RPCError;
  202. [FIRAuthBackend sendVerificationCode:request
  203. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  204. NSError *_Nullable error) {
  205. RPCResponse = response;
  206. RPCError = error;
  207. callbackInvoked = YES;
  208. }];
  209. [_RPCIssuer respondWithServerErrorMessage:kCaptchaCheckFailedErrorMessage];
  210. XCTAssert(callbackInvoked);
  211. XCTAssertNil(RPCResponse);
  212. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeCaptchaCheckFailed);
  213. }
  214. /** @fn testSuccessfulSendVerificationCodeResponse
  215. @brief Tests a successful to send a verification code.
  216. */
  217. - (void)testSuccessfulSendVerificationCodeResponse {
  218. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  219. secret:kTestSecret];
  220. FIRSendVerificationCodeRequest *request =
  221. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  222. appCredential:credential
  223. reCAPTCHAToken:nil
  224. requestConfiguration:_requestConfiguration];
  225. __block BOOL callbackInvoked;
  226. __block FIRSendVerificationCodeResponse *RPCResponse;
  227. __block NSError *RPCError;
  228. [FIRAuthBackend sendVerificationCode:request
  229. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  230. NSError *_Nullable error) {
  231. RPCResponse = response;
  232. RPCError = error;
  233. callbackInvoked = YES;
  234. }];
  235. [_RPCIssuer respondWithJSON:@{kVerificationIDKey : kFakeVerificationID}];
  236. XCTAssert(callbackInvoked);
  237. XCTAssertNotNil(RPCResponse);
  238. XCTAssertEqualObjects(RPCResponse.verificationID, kFakeVerificationID);
  239. }
  240. @end
  241. #endif