2018-02-10 06:39:05 +08:00
|
|
|
import pytest
|
2019-06-01 23:35:03 +08:00
|
|
|
|
2018-02-10 06:39:05 +08:00
|
|
|
from flaskr.db import get_db
|
|
|
|
|
|
|
|
|
|
|
|
def test_index(client, auth):
|
2019-05-07 03:39:41 +08:00
|
|
|
response = client.get("/")
|
2018-02-10 06:39:05 +08:00
|
|
|
assert b"Log In" in response.data
|
|
|
|
assert b"Register" in response.data
|
|
|
|
|
|
|
|
auth.login()
|
2019-05-07 03:39:41 +08:00
|
|
|
response = client.get("/")
|
|
|
|
assert b"test title" in response.data
|
|
|
|
assert b"by test on 2018-01-01" in response.data
|
|
|
|
assert b"test\nbody" in response.data
|
2018-02-10 06:39:05 +08:00
|
|
|
assert b'href="/1/update"' in response.data
|
|
|
|
|
|
|
|
|
2019-05-07 03:39:41 +08:00
|
|
|
@pytest.mark.parametrize("path", ("/create", "/1/update", "/1/delete"))
|
2018-02-10 06:39:05 +08:00
|
|
|
def test_login_required(client, path):
|
|
|
|
response = client.post(path)
|
2022-03-26 02:48:26 +08:00
|
|
|
assert response.headers["Location"] == "/auth/login"
|
2018-02-10 06:39:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_author_required(app, client, auth):
|
|
|
|
# change the post author to another user
|
|
|
|
with app.app_context():
|
|
|
|
db = get_db()
|
2019-05-07 03:39:41 +08:00
|
|
|
db.execute("UPDATE post SET author_id = 2 WHERE id = 1")
|
2018-02-10 06:39:05 +08:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
auth.login()
|
|
|
|
# current user can't modify other user's post
|
2019-05-07 03:39:41 +08:00
|
|
|
assert client.post("/1/update").status_code == 403
|
|
|
|
assert client.post("/1/delete").status_code == 403
|
2018-02-10 06:39:05 +08:00
|
|
|
# current user doesn't see edit link
|
2019-05-07 03:39:41 +08:00
|
|
|
assert b'href="/1/update"' not in client.get("/").data
|
2018-02-10 06:39:05 +08:00
|
|
|
|
|
|
|
|
2019-05-07 03:39:41 +08:00
|
|
|
@pytest.mark.parametrize("path", ("/2/update", "/2/delete"))
|
2018-02-10 06:39:05 +08:00
|
|
|
def test_exists_required(client, auth, path):
|
|
|
|
auth.login()
|
|
|
|
assert client.post(path).status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_create(client, auth, app):
|
|
|
|
auth.login()
|
2019-05-07 03:39:41 +08:00
|
|
|
assert client.get("/create").status_code == 200
|
|
|
|
client.post("/create", data={"title": "created", "body": ""})
|
2018-02-10 06:39:05 +08:00
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
db = get_db()
|
2019-05-07 03:39:41 +08:00
|
|
|
count = db.execute("SELECT COUNT(id) FROM post").fetchone()[0]
|
2018-02-10 06:39:05 +08:00
|
|
|
assert count == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_update(client, auth, app):
|
|
|
|
auth.login()
|
2019-05-07 03:39:41 +08:00
|
|
|
assert client.get("/1/update").status_code == 200
|
|
|
|
client.post("/1/update", data={"title": "updated", "body": ""})
|
2018-02-10 06:39:05 +08:00
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
db = get_db()
|
2019-05-07 03:39:41 +08:00
|
|
|
post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()
|
|
|
|
assert post["title"] == "updated"
|
2018-02-10 06:39:05 +08:00
|
|
|
|
|
|
|
|
2019-05-07 03:39:41 +08:00
|
|
|
@pytest.mark.parametrize("path", ("/create", "/1/update"))
|
2018-02-10 06:39:05 +08:00
|
|
|
def test_create_update_validate(client, auth, path):
|
|
|
|
auth.login()
|
2019-05-07 03:39:41 +08:00
|
|
|
response = client.post(path, data={"title": "", "body": ""})
|
|
|
|
assert b"Title is required." in response.data
|
2018-02-10 06:39:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete(client, auth, app):
|
|
|
|
auth.login()
|
2019-05-07 03:39:41 +08:00
|
|
|
response = client.post("/1/delete")
|
2022-03-26 02:48:26 +08:00
|
|
|
assert response.headers["Location"] == "/"
|
2018-02-10 06:39:05 +08:00
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
db = get_db()
|
2019-05-07 03:39:41 +08:00
|
|
|
post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()
|
2018-02-10 06:39:05 +08:00
|
|
|
assert post is None
|