|
| 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 | + |
| 15 | +#if os(iOS) || os(tvOS) || os(macOS) |
| 16 | + |
| 17 | + @testable import FirebaseAuth |
| 18 | + import XCTest |
| 19 | + |
| 20 | + @available(iOS 15.0, macOS 12.0, tvOS 16.0, *) |
| 21 | + class StartPasskeyEnrollmentResponseTests: XCTestCase { |
| 22 | + private func makeValidDictionary() -> [String: AnyHashable] { |
| 23 | + return [ |
| 24 | + "credentialCreationOptions": [ |
| 25 | + "rp": ["id": "example.com"] as [String: AnyHashable], |
| 26 | + "user": ["id": "USER_123"] as [String: AnyHashable], |
| 27 | + "challenge": "FAKE_CHALLENGE" as String, |
| 28 | + ] as [String: AnyHashable], |
| 29 | + ] |
| 30 | + } |
| 31 | + |
| 32 | + func testInitWithValidDictionary() throws { |
| 33 | + let response = try StartPasskeyEnrollmentResponse(dictionary: makeValidDictionary()) |
| 34 | + XCTAssertEqual(response.rpID, "example.com") |
| 35 | + XCTAssertEqual(response.userID, "USER_123") |
| 36 | + XCTAssertEqual(response.challenge, "FAKE_CHALLENGE") |
| 37 | + } |
| 38 | + |
| 39 | + func testInitWithMissingCredentialCreationOptionsThrowsError() { |
| 40 | + let invalidDict: [String: AnyHashable] = [:] |
| 41 | + XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: invalidDict)) |
| 42 | + } |
| 43 | + |
| 44 | + func testInitWithMissingRpThrowsError() { |
| 45 | + var dict = makeValidDictionary() |
| 46 | + if var options = dict["credentialCreationOptions"] as? [String: Any] { |
| 47 | + options.removeValue(forKey: "rp") |
| 48 | + dict["credentialCreationOptions"] = options as? AnyHashable |
| 49 | + } |
| 50 | + XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict)) |
| 51 | + } |
| 52 | + |
| 53 | + func testInitWithMissingRpIdThrowsError() { |
| 54 | + var dict = makeValidDictionary() |
| 55 | + if var options = dict["credentialCreationOptions"] as? [String: Any], |
| 56 | + var rp = options["rp"] as? [String: Any] { |
| 57 | + rp.removeValue(forKey: "id") |
| 58 | + options["rp"] = rp |
| 59 | + dict["credentialCreationOptions"] = options as? AnyHashable |
| 60 | + } |
| 61 | + XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict)) |
| 62 | + } |
| 63 | + |
| 64 | + func testInitWithMissingUserThrowsError() { |
| 65 | + var dict = makeValidDictionary() |
| 66 | + if var options = dict["credentialCreationOptions"] as? [String: Any] { |
| 67 | + options.removeValue(forKey: "user") |
| 68 | + dict["credentialCreationOptions"] = options as? AnyHashable |
| 69 | + } |
| 70 | + XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict)) |
| 71 | + } |
| 72 | + |
| 73 | + func testInitWithMissingUserIdThrowsError() { |
| 74 | + var dict = makeValidDictionary() |
| 75 | + if var options = dict["credentialCreationOptions"] as? [String: Any], |
| 76 | + var user = options["user"] as? [String: Any] { |
| 77 | + user.removeValue(forKey: "id") |
| 78 | + options["user"] = user |
| 79 | + dict["credentialCreationOptions"] = options as? AnyHashable |
| 80 | + } |
| 81 | + XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict)) |
| 82 | + } |
| 83 | + |
| 84 | + func testInitWithMissingChallengeThrowsError() { |
| 85 | + var dict = makeValidDictionary() |
| 86 | + if var options = dict["credentialCreationOptions"] as? [String: Any] { |
| 87 | + options.removeValue(forKey: "challenge") |
| 88 | + dict["credentialCreationOptions"] = options as? AnyHashable |
| 89 | + } |
| 90 | + XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict)) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +#endif |
0 commit comments