[go: up one dir, main page]

Module mock

Module mock 

Source
Expand description

§Mock credential store

To facilitate testing of clients, this crate provides a Mock credential store that is platform-independent, provides no persistence, and allows the client to specify the return values (including errors) for each call. The credentials in this store have no attributes at all.

To use this credential store instead of the default, make this call during application startup before creating any entries:

keyring::set_default_credential_builder(keyring::mock::default_credential_builder());

You can then create entries as you usually do, and call their usual methods to set, get, and delete passwords. There is no persistence other than in the entry itself, so getting a password before setting it will always result in a NoEntry error.

If you want a method call on an entry to fail in a specific way, you can downcast the entry to a MockCredential and then call set_error with the appropriate error. The next entry method called on the credential will fail with the error you set. The error will then be cleared, so the next call on the mock will operate as usual. Here’s a complete example:

let entry = Entry::new("service", "user").unwrap();
let mock: &MockCredential = entry.get_credential().downcast_ref().unwrap();
mock.set_error(Error::Invalid("mock error".to_string(), "takes precedence".to_string()));
entry.set_password("test").expect_err("error will override");
entry.set_password("test").expect("error has been cleared");

Structs§

MockCredential
The concrete mock credential
MockCredentialBuilder
The builder for mock credentials.
MockData
The (in-memory) persisted data for a mock credential.

Functions§

default_credential_builder
Return a mock credential builder for use by clients.