FIRIdentityToolkitRequestTests.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2020 Google LLC
  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 "FirebaseAuth/Sources/Backend/FIRIdentityToolkitRequest.h"
  18. /** @var kEndpoint
  19. @brief The endpoint for the requests.
  20. */
  21. static NSString *const kEndpoint = @"endpoint";
  22. /** @var kAPIKey
  23. @brief A testing API Key.
  24. */
  25. static NSString *const kAPIKey = @"APIKey";
  26. /** @var kFirebaseAppID
  27. @brief A testing Firebase app ID.
  28. */
  29. static NSString *const kFirebaseAppID = @"appID";
  30. /** @var kEmulatorHostAndPort
  31. @brief A testing emulator host and port.
  32. */
  33. static NSString *const kEmulatorHostAndPort = @"emulatorhost:12345";
  34. /** @class FIRIdentityToolkitRequestTests
  35. @brief Tests for @c FIRIdentityToolkitRequest
  36. */
  37. @interface FIRIdentityToolkitRequestTests : XCTestCase
  38. @end
  39. @implementation FIRIdentityToolkitRequestTests
  40. /** @fn testInitWithEndpointExpectedRequestURL
  41. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  42. request inputs.
  43. */
  44. - (void)testInitWithEndpointExpectedRequestURL {
  45. FIRAuthRequestConfiguration *requestConfiguration =
  46. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  47. FIRIdentityToolkitRequest *request =
  48. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  49. requestConfiguration:requestConfiguration];
  50. NSString *expectedURL = [NSString
  51. stringWithFormat:@"https://www.googleapis.com/identitytoolkit/v3/relyingparty/%@?key=%@",
  52. kEndpoint, kAPIKey];
  53. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  54. }
  55. /** @fn testInitWithEndpointUseStagingExpectedRequestURL
  56. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  57. request inputs when the staging endpoint is specified.
  58. */
  59. - (void)testInitWithEndpointUseStagingExpectedRequestURL {
  60. FIRAuthRequestConfiguration *requestConfiguration =
  61. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  62. FIRIdentityToolkitRequest *request =
  63. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  64. requestConfiguration:requestConfiguration
  65. useIdentityPlatform:NO
  66. useStaging:YES];
  67. NSString *expectedURL = [NSString
  68. stringWithFormat:
  69. @"https://staging-www.sandbox.googleapis.com/identitytoolkit/v3/relyingparty/%@?key=%@",
  70. kEndpoint, kAPIKey];
  71. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  72. }
  73. /** @fn testInitWithEndpointUseIdentityPlatformExpectedRequestURL
  74. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  75. request inputs when the Identity Platform endpoint is specified.
  76. */
  77. - (void)testInitWithEndpointUseIdentityPlatformExpectedRequestURL {
  78. FIRAuthRequestConfiguration *requestConfiguration =
  79. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  80. FIRIdentityToolkitRequest *request =
  81. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  82. requestConfiguration:requestConfiguration
  83. useIdentityPlatform:YES
  84. useStaging:NO];
  85. NSString *expectedURL = [NSString
  86. stringWithFormat:@"https://identitytoolkit.googleapis.com/v2/%@?key=%@", kEndpoint, kAPIKey];
  87. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  88. }
  89. /** @fn testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL
  90. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  91. request inputs when the Identity Platform and staging endpoint is specified.
  92. */
  93. - (void)testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL {
  94. FIRAuthRequestConfiguration *requestConfiguration =
  95. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  96. FIRIdentityToolkitRequest *request =
  97. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  98. requestConfiguration:requestConfiguration
  99. useIdentityPlatform:YES
  100. useStaging:YES];
  101. NSString *expectedURL = [NSString
  102. stringWithFormat:@"https://staging-identitytoolkit.sandbox.googleapis.com/v2/%@?key=%@",
  103. kEndpoint, kAPIKey];
  104. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  105. }
  106. /** @fn testInitWithEndpointUseEmulatorExpectedRequestURL
  107. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  108. request inputs when the emulator is used.
  109. */
  110. - (void)testInitWithEndpointUseEmulatorExpectedRequestURL {
  111. FIRAuthRequestConfiguration *requestConfiguration =
  112. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  113. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
  114. FIRIdentityToolkitRequest *request =
  115. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  116. requestConfiguration:requestConfiguration];
  117. NSString *expectedURL = [NSString
  118. stringWithFormat:@"http://%@/www.googleapis.com/identitytoolkit/v3/relyingparty/%@?key=%@",
  119. kEmulatorHostAndPort, kEndpoint, kAPIKey];
  120. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  121. }
  122. /** @fn testInitWithEndpointUseIdentityPlatformUseEmulatorExpectedRequestURL
  123. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  124. request inputs when the emulator is used with the Identity Platform endpoint.
  125. */
  126. - (void)testInitWithEndpointUseIdentityPlatformUseEmulatorExpectedRequestURL {
  127. FIRAuthRequestConfiguration *requestConfiguration =
  128. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  129. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
  130. FIRIdentityToolkitRequest *request =
  131. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  132. requestConfiguration:requestConfiguration
  133. useIdentityPlatform:YES
  134. useStaging:NO];
  135. NSString *expectedURL =
  136. [NSString stringWithFormat:@"http://%@/identitytoolkit.googleapis.com/v2/%@?key=%@",
  137. kEmulatorHostAndPort, kEndpoint, kAPIKey];
  138. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  139. }
  140. @end