FlaskTester =========== .. py:module:: FlaskTester .. autoapi-nested-parse:: FlaskTester - Pytest fixtures for Flask authenticated internal and external tests. PYTEST_DONT_REWRITE: local assertions are really that, not pytest assertions. Exceptions ---------- .. autoapisummary:: FlaskTester.FlaskTesterError FlaskTester.AuthError Classes ------- .. autoapisummary:: FlaskTester.Authenticator FlaskTester.RequestFlaskResponse FlaskTester.Client FlaskTester.RequestClient FlaskTester.FlaskClient Functions --------- .. autoapisummary:: FlaskTester.ft_authenticator FlaskTester.ft_client Module Contents --------------- .. py:exception:: FlaskTesterError Bases: :py:obj:`BaseException` Base exception for FlaskTester package. .. py:exception:: AuthError Bases: :py:obj:`FlaskTesterError` Authenticator Exception. .. py:class:: 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 in ``Authorization`` *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: :param allow: List of allowed schemes. defaults to ``["bearer", "basic", "param", "none"]``. :param user: Parameter for user on ``param`` password authentication, defaults to ``"USER"``. :param pwd: Parameter for password on ``param`` password authentication, defaults to ``"PASS"``. :param login: Parameter for user on ``fake`` authentication, defaults to ``"LOGIN"``. :param bearer: Name of bearer scheme for token, defaults to ``"Bearer"``. :param header: Name of header for token, defaults to ``"Auth"``. :param cookie: Name of cookie for token, defaults to ``"auth"``. :param tparam: Name of parameter for token, defaults to ``"AUTH"``. :param ptype: Default parameter type, either *data* or *json*, defaults to ``"data"``. Note: default values are consistent with `FlaskSimpleAuth `_. .. py:method:: setHook(hook: _AuthHook) Set on password hook. .. py:method:: setPass(login: str, pw: str | None) Associate a password to a user. Set to *None* to remove the password entry. .. py:method:: setPasses(pws: list[str]) Associate a list of *login:password*. .. py:method:: setToken(login: str, token: str | None) Associate a token to a user. Set to *None* to remove the token entry. .. py:method:: setCookie(login: str, name: str, val: str | None = None) Associate a cookie and its value to a login, *None* to remove. .. py:method:: setAuth(login: str | None, kwargs: dict[str, Any], cookies: dict[str, str], auth: str | None = None) Set request authentication. :param login: Login target, *None* means no authentication. :param kwargs: Request parameters to modify. :param cookies: Request cookies to modify. :param auth: Authentication method, default is *None*. The default behavior is to try allowed schemes: token first, then password, then fake. .. py:class:: 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: :param response: Response from request. .. py:class:: Client(auth: Authenticator, default_login: str | None = None) Common (partial) class for flask authenticated testing. Constructor parameters: :param auth: Authenticator. :param default_login: When ``login`` is not set. .. py:method:: setHook(hook: AuthHook) Set on password hook. .. py:method:: setToken(login: str, token: str | None) Associate a token to a login, *None* to remove. .. py:method:: setPass(login: str, password: str | None) Associate a password to a login, *None* to remove. .. py:method:: setCookie(login: str, name: str, val: str | None) Associate a cookie to a login, *None* name to remove. .. py:method:: 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: :param method: HTTP method ("GET", "POST", "PATCH", "DELETE"…). :param path: Local path under the base URL. Optional parameters: :param status: Expected HTTP status, *None* to skip status check. :param content: Regular expression for response body, *None* to skip content check. :param login: Authenticated user, use **explicit** *None* to skip default. :param auth: Authentication scheme to use instead of default behavior. :param **kwargs: More request parameters (headers, data, json…). .. py:method:: get(path: str, status: int | None = None, content: str | None = None, **kwargs) HTTP GET request, see `Client.request`. .. py:method:: post(path: str, status: int | None = None, content: str | None = None, **kwargs) HTTP POST request, see `Client.request`. .. py:method:: put(path: str, status: int | None = None, content: str | None = None, **kwargs) HTTP PUT request, see `Client.request`. .. py:method:: patch(path: str, status: int | None = None, content: str | None = None, **kwargs) HTTP PATCH request, see `Client.request`. .. py:method:: delete(path: str, status: int | None = None, content: str | None = None, **kwargs) HTTP DELETE request, see `Client.request`. .. py:class:: RequestClient(auth: Authenticator, base_url: str, default_login=None) Bases: :py:obj:`Client` Request-based test provider. Constructor parameters: :param auth: Authenticator. :param base_url: Target server. :param default_login: When ``login`` is not set. .. py:class:: FlaskClient(auth: Authenticator, client, default_login=None) Bases: :py:obj:`Client` Flask-based test provider. Constructor parameters: :param auth: Authenticator. :param client: Flask actual ``test_client``. :param default_login: When ``login`` is not set. Note: this client handles `cookies`. .. py:function:: ft_authenticator() Pytest Fixture: ft_authenticator. Environment variables: - ``FLASK_TESTER_LOG_LEVEL``: package log level in ``DEBUG INFO WARNING ERROR CRITICAL NOSET``, defaults to ``NOTSET``. - ``FLASK_TESTER_ALLOW``: allowed space-separated authentication schemes, in ``basic param bearer header cookie tparam fake none``, defaults to ``bearer basic param none``. - ``FLASK_TESTER_USER``: user login parameter for ``param`` authentication, defaults to ``USER``. - ``FLASK_TESTER_PASS``: user password parameter for ``param`` authentication, defaults to ``PASS``. - ``FLASK_TESTER_LOGIN``: user login parameter for ``fake`` authentication, defaults to ``LOGIN``. - ``FLASK_TESTER_BEARER``: bearer name for *token* authentication, defaults to ``Bearer``. - ``FLASK_TESTER_HEADER``: header name for *token* authentication, defaults to ``Auth``. - ``FLASK_TESTER_COOKIE``: cookie name for *token* authentication, defaults to ``auth``. - ``FLASK_TESTER_TPARAM``: parameter for *token* authentication, defaults to ``AUTH``. - ``FLASK_TESTER_PTYPE``: default parameter type, ``data`` or ``json``, defaults to ``data``. - ``FLASK_TESTER_AUTH``: initial comma-separated list of *login:password*, defaults to not set. .. py:function:: ft_client(ft_authenticator) Pytest Fixture: ft_client. Mandatory target environment variable: - ``FLASK_TESTER_APP``: find the Flask application, eg ``app:create_app`` for an internal test, or ``http://localhost:5000`` for an external test. Other environment variable: - ``FLASK_TESTER_DEFAULT``: Default client login, default is *None* for no default.