fixed typos and added tests for db and auth

This commit is contained in:
2021-10-11 22:14:06 -06:00
parent dffc3e983d
commit 30b21dbde6
3 changed files with 71 additions and 1 deletions
+18 -1
View File
@@ -32,4 +32,21 @@ def client(app):
@pytest.fixture @pytest.fixture
def runner(app): def runner(app):
return app.test_client() return app.test_cli_runner()
class AuthActions(object):
def __init__(self, client):
self._client = client
def login(self, username='test', password='test'):
return self._client.post(
'auth/login',
data={'username': username, 'password': password}
)
def logout(self):
return self._client.get('auth/logout')
@pytest.fixture
def auth(client):
return AuthActions(client)
+27
View File
@@ -0,0 +1,27 @@
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={'username': 'a', 'password': 'a'}
)
assert 'http://localhost/auth/login' == response.headers['Location']
with app.app_context():
assert get_db().execute(
"SELECT * FROM users 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'),
))
def test_register_validate_input(client, username, password, message):
response = client.post(
'/auth/register',
data={'username': username, 'password': password}
)
assert message in response.data
+26
View File
@@ -0,0 +1,26 @@
import sqlite3
import pytest
from flaskfdx.db import get_db
def test_get_close_db(app):
with app.app_context():
db = get_db()
assert db is get_db()
with pytest.raises(sqlite3.ProgrammingError) as e:
db.execute('SELECT 1')
assert 'closed' in str(e.value)
def test_init_db_command(runner, monkeypatch):
class Recorder(object):
called = False
def fake_init_db():
Recorder.called = True
monkeypatch.setattr('flaskfdx.db.init_db', fake_init_db)
result = runner.invoke(args=['init-db'])
assert 'initialized' in result.output
assert Recorder.called