FlaskTester
FlaskTester - Pytest fixtures for Flask authenticated internal and external tests.
PYTEST_DONT_REWRITE: local assertions are really that, not pytest assertions.
Exceptions
Base exception for FlaskTester package. |
|
Authenticator Exception. |
Classes
Manage HTTP request authentication. |
|
Wrapper to return a Flask-looking response from a request response. |
|
Common (partial) class for flask authenticated testing. |
|
Request-based test provider. |
|
Flask-based test provider. |
Functions
Pytest Fixture: ft_authenticator. |
|
|
Pytest Fixture: ft_client. |
Module Contents
- exception FlaskTester.FlaskTesterError
Bases:
BaseException
Base exception for FlaskTester package.
- exception FlaskTester.AuthError
Bases:
FlaskTesterError
Authenticator Exception.
- class FlaskTester.Authenticator(allow: list[str] = ['bearer', 'basic', 'param', 'none'], user: str = 'USER', pwd: str = 'PASS', login: str = 'LOGIN', bearer: str = 'Bearer', header: str = 'Auth', cookie: str = 'auth', tparam: str = 'AUTH', ptype: str = 'data')
Manage HTTP request authentication.
Supported schemes:
basic
: HTTP Basic Authentication.param
: password with HTTP or JSON parameters.bearer
: token inAuthorization
bearer header.header
: token in a header.cookie
: token in a cookie.tparam
: token in a parameter.fake
: fake scheme, login directly passed as a parameter.none
: no authentication, only cookies.
Constructor parameters:
- Parameters:
allow – List of allowed schemes. defaults to
["bearer", "basic", "param", "none"]
.user – Parameter for user on
param
password authentication, defaults to"USER"
.pwd – Parameter for password on
param
password authentication, defaults to"PASS"
.login – Parameter for user on
fake
authentication, defaults to"LOGIN"
.bearer – Name of bearer scheme for token, defaults to
"Bearer"
.header – Name of header for token, defaults to
"Auth"
.cookie – Name of cookie for token, defaults to
"auth"
.tparam – Name of parameter for token, defaults to
"AUTH"
.ptype – Default parameter type, either data or json, defaults to
"data"
.
Note: default values are consistent with FlaskSimpleAuth.
- setPass(login: str, pw: str | None)
Associate a password to a user.
Set to None to remove the password entry.
- setPasses(pws: list[str])
Associate a list of login:password.
- setToken(login: str, token: str | None)
Associate a token to a user.
Set to None to remove the token entry.
- setCookie(login: str, name: str, val: str | None = None)
Associate a cookie and its value to a login, None to remove.
- setAuth(login: str | None, kwargs: dict[str, Any], cookies: dict[str, str], auth: str | None = None)
Set request authentication.
- Parameters:
login – Login target, None means no authentication.
kwargs – Request parameters to modify.
cookies – Request cookies to modify.
auth – Authentication method, default is None.
The default behavior is to try allowed schemes: token first, then password, then fake.
- class FlaskTester.RequestFlaskResponse(response)
Wrapper to return a Flask-looking response from a request response.
This only works for simple responses.
Available public attributes:
status_code
: integer status code.data
: body as bytes.text
: body as a string.headers
: dict of headers and their values.cookies
: dict of cookies.json
: JSON-converted body, or None.is_json
: whether body was in JSON.
Constructor parameter:
- Parameters:
response – Response from request.
- class FlaskTester.Client(auth: Authenticator, default_login: str | None = None)
Common (partial) class for flask authenticated testing.
Constructor parameters:
- Parameters:
auth – Authenticator.
default_login – When
login
is not set.
- setToken(login: str, token: str | None)
Associate a token to a login, None to remove.
- setPass(login: str, password: str | None)
Associate a password to a login, None to remove.
- setCookie(login: str, name: str, val: str | None)
Associate a cookie to a login, None name to remove.
- request(method: str, path: str, status: int | None = None, content: str | None = None, auth: str | None = None, **kwargs)
Run a possibly authenticated HTTP request.
Mandatory parameters:
- Parameters:
method – HTTP method (“GET”, “POST”, “PATCH”, “DELETE”…).
path – Local path under the base URL.
Optional parameters:
- Parameters:
status – Expected HTTP status, None to skip status check.
content – Regular expression for response body, None to skip content check.
login – Authenticated user, use explicit None to skip default.
auth – Authentication scheme to use instead of default behavior.
**kwargs –
More request parameters (headers, data, json…).
- get(path: str, status: int | None = None, content: str | None = None, **kwargs)
HTTP GET request, see Client.request.
- post(path: str, status: int | None = None, content: str | None = None, **kwargs)
HTTP POST request, see Client.request.
- put(path: str, status: int | None = None, content: str | None = None, **kwargs)
HTTP PUT request, see Client.request.
- patch(path: str, status: int | None = None, content: str | None = None, **kwargs)
HTTP PATCH request, see Client.request.
- delete(path: str, status: int | None = None, content: str | None = None, **kwargs)
HTTP DELETE request, see Client.request.
- class FlaskTester.RequestClient(auth: Authenticator, base_url: str, default_login=None)
Bases:
Client
Request-based test provider.
Constructor parameters:
- Parameters:
auth – Authenticator.
base_url – Target server.
default_login – When
login
is not set.
- class FlaskTester.FlaskClient(auth: Authenticator, client, default_login=None)
Bases:
Client
Flask-based test provider.
Constructor parameters:
- Parameters:
auth – Authenticator.
client – Flask actual
test_client
.default_login – When
login
is not set.
Note: this client handles cookies.
- FlaskTester.ft_authenticator()
Pytest Fixture: ft_authenticator.
Environment variables:
FLASK_TESTER_LOG_LEVEL
: package log level inDEBUG INFO WARNING ERROR CRITICAL NOSET
, defaults toNOTSET
.FLASK_TESTER_ALLOW
: allowed space-separated authentication schemes, inbasic param bearer header cookie tparam fake none
, defaults tobearer basic param none
.FLASK_TESTER_USER
: user login parameter forparam
authentication, defaults toUSER
.FLASK_TESTER_PASS
: user password parameter forparam
authentication, defaults toPASS
.FLASK_TESTER_LOGIN
: user login parameter forfake
authentication, defaults toLOGIN
.FLASK_TESTER_BEARER
: bearer name for token authentication, defaults toBearer
.FLASK_TESTER_HEADER
: header name for token authentication, defaults toAuth
.FLASK_TESTER_COOKIE
: cookie name for token authentication, defaults toauth
.FLASK_TESTER_TPARAM
: parameter for token authentication, defaults toAUTH
.FLASK_TESTER_PTYPE
: default parameter type,data
orjson
, defaults todata
.FLASK_TESTER_AUTH
: initial comma-separated list of login:password, defaults to not set.
- FlaskTester.ft_client(ft_authenticator)
Pytest Fixture: ft_client.
Mandatory target environment variable:
FLASK_TESTER_APP
: find the Flask application, egapp:create_app
for an internal test, orhttp://localhost:5000
for an external test.
Other environment variable:
FLASK_TESTER_DEFAULT
: Default client login, default is None for no default.