From 3fa83b3745ca72e5d3ab215ce8c5c013306fadc9 Mon Sep 17 00:00:00 2001 From: Matt Molyneaux Date: Fri, 13 Sep 2019 13:51:01 +0100 Subject: [PATCH] Some initial tests --- test-requirements.txt | 40 +++++++++++++++++++++++++++++++++++ tests/__init__.py | 0 tests/conftest.py | 28 +++++++++++++++++++++++++ tests/test_homepage.py | 42 +++++++++++++++++++++++++++++++++++++ tests/utils.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 test-requirements.txt create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_homepage.py create mode 100644 tests/utils.py diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..521ee84 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1,40 @@ +asn1crypto==0.24.0 +atomicwrites==1.3.0 +attrs==19.1.0 +bcrypt==3.1.7 +beautifulsoup4==4.7.1 +certifi==2019.9.11 +cffi==1.12.3 +chardet==3.0.4 +Click==7.0 +cryptography==2.7 +decorator==4.4.0 +Flask==1.1.1 +Flask-MySQLdb==0.2.0 +http-ece==1.1.0 +idna==2.8 +importlib-metadata==0.22 +itsdangerous==1.1.0 +Jinja2==2.10.1 +markovify==0.7.1 +MarkupSafe==1.1.1 +Mastodon.py==1.4.6 +more-itertools==7.2.0 +mysqlclient==1.4.4 +packaging==19.1 +pluggy==0.13.0 +py==1.8.0 +pycparser==2.19 +pyparsing==2.4.2 +pytest==5.1.2 +python-dateutil==2.8.0 +python-magic==0.4.15 +pytz==2019.2 +requests==2.22.0 +six==1.12.0 +soupsieve==1.9.3 +Unidecode==1.1.1 +urllib3==1.25.3 +wcwidth==0.1.7 +Werkzeug==0.15.6 +zipp==0.6.0 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..5edf9f8 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,28 @@ +import json + +import pytest + +from .utils import get_mysql, setup_db, cleanup_db +from webui import app + +cfg = json.load(open('config.json')) + + +@pytest.fixture(scope="function") +def database(): + db = get_mysql() + setup_db(db) + + cursor = db.cursor() + cursor.execute("SELECT COUNT(*) FROM users") + assert cursor.fetchone() == (0,), "Something went wrong!" + yield db + cleanup_db(db) + + +@pytest.fixture(scope="function") +def client(): + app.config['TESTING'] = True + client = app.test_client() + + return client diff --git a/tests/test_homepage.py b/tests/test_homepage.py new file mode 100644 index 0000000..0e6d0fb --- /dev/null +++ b/tests/test_homepage.py @@ -0,0 +1,42 @@ +def test_homepage(client): + response = client.get("/") + assert response.status_code == 200 + + +def test_homepage_logged_in_no_bots(client, database): + with client.session_transaction() as session: + session["user_id"] = "123" + response = client.get("/") + assert response.status_code == 200 + assert b"Hi there! You have 0 bots." in response.data + + +def test_home_logged_in_has_bots(client, database): + cursor = database.cursor() + + # TODO move these lines into a factory + cursor.execute("INSERT INTO `users` (email, password) VALUES (%s, %s)", ("user1@localhost", "pass1")) + user1_id = cursor.lastrowid + cursor.execute("INSERT INTO `users` (email, password) VALUES (%s, %s)", ("user2@localhost", "pass2")) + user2_id = cursor.lastrowid + + cursor.execute("INSERT INTO `credentials` (client_id, client_secret, secret) VALUES (%s, %s, %s)", ("123", "123", "123")) + fake_credentials = cursor.lastrowid + + cursor.execute("INSERT INTO `bots` (handle, user_id, enabled, credentials_id, push_public_key, push_private_key, push_secret) VALUES (%s, %s, %s, %s, %s, %s, %s)", + ("handle1", user1_id, False, fake_credentials, "pubkey", "privkey", "pushsecret")) + cursor.execute("INSERT INTO `bots` (handle, user_id, enabled, credentials_id, push_public_key, push_private_key, push_secret) VALUES (%s, %s, %s, %s, %s, %s, %s)", + ("handle2", user2_id, True, fake_credentials, "pubkey", "privkey", "pushsecret")) + + + with client.session_transaction() as session: + session["user_id"] = user1_id + response = client.get("/") + assert response.status_code == 200 + assert b"Hi there! You have 1 bot, 0 of which are currently active." in response.data + + cursor.execute("UPDATE `bots` SET enabled = %s WHERE user_id = %s", (True, user1_id)) + + response = client.get("/") + assert response.status_code == 200 + assert b"Hi there! You have 1 bot, 1 of which is currently active." in response.data diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..c7153f1 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,47 @@ +import json +import os + +import MySQLdb + +SQL_SETUP_PATH = "setup.sql" + +cfg = json.load(open('config.json')) + + +def get_mysql(): + return MySQLdb.connect( + host=cfg['db_host'], + user=cfg['db_user'], + passwd=cfg['db_pass'], + use_unicode=True, + charset="utf8mb4" + ) + + +def setup_db(db): + cursor = db.cursor() + + db_exists = cursor.execute("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=%s", (cfg['db_name'],)) + if db_exists: + if not os.environ.get("FEDIBOOKS_TEST_OVERWRITE_DB"): + cursor.close() + db.close() + raise Exception("Database exists, I'm not touching this because tests destroy data!") + else: + cursor.execute("DROP DATABASE %s" % cfg["db_name"]) + + cursor.execute("CREATE DATABASE %s" % cfg["db_name"]) + + cursor.execute(open(SQL_SETUP_PATH).read()) + cursor.close() + + db.autocommit(True) + return db + + +def cleanup_db(db): + cursor = db.cursor() + + cursor.execute("DROP DATABASE %s" % cfg["db_name"]) + cursor.close() + db.close()