mirror of
https://github.com/YunoHost-Apps/django_example_ynh.git
synced 2024-09-03 18:26:21 +02:00
get pytest running with local test copy
This commit is contained in:
parent
b5f93958ff
commit
0c4bf1e8de
36 changed files with 529 additions and 386 deletions
2
.github/workflows/pytest.yml
vendored
2
.github/workflows/pytest.yml
vendored
|
@ -30,7 +30,7 @@ jobs:
|
||||||
|
|
||||||
- name: 'Run tests with Python v${{ matrix.python-version }}'
|
- name: 'Run tests with Python v${{ matrix.python-version }}'
|
||||||
run: |
|
run: |
|
||||||
poetry run pytest
|
make pytest
|
||||||
|
|
||||||
- name: 'Upload coverage report'
|
- name: 'Upload coverage report'
|
||||||
run: bash <(curl -s https://codecov.io/bash)
|
run: bash <(curl -s https://codecov.io/bash)
|
||||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -3,8 +3,10 @@
|
||||||
!.editorconfig
|
!.editorconfig
|
||||||
!.flake8
|
!.flake8
|
||||||
!.gitignore
|
!.gitignore
|
||||||
|
coverage.xml
|
||||||
__pycache__
|
__pycache__
|
||||||
secret.txt
|
secret.txt
|
||||||
|
/htmlcov/
|
||||||
/local_test/
|
/local_test/
|
||||||
/dist/
|
/dist/
|
||||||
/poetry.lock
|
/poetry.lock
|
||||||
|
|
9
Makefile
9
Makefile
|
@ -41,15 +41,14 @@ tox-listenvs: check-poetry ## List all tox test environments
|
||||||
tox: check-poetry ## Run pytest via tox with all environments
|
tox: check-poetry ## Run pytest via tox with all environments
|
||||||
poetry run tox
|
poetry run tox
|
||||||
|
|
||||||
pytest: check-poetry ## Run pytest
|
pytest: install ## Run pytest
|
||||||
poetry run pytest
|
poetry run python3 ./run_pytest.py
|
||||||
|
|
||||||
publish: ## Release new version to PyPi
|
publish: ## Release new version to PyPi
|
||||||
poetry run publish
|
poetry run publish
|
||||||
|
|
||||||
local-test: check-poetry ## Run local_test.py to run the project locally
|
local-test: install ## Run local_test.py to run the project locally
|
||||||
poetry install
|
poetry run python3 ./local_test.py
|
||||||
poetry run ./local_test.py
|
|
||||||
|
|
||||||
local-diff-settings: ## Run "manage.py diffsettings" with local test
|
local-diff-settings: ## Run "manage.py diffsettings" with local test
|
||||||
poetry run python3 local_test/opt_yunohost/manage.py diffsettings
|
poetry run python3 local_test/opt_yunohost/manage.py diffsettings
|
||||||
|
|
|
@ -40,6 +40,8 @@ PATH_URL = PATH_URL.strip('/')
|
||||||
|
|
||||||
ROOT_URLCONF = 'django_ynh_demo_urls'
|
ROOT_URLCONF = 'django_ynh_demo_urls'
|
||||||
|
|
||||||
|
YNH_SETUP_USER = 'setup_user.setup_demo_user'
|
||||||
|
|
||||||
SECRET_KEY = __get_or_create_secret(FINAL_HOME_PATH / 'secret.txt') # /opt/yunohost/$app/secret.txt
|
SECRET_KEY = __get_or_create_secret(FINAL_HOME_PATH / 'secret.txt') # /opt/yunohost/$app/secret.txt
|
||||||
|
|
||||||
ADMINS = (('__ADMIN__', '__ADMINMAIL__'),)
|
ADMINS = (('__ADMIN__', '__ADMINMAIL__'),)
|
||||||
|
|
|
@ -10,10 +10,8 @@ from django_ynh.views import request_media_debug_view
|
||||||
# Prefix all urls with "PATH_URL":
|
# Prefix all urls with "PATH_URL":
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path(f'{settings.PATH_URL}/', admin.site.urls),
|
path(f'{settings.PATH_URL}/', admin.site.urls),
|
||||||
|
path(f'{settings.PATH_URL}/debug/', request_media_debug_view),
|
||||||
]
|
]
|
||||||
|
|
||||||
if settings.SERVE_FILES:
|
if settings.SERVE_FILES:
|
||||||
urlpatterns += static.static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
urlpatterns += static.static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
||||||
|
|
||||||
if settings.DEBUG:
|
|
||||||
urlpatterns.append(path(f'{settings.PATH_URL}/debug/', request_media_debug_view))
|
|
||||||
|
|
8
conf/setup_user.py
Normal file
8
conf/setup_user.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
def setup_demo_user(user):
|
||||||
|
"""
|
||||||
|
The django_ynh DEMO use the Django admin. So we need a "staff" user ;)
|
||||||
|
"""
|
||||||
|
user.is_staff = True
|
||||||
|
user.save()
|
||||||
|
return user
|
169
django_ynh/local_test.py
Executable file
169
django_ynh/local_test.py
Executable file
|
@ -0,0 +1,169 @@
|
||||||
|
"""
|
||||||
|
Create a YunoHost package local test
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import shlex
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from django_ynh.path_utils import assert_is_dir, assert_is_file
|
||||||
|
from django_ynh.test_utils import generate_basic_auth
|
||||||
|
|
||||||
|
|
||||||
|
def verbose_check_call(command, verbose=True, **kwargs):
|
||||||
|
""" 'verbose' version of subprocess.check_call() """
|
||||||
|
if verbose:
|
||||||
|
print('_' * 100)
|
||||||
|
msg = f'Call: {command!r}'
|
||||||
|
verbose_kwargs = ', '.join(f'{k}={v!r}' for k, v in sorted(kwargs.items()))
|
||||||
|
if verbose_kwargs:
|
||||||
|
msg += f' (kwargs: {verbose_kwargs})'
|
||||||
|
print(f'{msg}\n', flush=True)
|
||||||
|
|
||||||
|
env = os.environ.copy()
|
||||||
|
env['PYTHONUNBUFFERED'] = '1'
|
||||||
|
|
||||||
|
popenargs = shlex.split(command)
|
||||||
|
subprocess.check_call(popenargs, universal_newlines=True, env=env, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def call_manage_py(final_home_path, args):
|
||||||
|
verbose_check_call(
|
||||||
|
command=f'{sys.executable} manage.py {args}',
|
||||||
|
cwd=final_home_path,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def copy_patch(src_file, replaces, final_home_path):
|
||||||
|
dst_file = final_home_path / src_file.name
|
||||||
|
print(f'{src_file} -> {dst_file}')
|
||||||
|
|
||||||
|
with src_file.open('r') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
if replaces:
|
||||||
|
for old, new in replaces.items():
|
||||||
|
if old in content:
|
||||||
|
print(f' * Replace "{old}" -> "{new}"')
|
||||||
|
content = content.replace(old, new)
|
||||||
|
|
||||||
|
with dst_file.open('w') as f:
|
||||||
|
f.write(content)
|
||||||
|
|
||||||
|
|
||||||
|
def create_local_test(django_settings_path, destination, runserver=False):
|
||||||
|
assert_is_file(django_settings_path)
|
||||||
|
|
||||||
|
django_settings_name = django_settings_path.stem
|
||||||
|
|
||||||
|
conf_path = django_settings_path.parent
|
||||||
|
base_path = conf_path.parent
|
||||||
|
|
||||||
|
assert isinstance(destination, Path)
|
||||||
|
destination = destination.resolve()
|
||||||
|
if not destination.is_dir():
|
||||||
|
destination.mkdir(parents=False)
|
||||||
|
|
||||||
|
assert_is_dir(destination)
|
||||||
|
|
||||||
|
FINAL_HOME_PATH = destination / 'opt_yunohost'
|
||||||
|
FINAL_WWW_PATH = destination / 'var_www'
|
||||||
|
LOG_FILE = destination / 'var_log_django_ynh.log'
|
||||||
|
|
||||||
|
REPLACES = {
|
||||||
|
'__FINAL_HOME_PATH__': str(FINAL_HOME_PATH),
|
||||||
|
'__FINAL_WWW_PATH__': str(FINAL_WWW_PATH),
|
||||||
|
'__LOG_FILE__': str(destination / 'var_log_django_ynh.log'),
|
||||||
|
'__PATH_URL__': 'app_path',
|
||||||
|
'__DOMAIN__': '127.0.0.1',
|
||||||
|
'django.db.backends.postgresql': 'django.db.backends.sqlite3',
|
||||||
|
"'NAME': '__APP__',": f"'NAME': '{destination / 'test_db.sqlite'}',",
|
||||||
|
'django_redis.cache.RedisCache': 'django.core.cache.backends.dummy.DummyCache',
|
||||||
|
# Just use the default logging setup from django_ynh project:
|
||||||
|
'LOGGING = {': 'HACKED_DEACTIVATED_LOGGING = {',
|
||||||
|
}
|
||||||
|
|
||||||
|
for p in (FINAL_HOME_PATH, FINAL_WWW_PATH):
|
||||||
|
if p.is_dir():
|
||||||
|
print(f'Already exists: "{p}", ok.')
|
||||||
|
else:
|
||||||
|
p.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
LOG_FILE.touch(exist_ok=True)
|
||||||
|
|
||||||
|
for src_file in conf_path.glob('*.py'):
|
||||||
|
copy_patch(src_file=src_file, replaces=REPLACES, final_home_path=FINAL_HOME_PATH)
|
||||||
|
|
||||||
|
with Path(FINAL_HOME_PATH / 'local_settings.py').open('w') as f:
|
||||||
|
f.write('# Only for local test run\n')
|
||||||
|
f.write('SERVE_FILES = True # used in src/inventory_project/urls.py\n')
|
||||||
|
f.write('AUTH_PASSWORD_VALIDATORS = [] # accept all passwords\n')
|
||||||
|
|
||||||
|
# call "local_test/manage.py" via subprocess:
|
||||||
|
call_manage_py(FINAL_HOME_PATH, 'check --deploy')
|
||||||
|
if runserver:
|
||||||
|
call_manage_py(FINAL_HOME_PATH, 'migrate --no-input')
|
||||||
|
call_manage_py(FINAL_HOME_PATH, 'collectstatic --no-input')
|
||||||
|
|
||||||
|
verbose_check_call(
|
||||||
|
command=(
|
||||||
|
f'{sys.executable} -m django_ynh.create_superuser'
|
||||||
|
f' --ds="{django_settings_name}" --username="test" --password="test123"'
|
||||||
|
),
|
||||||
|
cwd=FINAL_HOME_PATH,
|
||||||
|
)
|
||||||
|
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE'] = django_settings_name
|
||||||
|
|
||||||
|
# All environment variables are passed to Django's "runnserver" ;)
|
||||||
|
# "Simulate" SSOwat authentication, by set "http headers"
|
||||||
|
# Still missing is the 'SSOwAuthUser' cookie,
|
||||||
|
# but this is ignored, if settings.DEBUG=True ;)
|
||||||
|
os.environ['HTTP_AUTH_USER'] = 'test'
|
||||||
|
os.environ['HTTP_REMOTE_USER'] = 'test'
|
||||||
|
|
||||||
|
os.environ['HTTP_AUTHORIZATION'] = generate_basic_auth(username='test', password='test123')
|
||||||
|
|
||||||
|
try:
|
||||||
|
call_manage_py(FINAL_HOME_PATH, 'runserver --nostatic')
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print('\nBye ;)')
|
||||||
|
|
||||||
|
return FINAL_HOME_PATH
|
||||||
|
|
||||||
|
|
||||||
|
def cli():
|
||||||
|
parser = argparse.ArgumentParser(description='Generate a YunoHost package local test')
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'--django_settings_path',
|
||||||
|
action='store',
|
||||||
|
metavar='path',
|
||||||
|
help='Path to YunoHost package settings.py file (in "conf" directory)',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--destination',
|
||||||
|
action='store',
|
||||||
|
metavar='path',
|
||||||
|
help='Destination directory for the local test files',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--runserver',
|
||||||
|
action='store',
|
||||||
|
type=bool,
|
||||||
|
default=False,
|
||||||
|
help='Start Django "runserver" after local test file creation?',
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
create_local_test(
|
||||||
|
django_settings_path=Path(args.django_settings_path),
|
||||||
|
destination=Path(args.destination),
|
||||||
|
runserver=args.runserver,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
cli()
|
25
django_ynh/path_utils.py
Normal file
25
django_ynh/path_utils.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def assert_is_dir(dir_path):
|
||||||
|
assert isinstance(dir_path, Path)
|
||||||
|
assert dir_path.is_dir, f'Directory does not exists: {dir_path}'
|
||||||
|
|
||||||
|
|
||||||
|
def assert_is_file(file_path):
|
||||||
|
assert isinstance(file_path, Path)
|
||||||
|
assert file_path.is_file, f'File not found: {file_path}'
|
||||||
|
|
||||||
|
|
||||||
|
def is_relative_to(p, other):
|
||||||
|
"""
|
||||||
|
Path.is_relative_to() is new in Python 3.9
|
||||||
|
"""
|
||||||
|
p = Path(p)
|
||||||
|
other = Path(other)
|
||||||
|
try:
|
||||||
|
p.relative_to(other)
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
34
django_ynh/pytest_helper.py
Normal file
34
django_ynh/pytest_helper.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from django_ynh.local_test import create_local_test
|
||||||
|
from django_ynh.path_utils import assert_is_dir, assert_is_file
|
||||||
|
|
||||||
|
|
||||||
|
def run_pytest(django_settings_path, destination):
|
||||||
|
assert_is_file(django_settings_path)
|
||||||
|
|
||||||
|
conf_path = django_settings_path.parent
|
||||||
|
base_path = conf_path.parent
|
||||||
|
test_path = Path(base_path / 'tests')
|
||||||
|
|
||||||
|
assert_is_dir(test_path)
|
||||||
|
|
||||||
|
final_home_path = create_local_test(
|
||||||
|
django_settings_path=django_settings_path,
|
||||||
|
destination=destination,
|
||||||
|
runserver=False,
|
||||||
|
)
|
||||||
|
django_settings_name = django_settings_path.stem
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE'] = django_settings_name
|
||||||
|
print(f'DJANGO_SETTINGS_MODULE={django_settings_name}')
|
||||||
|
|
||||||
|
sys.path.insert(0, str(final_home_path))
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
# collect only project tests:
|
||||||
|
sys.argv = [__file__, str(test_path)]
|
||||||
|
|
||||||
|
raise SystemExit(pytest.console_main())
|
45
django_ynh/pytest_plugin.py
Normal file
45
django_ynh/pytest_plugin.py
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from django_ynh.path_utils import assert_is_dir, assert_is_file
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_addoption(parser):
|
||||||
|
group = parser.getgroup("django_ynh")
|
||||||
|
group.addoption(
|
||||||
|
"--django_settings_path",
|
||||||
|
action="store",
|
||||||
|
metavar="path",
|
||||||
|
help='Path to YunoHost package settings.py file (in "conf" directory)',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(tryfirst=True)
|
||||||
|
def pytest_load_initial_conftests(early_config, parser, args):
|
||||||
|
base_path = Path(__file__).parent.parent
|
||||||
|
|
||||||
|
local_test = Path(base_path / 'local_test')
|
||||||
|
assert_is_dir(local_test)
|
||||||
|
|
||||||
|
local_test_opt_yunohost = Path(local_test / 'opt_yunohost')
|
||||||
|
assert_is_dir(local_test_opt_yunohost)
|
||||||
|
|
||||||
|
assert_is_file(local_test_opt_yunohost / 'django_ynh_demo_settings.py')
|
||||||
|
|
||||||
|
sys.path.insert(0, str(local_test_opt_yunohost))
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_ynh_demo_settings'
|
||||||
|
|
||||||
|
# print(1111111111111, early_config) # _pytest.config.Config
|
||||||
|
# print(22222, parser) # _pytest.config.argparsing.Parser
|
||||||
|
# print(3333, args) # ['--import-mode=importlib', '--reuse-db', '--nomigrations', '--cov=.', '--cov-report', 'term-missing', '--cov-report', 'html', '--cov-report', 'xml', '--no-cov-on-fail', '--showlocals', '--doctest-modules', '--failed-first', '--last-failed-no-failures', 'all', '--new-first', '-p', 'django_ynh.pytest_plugin', '-x', '--trace-config']
|
||||||
|
#
|
||||||
|
# options = parser.parse_known_args(args)
|
||||||
|
# if options.version or options.help:
|
||||||
|
# return
|
||||||
|
#
|
||||||
|
# conf_path = options.conf_path
|
||||||
|
#
|
||||||
|
# raise RuntimeError(f'{conf_path}')
|
|
@ -27,8 +27,7 @@ import logging
|
||||||
|
|
||||||
from django.contrib.auth.backends import RemoteUserBackend
|
from django.contrib.auth.backends import RemoteUserBackend
|
||||||
|
|
||||||
from django_ynh.sso_auth.signals import setup_user
|
from django_ynh.sso_auth.user_profile import call_setup_user, update_user_profile
|
||||||
from django_ynh.sso_auth.user_profile import update_user_profile
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -51,12 +50,12 @@ class SSOwatUserBackend(RemoteUserBackend):
|
||||||
"""
|
"""
|
||||||
logger.warning('Configure user %s', user)
|
logger.warning('Configure user %s', user)
|
||||||
|
|
||||||
user = update_user_profile(request)
|
user = update_user_profile(request, user)
|
||||||
|
user = call_setup_user(user=user)
|
||||||
setup_user.send(sender=self.__class__, user=user)
|
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
def user_can_authenticate(self, user):
|
def user_can_authenticate(self, user):
|
||||||
logger.warning('Remote user login: %s', user)
|
logger.warning('Remote user login: %s', user)
|
||||||
|
assert not user.is_anonymous
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -5,8 +5,7 @@ from axes.exceptions import AxesBackendPermissionDenied
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.middleware import RemoteUserMiddleware
|
from django.contrib.auth.middleware import RemoteUserMiddleware
|
||||||
|
|
||||||
from django_ynh.sso_auth.signals import setup_user
|
from django_ynh.sso_auth.user_profile import call_setup_user, update_user_profile
|
||||||
from django_ynh.sso_auth.user_profile import update_user_profile
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -28,8 +27,10 @@ class SSOwatRemoteUserMiddleware(RemoteUserMiddleware):
|
||||||
|
|
||||||
super().process_request(request) # login remote user
|
super().process_request(request) # login remote user
|
||||||
|
|
||||||
if not request.user.is_authenticated:
|
user = request.user
|
||||||
# Not logged in -> nothing to verify here
|
|
||||||
|
if not user.is_authenticated:
|
||||||
|
logger.debug('Not logged in -> nothing to verify here')
|
||||||
return
|
return
|
||||||
|
|
||||||
# Check SSOwat cookie informations:
|
# Check SSOwat cookie informations:
|
||||||
|
@ -47,7 +48,7 @@ class SSOwatRemoteUserMiddleware(RemoteUserMiddleware):
|
||||||
raise AxesBackendPermissionDenied('Cookie missing')
|
raise AxesBackendPermissionDenied('Cookie missing')
|
||||||
else:
|
else:
|
||||||
logger.info('SSOwat username from cookies: %r', username)
|
logger.info('SSOwat username from cookies: %r', username)
|
||||||
if username != request.user.username:
|
if username != user.username:
|
||||||
raise AxesBackendPermissionDenied('Wrong username')
|
raise AxesBackendPermissionDenied('Wrong username')
|
||||||
|
|
||||||
# Compare with HTTP_AUTH_USER
|
# Compare with HTTP_AUTH_USER
|
||||||
|
@ -57,7 +58,7 @@ class SSOwatRemoteUserMiddleware(RemoteUserMiddleware):
|
||||||
logger.error('HTTP_AUTH_USER missing!')
|
logger.error('HTTP_AUTH_USER missing!')
|
||||||
raise AxesBackendPermissionDenied('No HTTP_AUTH_USER')
|
raise AxesBackendPermissionDenied('No HTTP_AUTH_USER')
|
||||||
|
|
||||||
if username != request.user.username:
|
if username != user.username:
|
||||||
raise AxesBackendPermissionDenied('Wrong HTTP_AUTH_USER username')
|
raise AxesBackendPermissionDenied('Wrong HTTP_AUTH_USER username')
|
||||||
|
|
||||||
# Also check 'HTTP_AUTHORIZATION', but only the username ;)
|
# Also check 'HTTP_AUTHORIZATION', but only the username ;)
|
||||||
|
@ -74,11 +75,12 @@ class SSOwatRemoteUserMiddleware(RemoteUserMiddleware):
|
||||||
|
|
||||||
creds = str(base64.b64decode(creds), encoding='utf-8')
|
creds = str(base64.b64decode(creds), encoding='utf-8')
|
||||||
username = creds.split(':', 1)[0]
|
username = creds.split(':', 1)[0]
|
||||||
if username != request.user.username:
|
if username != user.username:
|
||||||
raise AxesBackendPermissionDenied('Wrong HTTP_AUTHORIZATION username')
|
raise AxesBackendPermissionDenied('Wrong HTTP_AUTHORIZATION username')
|
||||||
|
|
||||||
if not was_authenticated:
|
if not was_authenticated:
|
||||||
# First request, after login -> update user informations
|
# First request, after login -> update user informations
|
||||||
logger.info('Remote used was logged in')
|
logger.info('Remote user "%s" was logged in', user)
|
||||||
user = update_user_profile(request)
|
user = update_user_profile(request, user)
|
||||||
setup_user.send(sender=self.__class__, user=user)
|
|
||||||
|
user = call_setup_user(user=user)
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
"""
|
|
||||||
"setup_user" called via:
|
|
||||||
|
|
||||||
* SSOwatUserBackend after a new user was created
|
|
||||||
* SSOwatRemoteUserMiddleware on login request
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
import django.dispatch
|
|
||||||
|
|
||||||
|
|
||||||
setup_user = django.dispatch.Signal(providing_args=['user'])
|
|
|
@ -1,12 +1,48 @@
|
||||||
import logging
|
import logging
|
||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.utils.module_loading import import_string
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def update_user_profile(request):
|
UserModel = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def get_setup_user_func():
|
||||||
|
setup_user_func = import_string(settings.YNH_SETUP_USER)
|
||||||
|
assert callable(setup_user_func)
|
||||||
|
return setup_user_func
|
||||||
|
|
||||||
|
|
||||||
|
def call_setup_user(user):
|
||||||
|
"""
|
||||||
|
Hook for the YunoHost package application to setup a Django user.
|
||||||
|
Call function defined in settings.YNH_SETUP_USER
|
||||||
|
|
||||||
|
called via:
|
||||||
|
* SSOwatUserBackend after a new user was created
|
||||||
|
* SSOwatRemoteUserMiddleware on login request
|
||||||
|
"""
|
||||||
|
old_pk = user.pk
|
||||||
|
|
||||||
|
setup_user_func = get_setup_user_func()
|
||||||
|
logger.debug('Call "%s" for user "%s"', settings.YNH_SETUP_USER, user)
|
||||||
|
|
||||||
|
user = setup_user_func(user=user)
|
||||||
|
|
||||||
|
assert isinstance(user, UserModel)
|
||||||
|
assert user.pk == old_pk
|
||||||
|
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
def update_user_profile(request, user):
|
||||||
"""
|
"""
|
||||||
Update existing user information:
|
Update existing user information:
|
||||||
* Email
|
* Email
|
||||||
|
@ -16,12 +52,9 @@ def update_user_profile(request):
|
||||||
* SSOwatUserBackend after a new user was created
|
* SSOwatUserBackend after a new user was created
|
||||||
* SSOwatRemoteUserMiddleware on login request
|
* SSOwatRemoteUserMiddleware on login request
|
||||||
"""
|
"""
|
||||||
user = request.user
|
|
||||||
assert user.is_authenticated
|
|
||||||
|
|
||||||
update_fields = []
|
update_fields = []
|
||||||
|
|
||||||
if not user.has_usable_password():
|
if user.is_authenticated and not user.has_usable_password():
|
||||||
# Empty password is not valid, so we can't save the model, because of full_clean() call
|
# Empty password is not valid, so we can't save the model, because of full_clean() call
|
||||||
logger.info('Set unusable password for user: %s', user)
|
logger.info('Set unusable password for user: %s', user)
|
||||||
user.set_unusable_password()
|
user.set_unusable_password()
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
from django.test.testcases import TestCase
|
|
||||||
|
|
||||||
|
|
||||||
class YnhTestCase(TestCase):
|
|
||||||
"""
|
|
||||||
|
|
||||||
"""
|
|
8
django_ynh/test_utils.py
Normal file
8
django_ynh/test_utils.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import base64
|
||||||
|
|
||||||
|
|
||||||
|
def generate_basic_auth(username, password):
|
||||||
|
basic_auth = f'{username}:{password}'
|
||||||
|
basic_auth_creds = bytes(basic_auth, encoding='utf-8')
|
||||||
|
creds = str(base64.b64encode(basic_auth_creds), encoding='utf-8')
|
||||||
|
return f'basic {creds}'
|
|
@ -1,6 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
import pprint
|
import pprint
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.http.response import HttpResponse
|
from django.http.response import HttpResponse
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
|
@ -12,6 +13,8 @@ logger = logging.getLogger(__name__)
|
||||||
def request_media_debug_view(request):
|
def request_media_debug_view(request):
|
||||||
""" debug request.META """
|
""" debug request.META """
|
||||||
|
|
||||||
|
assert settings.DEBUG is True, 'Only in DEBUG mode available!'
|
||||||
|
|
||||||
if not request.user.is_authenticated:
|
if not request.user.is_authenticated:
|
||||||
logger.info('Deny debug view: User not logged in!')
|
logger.info('Deny debug view: User not logged in!')
|
||||||
UserModel = get_user_model()
|
UserModel = get_user_model()
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
import os
|
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
|
||||||
from django.core.management import BaseCommand, call_command
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
|
||||||
"""
|
|
||||||
Expand django.contrib.staticfiles runserver
|
|
||||||
"""
|
|
||||||
|
|
||||||
help = "Setup test project and run django developer server"
|
|
||||||
|
|
||||||
def verbose_call(self, command, *args, **kwargs):
|
|
||||||
self.stderr.write("_" * 79)
|
|
||||||
self.stdout.write(f"Call {command!r} with: {args!r} {kwargs!r}")
|
|
||||||
call_command(command, *args, **kwargs)
|
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
|
||||||
|
|
||||||
if "RUN_MAIN" not in os.environ:
|
|
||||||
# RUN_MAIN added by auto reloader, see: django/utils/autoreload.py
|
|
||||||
|
|
||||||
# Create migrations for our test app
|
|
||||||
# But these migrations should never commit!
|
|
||||||
# On changes: Just delete the SQLite file and start fresh ;)
|
|
||||||
self.verbose_call("makemigrations")
|
|
||||||
|
|
||||||
self.verbose_call("migrate")
|
|
||||||
|
|
||||||
# django.contrib.staticfiles.management.commands.collectstatic.Command
|
|
||||||
self.verbose_call("collectstatic", interactive=False, link=True)
|
|
||||||
|
|
||||||
User = get_user_model()
|
|
||||||
qs = User.objects.filter(is_active=True, is_superuser=True)
|
|
||||||
if qs.count() == 0:
|
|
||||||
self.verbose_call("createsuperuser")
|
|
||||||
|
|
||||||
self.verbose_call("runserver", use_threading=False, use_reloader=True, verbosity=2)
|
|
|
@ -1 +0,0 @@
|
||||||
# no models ;)
|
|
|
@ -1,18 +0,0 @@
|
||||||
#!/usr/bin/env python3
|
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
try:
|
|
||||||
from django.core.management import execute_from_command_line
|
|
||||||
except ImportError as exc:
|
|
||||||
raise ImportError(
|
|
||||||
"Couldn't import Django. Are you sure it's installed and "
|
|
||||||
"available on your PYTHONPATH environment variable? Did you "
|
|
||||||
"forget to activate a virtual environment?"
|
|
||||||
) from exc
|
|
||||||
execute_from_command_line(sys.argv)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
|
@ -1,28 +0,0 @@
|
||||||
"""
|
|
||||||
Helper to publish this Project to PyPi
|
|
||||||
"""
|
|
||||||
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from poetry_publish.publish import poetry_publish
|
|
||||||
from poetry_publish.utils.subprocess_utils import verbose_check_call
|
|
||||||
|
|
||||||
import django_ynh
|
|
||||||
|
|
||||||
|
|
||||||
PACKAGE_ROOT = Path(django_ynh.__file__).parent.parent
|
|
||||||
|
|
||||||
|
|
||||||
def publish():
|
|
||||||
"""
|
|
||||||
Publish to PyPi
|
|
||||||
Call this via:
|
|
||||||
$ poetry run publish
|
|
||||||
"""
|
|
||||||
verbose_check_call('poetry', 'check')
|
|
||||||
|
|
||||||
# TODO:
|
|
||||||
# verbose_check_call('make', 'pytest') # don't publish if tests fail
|
|
||||||
# verbose_check_call('make', 'fix-code-style') # don't publish if code style wrong
|
|
||||||
|
|
||||||
poetry_publish(package_root=PACKAGE_ROOT, version=django_ynh.__version__)
|
|
|
@ -1,64 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
|
||||||
BASE_DIR = Path(__file__).parent.parent
|
|
||||||
|
|
||||||
|
|
||||||
SECRET_KEY = 'Only a test project!'
|
|
||||||
|
|
||||||
|
|
||||||
DEBUG = True
|
|
||||||
|
|
||||||
ALLOWED_HOSTS = []
|
|
||||||
|
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
|
||||||
'django.contrib.admin',
|
|
||||||
'django.contrib.auth',
|
|
||||||
'django.contrib.contenttypes',
|
|
||||||
'django.contrib.sessions',
|
|
||||||
'django.contrib.messages',
|
|
||||||
'django.contrib.staticfiles',
|
|
||||||
'django_ynh', # <<<<
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ROOT_URLCONF = 'django_ynh_tests.test_project.urls'
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
WSGI_APPLICATION = 'django_ynh_tests.test_project.wsgi.application'
|
|
||||||
|
|
||||||
|
|
||||||
DATABASES = {
|
|
||||||
'default': {
|
|
||||||
'ENGINE': 'django.db.backends.sqlite3',
|
|
||||||
'NAME': BASE_DIR / 'db.sqlite3',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
AUTH_PASSWORD_VALIDATORS = [] # Just a test project, so no restrictions
|
|
||||||
|
|
||||||
|
|
||||||
LANGUAGE_CODE = 'en-us'
|
|
||||||
TIME_ZONE = 'UTC'
|
|
||||||
USE_I18N = True
|
|
||||||
USE_L10N = True
|
|
||||||
USE_TZ = True
|
|
||||||
LOCALE_PATHS = (BASE_DIR.parent / 'django_ynh' / 'locale',)
|
|
||||||
|
|
||||||
|
|
||||||
STATIC_URL = '/static/'
|
|
||||||
STATIC_ROOT = BASE_DIR / 'static'
|
|
||||||
|
|
||||||
MEDIA_URL = '/media/'
|
|
||||||
MEDIA_ROOT = BASE_DIR / 'media'
|
|
||||||
|
|
||||||
|
|
||||||
INTERNAL_IPS = [
|
|
||||||
'127.0.0.1',
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
import logging
|
|
||||||
|
|
||||||
from django.dispatch import receiver
|
|
||||||
|
|
||||||
from django_ynh.sso_auth.signals import setup_user
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(setup_user)
|
|
||||||
def setup_user_handler(sender, **kwargs):
|
|
||||||
"""
|
|
||||||
Make user to a "staff" user, so he can use the Django admin.
|
|
||||||
|
|
||||||
This Signal is called via:
|
|
||||||
* SSOwatUserBackend after a new user was created
|
|
||||||
* SSOwatRemoteUserMiddleware on login request
|
|
||||||
"""
|
|
||||||
user = kwargs['user']
|
|
||||||
logger.info('Receive "setup_user" signal for user: "%s"', user)
|
|
||||||
|
|
||||||
if not user.is_staff:
|
|
||||||
user.is_staff = True
|
|
||||||
user.save(update_fields=['is_staff'])
|
|
||||||
logger.info('Make user %s to a staff user', user)
|
|
|
@ -1,11 +0,0 @@
|
||||||
import debug_toolbar
|
|
||||||
from django.contrib import admin
|
|
||||||
from django.urls import include, path
|
|
||||||
from django.views.generic import RedirectView
|
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
|
||||||
path('admin/', admin.site.urls),
|
|
||||||
path('', RedirectView.as_view(url='/admin/')),
|
|
||||||
path('__debug__/', include(debug_toolbar.urls)),
|
|
||||||
]
|
|
|
@ -1,9 +0,0 @@
|
||||||
"""
|
|
||||||
WSGI config
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
from django.core.wsgi import get_wsgi_application
|
|
||||||
|
|
||||||
|
|
||||||
application = get_wsgi_application()
|
|
146
local_test.py
Executable file → Normal file
146
local_test.py
Executable file → Normal file
|
@ -8,156 +8,24 @@
|
||||||
|
|
||||||
see README for details ;)
|
see README for details ;)
|
||||||
"""
|
"""
|
||||||
import base64
|
|
||||||
import os
|
|
||||||
import shlex
|
|
||||||
import subprocess
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_ynh_demo_settings'
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import django_ynh # noqa
|
from django_ynh.local_test import create_local_test
|
||||||
except ImportError as err:
|
except ImportError as err:
|
||||||
raise ImportError('Couldn\'t import django_ynh. Did you ' 'forget to activate a virtual environment?') from err
|
raise ImportError('Did you forget to activate a virtual environment?') from err
|
||||||
|
|
||||||
|
BASE_PATH = Path(__file__).parent
|
||||||
BASE_PATH = Path(__file__).parent.absolute()
|
|
||||||
TEST_PATH = BASE_PATH / 'local_test'
|
|
||||||
CONF_PATH = BASE_PATH / 'conf'
|
|
||||||
|
|
||||||
FINAL_HOME_PATH = TEST_PATH / 'opt_yunohost'
|
|
||||||
FINAL_WWW_PATH = TEST_PATH / 'var_www'
|
|
||||||
LOG_FILE = TEST_PATH / 'var_log_django_ynh.log'
|
|
||||||
|
|
||||||
MANAGE_PY_FILE = CONF_PATH / 'manage.py'
|
|
||||||
SETTINGS_FILE = CONF_PATH / 'django_ynh_demo_settings.py'
|
|
||||||
URLS_FILE = CONF_PATH / 'django_ynh_demo_urls.py'
|
|
||||||
|
|
||||||
REPLACES = {
|
|
||||||
'__FINAL_HOME_PATH__': str(FINAL_HOME_PATH),
|
|
||||||
'__FINAL_WWW_PATH__': str(FINAL_WWW_PATH),
|
|
||||||
'__LOG_FILE__': str(TEST_PATH / 'var_log_django_ynh.log'),
|
|
||||||
'__PATH_URL__': 'app_path',
|
|
||||||
'__DOMAIN__': '127.0.0.1',
|
|
||||||
'django.db.backends.postgresql': 'django.db.backends.sqlite3',
|
|
||||||
"'NAME': '__APP__',": f"'NAME': '{TEST_PATH / 'test_db.sqlite'}',",
|
|
||||||
'django_redis.cache.RedisCache': 'django.core.cache.backends.dummy.DummyCache',
|
|
||||||
|
|
||||||
# Just use the default logging setup from django_ynh project:
|
|
||||||
'LOGGING = {': 'HACKED_DEACTIVATED_LOGGING = {',
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def verbose_check_call(command, verbose=True, **kwargs):
|
|
||||||
""" 'verbose' version of subprocess.check_call() """
|
|
||||||
if verbose:
|
|
||||||
print('_' * 100)
|
|
||||||
msg = f'Call: {command!r}'
|
|
||||||
verbose_kwargs = ', '.join(f'{k}={v!r}' for k, v in sorted(kwargs.items()))
|
|
||||||
if verbose_kwargs:
|
|
||||||
msg += f' (kwargs: {verbose_kwargs})'
|
|
||||||
print(f'{msg}\n', flush=True)
|
|
||||||
|
|
||||||
env = os.environ.copy()
|
|
||||||
env['PYTHONUNBUFFERED'] = '1'
|
|
||||||
|
|
||||||
popenargs = shlex.split(command)
|
|
||||||
subprocess.check_call(popenargs, universal_newlines=True, env=env, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def call_manage_py(args):
|
|
||||||
verbose_check_call(
|
|
||||||
command=f'{sys.executable} manage.py {args}',
|
|
||||||
cwd=FINAL_HOME_PATH,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def copy_patch(src_file, replaces=None):
|
|
||||||
dst_file = FINAL_HOME_PATH / src_file.name
|
|
||||||
print(f'{src_file.relative_to(BASE_PATH)} -> {dst_file.relative_to(BASE_PATH)}')
|
|
||||||
|
|
||||||
with src_file.open('r') as f:
|
|
||||||
content = f.read()
|
|
||||||
|
|
||||||
if replaces:
|
|
||||||
for old, new in replaces.items():
|
|
||||||
content = content.replace(old, new)
|
|
||||||
|
|
||||||
with dst_file.open('w') as f:
|
|
||||||
f.write(content)
|
|
||||||
|
|
||||||
|
|
||||||
def assert_is_dir(dir_path):
|
|
||||||
assert dir_path.is_dir, f'Directory does not exists: {dir_path}'
|
|
||||||
|
|
||||||
|
|
||||||
def assert_is_file(file_path):
|
|
||||||
assert file_path.is_file, f'File not found: {file_path}'
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print('-' * 100)
|
create_local_test(
|
||||||
|
django_settings_path=BASE_PATH / 'conf' / 'django_ynh_demo_settings.py',
|
||||||
assert_is_dir(BASE_PATH)
|
destination=BASE_PATH / 'local_test',
|
||||||
assert_is_dir(CONF_PATH)
|
runserver=True,
|
||||||
assert_is_file(SETTINGS_FILE)
|
|
||||||
assert_is_file(URLS_FILE)
|
|
||||||
|
|
||||||
for p in (TEST_PATH, FINAL_HOME_PATH, FINAL_WWW_PATH):
|
|
||||||
if p.is_dir():
|
|
||||||
print(f'Already exists: "{p.relative_to(BASE_PATH)}", ok.')
|
|
||||||
else:
|
|
||||||
print(f'Create: "{p.relative_to(BASE_PATH)}"')
|
|
||||||
p.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
LOG_FILE.touch(exist_ok=True)
|
|
||||||
|
|
||||||
# conf/manage.py -> local_test/manage.py
|
|
||||||
copy_patch(src_file=MANAGE_PY_FILE)
|
|
||||||
|
|
||||||
# conf/django_ynh_demo_settings.py -> local_test/django_ynh_demo_settings.py
|
|
||||||
copy_patch(src_file=SETTINGS_FILE, replaces=REPLACES)
|
|
||||||
|
|
||||||
# conf/ynh_urls.py -> local_test/ynh_urls.py
|
|
||||||
copy_patch(src_file=URLS_FILE, replaces=REPLACES)
|
|
||||||
|
|
||||||
with Path(FINAL_HOME_PATH / 'local_settings.py').open('w') as f:
|
|
||||||
f.write('# Only for local test run\n')
|
|
||||||
f.write('SERVE_FILES = True # used in src/inventory_project/urls.py\n')
|
|
||||||
f.write('AUTH_PASSWORD_VALIDATORS = [] # accept all passwords\n')
|
|
||||||
|
|
||||||
# call "local_test/manage.py" via subprocess:
|
|
||||||
call_manage_py('check --deploy')
|
|
||||||
call_manage_py('migrate --no-input')
|
|
||||||
call_manage_py('collectstatic --no-input')
|
|
||||||
|
|
||||||
verbose_check_call(
|
|
||||||
command=(
|
|
||||||
f'{sys.executable} -m django_ynh.create_superuser'
|
|
||||||
' --ds="django_ynh_demo_settings" --username="test" --password="test"'
|
|
||||||
),
|
|
||||||
cwd=FINAL_HOME_PATH,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# All environment variables are passed to Django's "runnserver" ;)
|
|
||||||
# "Simulate" SSOwat authentication, by set "http headers"
|
|
||||||
# Still missing is the 'SSOwAuthUser' cookie,
|
|
||||||
# but this is ignored, if settings.DEBUG=True ;)
|
|
||||||
os.environ['HTTP_AUTH_USER'] = 'test'
|
|
||||||
os.environ['HTTP_REMOTE_USER'] = 'test'
|
|
||||||
|
|
||||||
creds = str(base64.b64encode(b'test:test'), encoding='utf-8')
|
|
||||||
basic_auth = f'basic {creds}'
|
|
||||||
os.environ['HTTP_AUTHORIZATION'] = basic_auth
|
|
||||||
|
|
||||||
try:
|
|
||||||
call_manage_py('runserver --nostatic')
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
print('\nBye ;)')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -13,12 +13,14 @@ packages = [
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = ">=3.7,<4.0.0"
|
python = ">=3.7,<4.0.0"
|
||||||
django = "*"
|
django = "*"
|
||||||
|
gunicorn = "*"
|
||||||
django-axes = "*" # https://github.com/jazzband/django-axes
|
django-axes = "*" # https://github.com/jazzband/django-axes
|
||||||
psycopg2-binary = "*"
|
psycopg2-binary = "*"
|
||||||
django-redis = "*"
|
django-redis = "*"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
poetry-publish = "*" # https://github.com/jedie/poetry-publish
|
poetry-publish = "*" # https://github.com/jedie/poetry-publish
|
||||||
|
bx_py_utils = "*"
|
||||||
tox = "*"
|
tox = "*"
|
||||||
pytest = "*"
|
pytest = "*"
|
||||||
pytest-randomly = "*"
|
pytest-randomly = "*"
|
||||||
|
@ -57,8 +59,7 @@ lines_after_imports=2
|
||||||
[tool.pytest.ini_options]
|
[tool.pytest.ini_options]
|
||||||
# https://docs.pytest.org/en/latest/customize.html#pyproject-toml
|
# https://docs.pytest.org/en/latest/customize.html#pyproject-toml
|
||||||
minversion = "6.0"
|
minversion = "6.0"
|
||||||
DJANGO_SETTINGS_MODULE="django_ynh_project.settings.tests"
|
norecursedirs = ".* .git __pycache__ conf coverage* dist htmlcov volumes"
|
||||||
norecursedirs = ".* .git __pycache__ coverage* dist htmlcov volumes"
|
|
||||||
# sometimes helpfull "addopts" arguments:
|
# sometimes helpfull "addopts" arguments:
|
||||||
# -vv
|
# -vv
|
||||||
# --verbose
|
# --verbose
|
||||||
|
@ -80,7 +81,6 @@ addopts = """
|
||||||
--failed-first
|
--failed-first
|
||||||
--last-failed-no-failures all
|
--last-failed-no-failures all
|
||||||
--new-first
|
--new-first
|
||||||
-p no:randomly
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
25
run_pytest.py
Normal file
25
run_pytest.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
"""
|
||||||
|
Run pytest against local test creation
|
||||||
|
"""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
from django_ynh.pytest_helper import run_pytest
|
||||||
|
except ImportError as err:
|
||||||
|
raise ImportError('Did you forget to activate a virtual environment?') from err
|
||||||
|
|
||||||
|
|
||||||
|
BASE_PATH = Path(__file__).parent
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
run_pytest(
|
||||||
|
django_settings_path=BASE_PATH / 'conf' / 'django_ynh_demo_settings.py',
|
||||||
|
destination=BASE_PATH / 'local_test',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
130
tests/test_django_ynh.py
Normal file
130
tests/test_django_ynh.py
Normal file
|
@ -0,0 +1,130 @@
|
||||||
|
from axes.models import AccessAttempt, AccessLog
|
||||||
|
from bx_py_utils.test_utils.html_assertion import HtmlAssertionMixin
|
||||||
|
from django.conf import settings
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from django.test import override_settings
|
||||||
|
from django.test.testcases import TestCase
|
||||||
|
from django.urls.base import reverse
|
||||||
|
|
||||||
|
from django_ynh.test_utils import generate_basic_auth
|
||||||
|
from django_ynh.views import request_media_debug_view
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(DEBUG=False)
|
||||||
|
class DjangoYnhTestCase(HtmlAssertionMixin, TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
|
||||||
|
# Always start a fresh session:
|
||||||
|
self.client = self.client_class()
|
||||||
|
|
||||||
|
def test_settings(self):
|
||||||
|
assert settings.PATH_URL == 'app_path'
|
||||||
|
|
||||||
|
assert str(settings.FINAL_HOME_PATH).endswith('/local_test/opt_yunohost')
|
||||||
|
assert str(settings.FINAL_WWW_PATH).endswith('/local_test/var_www')
|
||||||
|
assert str(settings.LOG_FILE).endswith('/local_test/var_log_django_ynh.log')
|
||||||
|
|
||||||
|
assert settings.ROOT_URLCONF == 'django_ynh_demo_urls'
|
||||||
|
|
||||||
|
def test_urls(self):
|
||||||
|
assert reverse('admin:index') == '/app_path/'
|
||||||
|
assert reverse(request_media_debug_view) == '/app_path/debug/'
|
||||||
|
|
||||||
|
def test_auth(self):
|
||||||
|
response = self.client.get('/app_path/')
|
||||||
|
self.assertRedirects(response, expected_url='/app_path/login/?next=/app_path/')
|
||||||
|
|
||||||
|
def test_create_unknown_user(self):
|
||||||
|
assert User.objects.count() == 0
|
||||||
|
|
||||||
|
self.client.cookies['SSOwAuthUser'] = 'test'
|
||||||
|
|
||||||
|
response = self.client.get(
|
||||||
|
path='/app_path/',
|
||||||
|
HTTP_REMOTE_USER='test',
|
||||||
|
HTTP_AUTH_USER='test',
|
||||||
|
HTTP_AUTHORIZATION='basic dGVzdDp0ZXN0MTIz',
|
||||||
|
)
|
||||||
|
|
||||||
|
assert User.objects.count() == 1
|
||||||
|
user = User.objects.first()
|
||||||
|
assert user.username == 'test'
|
||||||
|
assert user.is_active is True
|
||||||
|
assert user.is_staff is True # Set by: conf.django_ynh_demo_urls.setup_user_handler
|
||||||
|
assert user.is_superuser is False
|
||||||
|
|
||||||
|
self.assert_html_parts(
|
||||||
|
response, parts=('<title>Site administration | Django site admin</title>', '<strong>test</strong>')
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_wrong_auth_user(self):
|
||||||
|
assert User.objects.count() == 0
|
||||||
|
assert AccessLog.objects.count() == 0
|
||||||
|
|
||||||
|
self.client.cookies['SSOwAuthUser'] = 'test'
|
||||||
|
|
||||||
|
response = self.client.get(
|
||||||
|
path='/app_path/',
|
||||||
|
HTTP_REMOTE_USER='test',
|
||||||
|
HTTP_AUTH_USER='foobar', # <<< wrong user name
|
||||||
|
HTTP_AUTHORIZATION='basic dGVzdDp0ZXN0MTIz',
|
||||||
|
)
|
||||||
|
|
||||||
|
assert User.objects.count() == 1
|
||||||
|
user = User.objects.first()
|
||||||
|
assert user.username == 'test'
|
||||||
|
assert user.is_active is True
|
||||||
|
assert user.is_staff is True # Set by: conf.django_ynh_demo_urls.setup_user_handler
|
||||||
|
assert user.is_superuser is False
|
||||||
|
|
||||||
|
assert AccessLog.objects.count() == 1
|
||||||
|
|
||||||
|
assert response.status_code == 403 # Forbidden
|
||||||
|
|
||||||
|
def test_wrong_cookie(self):
|
||||||
|
assert User.objects.count() == 0
|
||||||
|
assert AccessLog.objects.count() == 0
|
||||||
|
|
||||||
|
self.client.cookies['SSOwAuthUser'] = 'foobar' # <<< wrong user name
|
||||||
|
|
||||||
|
response = self.client.get(
|
||||||
|
path='/app_path/',
|
||||||
|
HTTP_REMOTE_USER='test',
|
||||||
|
HTTP_AUTH_USER='test',
|
||||||
|
HTTP_AUTHORIZATION='basic dGVzdDp0ZXN0MTIz',
|
||||||
|
)
|
||||||
|
|
||||||
|
assert User.objects.count() == 1
|
||||||
|
user = User.objects.first()
|
||||||
|
assert user.username == 'test'
|
||||||
|
assert user.is_active is True
|
||||||
|
assert user.is_staff is True # Set by: conf.django_ynh_demo_urls.setup_user_handler
|
||||||
|
assert user.is_superuser is False
|
||||||
|
|
||||||
|
assert AccessLog.objects.count() == 1
|
||||||
|
|
||||||
|
assert response.status_code == 403 # Forbidden
|
||||||
|
|
||||||
|
def test_wrong_authorization_user(self):
|
||||||
|
assert User.objects.count() == 0
|
||||||
|
|
||||||
|
self.client.cookies['SSOwAuthUser'] = 'test'
|
||||||
|
|
||||||
|
response = self.client.get(
|
||||||
|
path='/app_path/',
|
||||||
|
HTTP_REMOTE_USER='test',
|
||||||
|
HTTP_AUTH_USER='test',
|
||||||
|
HTTP_AUTHORIZATION=generate_basic_auth(username='foobar', password='test123'), # <<< wrong user name
|
||||||
|
)
|
||||||
|
|
||||||
|
assert User.objects.count() == 1
|
||||||
|
user = User.objects.first()
|
||||||
|
assert user.username == 'test'
|
||||||
|
assert user.is_active is True
|
||||||
|
assert user.is_staff is True # Set by: conf.django_ynh_demo_urls.setup_user_handler
|
||||||
|
assert user.is_superuser is False
|
||||||
|
|
||||||
|
assert AccessLog.objects.count() == 1
|
||||||
|
|
||||||
|
assert response.status_code == 403 # Forbidden
|
8
tests/test_utils.py
Normal file
8
tests/test_utils.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
from unittest.case import TestCase
|
||||||
|
|
||||||
|
from django_ynh.test_utils import generate_basic_auth
|
||||||
|
|
||||||
|
|
||||||
|
class TestUtilsTestCase(TestCase):
|
||||||
|
def test_generate_basic_auth(self):
|
||||||
|
assert generate_basic_auth(username='test', password='test123') == 'basic dGVzdDp0ZXN0MTIz'
|
Loading…
Reference in a new issue