avoid triggering setupmethod late in tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent a406c29 · Browse files · 1 file·+42 added−52 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+42 added−52 removed
- tests:+42 added−52 removed
Provenance
- Commit
e044b00- Tree
f641a5b- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
a406c29
1 file changed+42 added−52 removed
modified:tests/test_basic.py+42 added-52 removed
327 unmodified lines328 def index():329 flask.session["testing"] = 42330 return "Hello World"331 332+ @app.route("/clear")333+ def clear():334+ flask.session.pop("testing", None)335+ return "Goodbye World"336+337 rv = client.get("/", "http://www.example.com:8080/test/")338 cookie = rv.headers["set-cookie"].lower()339 assert "domain=.example.com" in cookie340 assert "path=/" in cookie341 assert "secure" in cookie342 assert "httponly" not in cookie343 assert "samesite" in cookie344 340- @app.route("/clear")341- def clear():342- flask.session.pop("testing", None)343- return "Goodbye World"344-345 rv = client.get("/clear", "http://www.example.com:8080/test/")346 cookie = rv.headers["set-cookie"].lower()347 assert "session=;" in cookie348 assert "domain=.example.com" in cookie681 unmodified lines1030 rv = client.get("/E3")1031 assert rv.data == b"E2"1032 1033 1034-def test_trapping_of_bad_request_key_errors(app, client):1034+@pytest.mark.parametrize(1035+ ("debug", "trap", "expect_key", "expect_abort"),1036+ [(False, None, True, True), (True, None, False, True), (False, True, False, False)],1037+)1038+def test_trap_bad_request_key_error(app, client, debug, trap, expect_key, expect_abort):1039+ app.config["DEBUG"] = debug1040+ app.config["TRAP_BAD_REQUEST_ERRORS"] = trap1041+1042 @app.route("/key")1043 def fail():1044 flask.request.form["missing_key"]1045 1046 @app.route("/abort")1047 def allow_abort():1048 flask.abort(400)1049 1050+ if expect_key:1043- rv = client.get("/key")1044- assert rv.status_code == 4001045- assert b"missing_key" not in rv.data1046- rv = client.get("/abort")1047- assert rv.status_code == 4001051+ rv = client.get("/key")1052+ assert rv.status_code == 4001053+ assert b"missing_key" not in rv.data1054+ else:1055+ with pytest.raises(KeyError) as exc_info:1056+ client.get("/key")1057 1049- app.debug = True1050- with pytest.raises(KeyError) as e:1051- client.get("/key")1052- assert e.errisinstance(BadRequest)1053- assert "missing_key" in e.value.get_description()1058+ assert exc_info.errisinstance(BadRequest)1059+ assert "missing_key" in exc_info.value.get_description()1054- rv = client.get("/abort")1055- assert rv.status_code == 4001060 1057- app.debug = False1058- app.config["TRAP_BAD_REQUEST_ERRORS"] = True1059- with pytest.raises(KeyError):1060- client.get("/key")1061- with pytest.raises(BadRequest):1062- client.get("/abort")1061+ if expect_abort:1062+ rv = client.get("/abort")1063+ assert rv.status_code == 4001064+ else:1065+ with pytest.raises(BadRequest):1066+ client.get("/abort")1067 1068 1069 def test_trapping_of_all_http_exceptions(app, client):1070 app.config["TRAP_HTTP_EXCEPTIONS"] = True593 unmodified lines1664 rv = client.get("/киртест")1665 assert rv.data == b"Hello World!"1666 1667 1664-def test_debug_mode_complains_after_first_request(app, client):1668+def test_no_setup_after_first_request(app, client):1669 app.debug = True1670 1671 @app.route("/")1672 def index():1 unmodified line1674 1675 assert not app.got_first_request1676 assert client.get("/").data == b"Awesome"1677 1674- with pytest.raises(AssertionError) as e:1678+ with pytest.raises(AssertionError) as exc_info:1679 app.add_url_rule("/foo", endpoint="late")1680 1677- assert "A setup function was called" in str(e.value)1681+ assert "setup method 'add_url_rule'" in str(exc_info.value)1682 1679- app.debug = False1683 1681- @app.route("/foo")1682- def working():1683- return "Meh"1684-1685- assert client.get("/foo").data == b"Meh"1686- assert app.got_first_request1687-1688-1684 def test_before_first_request_functions(app, client):1685 got = []1686 1687 @app.before_first_request26 unmodified lines1714 assert app.got_first_request1715 1716 1717 def test_routing_redirect_debugging(monkeypatch, app, client):1723- @app.route("/foo/", methods=["GET", "POST"])1724- def foo():1725- return "success"1718+ app.config["DEBUG"] = True1719 1727- app.debug = False1728- rv = client.post("/foo", data={}, follow_redirects=True)1720+ @app.route("/user/", methods=["GET", "POST"])1721+ def user():1722+ return flask.request.form["status"]1723+1724+ # default redirect code preserves form data1725+ rv = client.post("/user", data={"status": "success"}, follow_redirects=True)1726 assert rv.data == b"success"1727 1731- app.debug = True1732-1733- with client:1734- rv = client.post("/foo", data={}, follow_redirects=True)1735- assert rv.data == b"success"1736- rv = client.get("/foo", data={}, follow_redirects=True)1737- assert rv.data == b"success"1738-1728+ # 301 and 302 raise error1729 monkeypatch.setattr(RequestRedirect, "code", 301)1730 1741- with client, pytest.raises(AssertionError) as e:1742- client.post("/foo", data={})1731+ with client, pytest.raises(AssertionError) as exc_info:1732+ client.post("/user", data={"status": "error"}, follow_redirects=True)1733 1744- assert "canonical URL 'http://localhost/foo/'" in str(e.value)1734+ assert "canonical URL 'http://localhost/user/'" in str(exc_info.value)1735 1736 1737 def test_route_decorator_custom_endpoint(app, client):1738 app.debug = True281 unmodified lines