1
0
Fork 0
mirror of https://github.com/Lynnesbian/FediBooks/ synced 2024-11-16 21:08:57 +00:00

Some initial tests

This commit is contained in:
Matt Molyneaux 2019-09-13 13:51:01 +01:00
parent a9fcda794c
commit 3fa83b3745
5 changed files with 157 additions and 0 deletions

40
test-requirements.txt Normal file
View file

@ -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

0
tests/__init__.py Normal file
View file

28
tests/conftest.py Normal file
View file

@ -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

42
tests/test_homepage.py Normal file
View file

@ -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

47
tests/utils.py Normal file
View file

@ -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()