Pytest fixtures for Flask internal and external authenticated tests
Project description
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.
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
@pytest.fixture
def app(ft_client):
# 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"])
# get user tokens, assume json result {"token": "<token-value>"}
res = ft_client.get("/login", login="calvin", auth="basic", status=200)
assert res.is_json
ft_client.setToken("calvin", res.json["token"])
res = ft_client.post("/login", login="hobbes", auth="param", status=201)
assert res.is_json
ft_client.setToken("hobbes", res.json["token"])
# 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) 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 local url
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flasktester-4.3.tar.gz
.
File metadata
- Download URL: flasktester-4.3.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6db70b7139d765202926d102a539e49f8cb082c9488c0dce94d332e7a4264fb3 |
|
MD5 | 260f7406376b6d04640e259f372455d9 |
|
BLAKE2b-256 | 15de07ccff44a597f3bd9d8d40bec48df8461bf7d4ff83affcc7a822de29eb63 |
File details
Details for the file FlaskTester-4.3-py3-none-any.whl
.
File metadata
- Download URL: FlaskTester-4.3-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cafbfd3d4e037090ecca683c73949d107c6dd21a0b405f69c376e36232b99ff |
|
MD5 | 99747538304ec6ac63b4ff70a7da443e |
|
BLAKE2b-256 | 0c415eda4312531e5b5143dd0bf1cb3a1e034c1f18ec14bce86e80f71696aa3c |