2023-11-09 20:37:13 +01:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import for_runners
|
2021-01-17 13:03:41 +01:00
|
|
|
from axes.models import AccessLog
|
2023-11-09 20:37:13 +01:00
|
|
|
from bx_django_utils.test_utils.html_assertion import HtmlAssertionMixin, assert_html_response_snapshot
|
2023-04-02 20:15:56 +02:00
|
|
|
from django.conf import LazySettings, settings
|
2021-01-17 13:03:41 +01:00
|
|
|
from django.contrib.auth.models import User
|
2023-11-09 20:37:13 +01:00
|
|
|
from django.template.defaulttags import CsrfTokenNode
|
2021-01-17 13:03:41 +01:00
|
|
|
from django.test import override_settings
|
|
|
|
from django.test.testcases import TestCase
|
|
|
|
from django.urls.base import reverse
|
2021-10-10 14:35:06 +02:00
|
|
|
from django_yunohost_integration.test_utils import generate_basic_auth
|
|
|
|
|
2021-01-17 13:03:41 +01:00
|
|
|
|
|
|
|
@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):
|
2023-04-02 20:15:56 +02:00
|
|
|
assert isinstance(settings, LazySettings)
|
|
|
|
assert settings.configured is True
|
|
|
|
|
2021-01-17 13:03:41 +01:00
|
|
|
assert settings.PATH_URL == 'app_path'
|
|
|
|
|
2023-11-09 20:37:13 +01:00
|
|
|
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')
|
2021-01-17 13:03:41 +01:00
|
|
|
|
|
|
|
assert settings.ROOT_URLCONF == 'urls'
|
|
|
|
|
2022-09-18 19:00:39 +02:00
|
|
|
def test_config_panel_settings(self):
|
|
|
|
# config_panel.toml settings, set via tests.conftest.pytest_configure():
|
2023-11-26 13:06:24 +01:00
|
|
|
assert settings.DEBUG_ENABLED == '0' and settings.DEBUG is False
|
2022-09-18 19:00:39 +02:00
|
|
|
assert settings.LOG_LEVEL == 'INFO'
|
|
|
|
assert settings.ADMIN_EMAIL == 'foo-bar@test.tld'
|
|
|
|
assert settings.DEFAULT_FROM_EMAIL == 'django_app@test.tld'
|
|
|
|
|
2021-01-17 13:03:41 +01:00
|
|
|
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/'
|
2022-04-15 16:59:30 +02:00
|
|
|
# assert reverse(
|
|
|
|
# 'serve_media_app:serve-media', kwargs={'user_token': 'token', 'path': 'foo/bar/'}
|
|
|
|
# ) == ('/app_path/media/token/foo/bar/')
|
2021-01-17 13:03:41 +01:00
|
|
|
|
|
|
|
def test_auth(self):
|
2022-09-18 19:00:39 +02:00
|
|
|
assert settings.PATH_URL == 'app_path'
|
2022-10-03 19:44:36 +02:00
|
|
|
assert reverse('admin:index') == '/app_path/'
|
2022-09-18 19:00:39 +02:00
|
|
|
|
2021-10-10 14:35:06 +02:00
|
|
|
# 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/',
|
2022-04-15 16:59:30 +02:00
|
|
|
fetch_redirect_response=False,
|
2021-10-10 14:35:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
response = self.client.get('/app_path/', secure=True)
|
|
|
|
self.assertRedirects(
|
2022-04-15 16:59:30 +02:00
|
|
|
response, expected_url='/app_path/login/?next=/app_path/', fetch_redirect_response=False
|
2021-10-10 14:35:06 +02:00
|
|
|
)
|
2021-01-17 13:03:41 +01:00
|
|
|
|
|
|
|
def test_create_unknown_user(self):
|
|
|
|
assert User.objects.count() == 0
|
|
|
|
|
|
|
|
self.client.cookies['SSOwAuthUser'] = 'test'
|
|
|
|
|
2023-11-09 20:37:13 +01:00
|
|
|
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,
|
|
|
|
)
|
2021-01-17 13:03:41 +01:00
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2022-09-18 19:00:39 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2021-01-17 13:03:41 +01:00
|
|
|
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>',
|
|
|
|
),
|
|
|
|
)
|
2023-11-09 20:37:13 +01:00
|
|
|
assert_html_response_snapshot(response, query_selector='#container', validate=False)
|
2021-01-17 13:03:41 +01:00
|
|
|
|
|
|
|
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',
|
2022-09-18 19:00:39 +02:00
|
|
|
secure=True,
|
2021-01-17 13:03:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2022-09-18 19:00:39 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2021-01-17 13:03:41 +01:00
|
|
|
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',
|
2022-09-18 19:00:39 +02:00
|
|
|
secure=True,
|
2021-01-17 13:03:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2022-09-18 19:00:39 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2021-01-17 13:03:41 +01:00
|
|
|
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',
|
2022-04-15 16:59:30 +02:00
|
|
|
HTTP_AUTHORIZATION=generate_basic_auth(
|
2022-09-18 19:00:39 +02:00
|
|
|
username='foobar', # <<< wrong user name
|
|
|
|
password='test123',
|
|
|
|
),
|
|
|
|
secure=True,
|
2021-01-17 13:03:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2022-09-18 19:00:39 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2021-01-17 13:03:41 +01:00
|
|
|
assert user.is_superuser is False
|
|
|
|
|
|
|
|
assert AccessLog.objects.count() == 1
|
|
|
|
|
|
|
|
assert response.status_code == 403 # Forbidden
|