FIRIdentityToolkitRequestTests.m 8.2 KB

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