FIRIdentityToolkitRequestTests.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. request.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. request.useIdentityPlatform = YES;
  84. NSString *expectedURL = [NSString
  85. stringWithFormat:@"https://identitytoolkit.googleapis.com/v2/%@?key=%@", kEndpoint, kAPIKey];
  86. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  87. }
  88. /** @fn testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL
  89. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  90. request inputs when the Identity Platform and staging endpoint is specified.
  91. */
  92. - (void)testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL {
  93. FIRAuthRequestConfiguration *requestConfiguration =
  94. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  95. FIRIdentityToolkitRequest *request =
  96. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  97. requestConfiguration:requestConfiguration];
  98. request.useIdentityPlatform = YES;
  99. request.useStaging = YES;
  100. NSString *expectedURL = [NSString
  101. stringWithFormat:@"https://staging-identitytoolkit.sandbox.googleapis.com/v2/%@?key=%@",
  102. kEndpoint, kAPIKey];
  103. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  104. }
  105. /** @fn testInitWithEndpointUseEmulatorExpectedRequestURL
  106. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  107. request inputs when the emulator is used.
  108. */
  109. - (void)testInitWithEndpointUseEmulatorExpectedRequestURL {
  110. FIRAuthRequestConfiguration *requestConfiguration =
  111. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  112. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
  113. FIRIdentityToolkitRequest *request =
  114. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  115. requestConfiguration:requestConfiguration];
  116. NSString *expectedURL = [NSString
  117. stringWithFormat:@"http://%@/www.googleapis.com/identitytoolkit/v3/relyingparty/%@?key=%@",
  118. kEmulatorHostAndPort, kEndpoint, kAPIKey];
  119. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  120. }
  121. /** @fn testInitWithEndpointUseIdentityPlatformUseEmulatorExpectedRequestURL
  122. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  123. request inputs when the emulator is used with the Identity Platform endpoint.
  124. */
  125. - (void)testInitWithEndpointUseIdentityPlatformUseEmulatorExpectedRequestURL {
  126. FIRAuthRequestConfiguration *requestConfiguration =
  127. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  128. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
  129. FIRIdentityToolkitRequest *request =
  130. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  131. requestConfiguration:requestConfiguration];
  132. request.useIdentityPlatform = YES;
  133. NSString *expectedURL =
  134. [NSString stringWithFormat:@"http://%@/identitytoolkit.googleapis.com/v2/%@?key=%@",
  135. kEmulatorHostAndPort, kEndpoint, kAPIKey];
  136. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  137. }
  138. /** @fn testExpectedTenantIDWithNonDefaultFIRApp
  139. @brief Tests the request correctly populated the tenant ID from a non default app.
  140. */
  141. - (void)testExpectedTenantIDWithNonDefaultFIRApp {
  142. FIRApp *nonDefaultApp = [FIRApp appForAuthUnitTestsWithName:@"nonDefaultApp"];
  143. FIRAuth *nonDefaultAuth = [FIRAuth authWithApp:nonDefaultApp];
  144. nonDefaultAuth.tenantID = @"tenant-id";
  145. FIRAuthRequestConfiguration *requestConfiguration =
  146. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
  147. appID:kFirebaseAppID
  148. auth:nonDefaultAuth];
  149. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
  150. FIRIdentityToolkitRequest *request =
  151. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  152. requestConfiguration:requestConfiguration];
  153. request.useIdentityPlatform = YES;
  154. XCTAssertEqualObjects(@"tenant-id", request.tenantID);
  155. }
  156. @end