FIRSendVerificationCodeRequestTests.m 4.8 KB

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