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

1import pytest2from flask import g, session3from flaskr.db import get_db456def test_register(client, app):7    # test that viewing the page renders without template errors8    assert client.get("/auth/register").status_code == 200910    # test that successful registration redirects to the login page11    response = client.post("/auth/register", data={"username": "a", "password": "a"})12    assert "http://localhost/auth/login" == response.headers["Location"]1314    # test that the user was inserted into the database15    with app.app_context():16        assert (17            get_db().execute("select * from user where username = 'a'").fetchone()18            is not None19        )202122@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)30def test_register_validate_input(client, username, password, message):31    response = client.post(32        "/auth/register", data={"username": username, "password": password}33    )34    assert message in response.data353637def test_login(client, auth):38    # test that viewing the page renders without template errors39    assert client.get("/auth/login").status_code == 2004041    # test that successful login redirects to the index page42    response = auth.login()43    assert response.headers["Location"] == "http://localhost/"4445    # login request set the user_id in the session46    # check that the user is loaded from the session47    with client:48        client.get("/")49        assert session["user_id"] == 150        assert g.user["username"] == "test"515253@pytest.mark.parametrize(54    ("username", "password", "message"),55    (("a", "test", b"Incorrect username."), ("test", "a", b"Incorrect password.")),56)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()67        assert "user_id" not in session