Skip to content
Myr

test_blog.py

This file

Path

examples/tutorial/tests/test_blog.py

83 lines · 2.4 KiB · Python

Last changed by

Not known.

History of this file

Take it

1import pytest23from flaskr.db import get_db456def test_index(client, auth):7    response = client.get("/")8    assert b"Log In" in response.data9    assert b"Register" in response.data1011    auth.login()12    response = client.get("/")13    assert b"test title" in response.data14    assert b"by test on 2018-01-01" in response.data15    assert b"test\nbody" in response.data16    assert b'href="/1/update"' in response.data171819@pytest.mark.parametrize("path", ("/create", "/1/update", "/1/delete"))20def test_login_required(client, path):21    response = client.post(path)22    assert response.headers["Location"] == "/auth/login"232425def test_author_required(app, client, auth):26    # change the post author to another user27    with app.app_context():28        db = get_db()29        db.execute("UPDATE post SET author_id = 2 WHERE id = 1")30        db.commit()3132    auth.login()33    # current user can't modify other user's post34    assert client.post("/1/update").status_code == 40335    assert client.post("/1/delete").status_code == 40336    # current user doesn't see edit link37    assert b'href="/1/update"' not in client.get("/").data383940@pytest.mark.parametrize("path", ("/2/update", "/2/delete"))41def test_exists_required(client, auth, path):42    auth.login()43    assert client.post(path).status_code == 404444546def test_create(client, auth, app):47    auth.login()48    assert client.get("/create").status_code == 20049    client.post("/create", data={"title": "created", "body": ""})5051    with app.app_context():52        db = get_db()53        count = db.execute("SELECT COUNT(id) FROM post").fetchone()[0]54        assert count == 2555657def test_update(client, auth, app):58    auth.login()59    assert client.get("/1/update").status_code == 20060    client.post("/1/update", data={"title": "updated", "body": ""})6162    with app.app_context():63        db = get_db()64        post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()65        assert post["title"] == "updated"666768@pytest.mark.parametrize("path", ("/create", "/1/update"))69def test_create_update_validate(client, auth, path):70    auth.login()71    response = client.post(path, data={"title": "", "body": ""})72    assert b"Title is required." in response.data737475def test_delete(client, auth, app):76    auth.login()77    response = client.post("/1/delete")78    assert response.headers["Location"] == "/"7980    with app.app_context():81        db = get_db()82        post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()83        assert post is None