diff options
| author | Mitsuo Tokumori <[email protected]> | 2023-10-29 06:59:37 -0500 |
|---|---|---|
| committer | Mitsuo Tokumori <[email protected]> | 2023-10-29 06:59:37 -0500 |
| commit | 9baa55436bc5a98d118a17656bbf25e563522964 (patch) | |
| tree | daa01118bd11f5cf13d386d2c727a384563df1da /tests/test_blog.py | |
| parent | 71e7d6516608486f67afad5aad1f7b7f9a45886f (diff) | |
| download | ustayml-9baa55436bc5a98d118a17656bbf25e563522964.tar.gz ustayml-9baa55436bc5a98d118a17656bbf25e563522964.tar.bz2 ustayml-9baa55436bc5a98d118a17656bbf25e563522964.zip | |
Add unit tests
Diffstat (limited to 'tests/test_blog.py')
| -rw-r--r-- | tests/test_blog.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/tests/test_blog.py b/tests/test_blog.py new file mode 100644 index 0000000..75f67a5 --- /dev/null +++ b/tests/test_blog.py @@ -0,0 +1,93 @@ +import pytest +from flaskr.db import get_db + + +def test_index(client, auth): + response = client.get('/') + assert b"Log In" in response.data + assert b"Register" in response.data + + auth.login() + response = client.get('/') + assert b'Log Out' in response.data + 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 + assert b'href="/1/update"' in response.data + + [email protected]('path', ( + '/create', + '/1/update', + '/1/delete', +)) +def test_login_required(client, path): + response = client.post(path) + assert response.headers["Location"] == "/auth/login" + + +def test_author_required(app, client, auth): + # change the post author to another user + with app.app_context(): + db = get_db() + db.execute('UPDATE post SET author_id = 2 WHERE id = 1') + db.commit() + + auth.login() + # current user can't modify other user's post + assert client.post('/1/update').status_code == 403 + assert client.post('/1/delete').status_code == 403 + # current user doesn't see edit link + assert b'href="/1/update"' not in client.get('/').data + + [email protected]('path', ( + '/2/update', + '/2/delete', +)) +def test_exists_required(client, auth, path): + auth.login() + assert client.post(path).status_code == 404 + + +def test_create(client, auth, app): + auth.login() + assert client.get('/create').status_code == 200 + client.post('/create', data={'title': 'created', 'body': ''}) + + with app.app_context(): + db = get_db() + count = db.execute('SELECT COUNT(id) FROM post').fetchone()[0] + assert count == 2 + + +def test_update(client, auth, app): + auth.login() + assert client.get('/1/update').status_code == 200 + client.post('/1/update', data={'title': 'updated', 'body': ''}) + + with app.app_context(): + db = get_db() + post = db.execute('SELECT * FROM post WHERE id = 1').fetchone() + assert post['title'] == 'updated' + + [email protected]('path', ( + '/create', + '/1/update', +)) +def test_create_update_validate(client, auth, path): + auth.login() + response = client.post(path, data={'title': '', 'body': ''}) + assert b'Title is required.' in response.data + + +def test_delete(client, auth, app): + auth.login() + response = client.post('/1/delete') + assert response.headers["Location"] == "/" + + with app.app_context(): + db = get_db() + post = db.execute('SELECT * FROM post WHERE id = 1').fetchone() + assert post is None
\ No newline at end of file |
