FIRSendVerificationCodeResponseTests.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /** @var _requestConfiguration
  79. @brief This is the request configuration used for testing.
  80. */
  81. FIRAuthRequestConfiguration *_requestConfiguration;
  82. }
  83. - (void)setUp {
  84. [super setUp];
  85. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  86. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  87. _RPCIssuer = RPCIssuer;
  88. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  89. }
  90. - (void)tearDown {
  91. _requestConfiguration = nil;
  92. _RPCIssuer = nil;
  93. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  94. [super tearDown];
  95. }
  96. /** @fn testSendVerificationCodeResponseInvalidPhoneNumber
  97. @brief Tests a failed attempt to send a verification code with an invalid phone number.
  98. */
  99. - (void)testSendVerificationCodeResponseInvalidPhoneNumber {
  100. FIRAuthAppCredential *credential =
  101. [[FIRAuthAppCredential alloc]initWithReceipt:kTestReceipt secret:kTestSecret];
  102. FIRSendVerificationCodeRequest *request =
  103. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestInvalidPhoneNumber
  104. appCredential:credential
  105. requestConfiguration:_requestConfiguration];
  106. __block BOOL callbackInvoked;
  107. __block FIRSendVerificationCodeResponse *RPCResponse;
  108. __block NSError *RPCError;
  109. [FIRAuthBackend sendVerificationCode:request
  110. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  111. NSError *_Nullable error) {
  112. RPCResponse = response;
  113. RPCError = error;
  114. callbackInvoked = YES;
  115. }];
  116. [_RPCIssuer respondWithServerErrorMessage:kInvalidPhoneNumberErrorMessage];
  117. XCTAssert(callbackInvoked);
  118. XCTAssertNil(RPCResponse);
  119. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidPhoneNumber);
  120. }
  121. /** @fn testSendVerificationCodeResponseQuotaExceededError
  122. @brief Tests a failed attempt to send a verification code due to SMS quota having been exceeded.
  123. */
  124. - (void)testSendVerificationCodeResponseQuotaExceededError {
  125. FIRAuthAppCredential *credential =
  126. [[FIRAuthAppCredential alloc]initWithReceipt:kTestReceipt secret:kTestSecret];
  127. FIRSendVerificationCodeRequest *request =
  128. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  129. appCredential:credential
  130. requestConfiguration:_requestConfiguration];
  131. __block BOOL callbackInvoked;
  132. __block FIRSendVerificationCodeResponse *RPCResponse;
  133. __block NSError *RPCError;
  134. [FIRAuthBackend sendVerificationCode:request
  135. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  136. NSError *_Nullable error) {
  137. RPCResponse = response;
  138. RPCError = error;
  139. callbackInvoked = YES;
  140. }];
  141. [_RPCIssuer respondWithServerErrorMessage:kQuotaExceededErrorMessage];
  142. XCTAssert(callbackInvoked);
  143. XCTAssertNil(RPCResponse);
  144. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeQuotaExceeded);
  145. }
  146. /** @fn testSendVerificationCodeResponseAppNotVerifiedError
  147. @brief Tests a failed attempt to send a verification code due to Firebase not being able to
  148. verify the app.
  149. */
  150. - (void)testSendVerificationCodeResponseAppNotVerifiedError {
  151. FIRAuthAppCredential *credential =
  152. [[FIRAuthAppCredential alloc]initWithReceipt:kTestReceipt secret:kTestSecret];
  153. FIRSendVerificationCodeRequest *request =
  154. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  155. appCredential:credential
  156. requestConfiguration:_requestConfiguration];
  157. __block BOOL callbackInvoked;
  158. __block FIRSendVerificationCodeResponse *RPCResponse;
  159. __block NSError *RPCError;
  160. [FIRAuthBackend sendVerificationCode:request
  161. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  162. NSError *_Nullable error) {
  163. RPCResponse = response;
  164. RPCError = error;
  165. callbackInvoked = YES;
  166. }];
  167. [_RPCIssuer respondWithServerErrorMessage:kAppNotVerifiedErrorMessage];
  168. XCTAssert(callbackInvoked);
  169. XCTAssertNil(RPCResponse);
  170. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeAppNotVerified);
  171. }
  172. /** @fn testSuccessfulSendVerificationCodeResponse
  173. @brief Tests a succesful to send a verification code.
  174. */
  175. - (void)testSuccessfulSendVerificationCodeResponse {
  176. FIRAuthAppCredential *credential =
  177. [[FIRAuthAppCredential alloc]initWithReceipt:kTestReceipt secret:kTestSecret];
  178. FIRSendVerificationCodeRequest *request =
  179. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  180. appCredential:credential
  181. requestConfiguration:_requestConfiguration];
  182. __block BOOL callbackInvoked;
  183. __block FIRSendVerificationCodeResponse *RPCResponse;
  184. __block NSError *RPCError;
  185. [FIRAuthBackend sendVerificationCode:request
  186. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  187. NSError *_Nullable error) {
  188. RPCResponse = response;
  189. RPCError = error;
  190. callbackInvoked = YES;
  191. }];
  192. [_RPCIssuer respondWithJSON:@{
  193. kVerificationIDKey : kFakeVerificationID
  194. }];
  195. XCTAssert(callbackInvoked);
  196. XCTAssertNotNil(RPCResponse);
  197. XCTAssertEqualObjects(RPCResponse.verificationID, kFakeVerificationID);
  198. }
  199. @end