Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than b78ebac point at it.

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

line 1why, one step back
1import pytest

Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697

lines 2–4why, one step back
2from flask import g
3from flask import session4

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

lines 5–9why, one step back
5from flaskr.db import get_db
678def test_register(client, app):9 # test that viewing the page renders without template errors

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

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

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

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

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

lines 13–14why, one step back
13 response = client.post("/auth/register", data={"username": "a", "password": "a"})
14 assert "http://localhost/auth/login" == response.headers["Location"]

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

lines 15–17why, one step back
15
16 # 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 back
18 assert (
19 get_db().execute("select * from user where username = 'a'").fetchone()20 is not None21 )

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

lines 22–23why, one step back
22
23

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

lines 24–31why, one step back
24@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 pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

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

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

lines 35–40why, one step back
35 )
36 assert message in response.data373839def test_login(client, auth):40 # test that viewing the page renders without template errors

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

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

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

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

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

lines 46–49why, one step back
46
47 # 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 back
50 client.get("/")
51 assert session["user_id"] == 152 assert g.user["username"] == "test"

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

lines 53–54why, one step back
53
54

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

lines 55–58why, one step back
55@pytest.mark.parametrize(
56 ("username", "password", "message"),57 (("a", "test", b"Incorrect username."), ("test", "a", b"Incorrect password.")),58)

Merge pull request #2996 from alysivji/spa-docsDavid Lordbefore adoptionb78ebac

lines 59–68why, one step back
59def 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 back
69 assert "user_id" not in session