2022-10-04 10:20:28 +02:00
|
|
|
from axes.models import AccessLog
|
2022-07-12 18:39:59 +02:00
|
|
|
from bx_django_utils.test_utils.html_assertion import (
|
|
|
|
HtmlAssertionMixin,
|
|
|
|
assert_html_response_snapshot,
|
|
|
|
)
|
2022-07-11 20:34:24 +02:00
|
|
|
from django.conf import LazySettings, settings
|
|
|
|
from django.contrib.auth.models import User
|
2022-07-12 18:39:59 +02:00
|
|
|
from django.http import FileResponse, HttpResponse
|
2022-07-11 20:34:24 +02:00
|
|
|
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
|
|
|
|
|
2022-07-12 18:39:59 +02:00
|
|
|
from findmydevice import __version__
|
2022-07-11 20:34:24 +02: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 isinstance(settings, LazySettings)
|
|
|
|
assert settings.configured is True
|
|
|
|
|
2022-08-15 16:56:45 +02:00
|
|
|
assert 'django_yunohost_integration' in settings.INSTALLED_APPS
|
|
|
|
assert 'findmydevice.apps.FindMyDeviceConfig' in settings.INSTALLED_APPS
|
|
|
|
|
2022-07-11 20:34:24 +02:00
|
|
|
assert settings.PATH_URL == 'app_path'
|
|
|
|
|
2022-10-04 10:20:28 +02:00
|
|
|
assert str(settings.FINALPATH).endswith('/local_test/opt_yunohost')
|
|
|
|
assert str(settings.PUBLIC_PATH).endswith('/local_test/var_www')
|
|
|
|
assert str(settings.LOG_FILE).endswith('/local_test/var_log_django-fmd.log')
|
2022-07-11 20:34:24 +02:00
|
|
|
|
2022-10-04 10:20:28 +02:00
|
|
|
assert settings.ROOT_URLCONF == 'urls'
|
2022-08-15 16:56:45 +02:00
|
|
|
|
2022-10-04 10:20:28 +02:00
|
|
|
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'
|
2022-08-15 16:56:45 +02:00
|
|
|
|
2022-10-04 10:20:28 +02:00
|
|
|
def test_auth(self):
|
|
|
|
assert settings.PATH_URL == 'app_path'
|
2022-07-11 20:34:24 +02:00
|
|
|
assert reverse('admin:index') == '/app_path/admin/'
|
|
|
|
|
|
|
|
# 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)
|
2022-07-12 18:39:59 +02:00
|
|
|
self.assertTemplateUsed(response, 'fmd/login_info.html')
|
|
|
|
self.assert_html_parts(
|
|
|
|
response,
|
|
|
|
parts=(
|
|
|
|
'<title>Log in | Find My Device</title>',
|
|
|
|
'<p class="errornote">To find your device, you must be logged in.</p>',
|
|
|
|
'<a href="/yunohost/sso/">Log in</a>',
|
|
|
|
),
|
2022-07-11 20:34:24 +02:00
|
|
|
)
|
2022-07-12 18:39:59 +02:00
|
|
|
assert_html_response_snapshot(response, query_selector=None, validate=False)
|
2022-07-11 20:34:24 +02:00
|
|
|
|
2022-07-12 18:39:59 +02:00
|
|
|
def test_web_page_as_sso_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',
|
|
|
|
secure=True,
|
2022-07-11 20:34:24 +02:00
|
|
|
)
|
2022-07-12 18:39:59 +02:00
|
|
|
assert isinstance(response, FileResponse)
|
2022-07-11 20:34:24 +02:00
|
|
|
|
2022-07-12 18:39:59 +02:00
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
|
|
|
|
response2 = HttpResponse(response.getvalue())
|
|
|
|
self.assert_html_parts(
|
|
|
|
response2,
|
|
|
|
parts=(
|
|
|
|
'<title>FMD</title>',
|
|
|
|
'<h2>Find My Device</h2>',
|
2022-08-10 18:22:25 +02:00
|
|
|
'<link rel="stylesheet" href="./static/fmd_externals/style.css">',
|
|
|
|
'<script src="./static/fmd_externals/logic.js"></script>',
|
2022-07-12 18:39:59 +02:00
|
|
|
),
|
2022-07-11 20:34:24 +02:00
|
|
|
)
|
2022-07-12 18:39:59 +02:00
|
|
|
assert_html_response_snapshot(response2, query_selector=None, validate=False)
|
2022-07-11 20:34:24 +02:00
|
|
|
|
|
|
|
def test_create_unknown_user(self):
|
|
|
|
assert User.objects.count() == 0
|
|
|
|
|
|
|
|
self.client.cookies['SSOwAuthUser'] = 'test'
|
|
|
|
|
|
|
|
response = self.client.get(
|
|
|
|
path='/app_path/admin/',
|
|
|
|
HTTP_REMOTE_USER='test',
|
|
|
|
HTTP_AUTH_USER='test',
|
|
|
|
HTTP_AUTHORIZATION='basic dGVzdDp0ZXN0MTIz',
|
2022-10-04 10:20:28 +02:00
|
|
|
secure=True,
|
2022-07-11 20:34:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2022-10-04 10:20:28 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2022-07-11 20:34:24 +02:00
|
|
|
assert user.is_superuser is False
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
self.assert_html_parts(
|
|
|
|
response,
|
|
|
|
parts=(
|
2022-07-12 18:39:59 +02:00
|
|
|
'<title>Site administration | Find My Device admin</title>',
|
2022-07-11 20:34:24 +02:00
|
|
|
'<strong>test</strong>',
|
2022-07-12 18:39:59 +02:00
|
|
|
f'<a href="/app_path/admin/">Django Find My Device v{__version__}</a>',
|
2022-07-11 20:34:24 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_wrong_auth_user(self):
|
|
|
|
assert User.objects.count() == 0
|
2022-10-04 10:20:28 +02:00
|
|
|
assert AccessLog.objects.count() == 0
|
2022-07-11 20:34:24 +02:00
|
|
|
|
|
|
|
self.client.cookies['SSOwAuthUser'] = 'test'
|
|
|
|
|
|
|
|
response = self.client.get(
|
|
|
|
path='/app_path/admin/',
|
|
|
|
HTTP_REMOTE_USER='test',
|
|
|
|
HTTP_AUTH_USER='foobar', # <<< wrong user name
|
|
|
|
HTTP_AUTHORIZATION='basic dGVzdDp0ZXN0MTIz',
|
2022-10-04 10:20:28 +02:00
|
|
|
secure=True,
|
2022-07-11 20:34:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2022-10-04 10:20:28 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2022-07-11 20:34:24 +02:00
|
|
|
assert user.is_superuser is False
|
|
|
|
|
2022-10-04 10:20:28 +02:00
|
|
|
assert AccessLog.objects.count() == 1
|
|
|
|
|
2022-07-11 20:34:24 +02:00
|
|
|
assert response.status_code == 403 # Forbidden
|
|
|
|
|
|
|
|
def test_wrong_cookie(self):
|
|
|
|
assert User.objects.count() == 0
|
2022-10-04 10:20:28 +02:00
|
|
|
assert AccessLog.objects.count() == 0
|
2022-07-11 20:34:24 +02:00
|
|
|
|
|
|
|
self.client.cookies['SSOwAuthUser'] = 'foobar' # <<< wrong user name
|
|
|
|
|
|
|
|
response = self.client.get(
|
|
|
|
path='/app_path/admin/',
|
|
|
|
HTTP_REMOTE_USER='test',
|
|
|
|
HTTP_AUTH_USER='test',
|
|
|
|
HTTP_AUTHORIZATION='basic dGVzdDp0ZXN0MTIz',
|
2022-10-04 10:20:28 +02:00
|
|
|
secure=True,
|
2022-07-11 20:34:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2022-10-04 10:20:28 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2022-07-11 20:34:24 +02:00
|
|
|
assert user.is_superuser is False
|
|
|
|
|
2022-10-04 10:20:28 +02:00
|
|
|
assert AccessLog.objects.count() == 1
|
|
|
|
|
2022-07-11 20:34:24 +02:00
|
|
|
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/admin/',
|
|
|
|
HTTP_REMOTE_USER='test',
|
|
|
|
HTTP_AUTH_USER='test',
|
|
|
|
HTTP_AUTHORIZATION=generate_basic_auth(
|
2022-10-04 10:20:28 +02:00
|
|
|
username='foobar', # <<< wrong user name
|
|
|
|
password='test123',
|
|
|
|
),
|
|
|
|
secure=True,
|
2022-07-11 20:34:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert User.objects.count() == 1
|
|
|
|
user = User.objects.first()
|
|
|
|
assert user.username == 'test'
|
|
|
|
assert user.is_active is True
|
2022-10-04 10:20:28 +02:00
|
|
|
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
|
2022-07-11 20:34:24 +02:00
|
|
|
assert user.is_superuser is False
|
|
|
|
|
2022-10-04 10:20:28 +02:00
|
|
|
assert AccessLog.objects.count() == 1
|
|
|
|
|
2022-07-11 20:34:24 +02:00
|
|
|
assert response.status_code == 403 # Forbidden
|