StartPasskeySignInResponseTests.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2025 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. #if os(iOS) || os(tvOS) || os(macOS)
  15. @testable import FirebaseAuth
  16. import XCTest
  17. @available(iOS 15.0, macOS 12.0, tvOS 16.0, *)
  18. final class StartPasskeySignInResponseTests: XCTestCase {
  19. private func makeValidDictionary() -> [String: AnyHashable] {
  20. return [
  21. "credentialRequestOptions": [
  22. "rpId": "FAKE_RPID",
  23. "challenge": "FAKE_CHALLENGE",
  24. ] as [String: AnyHashable],
  25. ]
  26. }
  27. func testInitWithValidDictionary() throws {
  28. let dict = makeValidDictionary()
  29. let response = try StartPasskeySignInResponse(dictionary: dict)
  30. XCTAssertEqual(response.rpID, "FAKE_RPID")
  31. XCTAssertEqual(response.challenge, "FAKE_CHALLENGE")
  32. }
  33. /// Helper function to remove nested field from dictionary
  34. private func removeField(_ dict: inout [String: AnyHashable], keyPath: [String]) {
  35. guard let first = keyPath.first else { return }
  36. if keyPath.count == 1 {
  37. dict.removeValue(forKey: first)
  38. } else if var inDict = dict[first] as? [String: AnyHashable] {
  39. removeField(&inDict, keyPath: Array(keyPath.dropFirst()))
  40. dict[first] = inDict
  41. }
  42. }
  43. func testInitWithInvalidDictionary() throws {
  44. struct TestCase {
  45. let name: String
  46. let removeFieldPath: [String]
  47. }
  48. let cases: [TestCase] = [
  49. .init(name: "Missing credential options", removeFieldPath: ["credentialRequestOptions"]),
  50. .init(name: "Missing rpId", removeFieldPath: ["credentialRequestOptions", "rpId"]),
  51. .init(
  52. name: "Missing challenge",
  53. removeFieldPath: ["credentialRequestOptions", "challenge"]
  54. ),
  55. ]
  56. for testCase in cases {
  57. var dict = makeValidDictionary()
  58. removeField(&dict, keyPath: testCase.removeFieldPath)
  59. XCTAssertThrowsError(try StartPasskeySignInResponse(dictionary: dict),
  60. testCase.name) { error in
  61. let nsError = error as NSError
  62. XCTAssertEqual(nsError.domain, AuthErrorDomain)
  63. XCTAssertEqual(nsError.code, AuthErrorCode.internalError.rawValue)
  64. }
  65. }
  66. }
  67. }
  68. #endif