FIRIdentityToolkitRequestTests.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. request.useStaging = YES;
  66. NSString *expectedURL = [NSString
  67. stringWithFormat:
  68. @"https://staging-www.sandbox.googleapis.com/identitytoolkit/v3/relyingparty/%@?key=%@",
  69. kEndpoint, kAPIKey];
  70. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  71. }
  72. /** @fn testInitWithEndpointUseIdentityPlatformExpectedRequestURL
  73. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  74. request inputs when the Identity Platform endpoint is specified.
  75. */
  76. - (void)testInitWithEndpointUseIdentityPlatformExpectedRequestURL {
  77. FIRAuthRequestConfiguration *requestConfiguration =
  78. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  79. FIRIdentityToolkitRequest *request =
  80. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  81. requestConfiguration:requestConfiguration];
  82. request.useIdentityPlatform = YES;
  83. NSString *expectedURL = [NSString
  84. stringWithFormat:@"https://identitytoolkit.googleapis.com/v2/%@?key=%@", kEndpoint, kAPIKey];
  85. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  86. }
  87. /** @fn testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL
  88. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  89. request inputs when the Identity Platform and staging endpoint is specified.
  90. */
  91. - (void)testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL {
  92. FIRAuthRequestConfiguration *requestConfiguration =
  93. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  94. FIRIdentityToolkitRequest *request =
  95. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  96. requestConfiguration:requestConfiguration];
  97. request.useIdentityPlatform = YES;
  98. request.useStaging = YES;
  99. NSString *expectedURL = [NSString
  100. stringWithFormat:@"https://staging-identitytoolkit.sandbox.googleapis.com/v2/%@?key=%@",
  101. kEndpoint, kAPIKey];
  102. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  103. }
  104. /** @fn testInitWithEndpointUseEmulatorExpectedRequestURL
  105. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  106. request inputs when the emulator is used.
  107. */
  108. - (void)testInitWithEndpointUseEmulatorExpectedRequestURL {
  109. FIRAuthRequestConfiguration *requestConfiguration =
  110. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  111. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
  112. FIRIdentityToolkitRequest *request =
  113. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  114. requestConfiguration:requestConfiguration];
  115. NSString *expectedURL = [NSString
  116. stringWithFormat:@"http://%@/www.googleapis.com/identitytoolkit/v3/relyingparty/%@?key=%@",
  117. kEmulatorHostAndPort, kEndpoint, kAPIKey];
  118. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  119. }
  120. /** @fn testInitWithEndpointUseIdentityPlatformUseEmulatorExpectedRequestURL
  121. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  122. request inputs when the emulator is used with the Identity Platform endpoint.
  123. */
  124. - (void)testInitWithEndpointUseIdentityPlatformUseEmulatorExpectedRequestURL {
  125. FIRAuthRequestConfiguration *requestConfiguration =
  126. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID];
  127. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
  128. FIRIdentityToolkitRequest *request =
  129. [[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
  130. requestConfiguration:requestConfiguration];
  131. request.useIdentityPlatform = YES;
  132. NSString *expectedURL =
  133. [NSString stringWithFormat:@"http://%@/identitytoolkit.googleapis.com/v2/%@?key=%@",
  134. kEmulatorHostAndPort, kEndpoint, kAPIKey];
  135. XCTAssertEqualObjects(expectedURL, request.requestURL.absoluteString);
  136. }
  137. @end