test_auth.py
This file
Path
examples/tutorial/tests/test_auth.py
67 lines · 2.0 KiB · Python
Last changed by
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
History of this fileTake it
Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 1–7why, one step back1import pytest2from flask import g, session3from flaskr.db import get_db456def test_register(client, app):7 # test that viewing the page renders without template errorsMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 8why, one step back8 assert client.get("/auth/register").status_code == 200Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 9–10why, one step back910 # test that successful registration redirects to the login pageMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 11–12why, one step back11 response = client.post("/auth/register", data={"username": "a", "password": "a"})12 assert "http://localhost/auth/login" == response.headers["Location"]Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 13–15why, one step back1314 # test that the user was inserted into the database15 with app.app_context():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 16–19why, one step back16 assert (17 get_db().execute("select * from user where username = 'a'").fetchone()18 is not None19 )Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 20–21why, one step back2021Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 22–29why, one step back22@pytest.mark.parametrize(23 ("username", "password", "message"),24 (25 ("", "", b"Username is required."),26 ("a", "", b"Password is required."),27 ("test", "test", b"already registered"),28 ),29)Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 30–31why, one step back30def test_register_validate_input(client, username, password, message):31 response = client.post(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 32why, one step back32 "/auth/register", data={"username": username, "password": password}Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 33–38why, one step back33 )34 assert message in response.data353637def test_login(client, auth):38 # test that viewing the page renders without template errorsMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 39why, one step back39 assert client.get("/auth/login").status_code == 200Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 40–42why, one step back4041 # test that successful login redirects to the index page42 response = auth.login()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 43why, one step back43 assert response.headers["Location"] == "http://localhost/"Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 44–47why, one step back4445 # login request set the user_id in the session46 # check that the user is loaded from the session47 with client:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 48–50why, one step back48 client.get("/")49 assert session["user_id"] == 150 assert g.user["username"] == "test"Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 51–52why, one step back5152Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 53–56why, one step back53@pytest.mark.parametrize(54 ("username", "password", "message"),55 (("a", "test", b"Incorrect username."), ("test", "a", b"Incorrect password.")),56)Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 57–66why, one step back57def test_login_validate_input(auth, username, password, message):58 response = auth.login(username, password)59 assert message in response.data606162def test_logout(client, auth):63 auth.login()6465 with client:66 auth.logout()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 67why, one step back67 assert "user_id" not in session