add tests

This commit is contained in:
Frédéric Péters 2022-04-02 14:16:09 +02:00
parent a8927e6ca5
commit 3ec7a34774
6 changed files with 149 additions and 0 deletions

5
get_wcs.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh -xue
test -d wcs || git clone https://git.entrouvert.org/wcs.git
(cd wcs && git pull)
(cd wcs && python3 setup.py develop --no-deps)

0
tests/__init__.py Normal file
View File

5
tests/settings.py Normal file
View File

@ -0,0 +1,5 @@
TIME_ZONE = 'Europe/Paris'
LANGUAGE_CODE = 'en-us'
ALLOWED_HOSTS = ['*']
TEMPLATES[0]['OPTIONS']['builtins'].append('imio_teleservices_templatetags.templatetags.imio_teleservices')

35
tests/test_tags.py Normal file
View File

@ -0,0 +1,35 @@
import os
import pytest
from wcs.qommon.http_request import HTTPRequest
from wcs.qommon.template import Template
from .utilities import clean_temporary_pub, create_temporary_pub
@pytest.fixture
def pub():
pub = create_temporary_pub()
pub.substitutions.feed(pub)
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
pub.set_app_dir(req)
return pub
def teardown_module(module):
clean_temporary_pub()
def test_nrn(pub):
tmpl = Template('{% if value|is_valid_belgian_nrn %}yes{% else %}no{% endif %}')
assert tmpl.render({'value': '85073003328'}) == 'yes'
assert tmpl.render({'value': '85073003329'}) == 'no'
def test_strong_authentication(pub):
tmpl = Template('{% if session_user|is_strong_authentication %}yes{% else %}no{% endif %}')
user = pub.user_class(name='foo bar')
assert tmpl.render({'session_user': user}) == 'no'
user.verified_fields = ['_niss']
assert tmpl.render({'session_user': user}) == 'yes'

61
tests/utilities.py Normal file
View File

@ -0,0 +1,61 @@
import os
import tempfile
from django.conf import settings
from django.utils.six.moves import configparser as ConfigParser
from quixote import cleanup, get_publisher
from webtest import TestApp
import wcs
import wcs.middleware
import wcs.wsgi
from wcs import compat, publisher
from wcs.qommon.http_request import HTTPRequest
from wcs.qommon.publisher import set_publisher_class
wcs.middleware.AfterJobsMiddleware.ASYNC = False
def create_temporary_pub():
config = ConfigParser.ConfigParser()
APP_DIR = tempfile.mkdtemp()
compat.CompatWcsPublisher.APP_DIR = APP_DIR
compat.CompatWcsPublisher.DATA_DIR = os.path.abspath(
os.path.join(os.path.dirname(wcs.__file__), '..', 'data')
)
compat.CompatWcsPublisher.cronjobs = None
config.add_section('extra')
config.set('extra', 'auquotidien', os.path.join(os.path.dirname(__file__), '..', 'auquotidien'))
compat.CompatWcsPublisher._initialized = False
compat.CompatWcsPublisher.configure(config)
compat.CompatWcsPublisher.init_publisher_class()
pub = compat.CompatWcsPublisher.create_publisher()
# allow saving the user
pub.app_dir = os.path.join(APP_DIR, 'example.net')
os.mkdir(pub.app_dir)
return pub
def clean_temporary_pub():
if get_publisher():
get_publisher().cleanup()
def get_app(pub, https=False):
extra_environ = {'HTTP_HOST': 'example.net', 'REMOTE_ADDR': '127.0.0.1'}
if https:
settings.SECURE_PROXY_SSL_HEADER = ('HTTPS', 'on')
extra_environ['HTTPS'] = 'on'
else:
extra_environ['HTTPS'] = 'off'
return TestApp(wcs.wsgi.application, extra_environ=extra_environ)
def login(app, username='admin', password='admin'):
login_page = app.get('/login/')
login_form = login_page.forms['login-form']
login_form['username'] = username
login_form['password'] = password
resp = login_form.submit()
assert resp.status_int == 302
return app

43
tox.ini Normal file
View File

@ -0,0 +1,43 @@
[tox]
envlist = py3-black-junit-coverage
toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/imio-teleservices-templatetags/{env:BRANCH_NAME:}
[testenv]
sitepackages = false
usedevelop =
coverage: True
nocoverage: False
setenv =
DJANGO_SETTINGS_MODULE=wcs.settings
SETUPTOOLS_USE_DISTUTILS=stdlib
WCS_SETTINGS_FILE=tests/settings.py
coverage: COVERAGE=--cov-report xml --cov-report html --cov=imio_teleservices_templatetags/
junit: JUNIT=--junitxml=junit-{envname}.xml
deps =
pytest
pytest-cov
pytest-django
WebTest
requests
vobject
qrcode
Pillow
workalendar
python-magic
docutils
langdetect
django-ratelimit<3
git+https://git.entrouvert.org/debian/django-ckeditor.git
https://git.entrouvert.org/godo.js.git/snapshot/godo.js-0.6.tar.gz
pyproj
pyzbar
bleach
dnspython
gadjo
Quixote>=3.0,<3.2
django>=2.2,<2.3
pre-commit
commands =
./get_wcs.sh
py.test {posargs: {env:JUNIT:} {env:COVERAGE:} tests/}
black: pre-commit run black --all-files --show-diff-on-failure