Skip to content
Myr

test_blog.py

This file

Path

examples/tutorial/tests/test_blog.py

92 lines · 2.5 KiB · Python

Last changed by

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

History of this file

Take it

1import pytest2from flaskr.db import get_db345def test_index(client, auth):6    response = client.get('/')7    assert b"Log In" in response.data8    assert b"Register" in response.data910    auth.login()11    response = client.get('/')12    assert b'test title' in response.data13    assert b'by test on 2018-01-01' in response.data14    assert b'test\nbody' in response.data15    assert b'href="/1/update"' in response.data161718@pytest.mark.parametrize('path', (19    '/create',20    '/1/update',21    '/1/delete',22))23def test_login_required(client, path):24    response = client.post(path)25    assert response.headers['Location'] == 'http://localhost/auth/login'262728def test_author_required(app, client, auth):29    # change the post author to another user30    with app.app_context():31        db = get_db()32        db.execute('UPDATE post SET author_id = 2 WHERE id = 1')33        db.commit()3435    auth.login()36    # current user can't modify other user's post37    assert client.post('/1/update').status_code == 40338    assert client.post('/1/delete').status_code == 40339    # current user doesn't see edit link40    assert b'href="/1/update"' not in client.get('/').data414243@pytest.mark.parametrize('path', (44    '/2/update',45    '/2/delete',46))47def test_exists_required(client, auth, path):48    auth.login()49    assert client.post(path).status_code == 404505152def test_create(client, auth, app):53    auth.login()54    assert client.get('/create').status_code == 20055    client.post('/create', data={'title': 'created', 'body': ''})5657    with app.app_context():58        db = get_db()59        count = db.execute('SELECT COUNT(id) FROM post').fetchone()[0]60        assert count == 2616263def test_update(client, auth, app):64    auth.login()65    assert client.get('/1/update').status_code == 20066    client.post('/1/update', data={'title': 'updated', 'body': ''})6768    with app.app_context():69        db = get_db()70        post = db.execute('SELECT * FROM post WHERE id = 1').fetchone()71        assert post['title'] == 'updated'727374@pytest.mark.parametrize('path', (75    '/create',76    '/1/update',77))78def test_create_update_validate(client, auth, path):79    auth.login()80    response = client.post(path, data={'title': '', 'body': ''})81    assert b'Title is required.' in response.data828384def test_delete(client, auth, app):85    auth.login()86    response = client.post('/1/delete')87    assert response.headers['Location'] == 'http://localhost/'8889    with app.app_context():90        db = get_db()91        post = db.execute('SELECT * FROM post WHERE id = 1').fetchone()92        assert post is None