misc: add unit testing base layout (#62849)

This commit is contained in:
Paul Marillonnet 2022-03-18 11:18:42 +01:00
parent 2e5dd01147
commit 64a9bf70d8
5 changed files with 225 additions and 0 deletions

22
getlasso3.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/sh
# Get venv site-packages path
DSTDIR=`python3 -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())'`
# Get not venv site-packages path
# Remove first path (assuming that is the venv path)
NONPATH=`echo $PATH | sed 's/^[^:]*://'`
SRCDIR=`PATH=$NONPATH python3 -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())'`
# Clean up
rm -f $DSTDIR/lasso.*
rm -f $DSTDIR/_lasso.*
# Link
ln -sv /usr/lib/python3/dist-packages/lasso.py $DSTDIR/
for SOFILE in /usr/lib/python3/dist-packages/_lasso.cpython-*.so
do
ln -sv $SOFILE $DSTDIR/
done
exit 0

92
tests/conftest.py Normal file
View File

@ -0,0 +1,92 @@
import copy
import time
from collections import namedtuple
import django_webtest
import pytest
try:
import pathlib
except ImportError:
import pathlib2 as pathlib
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django_rbac.utils import get_ou_model
User = get_user_model()
OU = get_ou_model()
TEST_DIR = pathlib.Path(__file__).parent
@pytest.fixture
def app(request, db, settings, tmpdir):
wtm = django_webtest.WebTestMixin()
wtm._patch_settings()
request.addfinalizer(wtm._unpatch_settings)
settings.MEDIA_DIR = str(tmpdir.mkdir('media'))
return django_webtest.DjangoTestApp(extra_environ={'HTTP_HOST': 'localhost'})
@pytest.fixture
def partner_ou(db):
return OU.objects.create(name='partner', slug='ou')
class AllHook:
def __init__(self):
self.calls = {}
from authentic2 import hooks
hooks.get_hooks.cache.clear()
def __call__(self, hook_name, *args, **kwargs):
calls = self.calls.setdefault(hook_name, [])
calls.append({'args': args, 'kwargs': kwargs})
def __getattr__(self, name):
return self.calls.get(name, [])
def clear(self):
self.calls = {}
@pytest.fixture
def user(db):
user = User.objects.create(
username='john.doe',
email='john.doe@example.net',
first_name='John',
last_name='Doe',
email_verified=True,
)
user.set_password('john.doe')
return user
@pytest.fixture
def hooks(settings):
if hasattr(settings, 'A2_HOOKS'):
hooks = settings.A2_HOOKS
else:
hooks = settings.A2_HOOKS = {}
hook = hooks['__all__'] = AllHook()
yield hook
hook.clear()
del settings.A2_HOOKS['__all__']
@pytest.fixture
def admin(db):
user = User(username='admin', email='admin@example.net', is_superuser=True, is_staff=True)
user.ou = OU.objects.get(slug='territoire')
user.set_password('admin')
user.save()
return user
@pytest.fixture(autouse=True)
def clean_caches():
from authentic2.apps.journal.models import event_type_cache
event_type_cache.cache.clear()

22
tests/settings.py Normal file
View File

@ -0,0 +1,22 @@
import os
ALLOWED_HOSTS = ['localhost']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'authentic2-gnm',
}
}
if 'postgres' in DATABASES['default']['ENGINE']:
for key in ('PGPORT', 'PGHOST', 'PGUSER', 'PGPASSWORD'):
if key in os.environ:
DATABASES['default'][key[2:]] = os.environ[key]
LANGUAGE_CODE = 'en'
A2_FC_CLIENT_ID = ''
A2_FC_CLIENT_SECRET = ''
# test hook handlers
A2_HOOKS_PROPAGATE_EXCEPTIONS = True

7
tests/test_commands.py Normal file
View File

@ -0,0 +1,7 @@
import pytest
def test_dummy(db, app):
assert 1
def test_another_dummy(db, app):
assert 1

82
tox.ini Normal file
View File

@ -0,0 +1,82 @@
# Tox (http://tox.testrun.org/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.
[tox]
toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/authentic2-cut/{env:BRANCH_NAME:}
envlist =
py3-dj22
[tox:jenkins]
envlist =
py3-dj22
code-style
pylint
[testenv]
setenv =
AUTHENTIC2_SETTINGS_FILE=tests/settings.py
DJANGO_SETTINGS_MODULE=authentic2.settings
JUNIT={tty::-o junit_suite_name={envname} --junit-xml=junit-{envname}.xml}
COVERAGE={tty::--junitxml=test_{envname}_results.xml --cov-report xml --cov-report html --cov=src/ --cov-config .coveragerc}
passenv=
BRANCH_NAME
# support for pg_virtualenv
PGPORT
PGHOST
PGUSER
PGPASSWORD
usedevelop = true
deps =
!local: https://git.entrouvert.org/authentic.git/snapshot/authentic-main.tar.gz
local: ../authentic2
# dependency constraints for authentic
py3: file-magic
djangorestframework>=3.12,<3.13
django-tables2==2.1.1
psycopg2-binary<2.9
oldldap: python-ldap<3
ldaptools
# pytest requirements
pytest
pytest-cov
pytest-django
pytest-freezegun
pytest-random
django-webtest
pyquery
commands =
py3: ./getlasso3.sh
py.test {env:COVERAGE:} {env:JUNIT:} {tty:--sw:} {posargs:tests/}
[testenv:code-style]
skip_install = true
deps =
pre-commit
commands =
pre-commit run --all-files --show-diff-on-failure
[testenv:pylint]
usedevelop = true
basepython = python3
deps =
https://git.entrouvert.org/authentic.git/snapshot/authentic-main.tar.gz
Django<2.3
pylint<1.8
pylint-django<0.8.1
commands =
/bin/bash -c "./pylint.sh src/*/"
[pytest]
junit_family=xunit2
filterwarnings =
once
ignore:::django\..*
ignore:::authentic2\..*
ignore:::rest_framework\..*
ignore:::gettext\..*