FIRSendVerificationCodeResponseTests.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "FIRAuthAppCredential.h"
  18. #import "FIRAuthErrors.h"
  19. #import "FIRAuthErrorUtils.h"
  20. #import "FIRAuthBackend.h"
  21. #import "FIRSendVerificationCodeRequest.h"
  22. #import "FIRSendVerificationCodeResponse.h"
  23. #import "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 kInvalidPhoneNumberErrorMessage
  53. @brief This is the error message the server will respond with if an incorrectly formatted phone
  54. number is provided.
  55. */
  56. static NSString *const kInvalidPhoneNumberErrorMessage = @"INVALID_PHONE_NUMBER";
  57. /** @var kQuotaExceededErrorMessage
  58. @brief This is the error message the server will respond with if the quota for SMS text messages
  59. has been exceeded for the project.
  60. */
  61. static NSString *const kQuotaExceededErrorMessage = @"QUOTA_EXCEEDED";
  62. /** @var kAppNotVerifiedErrorMessage
  63. @brief This is the error message the server will respond with if Firebase could not verify the
  64. app during a phone authentication flow.
  65. */
  66. static NSString *const kAppNotVerifiedErrorMessage = @"APP_NOT_VERIFIED";
  67. /** @class FIRSendVerificationCodeResponseTests
  68. @brief Tests for @c FIRSendVerificationCodeResponseTests.
  69. */
  70. @interface FIRSendVerificationCodeResponseTests : XCTestCase
  71. @end
  72. @implementation FIRSendVerificationCodeResponseTests {
  73. /** @var _RPCIssuer
  74. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  75. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  76. */
  77. FIRFakeBackendRPCIssuer *_RPCIssuer;
  78. }
  79. - (void)setUp {
  80. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  81. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  82. _RPCIssuer = RPCIssuer;
  83. }
  84. - (void)tearDown {
  85. _RPCIssuer = nil;
  86. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  87. [super tearDown];
  88. }
  89. /** @fn testSendVerificationCodeResponseInvalidPhoneNumber
  90. @brief Tests a failed attempt to send a verification code with an invalid phone number.
  91. */
  92. - (void)testSendVerificationCodeResponseInvalidPhoneNumber {
  93. FIRAuthAppCredential *credential =
  94. [[FIRAuthAppCredential alloc]initWithReceipt:kTestReceipt secret:kTestSecret];
  95. FIRSendVerificationCodeRequest *request =
  96. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestInvalidPhoneNumber
  97. appCredential:credential
  98. APIKey:kTestAPIKey];
  99. __block BOOL callbackInvoked;
  100. __block FIRSendVerificationCodeResponse *RPCResponse;
  101. __block NSError *RPCError;
  102. [FIRAuthBackend sendVerificationCode:request
  103. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  104. NSError *_Nullable error) {
  105. RPCResponse = response;
  106. RPCError = error;
  107. callbackInvoked = YES;
  108. }];
  109. [_RPCIssuer respondWithServerErrorMessage:kInvalidPhoneNumberErrorMessage];
  110. XCTAssert(callbackInvoked);
  111. XCTAssertNil(RPCResponse);
  112. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidPhoneNumber);
  113. }
  114. /** @fn testSendVerificationCodeResponseQuotaExceededError
  115. @brief Tests a failed attempt to send a verification code due to SMS quota having been exceeded.
  116. */
  117. - (void)testSendVerificationCodeResponseQuotaExceededError {
  118. FIRAuthAppCredential *credential =
  119. [[FIRAuthAppCredential alloc]initWithReceipt:kTestReceipt secret:kTestSecret];
  120. FIRSendVerificationCodeRequest *request =
  121. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  122. appCredential:credential
  123. APIKey:kTestAPIKey];
  124. __block BOOL callbackInvoked;
  125. __block FIRSendVerificationCodeResponse *RPCResponse;
  126. __block NSError *RPCError;
  127. [FIRAuthBackend sendVerificationCode:request
  128. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  129. NSError *_Nullable error) {
  130. RPCResponse = response;
  131. RPCError = error;
  132. callbackInvoked = YES;
  133. }];
  134. [_RPCIssuer respondWithServerErrorMessage:kQuotaExceededErrorMessage];
  135. XCTAssert(callbackInvoked);
  136. XCTAssertNil(RPCResponse);
  137. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeQuotaExceeded);
  138. }
  139. /** @fn testSendVerificationCodeResponseAppNotVerifiedError
  140. @brief Tests a failed attempt to send a verification code due to Firebase not being able to
  141. verify the app.
  142. */
  143. - (void)testSendVerificationCodeResponseAppNotVerifiedError {
  144. FIRAuthAppCredential *credential =
  145. [[FIRAuthAppCredential alloc]initWithReceipt:kTestReceipt secret:kTestSecret];
  146. FIRSendVerificationCodeRequest *request =
  147. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  148. appCredential:credential
  149. APIKey:kTestAPIKey];
  150. __block BOOL callbackInvoked;
  151. __block FIRSendVerificationCodeResponse *RPCResponse;
  152. __block NSError *RPCError;
  153. [FIRAuthBackend sendVerificationCode:request
  154. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  155. NSError *_Nullable error) {
  156. RPCResponse = response;
  157. RPCError = error;
  158. callbackInvoked = YES;
  159. }];
  160. [_RPCIssuer respondWithServerErrorMessage:kAppNotVerifiedErrorMessage];
  161. XCTAssert(callbackInvoked);
  162. XCTAssertNil(RPCResponse);
  163. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeAppNotVerified);
  164. }
  165. /** @fn testSuccessfulSendVerificationCodeResponse
  166. @brief Tests a succesful to send a verification code.
  167. */
  168. - (void)testSuccessfulSendVerificationCodeResponse {
  169. FIRAuthAppCredential *credential =
  170. [[FIRAuthAppCredential alloc]initWithReceipt:kTestReceipt secret:kTestSecret];
  171. FIRSendVerificationCodeRequest *request =
  172. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  173. appCredential:credential
  174. APIKey:kTestAPIKey];
  175. __block BOOL callbackInvoked;
  176. __block FIRSendVerificationCodeResponse *RPCResponse;
  177. __block NSError *RPCError;
  178. [FIRAuthBackend sendVerificationCode:request
  179. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  180. NSError *_Nullable error) {
  181. RPCResponse = response;
  182. RPCError = error;
  183. callbackInvoked = YES;
  184. }];
  185. [_RPCIssuer respondWithJSON:@{
  186. kVerificationIDKey : kFakeVerificationID
  187. }];
  188. XCTAssert(callbackInvoked);
  189. XCTAssertNotNil(RPCResponse);
  190. XCTAssertEqualObjects(RPCResponse.verificationID, kFakeVerificationID);
  191. }
  192. @end