Skip to content

Commit fadb2bf

Browse files
author
Fabien Coelho
committed
working app2
1 parent f34430a commit fadb2bf

File tree

8 files changed

+79
-16
lines changed

8 files changed

+79
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Only one set of tests is needed, switching from internal to external is
99
achieved through environment variables.
1010

1111
![Status](https://github.com/zx80/flask-tester/actions/workflows/package.yml/badge.svg?branch=main&style=flat)
12-
![Tests](https://img.shields.io/badge/tests-13%20✓-success)
12+
![Tests](https://img.shields.io/badge/tests-14%20✓-success)
1313
![Coverage](https://img.shields.io/badge/coverage-100%25-success)
1414
![Issues](https://img.shields.io/github/issues/zx80/flask-tester?style=flat)
1515
![Python](https://img.shields.io/badge/python-3-informational)

docs/documentation.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ from FlaskTester import ft_client, ft_authenticator
7171

7272
os.environ.update(
7373
FLASK_TESTER_ALLOW="basic param none",
74+
FLASK_TESTER_APP="app2:create_app",
7475
)
7576

7677
@pytest.fixture
@@ -83,14 +84,14 @@ def app(ft_client):
8384

8485
def test_something(app):
8586
# requires an authentication
86-
app.get("/authenticated-path", 401, login=None)
87-
res = app.get("/authenticated-path", 200, login="calvin")
87+
app.get("/authenticated", 401, login=None)
88+
res = app.get("/authenticated", 200, login="calvin")
8889
assert "Hello" in res.text
89-
app.get("/authenticated-path", 200, "Bonjour", login="hobbes")
90+
app.get("/authenticated", 200, "Bonjour", login="hobbes")
9091
# only allowed to calvin
91-
app.get("/only-for-calvin", 401, login=None)
92-
app.get("/only-for-calvin", 200, login="calvin")
93-
app.get("/only-for-calvin", 403, login="hobbes")
92+
app.get("/only-calvin", 401, login=None)
93+
app.get("/only-calvin", 200, login="calvin")
94+
app.get("/only-calvin", 403, login="hobbes")
9495
# no authentication required, but depends on lang
9596
res = app.get("/no-auth", 200, login="calvin", auth="none")
9697
assert "Hello" in res.text

docs/versions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ please report any [issues](https://github.com/zx80/flask-tester/issues).
77

88
## ? on ?
99

10-
Improved documentation.
10+
Improved documentation, including a working `app2`.
1111

1212
## 3.5 on 2024-03-30
1313

tests/Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ check.external:
2525
flask_pid=$$!
2626
sleep $(SLEEP)
2727
export FLASK_TESTER_URL="http://localhost:$(PORT)"
28-
$(PYTEST) $(PYTOPT) test.py
28+
$(PYTEST) $(PYTOPT) test_app.py
2929
kill $$flask_pid
3030

3131
.PHONY: check.internal
3232
check.internal:
3333
source $(VENV)/bin/activate
34-
export FLASK_TESTER_APP="app:create_app"
35-
$(PYTEST) $(PYTOPT) test.py
34+
$(PYTEST) $(PYTOPT) test_app2.py
35+
export FLASK_TESTER_APP="app"
36+
$(PYTEST) $(PYTOPT) test_app.py
3637

3738
.PHONY: check.coverage
3839
check.coverage:
39-
$(MAKE) PYTEST="coverage run -m $(PYTEST)" check.internal
40+
$(MAKE) PYTEST="coverage run -a -m $(PYTEST)" check.internal
4041
coverage html ../FlaskTester.py
4142
coverage report --show-missing --precision=1 --fail-under=100.0
4243

tests/app.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,3 @@ def get_admin(user: fsa.CurrentUser):
4242
@app.get("/hello", authorize="OPEN")
4343
def get_hello(lang: fsa.Cookie = "en"):
4444
return {"lang": lang, "hello": HELLO.get(lang, "Hi")}, 200
45-
46-
# only for coverage
47-
def create_app():
48-
return app

tests/app2.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# application suitable for the documentation example
2+
3+
import FlaskSimpleAuth as fsa
4+
5+
def create_app():
6+
7+
app = fsa.Flask("app2", FSA_AUTH=["basic", "param"])
8+
9+
# authentication
10+
PASSES = {"calvin": "clv-pw", "hobbes": "hbs-pw"}
11+
PASSDB = {l: app.hash_password(p) for l, p in PASSES.items()}
12+
app.get_user_pass(lambda l: PASSDB.get(l, None))
13+
14+
# authorization
15+
CALVIN = {"calvin"}
16+
app.group_check("CALVIN", lambda l: l in CALVIN)
17+
18+
HELLO = {"en": "Hello", "fr": "Bonjour", "de": "Guten Tag"}
19+
20+
# 3 routes
21+
@app.get("/authenticated", authorize="AUTH")
22+
def get_authenticated(user: fsa.CurrentUser, lang: fsa.Cookie = "de"):
23+
return fsa.jsonify(HELLO.get(lang, "Hey") + " " + user), 200
24+
25+
@app.get("/only-calvin", authorize="CALVIN")
26+
def get_only_calvin():
27+
return "Salut Calvin!", 200
28+
29+
@app.get("/no-auth", authorize="OPEN")
30+
def get_no_auth(lang: fsa.Cookie = "de"):
31+
return fsa.jsonify(HELLO.get(lang, "Hey")), 200
32+
33+
return app
File renamed without changes.

tests/test_app2.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
import pytest
3+
from FlaskTester import ft_client, ft_authenticator
4+
5+
# hardwired local test
6+
os.environ.update(
7+
FLASK_TESTER_ALLOW="basic param none",
8+
FLASK_TESTER_APP="app2:create_app",
9+
)
10+
11+
@pytest.fixture
12+
def app(ft_client):
13+
ft_client.setPass("calvin", "clv-pw")
14+
ft_client.setCookie("calvin", "lang", "en")
15+
ft_client.setPass("hobbes", "hbs-pw")
16+
ft_client.setCookie("hobbes", "lang", "fr")
17+
yield ft_client
18+
19+
def test_something(app):
20+
# requires an authentication
21+
app.get("/authenticated", 401, login=None)
22+
res = app.get("/authenticated", 200, login="calvin")
23+
assert "Hello" in res.text
24+
app.get("/authenticated", 200, "Bonjour", login="hobbes")
25+
# only allowed to calvin
26+
app.get("/only-calvin", 401, login=None)
27+
app.get("/only-calvin", 200, login="calvin")
28+
app.get("/only-calvin", 403, login="hobbes")
29+
# no authentication required, but depends on lang
30+
res = app.get("/no-auth", 200, login="calvin", auth="none")
31+
assert "Hello" in res.text
32+
app.get("/no-auth", 200, "Bonjour", login="hobbes", auth="none")

0 commit comments

Comments
 (0)