Skip to content

Commit 3495710

Browse files
author
Fabien Coelho
committed
include doc example in tests
1 parent fc1c0c0 commit 3495710

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import pytest
3030
from FlaskTester import ft_authenticator, ft_client
3131

3232
@pytest.fixture
33-
def api(ft_client):
33+
def app(ft_client):
3434
# add test passwords for Calvin and Hobbes (must be consistent with app!)
3535
ft_client.setPass("calvin", "clv-pass")
3636
ft_client.setPass("hobbes", "hbs-pass")
@@ -41,10 +41,10 @@ def api(ft_client):
4141
# return working client
4242
yield ft_client
4343

44-
def test_app(api):
45-
api.get("/admin", login="calvin", auth="bearer", status=200)
46-
api.get("/admin", login="calvin", auth="basic", status=200)
47-
res = api.get("/admin", login="hobbes", auth="basic", status=403)
44+
def test_app(app):
45+
app.get("/admin", login="calvin", auth="bearer", status=200)
46+
app.get("/admin", login="calvin", auth="basic", status=200)
47+
res = app.get("/admin", login="hobbes", auth="basic", status=403)
4848
assert 'not in group "ADMIN"' in res.text
4949
```
5050

tests/test.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,28 @@
1717
FLASK_TESTER_AUTH=",".join(f"{l}:{p}" for l, p in app.PASSES.items()),
1818
)
1919

20-
# must provide url or package of Flask application to test
21-
assert "FLASK_TESTER_URL" in os.environ or "FLASK_TESTER_APP" in os.environ
20+
def test_sanity():
21+
# must provide url or package of Flask application to test
22+
assert "FLASK_TESTER_URL" in os.environ or "FLASK_TESTER_APP" in os.environ
23+
24+
# example from README.md
25+
@pytest.fixture
26+
def app(ft_client):
27+
# add test passwords for Calvin and Hobbes (must be consistent with app!)
28+
ft_client.setPass("calvin", "clv-pass")
29+
ft_client.setPass("hobbes", "hbs-pass")
30+
# get Calvin's token, assume json result {"token": "<token-value>"}
31+
res = ft_client.get("/token", login="calvin", auth="basic", status=200)
32+
assert res.is_json
33+
ft_client.setToken("calvin", res.json["token"])
34+
# return working client
35+
yield ft_client
36+
37+
def test_app(app):
38+
app.get("/admin", login="calvin", auth="bearer", status=200)
39+
app.get("/admin", login="calvin", auth="basic", status=200)
40+
res = app.get("/admin", login="hobbes", auth="basic", status=403)
41+
assert 'not in group "ADMIN"' in res.text
2242

2343
@pytest.fixture
2444
def api(ft_client):
@@ -58,13 +78,6 @@ def api(ft_client):
5878
# add a bad password
5979
yield ft_client
6080

61-
def test_app(api):
62-
"""Test example from README."""
63-
api.get("/admin", login="calvin", auth="bearer", status=200)
64-
api.get("/admin", login="calvin", auth="basic", status=200)
65-
res = api.get("/admin", login="hobbes", auth="basic", status=403)
66-
assert 'not in group "ADMIN"' in res.text
67-
6881
def test_admin(api):
6982
# check authentication schemes
7083
for auth in (None, "basic", "param", "bearer"):

0 commit comments

Comments
 (0)