Begin testing

This commit is contained in:
2023-06-12 10:36:14 +02:00
parent 869ba82695
commit ae80d8b87f
11 changed files with 161 additions and 19 deletions

0
tests/__init__.py Normal file
View File

87
tests/conftest.py Normal file
View File

@@ -0,0 +1,87 @@
import pytest
from beflask import app as application
from flask_login import FlaskLoginClient
from beflask.models import Benchmark, User, db
class AuthActions(object):
def __init__(self, client):
self._client = client
def login(
self, username="guest", password="patata", follow_redirects=False
):
return self._client.post(
"/login",
data={"username": username, "password": password},
follow_redirects=follow_redirects,
)
def logout(self):
return self._client.get("/logout")
@pytest.fixture
def auth(client):
return AuthActions(client)
@pytest.fixture
def app():
socketio, app = application.create_app("testing")
app.test_client_class = FlaskLoginClient
with app.app_context():
db_seed(db)
return socketio, app
@pytest.fixture
def client(app):
return app[1].test_client()
@pytest.fixture
def runner(app):
return app[1].test_cli_runner()
def db_seed(db):
db.drop_all()
db.create_all()
b = Benchmark(
name="discretizbench",
folder="/Users/rmontanana/Code/discretizbench",
description="Experiments with local discretization and Bayesian "
"classifiers",
)
db.session.add(b)
b = Benchmark(
name="odtebench",
folder="/Users/rmontanana/Code/odtebench",
description="Experiments with STree and Ensemble classifiers",
)
db.session.add(b)
b = Benchmark(
name="covbench",
folder="/Users/rmontanana/Code/covbench",
description="Experiments with COVID-19 dataset",
)
db.session.add(b)
u = User(
username="rmontanana",
email="rmontanana@gmail.com",
admin=True,
benchmark_id=1,
)
u.set_password("patito")
u1 = User(
username="guest",
email="guest@example.com",
admin=False,
benchmark_id=1,
)
u1.set_password("patata")
db.session.add(b)
db.session.add(u)
db.session.add(u1)
db.session.commit()

35
tests/test_login.py Normal file
View File

@@ -0,0 +1,35 @@
import pytest
from urllib.parse import urlparse
from flask import session, g
def test_login(client, auth):
assert client.get("/login").status_code == 200
response = auth.login()
assert response.headers["Location"] == "/index"
auth.logout()
response = auth.login(username="rmontanana", password="patito")
assert response.headers["Location"] == "/index"
with client:
client.get("/index")
assert session["_user_id"] == "1"
assert g._login_user.username == "rmontanana"
auth.logout()
def test_login_invalid(client, auth):
response = auth.login(
username="rmontanana", password="patato", follow_redirects=True
)
assert b"Invalid username or password" in response.data
assert response.status_code == 200
def test_logout(client, auth):
auth.login()
with client:
auth.logout()
assert "user_id" not in session

12
tests/test_main.py Normal file
View File

@@ -0,0 +1,12 @@
from beflask import app
def test_config():
assert not app.create_app()[1].testing
assert app.create_app("testing")[1].testing
def test_index(client):
response = client.get("/")
# check image is in the response
assert b"img/robert-lukeman-_RBcxo9AU-U-unsplash.jpg" in response.data