From 15598e9d525c0586c8ed6f30a514ef427b4ce573 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Sun, 25 Aug 2024 20:00:07 +0200 Subject: [PATCH 1/2] Cleanup: Remove obsolete test files Tests are in for_runners_ynh/tests --- tests/__init__.py | 0 tests/conftest.py | 43 ----- tests/test_django_project.py | 176 ------------------ ...roject_create_unknown_user_1.snapshot.html | 90 --------- tests/test_manage_local_test.py | 17 -- tests/test_project_setup.py | 100 ---------- tests/test_utils.py | 8 - 7 files changed, 434 deletions(-) delete mode 100644 tests/__init__.py delete mode 100644 tests/conftest.py delete mode 100644 tests/test_django_project.py delete mode 100644 tests/test_django_project_create_unknown_user_1.snapshot.html delete mode 100644 tests/test_manage_local_test.py delete mode 100644 tests/test_project_setup.py delete mode 100644 tests/test_utils.py diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/conftest.py b/tests/conftest.py deleted file mode 100644 index bb04271..0000000 --- a/tests/conftest.py +++ /dev/null @@ -1,43 +0,0 @@ -""" - Special pytest init: - - - Build a "local_test" YunoHost installation - - init Django with this local test installation - - So the pytests will run against this local test installation -""" -import os -import sys -from pathlib import Path - -import django -from django_yunohost_integration.local_test import CreateResults, create_local_test - - -BASE_PATH = Path(__file__).parent.parent - -os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' - - -def pytest_configure(): - print('Compile YunoHost files...') - result: CreateResults = create_local_test( - django_settings_path=BASE_PATH / 'conf' / 'settings.py', - destination=BASE_PATH / 'local_test', - runserver=False, - extra_replacements={ - '__DEBUG_ENABLED__': '0', # "1" or "0" string - '__LOG_LEVEL__': 'INFO', - '__ADMIN_EMAIL__': 'foo-bar@test.tld', - '__DEFAULT_FROM_EMAIL__': 'django_app@test.tld', - }, - ) - print('Local test files created:') - print(result) - - os.chdir(result.data_dir_path) - data_dir = str(result.data_dir_path) - if data_dir not in sys.path: - sys.path.insert(0, data_dir) - - django.setup() diff --git a/tests/test_django_project.py b/tests/test_django_project.py deleted file mode 100644 index 1fcf08a..0000000 --- a/tests/test_django_project.py +++ /dev/null @@ -1,176 +0,0 @@ -from unittest.mock import patch - -import for_runners -from axes.models import AccessLog -from bx_django_utils.test_utils.html_assertion import HtmlAssertionMixin, assert_html_response_snapshot -from django.conf import LazySettings, settings -from django.contrib.auth.models import User -from django.template.defaulttags import CsrfTokenNode -from django.test import override_settings -from django.test.testcases import TestCase -from django.urls.base import reverse -from django_yunohost_integration.test_utils import generate_basic_auth - - -@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 isinstance(settings, LazySettings) - assert settings.configured is True - - assert settings.PATH_URL == 'app_path' - - assert str(settings.DATA_DIR_PATH).endswith('/local_test/opt_yunohost') - assert str(settings.INSTALL_DIR_PATH).endswith('/local_test/var_www') - assert str(settings.LOG_FILE_PATH).endswith('/local_test/var_log_django-for-runners.log') - - assert settings.ROOT_URLCONF == 'urls' - - def test_config_panel_settings(self): - # config_panel.toml settings, set via tests.conftest.pytest_configure(): - assert settings.DEBUG_ENABLED == '0' and settings.DEBUG is False - assert settings.LOG_LEVEL == 'INFO' - assert settings.ADMIN_EMAIL == 'foo-bar@test.tld' - assert settings.DEFAULT_FROM_EMAIL == 'django_app@test.tld' - - def test_urls(self): - assert reverse('admin:index') == '/app_path/' - - # TODO: https://github.com/jedie/django-for-runners/issues/25 - # Serve user uploads via django_tools.serve_media_app: - # assert settings.MEDIA_URL == '/app_path/media/' - # assert reverse( - # 'serve_media_app:serve-media', kwargs={'user_token': 'token', 'path': 'foo/bar/'} - # ) == ('/app_path/media/token/foo/bar/') - - def test_auth(self): - assert settings.PATH_URL == 'app_path' - assert reverse('admin:index') == '/app_path/' - - # SecurityMiddleware should redirects all non-HTTPS requests to HTTPS: - assert settings.SECURE_SSL_REDIRECT is True - response = self.client.get('/app_path/', secure=False) - self.assertRedirects( - response, - status_code=301, # permanent redirect - expected_url='https://testserver/app_path/', - fetch_redirect_response=False, - ) - - response = self.client.get('/app_path/', secure=True) - self.assertRedirects( - response, expected_url='/app_path/login/?next=/app_path/', fetch_redirect_response=False - ) - - def test_create_unknown_user(self): - assert User.objects.count() == 0 - - self.client.cookies['SSOwAuthUser'] = 'test' - - with patch.object(CsrfTokenNode, 'render', return_value='MockedCsrfTokenNode'): - response = self.client.get( - path='/app_path/', - HTTP_REMOTE_USER='test', - HTTP_AUTH_USER='test', - HTTP_AUTHORIZATION='basic dGVzdDp0ZXN0MTIz', - secure=True, - ) - - 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.setup_user.setup_project_user - assert user.is_superuser is False - - self.assert_html_parts( - response, - parts=( - f'Site administration | Django-ForRunners v{for_runners.__version__}', - 'test', - ), - ) - assert_html_response_snapshot(response, query_selector='#container', validate=False) - - 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', - secure=True, - ) - - 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.setup_user.setup_project_user - 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', - secure=True, - ) - - 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.setup_user.setup_project_user - 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', # <<< wrong user name - password='test123', - ), - secure=True, - ) - - 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.setup_user.setup_project_user - assert user.is_superuser is False - - assert AccessLog.objects.count() == 1 - - assert response.status_code == 403 # Forbidden diff --git a/tests/test_django_project_create_unknown_user_1.snapshot.html b/tests/test_django_project_create_unknown_user_1.snapshot.html deleted file mode 100644 index 3168351..0000000 --- a/tests/test_django_project_create_unknown_user_1.snapshot.html +++ /dev/null @@ -1,90 +0,0 @@ -
- - - -
-
- -
-

- Site administration -

-
-

- You don’t have permission to view or edit anything. -

-
- -
-
- - -
-
-
\ No newline at end of file diff --git a/tests/test_manage_local_test.py b/tests/test_manage_local_test.py deleted file mode 100644 index 17d5cd6..0000000 --- a/tests/test_manage_local_test.py +++ /dev/null @@ -1,17 +0,0 @@ -import subprocess -from unittest import TestCase - -from bx_py_utils.path import assert_is_file - -from django_yunohost_integration.path_utils import get_project_root - - -class ManageLocalTestTestCase(TestCase): - def test_manage_local_test_check(self): - - manage_local_test_bin = get_project_root() / 'manage_local_test.py' - assert_is_file(manage_local_test_bin) - - output = subprocess.check_output([manage_local_test_bin, 'check'], text=True) - self.assertIn('Setup local YunoHost package', output) - self.assertIn('django-for-runners_ynh/local_test/', output) diff --git a/tests/test_project_setup.py b/tests/test_project_setup.py deleted file mode 100644 index 85db8ca..0000000 --- a/tests/test_project_setup.py +++ /dev/null @@ -1,100 +0,0 @@ -import os -import tomllib -from pathlib import Path -from unittest import TestCase - -from bx_django_utils.filename import clean_filename -from bx_py_utils.path import assert_is_dir, assert_is_file -from django_tools.unittest_utils.project_setup import check_editor_config -from django_yunohost_integration.path_utils import get_project_root -from django_yunohost_integration.test_utils import assert_project_version -from for_runners import __version__ as upstream_version - -from for_runners_ynh import __version__ - - -def assert_file_contains_string(file_path, string): - with file_path.open('r') as f: - for line in f: - if string in line: - return - raise AssertionError(f'File {file_path} does not contain {string!r} !') - - -def test_version(): - assert '+ynh' in __version__, f'{__version__!r} does not contain "+ynh"' - assert upstream_version in __version__, f'{__version__!r} does not contain {upstream_version!r}' - - # pyproject.toml needs a PEP 440 conform version and used "+ynh" - # the YunoHost syntax is: "~ynh", just "convert this: - manifest_version = __version__.replace('+', '~') - - assert_file_contains_string( - file_path=Path(get_project_root(), 'manifest.toml'), - string=f'version = "{manifest_version}"', - ) - - if 'GITHUB_ACTION' not in os.environ: - # Github has a rate-limiting... So don't fetch the API if we run as GitHub action - assert_project_version( - current_version=__version__, - github_project_url='https://github.com/jedie/django-for-runners', - ) - - -def test_screenshot_filenames(): - """ - https://forum.yunohost.org/t/yunohost-bot-cant-handle-spaces-in-screenshots/19483 - """ - screenshot_path = get_project_root() / 'doc' / 'screenshots' - assert_is_dir(screenshot_path) - renamed = [] - for file_path in screenshot_path.iterdir(): - file_name = file_path.name - if file_name.startswith('.'): - continue - cleaned_name = clean_filename(file_name) - if cleaned_name != file_name: - new_path = file_path.with_name(cleaned_name) - file_path.rename(new_path) - renamed.append(f'{file_name!r} renamed to {cleaned_name!r}') - assert not renamed, f'Bad screenshots file names found: {", ".join(renamed)}' - - -def test_check_editor_config(): - check_editor_config(package_root=get_project_root()) - - -class ManifestTestCase(TestCase): - def test_manifest_toml(self): - manifest_path = get_project_root() / 'manifest.toml' - assert_is_file(manifest_path) - - cfg = tomllib.loads(manifest_path.read_text(encoding='UTF-8')) - - self.assertEqual(cfg['packaging_format'], 2) - self.assertEqual( - set(cfg['install'].keys()), - { - 'admin', - 'admin_email', - 'debug_enabled', - 'default_from_email', - 'domain', - 'init_main_permission', - 'log_level', - 'path', - }, - ) - self.assertEqual( - set(cfg['resources'].keys()), - { - 'apt', - 'data_dir', - 'database', - 'install_dir', - 'permissions', - 'ports', - 'system_user', - }, - ) diff --git a/tests/test_utils.py b/tests/test_utils.py deleted file mode 100644 index 1055e23..0000000 --- a/tests/test_utils.py +++ /dev/null @@ -1,8 +0,0 @@ -from unittest.case import TestCase - -from django_yunohost_integration.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' From 85bb7e5b0b39f41f40ca919d7d33352b729ea74d Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Sun, 25 Aug 2024 20:16:19 +0200 Subject: [PATCH 2/2] Bugfix Django user permissions A staff user can't see/change anything ;) --- conf/setup_user.py | 1 + for_runners_ynh/tests/test_django_project.py | 8 +- ...roject_create_unknown_user_1.snapshot.html | 208 +++++++++++++++++- 3 files changed, 210 insertions(+), 7 deletions(-) diff --git a/conf/setup_user.py b/conf/setup_user.py index d838d3e..26abbc3 100644 --- a/conf/setup_user.py +++ b/conf/setup_user.py @@ -4,5 +4,6 @@ def setup_project_user(user): Called from django_yunohost_integration.sso_auth """ user.is_staff = True + user.is_superuser = True user.save() return user diff --git a/for_runners_ynh/tests/test_django_project.py b/for_runners_ynh/tests/test_django_project.py index 5fdc416..30400e0 100644 --- a/for_runners_ynh/tests/test_django_project.py +++ b/for_runners_ynh/tests/test_django_project.py @@ -81,7 +81,7 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase): assert user.username == 'test' assert user.is_active is True assert user.is_staff is True # Set by: conf.setup_user.setup_project_user - assert user.is_superuser is False + assert user.is_superuser is True # Set by: conf.setup_user.setup_project_user self.assert_html_parts( response, @@ -111,7 +111,7 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase): assert user.username == 'test' assert user.is_active is True assert user.is_staff is True # Set by: conf.setup_user.setup_project_user - assert user.is_superuser is False + assert user.is_superuser is True # Set by: conf.setup_user.setup_project_user assert AccessLog.objects.count() == 1 @@ -136,7 +136,7 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase): assert user.username == 'test' assert user.is_active is True assert user.is_staff is True # Set by: conf.setup_user.setup_project_user - assert user.is_superuser is False + assert user.is_superuser is True # Set by: conf.setup_user.setup_project_user assert AccessLog.objects.count() == 1 @@ -163,7 +163,7 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase): assert user.username == 'test' assert user.is_active is True assert user.is_staff is True # Set by: conf.setup_user.setup_project_user - assert user.is_superuser is False + assert user.is_superuser is True # Set by: conf.setup_user.setup_project_user assert AccessLog.objects.count() == 1 diff --git a/for_runners_ynh/tests/test_django_project_create_unknown_user_1.snapshot.html b/for_runners_ynh/tests/test_django_project_create_unknown_user_1.snapshot.html index 84f96ea..05b2cde 100644 --- a/for_runners_ynh/tests/test_django_project_create_unknown_user_1.snapshot.html +++ b/for_runners_ynh/tests/test_django_project_create_unknown_user_1.snapshot.html @@ -6,9 +6,211 @@ Site administration