2021-02-28 10:56:42 +01:00
|
|
|
from axes.models import AccessLog
|
2021-09-15 19:16:35 +02:00
|
|
|
from bx_django_utils.test_utils.html_assertion import HtmlAssertionMixin
|
2020-12-28 18:52:29 +01:00
|
|
|
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
|
2021-09-15 19:16:35 +02:00
|
|
|
from django_yunohost_integration.test_utils import generate_basic_auth
|
|
|
|
from django_yunohost_integration.views import request_media_debug_view
|
2021-02-28 10:56:42 +01:00
|
|
|
|
2020-12-28 18:52:29 +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):
|
|
|
|
assert settings.PATH_URL == 'app_path'
|
|
|
|
|
2022-08-14 14:44:17 +02:00
|
|
|
# TODO: Switch to: assert str(settings.FINALPATH).endswith('/local_test/var_www')
|
|
|
|
assert str(settings.FINALPATH).endswith('/local_test/opt_yunohost')
|
|
|
|
|
2021-02-28 10:56:42 +01:00
|
|
|
assert str(settings.LOG_FILE).endswith('/local_test/var_log_django_example_ynh.log')
|
2020-12-28 18:52:29 +01:00
|
|
|
|
2020-12-29 11:35:39 +01:00
|
|
|
assert settings.ROOT_URLCONF == 'urls'
|
2020-12-28 18:52:29 +01:00
|
|
|
|
|
|
|
def test_urls(self):
|
|
|
|
assert reverse('admin:index') == '/app_path/'
|
2021-02-28 10:56:42 +01:00
|
|
|
|
2022-08-12 17:56:02 +02:00
|
|
|
# In example app, the debug view is in urls.py:
|
|
|
|
assert reverse(request_media_debug_view) == '/app_path/debug/'
|
2021-02-28 10:56:42 +01:00
|
|
|
|
2022-08-12 17:56:02 +02:00
|
|
|
def test_auth(self):
|
|
|
|
# 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,
|
2021-02-28 10:56:42 +01:00
|
|
|
)
|
2020-12-28 18:52:29 +01:00
|
|
|
|
2022-08-12 17:56:02 +02:00
|
|
|
response = self.client.get('/app_path/', secure=True)
|
|
|
|
self.assertRedirects(
|
|
|
|
response, expected_url='/app_path/login/?next=/app_path/', fetch_redirect_response=False
|
|
|
|
)
|
2020-12-28 18:52:29 +01:00
|
|
|
|
|
|
|
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',
|
2022-08-12 17:56:02 +02:00
|
|
|
secure=True,
|
2020-12-28 18:52:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2021-09-15 19:16:35 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2020-12-28 18:52:29 +01:00
|
|
|
assert user.is_superuser is False
|
|
|
|
|
|
|
|
self.assert_html_parts(
|
2021-02-28 10:56:42 +01:00
|
|
|
response,
|
|
|
|
parts=(
|
2022-08-12 17:56:02 +02:00
|
|
|
'<h1 id="site-name"><a href="/app_path/">Django administration</a></h1>',
|
2021-02-28 10:56:42 +01:00
|
|
|
'<strong>test</strong>',
|
|
|
|
),
|
2020-12-28 18:52:29 +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-08-12 17:56:02 +02:00
|
|
|
secure=True,
|
2020-12-28 18:52:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2021-09-15 19:16:35 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2020-12-28 18:52:29 +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-08-12 17:56:02 +02:00
|
|
|
secure=True,
|
2020-12-28 18:52:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2021-09-15 19:16:35 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2020-12-28 18:52:29 +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-08-12 17:56:02 +02:00
|
|
|
HTTP_AUTHORIZATION=generate_basic_auth(
|
|
|
|
username='foobar', # <<< wrong user name
|
|
|
|
password='test123',
|
|
|
|
),
|
|
|
|
secure=True,
|
2020-12-28 18:52:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2021-09-15 19:16:35 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2020-12-28 18:52:29 +01:00
|
|
|
assert user.is_superuser is False
|
|
|
|
|
|
|
|
assert AccessLog.objects.count() == 1
|
|
|
|
|
|
|
|
assert response.status_code == 403 # Forbidden
|