diff --git a/aurweb/testing/models.py b/aurweb/testing/models.py deleted file mode 100644 index 8a27c409..00000000 --- a/aurweb/testing/models.py +++ /dev/null @@ -1,25 +0,0 @@ -import warnings - -from sqlalchemy import exc - -import aurweb.db - - -def make_user(**kwargs): - with warnings.catch_warnings(): - warnings.simplefilter("ignore", exc.SAWarning) - from aurweb.models.user import User - user = User(**kwargs) - aurweb.db.session.add(user) - aurweb.db.session.commit() - return user - - -def make_session(**kwargs): - with warnings.catch_warnings(): - warnings.simplefilter("ignore", exc.SAWarning) - from aurweb.models.session import Session - session = Session(**kwargs) - aurweb.db.session.add(session) - aurweb.db.session.commit() - return session diff --git a/setup.cfg b/setup.cfg index 98261651..31a0eb8a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -2,18 +2,26 @@ max-line-length = 127 max-complexity = 10 -# Ignore some unavoidable flake8 warnings; we know this is against -# pycodestyle, but some of the existing codebase uses `I` variables, -# so specifically silence warnings about it in pre-defined files. -# In E741, the 'I', 'O', 'l' are ambiguous variable names. -# Our current implementation uses these variables through HTTP -# and the FastAPI form specification wants them named as such. -# In C901's case, our process_account_form function is way too -# complex for PEP (too many if statements). However, we need to -# process these anyways, and making it any more complex would -# just add confusion to the implementation. +# aurweb/routers/accounts.py +# Ignore some unavoidable flake8 warnings; we know this is against +# pycodestyle, but some of the existing codebase uses `I` variables, +# so specifically silence warnings about it in pre-defined files. +# In E741, the 'I', 'O', 'l' are ambiguous variable names. +# Our current implementation uses these variables through HTTP +# and the FastAPI form specification wants them named as such. +# In C901's case, our process_account_form function is way too +# complex for PEP (too many if statements). However, we need to +# process these anyways, and making it any more complex would +# just add confusion to the implementation. +# +# test/test_ssh_pub_key.py +# E501 is detected due to our >127 width test constant. Ignore it. +# Due to this, line width should _always_ be looked at in code reviews. +# Anything like this should be questioned. +# per-file-ignores = aurweb/routers/accounts.py:E741,C901 + test/test_ssh_pub_key.py:E501 [isort] line_length = 127 diff --git a/test/test_accepted_term.py b/test/test_accepted_term.py index 4dd8a5ca..4ddf1fc3 100644 --- a/test/test_accepted_term.py +++ b/test/test_accepted_term.py @@ -2,14 +2,14 @@ import pytest from sqlalchemy.exc import IntegrityError -from aurweb.db import create, delete, query +from aurweb.db import create, query from aurweb.models.accepted_term import AcceptedTerm from aurweb.models.account_type import AccountType from aurweb.models.term import Term from aurweb.models.user import User from aurweb.testing import setup_test_db -user, term, accepted_term = None, None, None +user = term = accepted_term = None @pytest.fixture(autouse=True) @@ -26,11 +26,6 @@ def setup(): term = create(Term, Description="Test term", URL="https://test.term") - yield term - - delete(Term, Term.ID == term.ID) - delete(User, User.ID == user.ID) - def test_accepted_term(): accepted_term = create(AcceptedTerm, User=user, Term=term) @@ -40,8 +35,6 @@ def test_accepted_term(): assert accepted_term in user.accepted_terms assert accepted_term in term.accepted - delete(AcceptedTerm, AcceptedTerm.User == user, AcceptedTerm.Term == term) - def test_accepted_term_null_user_raises_exception(): from aurweb.db import session diff --git a/test/test_account_type.py b/test/test_account_type.py index 9419970c..3bd76d1e 100644 --- a/test/test_account_type.py +++ b/test/test_account_type.py @@ -1,9 +1,9 @@ import pytest +from aurweb.db import create, delete, query from aurweb.models.account_type import AccountType from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_user account_type = None @@ -12,24 +12,17 @@ account_type = None def setup(): setup_test_db("Users") - from aurweb.db import session - global account_type - account_type = AccountType(AccountType="TestUser") - session.add(account_type) - session.commit() + account_type = create(AccountType, AccountType="TestUser") yield account_type - session.delete(account_type) - session.commit() + delete(AccountType, AccountType.ID == account_type.ID) def test_account_type(): """ Test creating an AccountType, and reading its columns. """ - from aurweb.db import session - # Make sure it got created and was given an ID. assert bool(account_type.ID) @@ -39,20 +32,17 @@ def test_account_type(): "" % ( account_type.ID) - record = session.query(AccountType).filter( - AccountType.AccountType == "TestUser").first() + record = query(AccountType, + AccountType.AccountType == "TestUser").first() assert account_type == record def test_user_account_type_relationship(): - from aurweb.db import session - - user = make_user(Username="test", Email="test@example.org", - RealName="Test User", Passwd="testPassword", - AccountType=account_type) + user = create(User, Username="test", Email="test@example.org", + RealName="Test User", Passwd="testPassword", + AccountType=account_type) assert user.AccountType == account_type assert account_type.users.filter(User.ID == user.ID).first() - session.delete(user) - session.commit() + delete(User, User.ID == user.ID) diff --git a/test/test_accounts_routes.py b/test/test_accounts_routes.py index c42736fa..0f813823 100644 --- a/test/test_accounts_routes.py +++ b/test/test_accounts_routes.py @@ -12,14 +12,13 @@ from fastapi.testclient import TestClient from aurweb import captcha from aurweb.asgi import app -from aurweb.db import create, delete, query +from aurweb.db import create, query from aurweb.models.account_type import AccountType from aurweb.models.ban import Ban from aurweb.models.session import Session from aurweb.models.ssh_pub_key import SSHPubKey, get_fingerprint from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_user from aurweb.testing.requests import Request # Some test global constants. @@ -39,9 +38,9 @@ def setup(): account_type = query(AccountType, AccountType.AccountType == "User").first() - user = make_user(Username=TEST_USERNAME, Email=TEST_EMAIL, - RealName="Test User", Passwd="testPassword", - AccountType=account_type) + user = create(User, Username=TEST_USERNAME, Email=TEST_EMAIL, + RealName="Test User", Passwd="testPassword", + AccountType=account_type) def test_get_passreset_authed_redirects(): @@ -751,8 +750,8 @@ def test_post_account_edit_error_unauthorized(): request = Request() sid = user.login(request, "testPassword") - test2 = create(User, Username="test2", Email="test2@example.org", - Passwd="testPassword") + create(User, Username="test2", + Email="test2@example.org", Passwd="testPassword") post_data = { "U": "test", diff --git a/test/test_auth.py b/test/test_auth.py index d43459cd..7837e7f7 100644 --- a/test/test_auth.py +++ b/test/test_auth.py @@ -5,16 +5,14 @@ import pytest from starlette.authentication import AuthenticationError from aurweb.auth import BasicAuthBackend, has_credential -from aurweb.db import query +from aurweb.db import create, query from aurweb.models.account_type import AccountType +from aurweb.models.session import Session +from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_session, make_user from aurweb.testing.requests import Request -# Persistent user object, initialized in our setup fixture. -user = None -backend = None -request = None +user = backend = request = None @pytest.fixture(autouse=True) @@ -23,16 +21,11 @@ def setup(): setup_test_db("Users", "Sessions") - from aurweb.db import session - account_type = query(AccountType, AccountType.AccountType == "User").first() - user = make_user(Username="test", Email="test@example.com", - RealName="Test User", Passwd="testPassword", - AccountType=account_type) - - session.add(user) - session.commit() + user = create(User, Username="test", Email="test@example.com", + RealName="Test User", Passwd="testPassword", + AccountType=account_type) backend = BasicAuthBackend() request = Request() @@ -60,8 +53,8 @@ async def test_auth_backend_invalid_sid(): async def test_auth_backend_invalid_user_id(): # Create a new session with a fake user id. now_ts = datetime.utcnow().timestamp() - make_session(UsersID=666, SessionID="realSession", - LastUpdateTS=now_ts + 5) + create(Session, UsersID=666, SessionID="realSession", + LastUpdateTS=now_ts + 5) # Here, we specify a real SID; but it's user is not there. request.cookies["AURSID"] = "realSession" @@ -74,8 +67,8 @@ async def test_basic_auth_backend(): # This time, everything matches up. We expect the user to # equal the real_user. now_ts = datetime.utcnow().timestamp() - make_session(UsersID=user.ID, SessionID="realSession", - LastUpdateTS=now_ts + 5) + create(Session, UsersID=user.ID, SessionID="realSession", + LastUpdateTS=now_ts + 5) _, result = await backend.authenticate(request) assert result == user diff --git a/test/test_auth_routes.py b/test/test_auth_routes.py index ff8a08e9..360b48cc 100644 --- a/test/test_auth_routes.py +++ b/test/test_auth_routes.py @@ -8,33 +8,34 @@ from fastapi.testclient import TestClient import aurweb.config from aurweb.asgi import app -from aurweb.db import query +from aurweb.db import create, query from aurweb.models.account_type import AccountType from aurweb.models.session import Session +from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_user # Some test global constants. TEST_USERNAME = "test" TEST_EMAIL = "test@example.org" # Global mutables. -client = TestClient(app) -user = None +user = client = None @pytest.fixture(autouse=True) def setup(): - global user + global user, client setup_test_db("Users", "Sessions", "Bans") account_type = query(AccountType, AccountType.AccountType == "User").first() - user = make_user(Username=TEST_USERNAME, Email=TEST_EMAIL, - RealName="Test User", Passwd="testPassword", - AccountType=account_type) + user = create(User, Username=TEST_USERNAME, Email=TEST_EMAIL, + RealName="Test User", Passwd="testPassword", + AccountType=account_type) + + client = TestClient(app) def test_login_logout(): diff --git a/test/test_ban.py b/test/test_ban.py index de4f5b1b..a4fa5a28 100644 --- a/test/test_ban.py +++ b/test/test_ban.py @@ -6,27 +6,23 @@ 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 = None - -request = Request() +ban = request = None @pytest.fixture(autouse=True) def setup(): - from aurweb.db import session - - global ban + global ban, request setup_test_db("Bans") - ban = Ban(IPAddress="127.0.0.1", - BanTS=datetime.utcnow() + timedelta(seconds=30)) - session.add(ban) - session.commit() + ts = datetime.utcnow() + timedelta(seconds=30) + ban = create(Ban, IPAddress="127.0.0.1", BanTS=ts) + request = Request() def test_ban(): diff --git a/test/test_exceptions.py b/test/test_exceptions.py index feac2656..7247106b 100644 --- a/test/test_exceptions.py +++ b/test/test_exceptions.py @@ -1,102 +1,99 @@ -from aurweb.exceptions import (AlreadyVotedException, AurwebException, BannedException, BrokenUpdateHookException, - InvalidArgumentsException, InvalidCommentException, InvalidPackageBaseException, - InvalidReasonException, InvalidRepositoryNameException, InvalidUserException, - MaintenanceException, NotVotedException, PackageBaseExistsException, PermissionDeniedException) +from aurweb import exceptions def test_aurweb_exception(): try: - raise AurwebException("test") - except AurwebException as exc: + raise exceptions.AurwebException("test") + except exceptions.AurwebException as exc: assert str(exc) == "test" def test_maintenance_exception(): try: - raise MaintenanceException("test") - except MaintenanceException as exc: + raise exceptions.MaintenanceException("test") + except exceptions.MaintenanceException as exc: assert str(exc) == "test" def test_banned_exception(): try: - raise BannedException("test") - except BannedException as exc: + raise exceptions.BannedException("test") + except exceptions.BannedException as exc: assert str(exc) == "test" def test_already_voted_exception(): try: - raise AlreadyVotedException("test") - except AlreadyVotedException as exc: + raise exceptions.AlreadyVotedException("test") + except exceptions.AlreadyVotedException as exc: assert str(exc) == "already voted for package base: test" def test_broken_update_hook_exception(): try: - raise BrokenUpdateHookException("test") - except BrokenUpdateHookException as exc: + raise exceptions.BrokenUpdateHookException("test") + except exceptions.BrokenUpdateHookException as exc: assert str(exc) == "broken update hook: test" def test_invalid_arguments_exception(): try: - raise InvalidArgumentsException("test") - except InvalidArgumentsException as exc: + raise exceptions.InvalidArgumentsException("test") + except exceptions.InvalidArgumentsException as exc: assert str(exc) == "test" def test_invalid_packagebase_exception(): try: - raise InvalidPackageBaseException("test") - except InvalidPackageBaseException as exc: + raise exceptions.InvalidPackageBaseException("test") + except exceptions.InvalidPackageBaseException as exc: assert str(exc) == "package base not found: test" def test_invalid_comment_exception(): try: - raise InvalidCommentException("test") - except InvalidCommentException as exc: + raise exceptions.InvalidCommentException("test") + except exceptions.InvalidCommentException as exc: assert str(exc) == "comment is too short: test" def test_invalid_reason_exception(): try: - raise InvalidReasonException("test") - except InvalidReasonException as exc: + raise exceptions.InvalidReasonException("test") + except exceptions.InvalidReasonException as exc: assert str(exc) == "invalid reason: test" def test_invalid_user_exception(): try: - raise InvalidUserException("test") - except InvalidUserException as exc: + raise exceptions.InvalidUserException("test") + except exceptions.InvalidUserException as exc: assert str(exc) == "unknown user: test" def test_not_voted_exception(): try: - raise NotVotedException("test") - except NotVotedException as exc: + raise exceptions.NotVotedException("test") + except exceptions.NotVotedException as exc: assert str(exc) == "missing vote for package base: test" def test_packagebase_exists_exception(): try: - raise PackageBaseExistsException("test") - except PackageBaseExistsException as exc: + raise exceptions.PackageBaseExistsException("test") + except exceptions.PackageBaseExistsException as exc: assert str(exc) == "package base already exists: test" def test_permission_denied_exception(): try: - raise PermissionDeniedException("test") - except PermissionDeniedException as exc: + raise exceptions.PermissionDeniedException("test") + except exceptions.PermissionDeniedException as exc: assert str(exc) == "permission denied: test" def test_repository_name_exception(): try: - raise InvalidRepositoryNameException("test") - except InvalidRepositoryNameException as exc: + raise exceptions.InvalidRepositoryNameException("test") + except exceptions.InvalidRepositoryNameException as exc: assert str(exc) == "invalid repository name: test" diff --git a/test/test_group.py b/test/test_group.py index bbb774b9..da017a96 100644 --- a/test/test_group.py +++ b/test/test_group.py @@ -2,16 +2,20 @@ import pytest from sqlalchemy.exc import IntegrityError -from aurweb.db import create, delete, get_engine +from aurweb.db import create from aurweb.models.group import Group +from aurweb.testing import setup_test_db + + +@pytest.fixture(autouse=True) +def setup(): + setup_test_db("Groups") def test_group_creation(): - get_engine() group = create(Group, Name="Test Group") assert bool(group.ID) assert group.Name == "Test Group" - delete(Group, Group.ID == group.ID) def test_group_null_name_raises_exception(): diff --git a/test/test_initdb.py b/test/test_initdb.py index ff089b63..eae33007 100644 --- a/test/test_initdb.py +++ b/test/test_initdb.py @@ -23,5 +23,6 @@ def test_run(): use_alembic = True verbose = False aurweb.initdb.run(Args()) - assert aurweb.db.session.query(AccountType).filter( - AccountType.AccountType == "User").first() is not None + record = aurweb.db.query(AccountType, + AccountType.AccountType == "User").first() + assert record is not None diff --git a/test/test_package.py b/test/test_package.py index 1d670087..66d557f3 100644 --- a/test/test_package.py +++ b/test/test_package.py @@ -34,8 +34,6 @@ def setup(): Description="Test description.", URL="https://test.package") - yield package - def test_package(): from aurweb.db import session diff --git a/test/test_package_base.py b/test/test_package_base.py index dcb0eb9e..e0359f4f 100644 --- a/test/test_package_base.py +++ b/test/test_package_base.py @@ -5,8 +5,8 @@ from sqlalchemy.exc import IntegrityError from aurweb.db import create, query from aurweb.models.account_type import AccountType from aurweb.models.package_base import PackageBase +from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_user user = None @@ -19,10 +19,9 @@ def setup(): account_type = query(AccountType, AccountType.AccountType == "User").first() - user = make_user(Username="test", Email="test@example.org", - RealName="Test User", Passwd="testPassword", - AccountType=account_type) - yield user + user = create(User, Username="test", Email="test@example.org", + RealName="Test User", Passwd="testPassword", + AccountType=account_type) def test_package_base(): diff --git a/test/test_package_keyword.py b/test/test_package_keyword.py index f110b123..316e7ca8 100644 --- a/test/test_package_keyword.py +++ b/test/test_package_keyword.py @@ -6,10 +6,10 @@ from aurweb.db import create, query from aurweb.models.account_type import AccountType from aurweb.models.package_base import PackageBase from aurweb.models.package_keyword import PackageKeyword +from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_user -user, pkgbase = None, None +user = pkgbase = None @pytest.fixture(autouse=True) @@ -20,15 +20,13 @@ def setup(): account_type = query(AccountType, AccountType.AccountType == "User").first() - user = make_user(Username="test", Email="test@example.org", - RealName="Test User", Passwd="testPassword", - AccountType=account_type) + user = create(User, Username="test", Email="test@example.org", + RealName="Test User", Passwd="testPassword", + AccountType=account_type) pkgbase = create(PackageBase, Name="beautiful-package", Maintainer=user) - yield pkgbase - def test_package_keyword(): pkg_keyword = create(PackageKeyword, diff --git a/test/test_routes.py b/test/test_routes.py index e4816231..f4bb063f 100644 --- a/test/test_routes.py +++ b/test/test_routes.py @@ -7,27 +7,28 @@ import pytest from fastapi.testclient import TestClient from aurweb.asgi import app -from aurweb.db import query +from aurweb.db import create, query from aurweb.models.account_type import AccountType +from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_user from aurweb.testing.requests import Request -client = TestClient(app) -user = None +user = client = None @pytest.fixture(autouse=True) def setup(): - global user + global user, client setup_test_db("Users", "Sessions") account_type = query(AccountType, AccountType.AccountType == "User").first() - user = make_user(Username="test", Email="test@example.org", - RealName="Test User", Passwd="testPassword", - AccountType=account_type) + user = create(User, Username="test", Email="test@example.org", + RealName="Test User", Passwd="testPassword", + AccountType=account_type) + + client = TestClient(app) def test_index(): diff --git a/test/test_session.py b/test/test_session.py index 560f628c..2877ea7f 100644 --- a/test/test_session.py +++ b/test/test_session.py @@ -4,39 +4,38 @@ from unittest import mock import pytest +from aurweb.db import create, query from aurweb.models.account_type import AccountType -from aurweb.models.session import generate_unique_sid +from aurweb.models.session import Session, generate_unique_sid +from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_session, make_user -user, _session = None, None +user = session = None @pytest.fixture(autouse=True) def setup(): - from aurweb.db import session - - global user, _session + global user, session setup_test_db("Users", "Sessions") - account_type = session.query(AccountType).filter( - AccountType.AccountType == "User").first() - user = make_user(Username="test", Email="test@example.org", - ResetKey="testReset", Passwd="testPassword", - AccountType=account_type) - _session = make_session(UsersID=user.ID, SessionID="testSession", - LastUpdateTS=datetime.utcnow()) + account_type = query(AccountType, + AccountType.AccountType == "User").first() + user = create(User, Username="test", Email="test@example.org", + ResetKey="testReset", Passwd="testPassword", + AccountType=account_type) + session = create(Session, UsersID=user.ID, SessionID="testSession", + LastUpdateTS=datetime.utcnow()) def test_session(): - assert _session.SessionID == "testSession" - assert _session.UsersID == user.ID + assert session.SessionID == "testSession" + assert session.UsersID == user.ID def test_session_user_association(): # Make sure that the Session user attribute is correct. - assert _session.User == user + assert session.User == user def test_generate_unique_sid(): diff --git a/test/test_ssh_pub_key.py b/test/test_ssh_pub_key.py index fe9df047..4072549e 100644 --- a/test/test_ssh_pub_key.py +++ b/test/test_ssh_pub_key.py @@ -1,46 +1,37 @@ import pytest -from aurweb.db import query +from aurweb.db import create, query from aurweb.models.account_type import AccountType from aurweb.models.ssh_pub_key import SSHPubKey, get_fingerprint +from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_user TEST_SSH_PUBKEY = """ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCycoCi5yGCvSclH2wmNBUuwsYEzRZZBJaQquRc4ysl+Tg+/jiDkR3Zn9fIznC4KnFoyrIHzkKuePZ3bNDYwkZxkJKoWBCh4hXKDXSm87FMN0+VDC+1QxF/z0XaAGr/P6f4XukabyddypBdnHcZiplbw+YOSqcAE2TCqOlSXwNMOcF9U89UsR/Q9i9I52hlvU0q8+fZVGhou1KCowFSnHYtrr5KYJ04CXkJ13DkVf3+pjQWyrByvBcf1hGEaczlgfobrrv/y96jDhgfXucxliNKLdufDPPkii3LhhsNcDmmI1VZ3v0irKvd9WZuauqloobY84zEFcDTyjn0hxGjVeYFejm4fBnvjga0yZXORuWksdNfXWLDxFk6MDDd1jF0ExRbP+OxDuU4IVyIuDL7S3cnbf2YjGhkms/8voYT2OBE7FwNlfv98Kr0NUp51zpf55Arxn9j0Rz9xTA7FiODQgCn6iQ0SDtzUNL0IKTCw26xJY5gzMxbfpvzPQGeulx/ioM= kevr@volcano """ -user, ssh_pub_key = None, None +user = ssh_pub_key = None @pytest.fixture(autouse=True) def setup(): - from aurweb.db import session - global user, ssh_pub_key setup_test_db("Users", "SSHPubKeys") account_type = query(AccountType, AccountType.AccountType == "User").first() - user = make_user(Username="test", Email="test@example.org", - RealName="Test User", Passwd="testPassword", - AccountType=account_type) + user = create(User, Username="test", Email="test@example.org", + RealName="Test User", Passwd="testPassword", + AccountType=account_type) assert account_type == user.AccountType assert account_type.ID == user.AccountTypeID - ssh_pub_key = SSHPubKey(UserID=user.ID, - Fingerprint="testFingerprint", - PubKey="testPubKey") - - session.add(ssh_pub_key) - session.commit() - - yield ssh_pub_key - - session.delete(ssh_pub_key) - session.commit() + ssh_pub_key = create(SSHPubKey, + UserID=user.ID, + Fingerprint="testFingerprint", + PubKey="testPubKey") def test_ssh_pub_key(): diff --git a/test/test_term.py b/test/test_term.py index 00397b33..aa1dfcc6 100644 --- a/test/test_term.py +++ b/test/test_term.py @@ -2,13 +2,14 @@ import pytest from sqlalchemy.exc import IntegrityError -from aurweb.db import create, delete, get_engine +from aurweb.db import create from aurweb.models.term import Term +from aurweb.testing import setup_test_db @pytest.fixture(autouse=True) def setup(): - get_engine() + setup_test_db("Terms") def test_term_creation(): @@ -18,7 +19,6 @@ def test_term_creation(): assert term.Description == "Term description" assert term.URL == "https://fake_url.io" assert term.Revision == 1 - delete(Term, Term.ID == term.ID) def test_term_null_description_raises_exception(): diff --git a/test/test_user.py b/test/test_user.py index e8056681..8b4da61e 100644 --- a/test/test_user.py +++ b/test/test_user.py @@ -8,23 +8,20 @@ import pytest import aurweb.auth import aurweb.config -from aurweb.db import query +from aurweb.db import create, query from aurweb.models.account_type import AccountType from aurweb.models.ban import Ban from aurweb.models.session import Session from aurweb.models.ssh_pub_key import SSHPubKey from aurweb.models.user import User from aurweb.testing import setup_test_db -from aurweb.testing.models import make_session, make_user from aurweb.testing.requests import Request -account_type, user = None, None +account_type = user = None @pytest.fixture(autouse=True) def setup(): - from aurweb.db import session - global account_type, user setup_test_db("Users", "Sessions", "Bans", "SSHPubKeys") @@ -32,15 +29,13 @@ def setup(): account_type = query(AccountType, AccountType.AccountType == "User").first() - user = make_user(Username="test", Email="test@example.org", - RealName="Test User", Passwd="testPassword", - AccountType=account_type) + user = create(User, Username="test", Email="test@example.org", + RealName="Test User", Passwd="testPassword", + AccountType=account_type) def test_user_login_logout(): """ Test creating a user and reading its columns. """ - from aurweb.db import session - # Assert that make_user created a valid user. assert bool(user.ID) @@ -61,8 +56,8 @@ def test_user_login_logout(): assert "AURSID" in request.cookies # Expect that User session relationships work right. - user_session = session.query(Session).filter( - Session.UsersID == user.ID).first() + user_session = query(Session, + Session.UsersID == user.ID).first() assert user_session == user.session assert user.session.SessionID == sid assert user.session.User == user @@ -103,13 +98,9 @@ def test_user_login_twice(): def test_user_login_banned(): - from aurweb.db import session - # Add ban for the next 30 seconds. banned_timestamp = datetime.utcnow() + timedelta(seconds=30) - ban = Ban(IPAddress="127.0.0.1", BanTS=banned_timestamp) - session.add(ban) - session.commit() + create(Ban, IPAddress="127.0.0.1", BanTS=banned_timestamp) request = Request() request.client.host = "127.0.0.1" @@ -138,19 +129,14 @@ def test_legacy_user_authentication(): def test_user_login_with_outdated_sid(): - from aurweb.db import session - # Make a session with a LastUpdateTS 5 seconds ago, causing # user.login to update it with a new sid. - _session = make_session(UsersID=user.ID, SessionID="stub", - LastUpdateTS=datetime.utcnow().timestamp() - 5) + create(Session, UsersID=user.ID, SessionID="stub", + LastUpdateTS=datetime.utcnow().timestamp() - 5) sid = user.login(Request(), "testPassword") assert sid and user.is_authenticated() assert sid != "stub" - session.delete(_session) - session.commit() - def test_user_update_password(): user.update_password("secondPassword") @@ -169,21 +155,14 @@ def test_user_has_credential(): def test_user_ssh_pub_key(): - from aurweb.db import session - assert user.ssh_pub_key is None - ssh_pub_key = SSHPubKey(UserID=user.ID, - Fingerprint="testFingerprint", - PubKey="testPubKey") - session.add(ssh_pub_key) - session.commit() + ssh_pub_key = create(SSHPubKey, UserID=user.ID, + Fingerprint="testFingerprint", + PubKey="testPubKey") assert user.ssh_pub_key == ssh_pub_key - session.delete(ssh_pub_key) - session.commit() - def test_user_credential_types(): from aurweb.db import session @@ -203,8 +182,7 @@ def test_user_credential_types(): assert aurweb.auth.trusted_user_or_dev(user) developer_type = query(AccountType, - AccountType.AccountType == "Developer")\ - .first() + AccountType.AccountType == "Developer").first() user.AccountType = developer_type session.commit()