test_blog.py
This file
Path
examples/tutorial/tests/test_blog.py
82 lines · 2.5 KiB · Python
Last changed by
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
History of this fileTake 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", ("/create", "/1/update", "/1/delete"))19def test_login_required(client, path):20 response = client.post(path)21 assert response.headers["Location"] == "http://localhost/auth/login"222324def test_author_required(app, client, auth):25 # change the post author to another user26 with app.app_context():27 db = get_db()28 db.execute("UPDATE post SET author_id = 2 WHERE id = 1")29 db.commit()3031 auth.login()32 # current user can't modify other user's post33 assert client.post("/1/update").status_code == 40334 assert client.post("/1/delete").status_code == 40335 # current user doesn't see edit link36 assert b'href="/1/update"' not in client.get("/").data373839@pytest.mark.parametrize("path", ("/2/update", "/2/delete"))40def test_exists_required(client, auth, path):41 auth.login()42 assert client.post(path).status_code == 404434445def test_create(client, auth, app):46 auth.login()47 assert client.get("/create").status_code == 20048 client.post("/create", data={"title": "created", "body": ""})4950 with app.app_context():51 db = get_db()52 count = db.execute("SELECT COUNT(id) FROM post").fetchone()[0]53 assert count == 2545556def test_update(client, auth, app):57 auth.login()58 assert client.get("/1/update").status_code == 20059 client.post("/1/update", data={"title": "updated", "body": ""})6061 with app.app_context():62 db = get_db()63 post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()64 assert post["title"] == "updated"656667@pytest.mark.parametrize("path", ("/create", "/1/update"))68def test_create_update_validate(client, auth, path):69 auth.login()70 response = client.post(path, data={"title": "", "body": ""})71 assert b"Title is required." in response.data727374def test_delete(client, auth, app):75 auth.login()76 response = client.post("/1/delete")77 assert response.headers["Location"] == "http://localhost/"7879 with app.app_context():80 db = get_db()81 post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()82 assert post is None