53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
import pytest
|
|
from flask import g, session
|
|
from flaskfdx.db import get_db
|
|
|
|
def test_register(client, app):
|
|
assert client.get('/auth/register').status_code == 200
|
|
response = client.post(
|
|
'auth/register', data={'userid': 'a', 'email': 'a@b.c', 'password': 'a'}
|
|
)
|
|
assert 'http://localhost/auth/login' == response.headers['Path']
|
|
|
|
with app.app_context():
|
|
assert get_db().execute(
|
|
"SELECT * FROM users WHERE userid = 'a'",
|
|
).fetchone() is not None
|
|
|
|
@pytest.mark.parametrize(('userid', 'email', 'password', 'message'), (
|
|
('', '', '', b'Userid is required.'),
|
|
('a', 'a@b.c', '', b'Password is required.'),
|
|
('test', 'test', 'test', b'already registered'),
|
|
))
|
|
def test_register_validate_input(client, userid, email, password, message):
|
|
response = client.post(
|
|
'/auth/register',
|
|
data={'userid': userid, 'email': email, 'password': password}
|
|
)
|
|
assert message in response.data
|
|
|
|
def test_login(client, auth):
|
|
assert client.get('/auth/login').status_code == 200
|
|
response = auth.login()
|
|
assert response.headers['Path'] == 'http://localhost/'
|
|
|
|
with client:
|
|
client.get('/')
|
|
assert session['user_id'] == 1
|
|
assert g.user['userid'] == 'test'
|
|
|
|
@pytest.mark.parametrize(('userid', 'password', 'message'), (
|
|
('a', 'test', b'Userid or Password incorrect.'),
|
|
('test', 'a', b'Userid or Password incorrect.'),
|
|
))
|
|
def test_login_validate_input(auth, userid, password, message):
|
|
response = auth.login(userid, password)
|
|
assert message in response.data
|
|
|
|
def testz_logout(client, auth):
|
|
auth.login()
|
|
|
|
with client:
|
|
auth.logout()
|
|
assert 'user_id' not in session
|