FIRSendVerificationCodeRequestTests.m 4.5 KB

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