forked from orbit-oss/flask
parent
5b309831ec
commit
025589ee76
63 changed files with 3784 additions and 3459 deletions
|
|
@ -6,8 +6,8 @@ from flaskr import create_app
|
|||
from flaskr.db import get_db, init_db
|
||||
|
||||
# read in SQL for populating test data
|
||||
with open(os.path.join(os.path.dirname(__file__), 'data.sql'), 'rb') as f:
|
||||
_data_sql = f.read().decode('utf8')
|
||||
with open(os.path.join(os.path.dirname(__file__), "data.sql"), "rb") as f:
|
||||
_data_sql = f.read().decode("utf8")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -16,10 +16,7 @@ def app():
|
|||
# create a temporary file to isolate the database for each test
|
||||
db_fd, db_path = tempfile.mkstemp()
|
||||
# create the app with common test config
|
||||
app = create_app({
|
||||
'TESTING': True,
|
||||
'DATABASE': db_path,
|
||||
})
|
||||
app = create_app({"TESTING": True, "DATABASE": db_path})
|
||||
|
||||
# create the database and load test data
|
||||
with app.app_context():
|
||||
|
|
@ -49,14 +46,13 @@ class AuthActions(object):
|
|||
def __init__(self, client):
|
||||
self._client = client
|
||||
|
||||
def login(self, username='test', password='test'):
|
||||
def login(self, username="test", password="test"):
|
||||
return self._client.post(
|
||||
'/auth/login',
|
||||
data={'username': username, 'password': password}
|
||||
"/auth/login", data={"username": username, "password": password}
|
||||
)
|
||||
|
||||
def logout(self):
|
||||
return self._client.get('/auth/logout')
|
||||
return self._client.get("/auth/logout")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
|
|||
|
|
@ -5,54 +5,55 @@ from flaskr.db import get_db
|
|||
|
||||
def test_register(client, app):
|
||||
# test that viewing the page renders without template errors
|
||||
assert client.get('/auth/register').status_code == 200
|
||||
assert client.get("/auth/register").status_code == 200
|
||||
|
||||
# test that successful registration redirects to the login page
|
||||
response = client.post(
|
||||
'/auth/register', data={'username': 'a', 'password': 'a'}
|
||||
)
|
||||
assert 'http://localhost/auth/login' == response.headers['Location']
|
||||
response = client.post("/auth/register", data={"username": "a", "password": "a"})
|
||||
assert "http://localhost/auth/login" == response.headers["Location"]
|
||||
|
||||
# test that the user was inserted into the database
|
||||
with app.app_context():
|
||||
assert get_db().execute(
|
||||
"select * from user where username = 'a'",
|
||||
).fetchone() is not None
|
||||
assert (
|
||||
get_db().execute("select * from user where username = 'a'").fetchone()
|
||||
is not None
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('username', 'password', 'message'), (
|
||||
('', '', b'Username is required.'),
|
||||
('a', '', b'Password is required.'),
|
||||
('test', 'test', b'already registered'),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
("username", "password", "message"),
|
||||
(
|
||||
("", "", b"Username is required."),
|
||||
("a", "", b"Password is required."),
|
||||
("test", "test", b"already registered"),
|
||||
),
|
||||
)
|
||||
def test_register_validate_input(client, username, password, message):
|
||||
response = client.post(
|
||||
'/auth/register',
|
||||
data={'username': username, 'password': password}
|
||||
"/auth/register", data={"username": username, "password": password}
|
||||
)
|
||||
assert message in response.data
|
||||
|
||||
|
||||
def test_login(client, auth):
|
||||
# test that viewing the page renders without template errors
|
||||
assert client.get('/auth/login').status_code == 200
|
||||
assert client.get("/auth/login").status_code == 200
|
||||
|
||||
# test that successful login redirects to the index page
|
||||
response = auth.login()
|
||||
assert response.headers['Location'] == 'http://localhost/'
|
||||
assert response.headers["Location"] == "http://localhost/"
|
||||
|
||||
# login request set the user_id in the session
|
||||
# check that the user is loaded from the session
|
||||
with client:
|
||||
client.get('/')
|
||||
assert session['user_id'] == 1
|
||||
assert g.user['username'] == 'test'
|
||||
client.get("/")
|
||||
assert session["user_id"] == 1
|
||||
assert g.user["username"] == "test"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('username', 'password', 'message'), (
|
||||
('a', 'test', b'Incorrect username.'),
|
||||
('test', 'a', b'Incorrect password.'),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
("username", "password", "message"),
|
||||
(("a", "test", b"Incorrect username."), ("test", "a", b"Incorrect password.")),
|
||||
)
|
||||
def test_login_validate_input(auth, username, password, message):
|
||||
response = auth.login(username, password)
|
||||
assert message in response.data
|
||||
|
|
@ -63,4 +64,4 @@ def test_logout(client, auth):
|
|||
|
||||
with client:
|
||||
auth.logout()
|
||||
assert 'user_id' not in session
|
||||
assert "user_id" not in session
|
||||
|
|
|
|||
|
|
@ -3,47 +3,40 @@ from flaskr.db import get_db
|
|||
|
||||
|
||||
def test_index(client, auth):
|
||||
response = client.get('/')
|
||||
response = client.get("/")
|
||||
assert b"Log In" in response.data
|
||||
assert b"Register" in response.data
|
||||
|
||||
auth.login()
|
||||
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
|
||||
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
|
||||
assert b'href="/1/update"' in response.data
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path', (
|
||||
'/create',
|
||||
'/1/update',
|
||||
'/1/delete',
|
||||
))
|
||||
@pytest.mark.parametrize("path", ("/create", "/1/update", "/1/delete"))
|
||||
def test_login_required(client, path):
|
||||
response = client.post(path)
|
||||
assert response.headers['Location'] == 'http://localhost/auth/login'
|
||||
assert response.headers["Location"] == "http://localhost/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.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
|
||||
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
|
||||
assert b'href="/1/update"' not in client.get("/").data
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path', (
|
||||
'/2/update',
|
||||
'/2/delete',
|
||||
))
|
||||
@pytest.mark.parametrize("path", ("/2/update", "/2/delete"))
|
||||
def test_exists_required(client, auth, path):
|
||||
auth.login()
|
||||
assert client.post(path).status_code == 404
|
||||
|
|
@ -51,42 +44,39 @@ def test_exists_required(client, auth, path):
|
|||
|
||||
def test_create(client, auth, app):
|
||||
auth.login()
|
||||
assert client.get('/create').status_code == 200
|
||||
client.post('/create', data={'title': 'created', 'body': ''})
|
||||
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]
|
||||
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': ''})
|
||||
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'
|
||||
post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()
|
||||
assert post["title"] == "updated"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path', (
|
||||
'/create',
|
||||
'/1/update',
|
||||
))
|
||||
@pytest.mark.parametrize("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
|
||||
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'] == 'http://localhost/'
|
||||
response = client.post("/1/delete")
|
||||
assert response.headers["Location"] == "http://localhost/"
|
||||
|
||||
with app.app_context():
|
||||
db = get_db()
|
||||
post = db.execute('SELECT * FROM post WHERE id = 1').fetchone()
|
||||
post = db.execute("SELECT * FROM post WHERE id = 1").fetchone()
|
||||
assert post is None
|
||||
|
|
|
|||
|
|
@ -10,9 +10,9 @@ def test_get_close_db(app):
|
|||
assert db is get_db()
|
||||
|
||||
with pytest.raises(sqlite3.ProgrammingError) as e:
|
||||
db.execute('SELECT 1')
|
||||
db.execute("SELECT 1")
|
||||
|
||||
assert 'closed' in str(e)
|
||||
assert "closed" in str(e)
|
||||
|
||||
|
||||
def test_init_db_command(runner, monkeypatch):
|
||||
|
|
@ -22,7 +22,7 @@ def test_init_db_command(runner, monkeypatch):
|
|||
def fake_init_db():
|
||||
Recorder.called = True
|
||||
|
||||
monkeypatch.setattr('flaskr.db.init_db', fake_init_db)
|
||||
result = runner.invoke(args=['init-db'])
|
||||
assert 'Initialized' in result.output
|
||||
monkeypatch.setattr("flaskr.db.init_db", fake_init_db)
|
||||
result = runner.invoke(args=["init-db"])
|
||||
assert "Initialized" in result.output
|
||||
assert Recorder.called
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ from flaskr import create_app
|
|||
def test_config():
|
||||
"""Test create_app without passing test config."""
|
||||
assert not create_app().testing
|
||||
assert create_app({'TESTING': True}).testing
|
||||
assert create_app({"TESTING": True}).testing
|
||||
|
||||
|
||||
def test_hello(client):
|
||||
response = client.get('/hello')
|
||||
assert response.data == b'Hello, World!'
|
||||
response = client.get("/hello")
|
||||
assert response.data == b"Hello, World!"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue