Skip to content

Commit

Permalink
Fix for multiple IP addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyface committed Mar 24, 2020
1 parent 0747971 commit c157e9a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
23 changes: 20 additions & 3 deletions termsandconditions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from .pipeline import user_accept_terms
from .templatetags.terms_tags import show_terms_if_not_agreed


LOGGER = logging.getLogger(name="termsandconditions")


Expand Down Expand Up @@ -248,6 +247,20 @@ def test_accept_store_ip_address(self):
self.assertEqual(user_terms.terms, self.terms2)
self.assertTrue(user_terms.ip_address)

def test_accept_store_ip_address_multiple(self):
"""Test storing IP address when it is a list"""
self.client.login(username="user1", password="user1password")
self.client.post(
"/terms/accept/",
{"terms": 2, "returnTo": "/secure/"},
follow=True,
REMOTE_ADDR="0.0.0.0, 1.1.1.1",
)
user_terms = UserTermsAndConditions.objects.all()[0]
self.assertEqual(user_terms.user, self.user1)
self.assertEqual(user_terms.terms, self.terms2)
self.assertTrue(user_terms.ip_address)

def test_accept_no_ip_address(self):
"""Test with IP address storage setting false"""
self.client.login(username="user1", password="user1password")
Expand Down Expand Up @@ -337,7 +350,9 @@ def test_user_terms_view(self):
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)
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")
Expand All @@ -349,7 +364,9 @@ def test_user_terms_view(self):

# 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)
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)
Expand Down
4 changes: 3 additions & 1 deletion termsandconditions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def post(self, request, *args, **kwargs):
ip_address = request.META.get(
getattr(settings, "TERMS_IP_HEADER_NAME", DEFAULT_TERMS_IP_HEADER_NAME)
)
if "," in ip_address:
ip_address = ip_address.split(",")[0].strip()
else:
ip_address = ""

Expand Down Expand Up @@ -216,4 +218,4 @@ class TermsActiveView(TermsView):
def get_object(self, queryset=None):
"""Override of DetailView method, queries for which T&C to return"""
LOGGER.debug("termsandconditions.views.AllTermsView.get_object")
return TermsAndConditions.get_active_terms_list()
return TermsAndConditions.get_active_terms_list()
4 changes: 3 additions & 1 deletion termsandconditions_demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,13 @@

# Custom Variables Below Here #######

LOGIN_REDIRECT_URL = "/"

# Terms & Conditions (termsandconditions) Settings #######
DEFAULT_TERMS_SLUG = "site-terms"
ACCEPT_TERMS_PATH = "/terms/accept/"
TERMS_EXCLUDE_URL_PREFIX_LIST = {"/admin", "/terms"}
TERMS_EXCLUDE_URL_LIST = {"/", "/termsrequired/", "/logout/", "/securetoo/"}
TERMS_EXCLUDE_URL_LIST = {"/", "/termsrequired/", "/accounts/logout/", "/securetoo/"}
TERMS_EXCLUDE_URL_CONTAINS_LIST = (
{}
) # Useful if you are using internationalization and your URLs could change per language
Expand Down

0 comments on commit c157e9a

Please sign in to comment.