Skip to content

Commit

Permalink
Update caching and anonymous user handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyface committed Mar 20, 2020
1 parent 27a25fe commit 5ba6f35
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 19 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="django-termsandconditions",
version="2.0.3",
version="2.0.4",
url="https://github.com/cyface/django-termsandconditions",
license="BSD",
description="django-termsandconditions is a Django app that enables users to accept terms and conditions of a site.",
Expand Down
2 changes: 1 addition & 1 deletion termsandconditions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, *args, **kwargs):
else:
self.terms = terms_list

super(UserTermsAndConditionsModelForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)


class EmailTermsForm(forms.Form):
Expand Down
3 changes: 3 additions & 0 deletions termsandconditions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ def get_active_terms_list():
def get_active_terms_not_agreed_to(user):
"""Checks to see if a specified user has agreed to all the latest terms and conditions"""

if not user.is_authenticated:
return TermsAndConditions.get_active_terms_list()

if TERMS_EXCLUDE_USERS_WITH_PERM is not None:
if user.has_perm(TERMS_EXCLUDE_USERS_WITH_PERM) and not user.is_superuser:
# Django's has_perm() returns True if is_superuser, we don't want that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1>{{ terms.name|safe }} {{ terms.version_number|safe }}</h1>
<h1>No terms defined.</h1>
{% endif %}
{% empty %}
<h1>No terms defined.</h1>
<h1>No terms for you to accept.</h1>
{% endfor %}
</section>
{% endblock %}
62 changes: 56 additions & 6 deletions termsandconditions/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unit Tests for the termsandconditions module"""

# pylint: disable=R0904, C0103
import time
from importlib import import_module
import logging

Expand Down Expand Up @@ -309,20 +310,69 @@ def test_no_middleware(self):
admin_response = self.client.get("/admin", follow=True)
self.assertContains(admin_response, "administration")

def test_terms_view(self):
"""Test Accessing the View Terms and Conditions Functions"""
def test_anonymous_terms_view(self):
"""Test Accessing the View Terms and Conditions for Anonymous User"""
active_terms = TermsAndConditions.get_active_terms_list()

LOGGER.debug("Test /terms/")
LOGGER.debug("Test /terms/ with anon")
root_response = self.client.get("/terms/", follow=True)
for terms in active_terms:
self.assertContains(root_response, terms.name)
self.assertContains(root_response, terms.text)
self.assertContains(root_response, "Terms and Conditions")

LOGGER.debug("Test /terms/view/site-terms")
slug_response = self.client.get("/terms/view/site-terms", follow=True)
LOGGER.debug("Test /terms/view/site-terms with anon")
slug_response = self.client.get(self.terms2.get_absolute_url(), follow=True)
self.assertContains(slug_response, self.terms2.name)
self.assertContains(slug_response, self.terms2.text)
self.assertContains(slug_response, "Terms and Conditions")

LOGGER.debug("Test /terms/view/site-terms/1.5")
LOGGER.debug("Test /terms/view/contributor-terms/1.5 with anon")
version_response = self.client.get(self.terms3.get_absolute_url(), follow=True)
self.assertContains(version_response, self.terms3.name)
self.assertContains(version_response, self.terms3.text)

def test_user_terms_view(self):
"""Test Accessing the View Terms and Conditions Page for Logged In User"""
login_response = self.client.login(username="user1", password="user1password")
self.assertTrue(login_response)

user1_not_agreed_terms = TermsAndConditions.get_active_terms_not_agreed_to(self.user1)
self.assertEqual(len(user1_not_agreed_terms), 2)

LOGGER.debug("Test /terms/ with user1")
root_response = self.client.get("/terms/", follow=True)
for terms in user1_not_agreed_terms:
self.assertContains(root_response, terms.text)
self.assertContains(root_response, "Terms and Conditions")
self.assertContains(root_response, "Sign Out")

# Accept terms and check again
UserTermsAndConditions.objects.create(user=self.user1, terms=self.terms3)
user1_not_agreed_terms = TermsAndConditions.get_active_terms_not_agreed_to(self.user1)
self.assertEqual(len(user1_not_agreed_terms), 1)
LOGGER.debug("Test /terms/ with user1 after accept")
post_accept_response = self.client.get("/terms/", follow=True)
for terms in user1_not_agreed_terms:
self.assertContains(post_accept_response, terms.text)
self.assertNotContains(post_accept_response, self.terms3.name)
self.assertContains(post_accept_response, "Terms and Conditions")
self.assertContains(post_accept_response, "Sign Out")

# Check by slug and version while logged in
LOGGER.debug("Test /terms/view/site-terms as user1")
slug_response = self.client.get(self.terms2.get_absolute_url(), follow=True)
self.assertContains(slug_response, self.terms2.name)
self.assertContains(slug_response, self.terms2.text)
self.assertContains(slug_response, "Terms and Conditions")
self.assertContains(slug_response, "Sign Out")

LOGGER.debug("Test /terms/view/site-terms/1.5 as user1")
version_response = self.client.get(self.terms3.get_absolute_url(), follow=True)
self.assertContains(version_response, self.terms3.name)
self.assertContains(version_response, self.terms3.text)
self.assertContains(version_response, "Terms and Conditions")
self.assertContains(slug_response, "Sign Out")

def test_user_pipeline(self):
"""Test the case of a user being partially created via the django-socialauth pipeline"""
Expand Down
11 changes: 6 additions & 5 deletions termsandconditions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from django.contrib import admin
from django.urls import path, register_converter
from django.views.decorators.cache import never_cache

from .views import TermsView, AcceptTermsView, EmailTermsView
from .models import DEFAULT_TERMS_SLUG
Expand All @@ -31,23 +32,23 @@ def to_url(self, value):

urlpatterns = (
# View Default Terms
path("", TermsView.as_view(), name="tc_view_page"),
path("", never_cache(TermsView.as_view()), name="tc_view_page"),
# View Specific Active Terms
path(
"view/<slug:slug>/",
TermsView.as_view(),
never_cache(TermsView.as_view()),
name="tc_view_specific_page",
),
# View Specific Version of Terms
path(
"view/<slug:slug>/<termsversion:version>/",
TermsView.as_view(),
never_cache(TermsView.as_view()),
name="tc_view_specific_version_page",
),
# Print Specific Version of Terms
path(
"print/<slug:slug>/<termsversion:version>/",
TermsView.as_view(template_name="termsandconditions/tc_print_terms.html"),
never_cache(TermsView.as_view(template_name="termsandconditions/tc_print_terms.html")),
name="tc_print_page",
),
# Accept Terms
Expand All @@ -69,7 +70,7 @@ def to_url(self, value):
# Email Specific Terms Version
path(
"email/<slug:slug>/<termsversion:version>/",
EmailTermsView.as_view(),
never_cache(EmailTermsView.as_view()),
name="tc_specific_version_page",
),
)
10 changes: 5 additions & 5 deletions termsandconditions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TermsView(DetailView, GetTermsViewMixin):

def get_context_data(self, **kwargs):
"""Pass additional context data"""
context = super(TermsView, self).get_context_data(**kwargs)
context = super().get_context_data(**kwargs)
context["terms_base_template"] = getattr(
settings, "TERMS_BASE_TEMPLATE", DEFAULT_TERMS_BASE_TEMPLATE
)
Expand All @@ -81,7 +81,7 @@ class AcceptTermsView(CreateView, GetTermsViewMixin):

def get_context_data(self, **kwargs):
"""Pass additional context data"""
context = super(AcceptTermsView, self).get_context_data(**kwargs)
context = super().get_context_data(**kwargs)
context["terms_base_template"] = getattr(
settings, "TERMS_BASE_TEMPLATE", DEFAULT_TERMS_BASE_TEMPLATE
)
Expand Down Expand Up @@ -151,7 +151,7 @@ class EmailTermsView(FormView, GetTermsViewMixin):

def get_context_data(self, **kwargs):
"""Pass additional context data"""
context = super(EmailTermsView, self).get_context_data(**kwargs)
context = super().get_context_data(**kwargs)
context["terms_base_template"] = getattr(
settings, "TERMS_BASE_TEMPLATE", DEFAULT_TERMS_BASE_TEMPLATE
)
Expand Down Expand Up @@ -197,10 +197,10 @@ def form_valid(self, form):

self.success_url = form.cleaned_data.get("returnTo", "/") or "/"

return super(EmailTermsView, self).form_valid(form)
return super().form_valid(form)

def form_invalid(self, form):
"""Override of CreateView method, logs invalid email form submissions."""
LOGGER.debug("Invalid Email Form Submitted")
messages.add_message(self.request, messages.ERROR, _("Invalid Email Address."))
return super(EmailTermsView, self).form_invalid(form)
return super().form_invalid(form)

0 comments on commit 5ba6f35

Please sign in to comment.