Skip to main content

testing.StaticTokenCredential

A credential that returns a fixed token and records what it was asked for.

Usage

Source

testing.StaticTokenCredential(
    token="fake-token",
    *,
    expires_in=3600,
)

Implements azure-core’s TokenCredential and SupportsTokenInfo protocols without signing in or touching the network, so an ApiClient (or any Azure SDK client) can be tested offline. Each call returns a fresh token that expires expires_in seconds from now.

Parameters

token: str = "fake-token"

The access token to hand out; default "fake-token", so requests carry Authorization: Bearer fake-token.

expires_in: int = 3600
Seconds until each returned token expires; default 3600.

Attributes

calls: list of tuple of str
The scopes of each get_token() or get_token_info() call, in order, one tuple per call.

Notes

It wraps nothing: it is a plain object satisfying azure-core’s azure.core.credentials.TokenCredential and SupportsTokenInfo protocols. claims, tenant_id and the options of get_token_info() are accepted and ignored. The token is redacted in repr (design §6), although it is only a test value.

Examples

from dazapiz.testing import StaticTokenCredential

credential = StaticTokenCredential()
client = MyApiClient(credential=credential)  # requests send "Bearer fake-token"
...
assert credential.calls == [(MyApiClient.scope,)]

Methods

Name Description
get_token() Return the fixed token for scopes, and record them.
get_token_info() Return the fixed token for scopes as AccessTokenInfo, and record them.
close() Do nothing: there is nothing to release.

get_token()

Return the fixed token for scopes, and record them.

Usage

Source

get_token(*scopes, claims=None, tenant_id=None, enable_cae=False, **kwargs)
Parameters
*scopes: str

The scopes asked for, recorded in calls.

claims: str or None = None

Ignored.

tenant_id: str or None = None

Ignored.

enable_cae: bool = False

Ignored.

**kwargs: Any
Ignored.
Returns
azure.core.credentials.AccessToken
The token, expiring expires_in seconds from now.

get_token_info()

Return the fixed token for scopes as AccessTokenInfo, and record them.

Usage

Source

get_token_info(*scopes, options=None)
Parameters
*scopes: str

The scopes asked for, recorded in calls.

options: TokenRequestOptions or None = None
Ignored.
Returns
azure.core.credentials.AccessTokenInfo
The token, of type Bearer, expiring expires_in seconds from now.

close()

Do nothing: there is nothing to release.

Usage

Source

close()