Skip to content
Myr

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 file

Take it

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 1–7why, one step back
1import pytest
2from flask import g, session3from flaskr.db import get_db456def test_register(client, app):7 # test that viewing the page renders without template errors

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 8why, one step back
8 assert client.get("/auth/register").status_code == 200

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 9–10why, one step back
9
10 # test that successful registration redirects to the login page

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 11–12why, one step back
11 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 back
13
14 # 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 back
16 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 back
20
21

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 22–29why, one step back
22@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 back
30def 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 back
32 "/auth/register", data={"username": username, "password": password}

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 33–38why, one step back
33 )
34 assert message in response.data353637def test_login(client, auth):38 # test that viewing the page renders without template errors

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 39why, one step back
39 assert client.get("/auth/login").status_code == 200

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 40–42why, one step back
40
41 # 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 back
43 assert response.headers["Location"] == "http://localhost/"

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 44–47why, one step back
44
45 # 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 back
48 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 back
51
52

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 53–56why, one step back
53@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 back
57def 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 back
67 assert "user_id" not in session