Skip to main content

FlaskTester - Pytest fixtures for Flask internal and external authenticated tests

This package allows to run authenticated tests against a Flask application, either with internal Flask tests (aka test_client) or external tests (with requests which performs actual HTTP requests), including password and token authentication and per-user cookies.

Only one set of tests is needed, switching from internal to external is achieved by setting an environment variable.

Status Tests Coverage Issues Python Version Badges License

Usage

Install package with pip install FlaskTester or equivalent.

The following test creates a local fixture with 2 users identified by a password, and retrieves tokens for both users using a /login route provided by the application. It then proceeds to run authenticated requests against the /admin route.

import pytest
from FlaskTester import ft_authenticator, ft_client
import secret

def authHook(api, user: str, pwd: str|None):
    if pwd is not None:  # get a token when a login/password is provided
        res = api.get("/login", login=user, auth="basic", status=200)
        api.setToken(user, res.json["token"])
    else:  # remove token
        api.setToken(user, None)

@pytest.fixture
def app(ft_client):
    # register authentication hook
    ft_client.setHook(authHook)
    # add test passwords for Calvin and Hobbes (must be consistent with app!)
    ft_client.setPass("calvin", secret.PASSES["calvin"])
    ft_client.setPass("hobbes", secret.PASSES["hobbes"])
    # also set a cookie
    ft_client.setCookie("hobbes", "lang", "fr")
    ft_client.setCookie("calvin", "lang", "en")
    # return working client
    yield ft_client

def test_app_admin(app):
    app.get("/admin", login=None, status=401)
    for auth in ["bearer", "basic", "param"]:
        res = app.get("/admin", login="calvin", auth=auth, status=200)
        assert res.json["user"] == "calvin" and res.json["isadmin"]
        res = app.get("/admin", login="hobbes", auth=auth, status=403)
        assert 'not in group "ADMIN"' in res.text

This can be run against a local or remote server:

export TEST_SEED="some-random-data"              # shared test seed
flask --app app:app run &                        # start flask app
pid=$!                                           # keep pid
export FLASK_TESTER_APP="http://localhost:5000"  # set app url, local or remote
pytest test.py                                   # run external tests
kill $pid                                        # stop app with pid

Or locally with the Flask internal test infrastructure:

export FLASK_TESTER_APP="app:app"                # set app package
pytest test.py                                   # run internal tests

The above test runs with tests/app.py Flask REST application back-end with password and token authentication based on FlaskSimpleAuth. The code uses 23 lines of Python for implementing password (basic and parameters) and token authentications, admin group authorization, and routes for token generation (2), identity tests (2) and an incredible open cookie-based translation service.

See the documentation.

License

This code is Public Domain.

All software has bug, this is software, hence… Beware that you may lose your hairs or your friends because of it. If you like it, feel free to send a postcard to the author.

Versions

Packages are distributed from PyPI, sources are available on GitHub, please report any issues.

Metadata

Release files for FlaskTester 6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for FlaskTester 6.0
File Size Uploaded Provenance Actions
flasktester-6.0.tar.gz 12.1 kB Without provenance Details for flasktester-6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for FlaskTester 6.0
File Interpreter ABI Platform Actions
flasktester-6.0-py3-none-any.whl Python 3 none any Details for flasktester-6.0-py3-none-any.whl

Total release size: 21.9 kB

Release files / flasktester-6.0.tar.gz

Download URL flasktester-6.0.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e9acb6d26abaf960cc1522ceef403a5c62bb9b3c33b2c4f44a4b710c5c88a6e7
BLAKE2b-256 checksum
How to use checksums
60a551efca6b0bbd88f2429355efc3f17188eebb4f79e3152e93bcbb0ae3ead4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / flasktester-6.0-py3-none-any.whl

Download URL flasktester-6.0-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6063c51cfc353854686a212a7da9d296442f0a473ca79f89d809a5ffff8b7569
BLAKE2b-256 checksum
How to use checksums
42b862c7641e7e5729aad8cd48edf3f34c93e4d46b4ad2457c0f1e66b7e29803
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for FlaskTester 6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page