Merge pull request #70 from YunoHost-Apps/testing

master <- testing
This commit is contained in:
Jens Diemer 2024-08-26 08:56:39 +02:00 committed by GitHub
commit 362d249dd3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 210 additions and 441 deletions

View file

@ -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

View file

@ -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

View file

@ -6,9 +6,211 @@
Site administration
</h1>
<div id="content-main">
<p>
You dont have permission to view or edit anything.
</p>
<div class="app-auth module">
<table>
<caption>
<a class="section" href="/app_path/auth/" title="Models in the Authentication and Authorization application">
Authentication and Authorization
</a>
</caption>
<tr class="model-group">
<th id="auth-group" scope="row">
<a href="/app_path/auth/group/">
Groups
</a>
</th>
<td>
<a aria-describedby="auth-group" class="addlink" href="/app_path/auth/group/add/">
Add
</a>
</td>
<td>
<a aria-describedby="auth-group" class="changelink" href="/app_path/auth/group/">
Change
</a>
</td>
</tr>
<tr class="model-user">
<th id="auth-user" scope="row">
<a href="/app_path/auth/user/">
Users
</a>
</th>
<td>
<a aria-describedby="auth-user" class="addlink" href="/app_path/auth/user/add/">
Add
</a>
</td>
<td>
<a aria-describedby="auth-user" class="changelink" href="/app_path/auth/user/">
Change
</a>
</td>
</tr>
</table>
</div>
<div class="app-axes module">
<table>
<caption>
<a class="section" href="/app_path/axes/" title="Models in the Axes application">
Axes
</a>
</caption>
<tr class="model-accessattempt">
<th id="axes-accessattempt" scope="row">
<a href="/app_path/axes/accessattempt/">
Access attempts
</a>
</th>
<td>
</td>
<td>
<a aria-describedby="axes-accessattempt" class="changelink" href="/app_path/axes/accessattempt/">
Change
</a>
</td>
</tr>
<tr class="model-accessfailurelog">
<th id="axes-accessfailurelog" scope="row">
<a href="/app_path/axes/accessfailurelog/">
Access failures
</a>
</th>
<td>
</td>
<td>
<a aria-describedby="axes-accessfailurelog" class="changelink" href="/app_path/axes/accessfailurelog/">
Change
</a>
</td>
</tr>
<tr class="model-accesslog">
<th id="axes-accesslog" scope="row">
<a href="/app_path/axes/accesslog/">
Access logs
</a>
</th>
<td>
</td>
<td>
<a aria-describedby="axes-accesslog" class="changelink" href="/app_path/axes/accesslog/">
Change
</a>
</td>
</tr>
</table>
</div>
<div class="app-for_runners module">
<table>
<caption>
<a class="section" href="/app_path/for_runners/" title="Models in the ForRunners application">
ForRunners
</a>
</caption>
<tr class="model-disciplinemodel">
<th id="for_runners-disciplinemodel" scope="row">
<a href="/app_path/for_runners/disciplinemodel/">
Discipline models
</a>
</th>
<td>
<a aria-describedby="for_runners-disciplinemodel" class="addlink" href="/app_path/for_runners/disciplinemodel/add/">
Add
</a>
</td>
<td>
<a aria-describedby="for_runners-disciplinemodel" class="changelink" href="/app_path/for_runners/disciplinemodel/">
Change
</a>
</td>
</tr>
<tr class="model-distancemodel">
<th id="for_runners-distancemodel" scope="row">
<a href="/app_path/for_runners/distancemodel/">
Distances
</a>
</th>
<td>
<a aria-describedby="for_runners-distancemodel" class="addlink" href="/app_path/for_runners/distancemodel/add/">
Add
</a>
</td>
<td>
<a aria-describedby="for_runners-distancemodel" class="changelink" href="/app_path/for_runners/distancemodel/">
Change
</a>
</td>
</tr>
<tr class="model-participationmodel">
<th id="for_runners-participationmodel" scope="row">
<a href="/app_path/for_runners/participationmodel/">
Event Participations
</a>
</th>
<td>
<a aria-describedby="for_runners-participationmodel" class="addlink" href="/app_path/for_runners/participationmodel/add/">
Add
</a>
</td>
<td>
<a aria-describedby="for_runners-participationmodel" class="changelink" href="/app_path/for_runners/participationmodel/">
Change
</a>
</td>
</tr>
<tr class="model-eventlinkmodel">
<th id="for_runners-eventlinkmodel" scope="row">
<a href="/app_path/for_runners/eventlinkmodel/">
Event link models
</a>
</th>
<td>
<a aria-describedby="for_runners-eventlinkmodel" class="addlink" href="/app_path/for_runners/eventlinkmodel/add/">
Add
</a>
</td>
<td>
<a aria-describedby="for_runners-eventlinkmodel" class="changelink" href="/app_path/for_runners/eventlinkmodel/">
Change
</a>
</td>
</tr>
<tr class="model-eventmodel">
<th id="for_runners-eventmodel" scope="row">
<a href="/app_path/for_runners/eventmodel/">
Events
</a>
</th>
<td>
<a aria-describedby="for_runners-eventmodel" class="addlink" href="/app_path/for_runners/eventmodel/add/">
Add
</a>
</td>
<td>
<a aria-describedby="for_runners-eventmodel" class="changelink" href="/app_path/for_runners/eventmodel/">
Change
</a>
</td>
</tr>
<tr class="model-gpxmodel">
<th id="for_runners-gpxmodel" scope="row">
<a href="/app_path/for_runners/gpxmodel/">
GPX Tracks
</a>
</th>
<td>
<a aria-describedby="for_runners-gpxmodel" class="addlink" href="/app_path/for_runners/gpxmodel/add/">
Add
</a>
</td>
<td>
<a aria-describedby="for_runners-gpxmodel" class="changelink" href="/app_path/for_runners/gpxmodel/">
Change
</a>
</td>
</tr>
</table>
</div>
</div>
<div id="content-related">
<div class="module" id="recent-actions-module">

View file

View file

@ -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()

View file

@ -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'<title>Site administration | Django-ForRunners v{for_runners.__version__}</title>',
'<strong>test</strong>',
),
)
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

View file

@ -1,90 +0,0 @@
<div id="container">
<!-- Header -->
<div id="header">
<div id="branding">
<h1 id="site-name">
<a href="/app_path/">
<img alt="Logo" height="40px" src="/app_path/static/Django-ForRunners.svg"/>
Django-ForRunners v0.19.0
</a>
</h1>
</div>
<div id="user-tools">
Welcome,
<strong>
test
</strong>
.
<a href="/">
View site
</a>
/
<a href="/app_path/password_change/">
Change password
</a>
/
<form action="/app_path/logout/" id="logout-form" method="post">
MockedCsrfTokenNode
<button type="submit">
Log out
</button>
</form>
<button class="theme-toggle">
<div class="visually-hidden theme-label-when-auto">
Toggle theme (current theme: auto)
</div>
<div class="visually-hidden theme-label-when-light">
Toggle theme (current theme: light)
</div>
<div class="visually-hidden theme-label-when-dark">
Toggle theme (current theme: dark)
</div>
<svg aria-hidden="true" class="theme-icon-when-auto">
<use xlink:href="#icon-auto">
</use>
</svg>
<svg aria-hidden="true" class="theme-icon-when-dark">
<use xlink:href="#icon-moon">
</use>
</svg>
<svg aria-hidden="true" class="theme-icon-when-light">
<use xlink:href="#icon-sun">
</use>
</svg>
</button>
</div>
</div>
<!-- END Header -->
<div class="main" id="main">
<div class="content" id="content-start" tabindex="-1">
<!-- Content -->
<div class="colMS" id="content">
<h1>
Site administration
</h1>
<div id="content-main">
<p>
You dont have permission to view or edit anything.
</p>
</div>
<div id="content-related">
<div class="module" id="recent-actions-module">
<h2>
Recent actions
</h2>
<h3>
My actions
</h3>
<p>
None available
</p>
</div>
</div>
<br class="clear"/>
</div>
<!-- END Content -->
<div id="footer">
</div>
</div>
</div>
</div>

View file

@ -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)

View file

@ -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',
},
)

View file

@ -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'