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

Not known.

History of this file

Take it

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

move package metadata to setup.cfgDavid Lordbefore adoption024f0d3

lines 1–69why, 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"})14 assert "http://localhost/auth/login" == response.headers["Location"]1516 # test that the user was inserted into the database17 with app.app_context():18 assert (19 get_db().execute("select * from user where username = 'a'").fetchone()20 is not None21 )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()45 assert response.headers["Location"] == "http://localhost/"4647 # 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