IdentityToolkitRequestTests.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import XCTest
  16. @testable import FirebaseAuth
  17. import FirebaseCore
  18. /// @class IdentityToolkitRequestTests
  19. /// @brief Tests for @c IdentityToolkitRequest
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. class IdentityToolkitRequestTests: XCTestCase {
  22. let kEndpoint = "endpoint"
  23. let kAPIKey = "APIKey"
  24. let kEmulatorHostAndPort = "emulatorhost:12345"
  25. /** @fn testInitWithEndpointExpectedRequestURL
  26. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  27. request inputs.
  28. */
  29. func testInitWithEndpointExpectedRequestURL() {
  30. let requestConfiguration = AuthRequestConfiguration(apiKey: kAPIKey, appID: "appID")
  31. let request = IdentityToolkitRequest(endpoint: kEndpoint,
  32. requestConfiguration: requestConfiguration)
  33. let expectedURL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/\(kEndpoint)" +
  34. "?key=\(kAPIKey)"
  35. XCTAssertEqual(expectedURL, request.requestURL().absoluteString)
  36. }
  37. /** @fn testInitWithEndpointUseStagingExpectedRequestURL
  38. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  39. request inputs when the staging endpoint is specified.
  40. */
  41. func testInitWithEndpointUseStagingExpectedRequestURL() {
  42. let requestConfiguration = AuthRequestConfiguration(apiKey: kAPIKey, appID: "appID")
  43. let request = IdentityToolkitRequest(endpoint: kEndpoint,
  44. requestConfiguration: requestConfiguration,
  45. useStaging: true)
  46. let expectedURL = "https://staging-www.sandbox.googleapis.com/identitytoolkit/v3/" +
  47. "relyingparty/\(kEndpoint)?key=\(kAPIKey)"
  48. XCTAssertEqual(expectedURL, request.requestURL().absoluteString)
  49. }
  50. /** @fn testInitWithEndpointUseIdentityPlatformExpectedRequestURL
  51. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  52. request inputs when the Identity Platform endpoint is specified.
  53. */
  54. func testInitWithEndpointUseIdentityPlatformExpectedRequestURL() {
  55. let requestConfiguration = AuthRequestConfiguration(apiKey: kAPIKey, appID: "appID")
  56. let request = IdentityToolkitRequest(endpoint: kEndpoint,
  57. requestConfiguration: requestConfiguration,
  58. useIdentityPlatform: true)
  59. let expectedURL = "https://identitytoolkit.googleapis.com/v2/\(kEndpoint)?key=\(kAPIKey)"
  60. XCTAssertEqual(expectedURL, request.requestURL().absoluteString)
  61. }
  62. /** @fn testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL
  63. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  64. request inputs when the Identity Platform and staging endpoint is specified.
  65. */
  66. func testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL() {
  67. let requestConfiguration = AuthRequestConfiguration(apiKey: kAPIKey, appID: "appID")
  68. let request = IdentityToolkitRequest(endpoint: kEndpoint,
  69. requestConfiguration: requestConfiguration,
  70. useIdentityPlatform: true,
  71. useStaging: true)
  72. let expectedURL = "https://staging-identitytoolkit.sandbox.googleapis.com/v2" +
  73. "/\(kEndpoint)?key=\(kAPIKey)"
  74. XCTAssertEqual(expectedURL, request.requestURL().absoluteString)
  75. }
  76. /** @fn testInitWithEndpointUseEmulatorExpectedRequestURL
  77. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  78. request inputs when the emulator is used.
  79. */
  80. func testInitWithEndpointUseEmulatorExpectedRequestURL() {
  81. let requestConfiguration = AuthRequestConfiguration(apiKey: kAPIKey, appID: "appID")
  82. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort
  83. let request = IdentityToolkitRequest(endpoint: kEndpoint,
  84. requestConfiguration: requestConfiguration)
  85. let expectedURL = "http://\(kEmulatorHostAndPort)/www.googleapis.com/identitytoolkit/v3/" +
  86. "relyingparty/\(kEndpoint)?key=\(kAPIKey)"
  87. XCTAssertEqual(expectedURL, request.requestURL().absoluteString)
  88. }
  89. /** @fn testInitWithEndpointUseIdentityPlatformUseEmulatorExpectedRequestURL
  90. @brief Tests the @c requestURL method to make sure the URL it produces corresponds to the
  91. request inputs when the emulator is used with the Identity Platform endpoint.
  92. */
  93. func testInitWithEndpointUseIdentityPlatformUseEmulatorExpectedRequestURL() {
  94. let requestConfiguration = AuthRequestConfiguration(apiKey: kAPIKey, appID: "appID")
  95. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort
  96. let request = IdentityToolkitRequest(endpoint: kEndpoint,
  97. requestConfiguration: requestConfiguration,
  98. useIdentityPlatform: true)
  99. let expectedURL = "http://\(kEmulatorHostAndPort)/identitytoolkit.googleapis.com/v2/" +
  100. "\(kEndpoint)?key=\(kAPIKey)"
  101. XCTAssertEqual(expectedURL, request.requestURL().absoluteString)
  102. }
  103. /** @fn testExpectedTenantIDWithNonDefaultFIRApp
  104. @brief Tests the request correctly populated the tenant ID from a non default app.
  105. */
  106. func testExpectedTenantIDWithNonDefaultFIRApp() {
  107. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  108. gcmSenderID: "00000000000000000-00000000000-000000000")
  109. options.apiKey = kAPIKey
  110. let nonDefaultApp = FirebaseApp(instanceWithName: "nonDefaultApp", options: options)
  111. let nonDefaultAuth = Auth(app: nonDefaultApp)
  112. nonDefaultAuth.tenantID = "tenant-id"
  113. let requestConfiguration = AuthRequestConfiguration(apiKey: kAPIKey, appID: "appID",
  114. auth: nonDefaultAuth)
  115. requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort
  116. let request = IdentityToolkitRequest(endpoint: kEndpoint,
  117. requestConfiguration: requestConfiguration,
  118. useIdentityPlatform: true)
  119. XCTAssertEqual("tenant-id", request.tenantID)
  120. }
  121. }