mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
First off: This commit changes the default development database backend to mysql. sqlite, however, is still completely supported with the caveat that a user must now modify config.dev to use the sqlite backend. While looking into this, it was discovered that our SQLAlchemy backend for mysql (mysql-connector) completely broke model attributes when we switched to utf8mb4_bin (binary) -- it does not correct the correct conversion to and from binary utf8mb4. The new, replacement dependency mysqlclient does. mysqlclient is also recommended in SQLAlchemy documentation as the "best" one available. The mysqlclient backend uses a different exception flow then sqlite, and so tests expecting IntegrityError has to be modified to expect OperationalError from sqlalchemy.exc. So, for each model that we define, check keys that can't be NULL and raise sqlalchemy.exc.IntegrityError if we have to. This way we keep our exceptions uniform. Signed-off-by: Kevin Morris <kevr@0cost.org>
58 lines
1.3 KiB
Python
58 lines
1.3 KiB
Python
import warnings
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
import pytest
|
|
|
|
from sqlalchemy import exc as sa_exc
|
|
|
|
from aurweb.db import create
|
|
from aurweb.models.ban import Ban, is_banned
|
|
from aurweb.testing import setup_test_db
|
|
from aurweb.testing.requests import Request
|
|
|
|
ban = request = None
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup():
|
|
global ban, request
|
|
|
|
setup_test_db("Bans")
|
|
|
|
ts = datetime.utcnow() + timedelta(seconds=30)
|
|
ban = create(Ban, IPAddress="127.0.0.1", BanTS=ts)
|
|
request = Request()
|
|
|
|
|
|
def test_ban():
|
|
assert ban.IPAddress == "127.0.0.1"
|
|
assert bool(ban.BanTS)
|
|
|
|
|
|
def test_invalid_ban():
|
|
from aurweb.db import session
|
|
|
|
with pytest.raises(sa_exc.IntegrityError):
|
|
bad_ban = Ban(BanTS=datetime.utcnow())
|
|
session.add(bad_ban)
|
|
|
|
# We're adding a ban with no primary key; this causes an
|
|
# SQLAlchemy warnings when committing to the DB.
|
|
# Ignore them.
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore", sa_exc.SAWarning)
|
|
session.commit()
|
|
|
|
# Since we got a transaction failure, we need to rollback.
|
|
session.rollback()
|
|
|
|
|
|
def test_banned():
|
|
request.client.host = "127.0.0.1"
|
|
assert is_banned(request)
|
|
|
|
|
|
def test_not_banned():
|
|
request.client.host = "192.168.0.1"
|
|
assert not is_banned(request)
|