Skip to content
Myr

test_auth.py

This file

Path

examples/tutorial/tests/test_auth.py

69 lines · 2.0 KiB · Python

Last changed by

Merge pull request #4496 from pallets/werkzeug-relative-redirectDavid Lordbefore adoption425a626

History of this file

Take it

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

refer to encoder instead of decoderMatěj Volfbefore adoptionbdf7083

lines 1–13why, one step back
1import pytest
2from flask import g3from flask import session45from flaskr.db import get_db678def test_register(client, app):9 # test that viewing the page renders without template errors10 assert client.get("/auth/register").status_code == 2001112 # test that successful registration redirects to the login page13 response = client.post("/auth/register", data={"username": "a", "password": "a"})

Merge pull request #4496 from pallets/werkzeug-relative-redirectDavid Lordbefore adoption425a626

line 14why, one step back
14 assert response.headers["Location"] == "/auth/login"

refer to encoder instead of decoderMatěj Volfbefore adoptionbdf7083

lines 15–18why, one step back
15
16 # test that the user was inserted into the database17 with app.app_context():18 assert (

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoptionafc13b9

line 19why, one step back
19 get_db().execute("SELECT * FROM user WHERE username = 'a'").fetchone()

refer to encoder instead of decoderMatěj Volfbefore adoptionbdf7083

lines 20–44why, one step back
20 is not None
21 )222324@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)32def test_register_validate_input(client, username, password, message):33 response = client.post(34 "/auth/register", data={"username": username, "password": password}35 )36 assert message in response.data373839def test_login(client, auth):40 # test that viewing the page renders without template errors41 assert client.get("/auth/login").status_code == 2004243 # test that successful login redirects to the index page44 response = auth.login()

Merge pull request #4496 from pallets/werkzeug-relative-redirectDavid Lordbefore adoption425a626

line 45why, one step back
45 assert response.headers["Location"] == "/"

refer to encoder instead of decoderMatěj Volfbefore adoptionbdf7083

lines 46–69why, 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:50 client.get("/")51 assert session["user_id"] == 152 assert g.user["username"] == "test"535455@pytest.mark.parametrize(56 ("username", "password", "message"),57 (("a", "test", b"Incorrect username."), ("test", "a", b"Incorrect password.")),58)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()69 assert "user_id" not in session