Skip to content
Myr

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 file

Take it

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 1–5why, one step back
1import pytest
2from flaskr.db import get_db345def test_index(client, auth):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 6why, one step back
6 response = client.get("/")

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 7–10why, one step back
7 assert b"Log In" in response.data
8 assert b"Register" in response.data910 auth.login()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 11–14why, one step back
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.data

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 15–17why, one step back
15 assert b'href="/1/update"' in response.data
1617

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 18why, one step back
18@pytest.mark.parametrize("path", ("/create", "/1/update", "/1/delete"))

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 19–20why, one step back
19def test_login_required(client, path):
20 response = client.post(path)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 21why, one step back
21 assert response.headers["Location"] == "http://localhost/auth/login"

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 22–27why, one step back
22
2324def test_author_required(app, client, auth):25 # change the post author to another user26 with app.app_context():27 db = get_db()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 28why, one step back
28 db.execute("UPDATE post SET author_id = 2 WHERE id = 1")

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 29–32why, one step back
29 db.commit()
3031 auth.login()32 # current user can't modify other user's post

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 33–34why, one step back
33 assert client.post("/1/update").status_code == 403
34 assert client.post("/1/delete").status_code == 403

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

line 35why, one step back
35 # current user doesn't see edit link

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 36why, one step back
36 assert b'href="/1/update"' not in client.get("/").data

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 37–38why, one step back
37
38

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 39why, one step back
39@pytest.mark.parametrize("path", ("/2/update", "/2/delete"))

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 40–46why, one step back
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()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 47–48why, one step back
47 assert client.get("/create").status_code == 200
48 client.post("/create", data={"title": "created", "body": ""})

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 49–51why, one step back
49
50 with app.app_context():51 db = get_db()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 52why, one step back
52 count = db.execute("SELECT COUNT(id) FROM post").fetchone()[0]

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 53–57why, one step back
53 assert count == 2
545556def test_update(client, auth, app):57 auth.login()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 58–59why, one step back
58 assert client.get("/1/update").status_code == 200
59 client.post("/1/update", data={"title": "updated", "body": ""})

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 60–62why, one step back
60
61 with app.app_context():62 db = get_db()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 63–64why, one step back
63 post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()
64 assert post["title"] == "updated"

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 65–66why, one step back
65
66

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 67why, one step back
67@pytest.mark.parametrize("path", ("/create", "/1/update"))

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 68–69why, one step back
68def test_create_update_validate(client, auth, path):
69 auth.login()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 70–71why, one step back
70 response = client.post(path, data={"title": "", "body": ""})
71 assert b"Title is required." in response.data

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 72–75why, one step back
72
7374def test_delete(client, auth, app):75 auth.login()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 76–77why, one step back
76 response = client.post("/1/delete")
77 assert response.headers["Location"] == "http://localhost/"

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 78–80why, one step back
78
79 with app.app_context():80 db = get_db()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 81why, one step back
81 post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

line 82why, one step back
82 assert post is None