Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#427 Fix error on index page when setting max expiry setting too high #429

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add max values to site settings
  • Loading branch information
TobySuch committed Dec 23, 2024
commit cf5bd3b7783ecd18761844c6665a648fbf9e3f4c
4 changes: 4 additions & 0 deletions shifter/shifter/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,15 @@
"default": 24 * 14, # 2 weeks
"label": "Default Expiry Offset (hours)",
"field_type": forms.IntegerField,
"min_value": 0,
"max_value": 2147483647,
},
"max_expiry_offset": {
"default": 24 * 365 * 5, # 5 years
"label": "Maximum Expiry Offset (hours)",
"field_type": forms.IntegerField,
"min_value": 0,
"max_value": 2147483647,
},
}

Expand Down
15 changes: 15 additions & 0 deletions shifter/shifter_site_settings/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import forms
from django.conf import settings
from django.core.validators import MaxValueValidator, MinValueValidator

from .models import SiteSetting

Expand All @@ -19,3 +20,17 @@ def __init__(self, *args, **kwargs):
self.fields[
f"setting_{setting.name}"
].help_text = setting_config["tooltip"]
if "min_value" in setting_config:
self.fields[f"setting_{setting.name}"].validators.append(
MinValueValidator(
setting_config["min_value"],
"Minimum value: %(limit_value)s",
)
)
if "max_value" in setting_config:
self.fields[f"setting_{setting.name}"].validators.append(
MaxValueValidator(
setting_config["max_value"],
"Maximum value: %(limit_value)s",
)
)
39 changes: 39 additions & 0 deletions shifter/shifter_site_settings/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,45 @@ def test_standard_user_cannot_access(self):
response = client.get(url)
self.assertEqual(response.status_code, 403)

# #427 - Setting max expiry date in hours to large number causes 500 error
# when accessing file upload page.
def test_max_expiry_limits(self):
# Test submitting the form changes the settings values
client = Client()
client.login(email=TEST_USER_EMAIL, password=TEST_USER_PASSWORD)
url = reverse("shifter_site_settings:site-settings")
response = client.get(url)
self.assertEqual(response.status_code, 200)

# Change setting values
old_setting_max_expiry_offset = SiteSetting.get_setting(
"max_expiry_offset"
)
setting_max_expiry_offset = "99999999999999999999999999999999999999"
response = client.post(
url,
{
"setting_max_file_size": "5120MB",
"setting_default_expiry_offset": "336",
"setting_max_expiry_offset": setting_max_expiry_offset,
},
)
self.assertEqual(response.status_code, 200)
# Check response contains error
self.assertContains(
response,
(
"Maximum value: "
f"{settings.SITE_SETTINGS["max_expiry_offset"]["max_value"]}"
),
)

# Check that the setting was not changed
self.assertEqual(
SiteSetting.get_setting("max_expiry_offset"),
old_setting_max_expiry_offset,
)


class SiteSettingsSiteInformationTest(TestCase):
def setUp(self):
Expand Down