FIRSendVerificationCodeResponseTests.m 12 KB

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