|
35 | 35 | XCTAssertEqual(response.challenge, "FAKE_CHALLENGE")
|
36 | 36 | }
|
37 | 37 |
|
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 |
44 | 46 | }
|
45 | 47 | }
|
46 | 48 |
|
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] |
52 | 53 | }
|
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 | + } |
70 | 71 | }
|
71 | 72 | }
|
72 | 73 | }
|
|
0 commit comments