diff --git a/get_wcs.sh b/get_wcs.sh new file mode 100755 index 0000000..af0a596 --- /dev/null +++ b/get_wcs.sh @@ -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) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..d9941ac --- /dev/null +++ b/tests/settings.py @@ -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') diff --git a/tests/test_tags.py b/tests/test_tags.py new file mode 100644 index 0000000..60c7d8b --- /dev/null +++ b/tests/test_tags.py @@ -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' diff --git a/tests/utilities.py b/tests/utilities.py new file mode 100644 index 0000000..c783565 --- /dev/null +++ b/tests/utilities.py @@ -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 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..40e6b08 --- /dev/null +++ b/tox.ini @@ -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