aurweb/test
Kevin Morris 51b60f4210
feat(auth): add requires_{auth,guest} decorators
These new decorators are meant to be used without any arguments
and provide aliases to auth_required:
- `auth_required(True) -> requires_auth`
- `auth_required(False) -> requires_guest`

These decorators should be used without arguments, e.g.:

    @router.get("/")
    @requires_guest
    async def my_route(request: Request):
        return HTMLResponse()

Signed-off-by: Kevin Morris <kevr@0cost.org>
2022-01-02 16:57:42 -08:00
..
scripts Makefile: run pytest units 2021-06-05 19:49:19 -07:00
__init__.py aurweb: Globalize a Translator instance, add more utility 2021-06-05 19:49:42 -07:00
conftest.py feat(testing.email): add Email.dump 2021-12-09 15:10:04 -08:00
Makefile change: set -v for sh tests 2021-11-22 10:22:32 -08:00
README.md update test/README.md to be more aligned with the current state 2021-12-01 12:27:14 -08:00
setup.sh fix(sharness): fix AUR_CONFIG generation for mkpkglists test 2021-11-10 07:39:23 -08:00
sharness.sh sharness: do not use spaces in trash directory 2021-07-27 16:24:34 -07:00
t1100-git-auth.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t1200-git-serve.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t1300-git-update.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
test_accepted_term.py housekeep(fastapi): rewrite test_accepted_term with fixtures 2021-12-01 00:30:18 -08:00
test_account_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_accounts_routes.py revert account type permission changes 2021-12-27 22:41:18 -08:00
test_api_rate_limit.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_asgi.py change(fastapi): centralize HTTPException 2021-12-02 23:23:27 -08:00
test_aurblup.py fix(test_aurblup): use correct type hint for tmpdir 2021-12-01 00:33:31 -08:00
test_auth.py feat(auth): add requires_{auth,guest} decorators 2022-01-02 16:57:42 -08:00
test_auth_routes.py change(python): rework session timing 2021-12-04 02:16:22 -08:00
test_ban.py housekeep(fastapi): rewrite test_ban with fixtures 2021-12-01 00:33:30 -08:00
test_cache.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_captcha.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_config.py feat(aurweb-config): add unset action and simplify 2021-11-27 22:34:15 -08:00
test_db.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_defaults.py feat(FastAPI): added /requests (get) route 2021-10-02 22:43:48 -07:00
test_dependency_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_email.py feat(testing.email): add Email.dump 2021-12-09 15:10:04 -08:00
test_exceptions.py feat(exceptions): add InvariantError 2021-12-09 15:10:06 -08:00
test_filelock.py fix(conftest): use synchronization locks for setup_database 2021-11-28 19:55:11 -08:00
test_group.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_homepage.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_html.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_initdb.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_l10n.py jinja2: add 'tn' filter, a numerical translation 2021-08-30 18:49:01 -07:00
test_license.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_logging.py fix(fastapi): centralize logging initialization 2021-10-21 10:40:52 -07:00
test_mkpkglists.py change(mkpkglists): converted to use aurweb.db ORM 2021-11-28 19:55:08 -08:00
test_notify.py fix(notify): gracefully fail notifications 2021-12-17 18:08:36 -08:00
test_official_provider.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_package.py housekeep(fastapi): rewrite test_package with fixtures 2021-12-01 00:33:29 -08:00
test_package_base.py housekeep(fastapi): rewrite test_package_base with fixtures 2021-12-01 00:30:21 -08:00
test_package_blacklist.py housekeep(fastapi): rewrite test_package_blacklist 2021-12-01 00:28:40 -08:00
test_package_comaintainer.py housekeep(fastapi): rewrite test_package_comaintainer with fixtures 2021-12-01 00:30:19 -08:00
test_package_comment.py housekeep(fastapi): rewrite test_package_comment with fixtures 2021-12-01 00:28:39 -08:00
test_package_dependency.py housekeep(fastapi): rewrite test_package_dependency with fixtures 2021-12-01 00:30:23 -08:00
test_package_group.py housekeep(fastapi): rewrite test_package_group with fixtures 2021-12-01 00:30:22 -08:00
test_package_keyword.py housekeep(fastapi): rewrite test_package_keyword with fixtures 2021-12-01 00:30:18 -08:00
test_package_license.py housekeep(fastapi): rewrite test_package_license with fixtures 2021-12-01 00:30:19 -08:00
test_package_notification.py housekeep(fastapi): rewrite test_package_notification with fixtures 2021-12-01 00:30:19 -08:00
test_package_relation.py housekeep(fastapi): rewrite test_package_relation with fixtures 2021-12-01 00:30:20 -08:00
test_package_request.py housekeep(fastapi): rewrite test_package_request with fixtures 2021-12-01 00:28:39 -08:00
test_package_source.py housekeep(fastapi): rewrite test_package_source with fixtures 2021-12-01 00:30:22 -08:00
test_package_vote.py housekeep(fastapi): rewrite test_package_vote with fixtures 2021-12-01 00:30:22 -08:00
test_packages_routes.py change(routers.packages): delete_package -> pkgbase_delete_instance 2022-01-01 12:29:50 -08:00
test_packages_util.py change(python): move request & pkgbase request routes 2022-01-02 01:44:36 -08:00
test_pkgmaint.py change(pkgmaint): converted to use aurweb.db ORM 2021-11-28 19:55:10 -08:00
test_popupdate.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_ratelimit.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_redis.py FastAPI: add redis integration 2021-08-17 19:58:28 -07:00
test_relation_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_rendercomment.py change(rendercomment): converted to use aurweb.db ORM 2021-11-28 19:55:11 -08:00
test_request_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_requests.py change(python): move request & pkgbase request routes 2022-01-02 01:44:36 -08:00
test_routes.py housekeep(fastapi): rewrite test_routes with fixtures 2021-12-01 00:30:21 -08:00
test_rpc.py fix(packages.search): fix default ordering & improve performance 2021-12-25 11:15:20 -08:00
test_rss.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_session.py housekeep(fastapi): rewrite test_sesion with fixtures 2021-12-01 00:30:20 -08:00
test_spawn.py feat: add test_spawn, an aurweb.spawn test 2021-11-20 15:47:25 -08:00
test_ssh_pub_key.py housekeep(fastapi): rewrite test_ssh_pub_key with fixtures 2021-12-01 00:30:18 -08:00
test_templates.py fix(templates): require valid User relationships for <a> usage 2021-12-31 18:31:48 -08:00
test_term.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_time.py add aurweb.time module 2021-06-05 20:11:17 -07:00
test_trusted_user_routes.py fix(FastAPI): remove login and redirect parameters from auth_required 2021-12-01 02:57:23 -05:00
test_tu_vote.py housekeep(fastapi): rewrite test_tu_vote with fixtures 2021-12-01 00:28:39 -08:00
test_tu_voteinfo.py housekeep(fastapi): rewrite test_tu_voteinfo with fixtures 2021-12-01 00:30:21 -08:00
test_tuvotereminder.py change(tuvotereminder): converted to use aurweb.db ORM 2021-11-28 19:55:09 -08:00
test_user.py change(models.user): can_edit_user should check account type id priority 2021-12-27 16:04:57 -08:00
test_usermaint.py change(usermaint): converted to use aurweb.db ORM 2021-11-28 19:55:09 -08:00
test_util.py change(rendercomment): converted to use aurweb.db ORM 2021-11-28 19:55:11 -08:00

aurweb Test Collection

To run all tests, you may run make check under test/ (alternative targets: make pytest, make sh).

For more control, you may use the prove or pytest command, which receives a directory or a list of files to run, and produces a report.

Each test script is standalone, so you may run them individually. Some tests may receive command-line options to help debugging. See for example sharness's documentation for shell test scripts: https://github.com/chriscool/sharness/blob/master/README.git

Dependencies

For all the test to run, the following Arch packages should be installed:

  • pyalpm
  • python-alembic
  • python-bleach
  • python-markdown
  • python-pygit2
  • python-sqlalchemy
  • python-srcinfo
  • python-coverage
  • python-pytest
  • python-pytest-cov
  • python-pytest-asyncio
  • postfix
  • openssh

Optional (faster testing)

  • libeatmydata

Test Configuration

To perform any tests, we need to supply aurweb with a valid configuration. For development (and testing) purposes, an example conf/config.dev can be slightly modified.

Start off by copying config.dev to a new configuration.

$ cp -v conf/config.dev conf/config

First, we must tell aurweb where the root of our project lives by replacing YOUR_AUR_ROOT with the path to the aurweb repository.

$ sed -i "s;YOUR_AUR_ROOT;/path/to/aurweb;g" conf/config

Now, one must decide a database backend to use; see Test Database for details on configuring the different supported backends.

Test Database

Users may choose to configure one of several backends, including: mysql and sqlite. By default, conf/config.dev is configured for a the mysql backend using a test database named aurweb_test.

Users can initialize an empty MySQL database by performing the following:

$ cat conf/config
[database]
backend = mysql
name = aurweb_test
user = aur
password = aur
socket = /var/run/mysqld/mysqld.sock
...

# mysql -u root -e "CREATE USER 'aur'@'localhost' IDENTIFIED BY 'aur'"
# mysql -u root -e "CREATE DATABASE aurweb_test"
# mysql -u root -e "GRANT ALL ON aurweb_test.* TO 'aur'@'localhost'"
# mysql -u root -e "FLUSH ALL PRIVILEGES"

$ export AUR_CONFIG=conf/config
$ python3 -m aurweb.initdb

Or more lightweight with sqlite:

$ cat $AUR_CONFIG
[database]
backend = sqlite
name = aurweb.sqlite3
...

$ export AUR_CONFIG=conf/config
$ python3 -m aurweb.initdb

After initializing a fresh test database, users can continue on to Running Tests.

Running tests

Recommended method of running tests: make check.

Makefile test targets: sh, pytest.

Run tests from the project root.

$ cd /path/to/aurweb

Ensure you have the proper AUR_CONFIG exported:

$ export AUR_CONFIG=conf/config

To run sharness shell test suites (requires Arch Linux):

$ make -C test sh

To run pytest Python test suites:

$ pytest

Note: For SQLite tests, users may want to use eatmydata to improve speed:

$ eatmydata -- make -C test sh

To produce coverage reports related to Python when running tests manually, use the following method:

$ coverage run --append /path/to/python/file.py

Note: Sharness test suites (shell) internally run coverage run.

After tests are run, one can produce coverage reports.

# Print out a CLI coverage report.
$ coverage report

# Produce an HTML-based coverage report.
$ coverage html

Writing Python tests (current)

Almost all of our pytest suites use the database in some way. There are a few particular testing utilities in aurweb that one should keep aware of to aid testing code:

  • db_test pytest fixture
    • Prepares test databases for the module and cleans out database tables for each test function requiring this fixture.
  • aurweb.testing.requests.Request
    • A fake stripped down version of fastapi.Request that can be passed to any functions in our codebase which use fastapi.Request parameters.

Example code:

import pytest

from aurweb import db
from aurweb.models.user import User
from aurweb.testing import setup_test_db
from aurweb.testing.requests import Request


@pytest.fixture(autouse=True)
def setup(db_test):
    return

@pytest.fixture
def user():
    with db.begin():
        user = db.create(User, Passwd="testPassword", ...)
    yield user

def test_user_login(user: User):
    assert isinstance(user, User) is True

    fake_request = Request()
    sid = user.login(fake_request, "testPassword")
    assert sid is not None

Writing Sharness tests (legacy)

Shell test scripts must follow the Test Anything Protocol specification: http://testanything.org/tap-specification.html

Python tests must be compatible with pytest and included in pytest test/ execution after setting up a configuration.

Tests must support being run from any directory. They may use $0 to determine their location. Python scripts should expect aurweb to be installed and importable without toying with os.path or PYTHONPATH.

Tests written in shell should use sharness. In general, new tests should be consistent with existing tests unless they have a good reason not to.

Debugging Sharness tests

By default, make -C test is quiet and does not print out verbose information about tests being run. If a test is failing, one can look into verbose details of sharness tests by executing them with the --verbose flag. Example: ./t1100_git_auth.t --verbose. This is particularly useful when tests happen to fail in a remote continuous integration environment, where the reader does not have complete access to the runner.