From 5bcc1ed9df4e7a6b8086cac3f25a466aee3bebcd Mon Sep 17 00:00:00 2001 From: neodarz Date: Sat, 16 Jun 2018 17:52:31 +0200 Subject: Add first structure of the application --- src/tests/conftest.py | 34 ++++++++++++++++++++++++++++++++++ src/tests/test_api.py | 22 ++++++++++++++++++++++ src/tests/test_db.py | 8 ++++++++ src/tests/test_factory.py | 5 +++++ src/tests/test_user.py | 15 +++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 src/tests/conftest.py create mode 100644 src/tests/test_api.py create mode 100644 src/tests/test_db.py create mode 100644 src/tests/test_factory.py create mode 100644 src/tests/test_user.py (limited to 'src/tests') diff --git a/src/tests/conftest.py b/src/tests/conftest.py new file mode 100644 index 0000000..6a54ae4 --- /dev/null +++ b/src/tests/conftest.py @@ -0,0 +1,34 @@ +import pytest +from lb_app import create_app +from lb_app.app_db.db import MongoDB + +import pprint + +data = {'username': 'neo'} + +@pytest.fixture +def app(): + app = create_app({ + 'TESTING': True + }) + + app.config.from_mapping( + MONGO_URI= 'mongodb://localhost:27017/test_liberationCenter' + ) + + + with app.app_context(): + db = MongoDB(app) + db.set_up() + mongo = db.connection() + mongo.db.users.insert_one(data) + + yield app + +@pytest.fixture +def client(app): + return app.test_client() + +@pytest.fixture +def runner(app): + return app.test_cli_runner() diff --git a/src/tests/test_api.py b/src/tests/test_api.py new file mode 100644 index 0000000..063f32f --- /dev/null +++ b/src/tests/test_api.py @@ -0,0 +1,22 @@ +import pytest, re + +from flask import session +from bson.json_util import dumps + +from pprint import pprint + +def test_api_get_all_users(client, app): + response = client.get('/api/users') + assert response.status_code == 200 + assert response.is_json + assert re.search('username', str(response.data)) + +@pytest.mark.parametrize(('username', 'message'), ( + ('', b'{"msg": "Username is required."}'), + ('neo', b'{"msg": "User neo is already registered."}'), + ('neodarz', b'{"msg": "done"}') +)) +def test_api_register(client, app, username, message): + response = client.post('/api/user/register', data='{"username": "'+username+'"}') + print(response.data) + assert message in response.data diff --git a/src/tests/test_db.py b/src/tests/test_db.py new file mode 100644 index 0000000..5830f78 --- /dev/null +++ b/src/tests/test_db.py @@ -0,0 +1,8 @@ +import pytest + +from lb_app.app_db.db import MongoDB + +def test_get_close_db(app): + with app.app_context(): + db = MongoDB(app) + assert type(db) is MongoDB diff --git a/src/tests/test_factory.py b/src/tests/test_factory.py new file mode 100644 index 0000000..20a10b4 --- /dev/null +++ b/src/tests/test_factory.py @@ -0,0 +1,5 @@ +from lb_app import create_app + +def test_config(): + assert not create_app().testing + assert create_app({'TESTING': True}).testing diff --git a/src/tests/test_user.py b/src/tests/test_user.py new file mode 100644 index 0000000..3d7ab50 --- /dev/null +++ b/src/tests/test_user.py @@ -0,0 +1,15 @@ +import pytest, re + +from bson.json_util import dumps + +from lb_app.app_db.user import User + +def test_get_all_users(app): + with app.app_context(): + user = User(app) + assert re.search('username', dumps(user.all())) + +def test_register_user(app): + with app.app_context(): + user = User(app) + assert user.register("neodarz") is None -- cgit v1.2.1