pub struct TestClerk { /* private fields */ }testing only.Expand description
A ready-made Clerk setup for tests that are about the application rather than about authentication.
Wraps a TestIssuer so the common case — “this request is signed in as
someone, now test what my app does” — is two calls and needs no knowledge of
JWKS or token claims:
let clerk = TestClerk::new()?;
// Wire the layer into your router, then send authenticated requests.
let layer = clerk.layer()?;
let cookie = clerk.cookie("user_2abc")?;Reach past it when authentication is what you are testing: session and
cookie_for take a full TestSession, and issuer exposes the
underlying TestIssuer.
Implementations§
Source§impl TestClerk
impl TestClerk
Sourcepub fn new() -> Result<Self, TestIssuerError>
pub fn new() -> Result<Self, TestIssuerError>
Generates a fresh key and a Clerk setup around it.
Costs one RSA-2048 key generation (~100ms, variable). Build it once per suite and share it — see the module docs.
Sourcepub fn from_issuer(issuer: TestIssuer) -> Self
pub fn from_issuer(issuer: TestIssuer) -> Self
Builds a Clerk setup around an existing issuer, so the key can be shared
with another process. See
TestIssuer::from_pem_file_or_generate.
Sourcepub fn issuer(&self) -> &TestIssuer
pub fn issuer(&self) -> &TestIssuer
The underlying issuer, for anything this wrapper does not cover.
Sourcepub fn jwks_json(&self) -> Result<String, TestIssuerError>
pub fn jwks_json(&self) -> Result<String, TestIssuerError>
The JWKS document for this setup’s key.
Sourcepub fn session(&self, user_id: impl Into<String>) -> TestSession
pub fn session(&self, user_id: impl Into<String>) -> TestSession
A signed-in session for user_id, to customize before signing.
TestClerk::session("user_2abc") is TestSession::new("user_2abc");
it is here so a test does not have to import both types.
Sourcepub fn token(
&self,
user_id: impl Into<String>,
) -> Result<String, TestIssuerError>
pub fn token( &self, user_id: impl Into<String>, ) -> Result<String, TestIssuerError>
A session token for user_id.
A __session cookie header value for user_id, the credential Clerk’s
browser SDK sends.
Sourcepub fn bearer(
&self,
user_id: impl Into<String>,
) -> Result<String, TestIssuerError>
pub fn bearer( &self, user_id: impl Into<String>, ) -> Result<String, TestIssuerError>
An Authorization header value for user_id.
Sourcepub fn token_for(
&self,
session: &TestSession,
) -> Result<String, TestIssuerError>
pub fn token_for( &self, session: &TestSession, ) -> Result<String, TestIssuerError>
A session token for a customized session.
A __session cookie header value for a customized session.
Sourcepub fn bearer_for(
&self,
session: &TestSession,
) -> Result<String, TestIssuerError>
pub fn bearer_for( &self, session: &TestSession, ) -> Result<String, TestIssuerError>
An Authorization header value for a customized session.
Source§impl TestClerk
impl TestClerk
Sourcepub fn config(&self) -> Result<ClerkAuthLayerConfig, TestIssuerError>
Available on crate feature server only.
pub fn config(&self) -> Result<ClerkAuthLayerConfig, TestIssuerError>
server only.A layer config that verifies against this setup’s key and never reaches the network.
Use this when the layer needs further configuration; otherwise
layer builds it directly.
Sourcepub fn layer(&self) -> Result<ClerkAuthLayer, TestIssuerError>
Available on crate feature server only.
pub fn layer(&self) -> Result<ClerkAuthLayer, TestIssuerError>
server only.A ClerkAuthLayer that verifies tokens minted by this setup.