FIRSendVerificationCodeRequestTests.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/Backend/FIRAuthBackend.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeResponse.h"
  22. #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredential.h"
  23. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  24. /** @var kTestAPIKey
  25. @brief Fake API key used for testing.
  26. */
  27. static NSString *const kTestAPIKey = @"APIKey";
  28. /** @var kTestFirebaseAppID
  29. @brief Fake Firebase app ID used for testing.
  30. */
  31. static NSString *const kTestFirebaseAppID = @"appID";
  32. /** @var kTestPhoneNumber
  33. @brief Fake phone number used for testing.
  34. */
  35. static NSString *const kTestPhoneNumber = @"12345678";
  36. /** @var kTestSecret
  37. @brief Fake secret used for testing.
  38. */
  39. static NSString *const kTestSecret = @"secret";
  40. /** @var kTestReceipt
  41. @brief Fake receipt used for testing.
  42. */
  43. static NSString *const kTestReceipt = @"receipt";
  44. /** @var kTestReCAPTCHAToken
  45. @brief Fake reCAPTCHA token used for testing.
  46. */
  47. static NSString *const kTestReCAPTCHAToken = @"reCAPTCHAToken";
  48. /** @var kPhoneNumberKey
  49. @brief The key for the "phone number" value in the request.
  50. */
  51. static NSString *const kPhoneNumberKey = @"phoneNumber";
  52. /** @var kReceiptKey
  53. @brief The key for the receipt parameter in the request.
  54. */
  55. static NSString *const kReceiptKey = @"iosReceipt";
  56. /** @var kSecretKey
  57. @brief The key for the Secret parameter in the request.
  58. */
  59. static NSString *const kSecretKey = @"iosSecret";
  60. /** @var kExpectedAPIURL
  61. @brief The expected URL for the test calls.
  62. */
  63. static NSString *const kExpectedAPIURL =
  64. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/sendVerificationCode?key=APIKey";
  65. /** @class FIRSendVerificationCodeRequestTests
  66. @brief Tests for @c FIRSendVerificationCodeRequest.
  67. */
  68. @interface FIRSendVerificationCodeRequestTests : XCTestCase
  69. @end
  70. @implementation FIRSendVerificationCodeRequestTests {
  71. /** @var _RPCIssuer
  72. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  73. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  74. */
  75. FIRFakeBackendRPCIssuer *_RPCIssuer;
  76. }
  77. - (void)setUp {
  78. [super setUp];
  79. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  80. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  81. _RPCIssuer = RPCIssuer;
  82. }
  83. - (void)tearDown {
  84. _RPCIssuer = nil;
  85. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  86. [super tearDown];
  87. }
  88. /** @fn testSendVerificationCodeRequest
  89. @brief Tests the sendVerificationCode request.
  90. */
  91. - (void)testSendVerificationCodeRequest {
  92. FIRAuthRequestConfiguration *requestConfiguration =
  93. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  94. FIRAuthAppCredential *credential = [[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt
  95. secret:kTestSecret];
  96. FIRSendVerificationCodeRequest *request =
  97. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:kTestPhoneNumber
  98. appCredential:credential
  99. reCAPTCHAToken:kTestReCAPTCHAToken
  100. requestConfiguration:requestConfiguration];
  101. XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
  102. XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
  103. XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
  104. XCTAssertEqualObjects(request.reCAPTCHAToken, kTestReCAPTCHAToken);
  105. [FIRAuthBackend sendVerificationCode:request
  106. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  107. NSError *_Nullable error){
  108. }];
  109. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  110. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  111. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPhoneNumberKey], kTestPhoneNumber);
  112. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPhoneNumberKey], kTestPhoneNumber);
  113. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kReceiptKey], kTestReceipt);
  114. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kSecretKey], kTestSecret);
  115. }
  116. @end
  117. #endif