Skip to content

Commit e1cd312

Browse files
committed
updating unit tests
1 parent 527fd31 commit e1cd312

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

FirebaseAuth/Tests/Unit/StartPasskeySignInResponseTests.swift

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,39 @@
3535
XCTAssertEqual(response.challenge, "FAKE_CHALLENGE")
3636
}
3737

38-
func testInitWithMissingCredentialRequestOptions() {
39-
let dict: [String: AnyHashable] = [:]
40-
XCTAssertThrowsError(try StartPasskeySignInResponse(dictionary: dict)) { error in
41-
let nsError = error as NSError
42-
XCTAssertEqual(nsError.domain, AuthErrorDomain)
43-
XCTAssertEqual(nsError.code, AuthErrorCode.internalError.rawValue)
38+
/// Helper function to remove nested field from dictionary
39+
private func removeField(_ dict: inout [String: AnyHashable], keyPath: [String]) {
40+
guard let first = keyPath.first else { return }
41+
if keyPath.count == 1 {
42+
dict.removeValue(forKey: first)
43+
} else if var inDict = dict[first] as? [String: AnyHashable] {
44+
removeField(&inDict, keyPath: Array(keyPath.dropFirst()))
45+
dict[first] = inDict
4446
}
4547
}
4648

47-
func testInitWithMissingRpId() {
48-
var dict = makeValidDictionary()
49-
if var options = dict["credentialRequestOptions"] as? [String: AnyHashable] {
50-
options.removeValue(forKey: "rpId")
51-
dict["credentialRequestOptions"] = options
49+
func testInitWithInvalidDictionary() throws {
50+
struct TestCase {
51+
let name: String
52+
let removeFieldPath: [String]
5253
}
53-
XCTAssertThrowsError(try StartPasskeySignInResponse(dictionary: dict)) { error in
54-
let nsError = error as NSError
55-
XCTAssertEqual(nsError.domain, AuthErrorDomain)
56-
XCTAssertEqual(nsError.code, AuthErrorCode.internalError.rawValue)
57-
}
58-
}
59-
60-
func testInitWithMissingChallenge() {
61-
var dict = makeValidDictionary()
62-
if var options = dict["credentialRequestOptions"] as? [String: AnyHashable] {
63-
options.removeValue(forKey: "challenge")
64-
dict["credentialRequestOptions"] = options
65-
}
66-
XCTAssertThrowsError(try StartPasskeySignInResponse(dictionary: dict)) { error in
67-
let nsError = error as NSError
68-
XCTAssertEqual(nsError.domain, AuthErrorDomain)
69-
XCTAssertEqual(nsError.code, AuthErrorCode.internalError.rawValue)
54+
let cases: [TestCase] = [
55+
.init(name: "Missing credential options", removeFieldPath: ["credentialRequestOptions"]),
56+
.init(name: "Missing rpId", removeFieldPath: ["credentialRequestOptions", "rpId"]),
57+
.init(
58+
name: "Missing challenge",
59+
removeFieldPath: ["credentialRequestOptions", "challenge"]
60+
),
61+
]
62+
for testCase in cases {
63+
var dict = makeValidDictionary()
64+
removeField(&dict, keyPath: testCase.removeFieldPath)
65+
XCTAssertThrowsError(try StartPasskeySignInResponse(dictionary: dict),
66+
testCase.name) { error in
67+
let nsError = error as NSError
68+
XCTAssertEqual(nsError.domain, AuthErrorDomain)
69+
XCTAssertEqual(nsError.code, AuthErrorCode.internalError.rawValue)
70+
}
7071
}
7172
}
7273
}

0 commit comments

Comments
 (0)