FakePasskeys

class FakePasskeys(origin: String, random: Random = Random.Default) : Passkeys

Drop-in Passkeys for unit tests: real WebAuthn responses, no device.

Behaves like a platform implementation: operations fail with PasskeyError.NotSupported when the matching capability flag is off, signals validate and compare ids by their bytes, and the RP ID is checked against origin (see FakeAuthenticator).

Operations are safe to call from concurrent coroutines. The public mutable collections savedPasswords and receivedSignals are not synchronised: mutate or inspect them from test code before or after calls, not concurrently with them.

Logs like the platform implementations do, through com.alirezaiyan.passki.PassKi.logHandler, so a test can assert what your app's users would see in logcat. A test that changes PassKi.logLevel or PassKi.logHandler should restore them in @AfterTest: they are process-wide.

Parameters

origin

the web origin written into clientDataJSON, e.g. https://example.com.

random

source for credential ids; pass a seeded Random for deterministic ids.

Samples

runTest {
        val device = FakePasskeys(origin = "https://example.com") // your server's allowed origin
        val created = device.register(TestOptions.creation("example.com")).getOrThrow()
        // send created.json to your server's verifier here
        val assertion = device.signIn(TestOptions.request("example.com")).getOrThrow()
        assertEquals(created.credentialId, assertion.credentialId)
    }

Constructors

Link copied to clipboard
constructor(origin: String, random: Random = Random.Default)

Properties

Link copied to clipboard

The backing authenticator; inspect FakeAuthenticator.storedCredentials in assertions.

Link copied to clipboard

SignInMode.Autofill is available (PasskeyCapabilities.autofill). When false, signIn(mode = Autofill) fails with PasskeyError.NotSupported. Default true.

Link copied to clipboard

upgrade (conditional create) is available (PasskeyCapabilities.conditionalCreate). When false, it fails with PasskeyError.NotSupported. Default true.

Link copied to clipboard

SignInMode.ImmediateOnly is available (PasskeyCapabilities.immediateSignIn). When false, signIn(mode = ImmediateOnly) fails with PasskeyError.NotSupported. Default true.

Link copied to clipboard

Passkeys can be used at all (PasskeyCapabilities.isSupported). When false, every operation fails with PasskeyError.NotSupported and capabilities reports every capability as false. Default true.

Link copied to clipboard

Every signal received through signals, in call order (only signals that passed validation). Inspect from test code before/after calls, not concurrently with them.

Link copied to clipboard

Username → password offered by signInOrPassword when no passkey matches. Mutate from test code before/after calls, not concurrently with them.

Link copied to clipboard
open override val signals: PasskeySignals
Link copied to clipboard

signals are available (PasskeyCapabilities.signals). When false, every signal returns SignalOutcome.Skipped and is not recorded, as on a device without the Signal API. Default true.

Link copied to clipboard

Functions

Link copied to clipboard
open suspend override fun capabilities(): PasskeyCapabilities
Link copied to clipboard

The next operation fails with error; later ones behave normally. Calls queue up in order. A queued error takes precedence over the capability flags. Call from test code before/after operations, not concurrently with them.

Link copied to clipboard
open suspend override fun register(optionsJson: String): PasskeyResult<PasskeyRegistration>
Link copied to clipboard
open suspend override fun signIn(optionsJson: String, mode: SignInMode): PasskeyResult<PasskeyAssertion>
Link copied to clipboard
open suspend override fun signInOrPassword(optionsJson: String): PasskeyResult<SignInCredential>
Link copied to clipboard
open suspend override fun upgrade(optionsJson: String): PasskeyResult<PasskeyRegistration>