FIRSendVerificationCodeRequestTests.m 4.2 KB

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