test_auth.py
This file
Path
examples/tutorial/tests/test_auth.py
66 lines · 2.0 KiB · Python
Last changed by
Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
History of this fileTake it
Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 1–66why, one step back1import 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(12 '/auth/register', data={'username': 'a', 'password': 'a'}13 )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 get_db().execute(19 "select * from user where username = 'a'",20 ).fetchone() is not None212223@pytest.mark.parametrize(('username', 'password', 'message'), (24 ('', '', b'Username is required.'),25 ('a', '', b'Password is required.'),26 ('test', 'test', b'already registered'),27))28def test_register_validate_input(client, username, password, message):29 response = client.post(30 '/auth/register',31 data={'username': username, 'password': password}32 )33 assert message in response.data343536def test_login(client, auth):37 # test that viewing the page renders without template errors38 assert client.get('/auth/login').status_code == 2003940 # test that successful login redirects to the index page41 response = auth.login()42 assert response.headers['Location'] == 'http://localhost/'4344 # login request set the user_id in the session45 # check that the user is loaded from the session46 with client:47 client.get('/')48 assert session['user_id'] == 149 assert g.user['username'] == 'test'505152@pytest.mark.parametrize(('username', 'password', 'message'), (53 ('a', 'test', b'Incorrect username.'),54 ('test', 'a', b'Incorrect password.'),55))56def test_login_validate_input(auth, username, password, message):57 response = auth.login(username, password)58 assert message in response.data596061def test_logout(client, auth):62 auth.login()6364 with client:65 auth.logout()66 assert 'user_id' not in session