test_auth.py
This file
Path
examples/tutorial/tests/test_auth.py
69 lines · 2.1 KiB · Python
Last changed by
Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
History of this fileTake it
The walk stopped after 200 commits: lines older than 3b45b82 point at it.
Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
line 1why, one step back1import pytestMerge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
lines 2–4why, one step back2from flask import g3from flask import session4Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 5–9why, one step back5from flaskr.db import get_db678def test_register(client, app):9 # test that viewing the page renders without template errorsMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 10why, one step back10 assert client.get("/auth/register").status_code == 200Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 11–12why, one step back1112 # test that successful registration redirects to the login pageMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 13–14why, one step back13 response = client.post("/auth/register", data={"username": "a", "password": "a"})14 assert "http://localhost/auth/login" == response.headers["Location"]Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 15–17why, one step back1516 # test that the user was inserted into the database17 with app.app_context():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 18–21why, one step back18 assert (19 get_db().execute("select * from user where username = 'a'").fetchone()20 is not None21 )Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 22–23why, one step back2223Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 24–31why, one step back24@pytest.mark.parametrize(25 ("username", "password", "message"),26 (27 ("", "", b"Username is required."),28 ("a", "", b"Password is required."),29 ("test", "test", b"already registered"),30 ),31)Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 32–33why, one step back32def test_register_validate_input(client, username, password, message):33 response = client.post(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 34why, one step back34 "/auth/register", data={"username": username, "password": password}Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 35–40why, one step back35 )36 assert message in response.data373839def test_login(client, auth):40 # test that viewing the page renders without template errorsMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 41why, one step back41 assert client.get("/auth/login").status_code == 200Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 42–44why, one step back4243 # test that successful login redirects to the index page44 response = auth.login()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 45why, one step back45 assert response.headers["Location"] == "http://localhost/"Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 46–49why, one step back4647 # login request set the user_id in the session48 # check that the user is loaded from the session49 with client:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 50–52why, one step back50 client.get("/")51 assert session["user_id"] == 152 assert g.user["username"] == "test"Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 53–54why, one step back5354Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 55–58why, one step back55@pytest.mark.parametrize(56 ("username", "password", "message"),57 (("a", "test", b"Incorrect username."), ("test", "a", b"Incorrect password.")),58)Merge branch '1.0-maintenance'David Lordbefore adoption3b45b82
lines 59–68why, one step back59def test_login_validate_input(auth, username, password, message):60 response = auth.login(username, password)61 assert message in response.data626364def test_logout(client, auth):65 auth.login()6667 with client:68 auth.logout()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 69why, one step back69 assert "user_id" not in session