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

Middleware: Only warn when settings.DEBUG is True #84

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 14 additions & 6 deletions tos/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
Expand All @@ -21,9 +23,12 @@ def get_current_tos(self):
try:
return self.get(active=True)
except self.model.DoesNotExist:
raise NoActiveTermsOfService(
'Please create an active Terms-of-Service'
)
if settings.DEBUG:
warnings.warn("There is no active Terms-of-Service")
else:
raise NoActiveTermsOfService(
'Please create an active Terms-of-Service'
)


class TermsOfService(BaseModel):
Expand Down Expand Up @@ -60,9 +65,12 @@ def save(self, *args, **kwargs):
.exclude(id=self.id)\
.filter(active=True)\
.exists():
raise NoActiveTermsOfService(
'One of the terms of service must be marked active'
)
if settings.DEBUG:
warnings.warn("There is no active Terms-of-Service")
else:
raise NoActiveTermsOfService(
'One of the terms of service must be marked active'
)

super().save(*args, **kwargs)

Expand Down
38 changes: 37 additions & 1 deletion tos/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test import TestCase, override_settings

from tos.models import (
NoActiveTermsOfService,
Expand Down Expand Up @@ -104,3 +104,39 @@ def test_terms_of_service_manager(self):
def test_terms_of_service_manager_raises_error(self):

self.assertRaises(NoActiveTermsOfService, TermsOfService.objects.get_current_tos)


class TestNoActiveTOS(TestCase):
@classmethod
def setUpClass(cls):
# Use bulk_create to avoid calling the model's save() method
TermsOfService.objects.bulk_create([
TermsOfService(
content="The only TOS",
active=False,
)
])

@classmethod
def tearDownClass(cls):
TermsOfService.objects.all().delete()

@override_settings(DEBUG=True)
def test_model_save_raises_warning(self):
with self.assertWarns(Warning):
TermsOfService.objects.first().save()

@override_settings(DEBUG=True)
def test_get_current_tos_raises_warning(self):
with self.assertWarns(Warning):
TermsOfService.objects.get_current_tos()

@override_settings(DEBUG=False)
def test_model_save_raises_exception(self):
with self.assertRaises(NoActiveTermsOfService):
TermsOfService.objects.first().save()

@override_settings(DEBUG=False)
def test_get_current_tos_raises_exception(self):
with self.assertRaises(NoActiveTermsOfService):
TermsOfService.objects.get_current_tos()
Loading