GetRecaptchaConfigTests.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  18. class GetRecaptchaConfigTests: RPCBaseTests {
  19. /** @fn testGetRecaptchaConfigRequest
  20. @brief Tests get Recaptcha config request.
  21. */
  22. func testGetRecaptchaConfigRequest() async throws {
  23. let request = GetRecaptchaConfigRequest(requestConfiguration: makeRequestConfiguration())
  24. // let _ = try await authBackend.call(with: request)
  25. XCTAssertNil(request.unencodedHTTPRequestBody)
  26. // Confirm that the request has no decoded body as it is get request.
  27. XCTAssertNil(rpcIssuer.decodedRequest)
  28. let urlString = "https://identitytoolkit.googleapis.com/v2/recaptchaConfig?key=\(kTestAPIKey)" +
  29. "&clientType=CLIENT_TYPE_IOS&version=RECAPTCHA_ENTERPRISE"
  30. try await checkRequest(
  31. request: request,
  32. expected: urlString,
  33. key: "should_be_empty_dictionary",
  34. value: nil
  35. )
  36. }
  37. /** @fn testSuccessfulGetRecaptchaConfigRequestRecaptchaEnabled
  38. @brief This test simulates a successful @c getRecaptchaConfig Flow when recaptcha is enabled.
  39. */
  40. func testSuccessfulGetRecaptchaConfigRequestRecaptchaEnabled() async throws {
  41. let kTestRecaptchaKey = "projects/123/keys/456"
  42. let request = GetRecaptchaConfigRequest(requestConfiguration: makeRequestConfiguration())
  43. rpcIssuer.recaptchaSiteKey = kTestRecaptchaKey
  44. let enforcementMode = "AUDIT"
  45. rpcIssuer.rceMode = enforcementMode
  46. let response = try await authBackend.call(with: request)
  47. XCTAssertEqual(response.recaptchaKey, kTestRecaptchaKey)
  48. XCTAssertEqual(
  49. response.enforcementState,
  50. [
  51. ["provider": "EMAIL_PASSWORD_PROVIDER", "enforcementState": enforcementMode],
  52. ["provider": "PHONE_PROVIDER", "enforcementState": enforcementMode],
  53. ]
  54. )
  55. }
  56. /** @fn testSuccessfulGetRecaptchaConfigRequestRecaptchaDisabled
  57. @brief This test simulates a successful @c getRecaptchaConfig Flow when recaptcha is disabled.
  58. */
  59. func testSuccessfulGetRecaptchaConfigRequestRecaptchaDisabled() async throws {
  60. let request = GetRecaptchaConfigRequest(requestConfiguration: makeRequestConfiguration())
  61. let enforcementMode = "OFF"
  62. rpcIssuer.rceMode = enforcementMode
  63. let response = try await authBackend.call(with: request)
  64. XCTAssertEqual(response.recaptchaKey, nil)
  65. XCTAssertEqual(
  66. response.enforcementState,
  67. [
  68. ["provider": "EMAIL_PASSWORD_PROVIDER", "enforcementState": enforcementMode],
  69. ["provider": "PHONE_PROVIDER", "enforcementState": enforcementMode],
  70. ]
  71. )
  72. }
  73. }