diff --git a/tests/conftest.py b/tests/conftest.py index bb03581..646053c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -32,4 +32,21 @@ def client(app): @pytest.fixture 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) diff --git a/tests/test_auth.py b/tests/test_auth.py new file mode 100644 index 0000000..6a75d9c --- /dev/null +++ b/tests/test_auth.py @@ -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 diff --git a/tests/test_db.py b/tests/test_db.py new file mode 100644 index 0000000..959f7ac --- /dev/null +++ b/tests/test_db.py @@ -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