Skip to content

Commit 62dd329

Browse files
committed
add unit tests
1 parent 534d81c commit 62dd329

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Tests/AuthenticatorTests/Mocks/MockAuthenticationService.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,11 @@ extension MockAuthenticationService {
263263
var isSignedIn: Bool
264264
}
265265
}
266+
267+
@available(iOS 17.4, macOS 13.5, visionOS 1.0, *)
268+
struct MockWebAuthnCredential: AuthWebAuthnCredential {
269+
var credentialId: String
270+
var friendlyName: String?
271+
var relyingPartyId: String
272+
var createdAt: Date
273+
}

Tests/AuthenticatorTests/States/PasskeyCreatedStateTests.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,65 @@ class PasskeyCreatedStateTests: XCTestCase {
9090
}
9191
}
9292
}
93+
94+
/// Given: A PasskeyCreatedState
95+
/// When: fetchPasskeyCredentials is called
96+
/// Then: Should fetch and populate passkey credentials
97+
func testPasskeyMetadata_shouldBeAvailable() async {
98+
// Mock passkey credentials
99+
let credential1 = MockWebAuthnCredential(
100+
credentialId: "cred1",
101+
friendlyName: "iPhone 15 Pro",
102+
relyingPartyId: "example.com",
103+
createdAt: Date()
104+
)
105+
authenticationService.mockedWebAuthnCredentials = [credential1]
106+
107+
await state.fetchPasskeyCredentials()
108+
109+
XCTAssertEqual(authenticationService.listWebAuthnCredentialsCount, 1)
110+
111+
await MainActor.run {
112+
XCTAssertEqual(state.passkeyCredentials.count, 1)
113+
XCTAssertEqual(state.passkeyCredentials.first?.credentialId, "cred1")
114+
XCTAssertEqual(state.passkeyCredentials.first?.friendlyName, "iPhone 15 Pro")
115+
}
116+
}
117+
118+
/// Given: A PasskeyCreatedState
119+
/// When: fetchPasskeyCredentials is called with multiple passkeys
120+
/// Then: Should fetch and display all passkeys
121+
func testMultiplePasskeys_shouldBeSupported() async {
122+
// Mock multiple passkey credentials
123+
let credential1 = MockWebAuthnCredential(
124+
credentialId: "cred1",
125+
friendlyName: "iPhone 15 Pro",
126+
relyingPartyId: "example.com",
127+
createdAt: Date()
128+
)
129+
let credential2 = MockWebAuthnCredential(
130+
credentialId: "cred2",
131+
friendlyName: "MacBook Pro",
132+
relyingPartyId: "example.com",
133+
createdAt: Date()
134+
)
135+
let credential3 = MockWebAuthnCredential(
136+
credentialId: "cred3",
137+
friendlyName: "iPad Air",
138+
relyingPartyId: "example.com",
139+
createdAt: Date()
140+
)
141+
authenticationService.mockedWebAuthnCredentials = [credential1, credential2, credential3]
142+
143+
await state.fetchPasskeyCredentials()
144+
145+
XCTAssertEqual(authenticationService.listWebAuthnCredentialsCount, 1)
146+
147+
await MainActor.run {
148+
XCTAssertEqual(state.passkeyCredentials.count, 3)
149+
XCTAssertEqual(state.passkeyCredentials[0].friendlyName, "iPhone 15 Pro")
150+
XCTAssertEqual(state.passkeyCredentials[1].friendlyName, "MacBook Pro")
151+
XCTAssertEqual(state.passkeyCredentials[2].friendlyName, "iPad Air")
152+
}
153+
}
93154
}

0 commit comments

Comments
 (0)