Skip to content

Commit

Permalink
make pinax-referrals tolerant to the ending slash in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Jun 7, 2024
1 parent 6ad4640 commit edde4a8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ You may need to do this if you use a custom user model and upgrade Django.

## Change Log

### 4.3.0 (2023-06-07)

* added tolerance to the ending slash in URL

### 4.2.0 (2023-01-12)

* Added referral model instance parameter to `generate_code` callback (`PINAX_REFERRALS_CODE_GENERATOR_CALLBACK` variable)
Expand Down
23 changes: 22 additions & 1 deletion pinax/referrals/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth import get_user_model
from django.test import TestCase

from pinax.referrals.models import Referral
from pinax.referrals.models import Referral, ReferralResponse


class Tests(TestCase):
Expand Down Expand Up @@ -74,3 +74,24 @@ def test_process_referral_authenticated_next_unsafe(self):
self.assertEqual(referral.responses.count(), 1)
referral_response = referral.responses.first()
self.assertEqual(referral_response.user, referred_user)

def test_process_referral_slash_both_variants(self):
referral = Referral.create(redirect_to="https://example.com/")
self.assertFalse(referral.responses.exists())
response = self.client.get(referral.url.rstrip("/"))
self.assertEqual(response.status_code, 302)
self.assertEqual(response["Location"], "https://example.com/")
self.assertEqual(referral.responses.count(), 1)
referral_response = referral.responses.first()
self.assertIsNone(referral_response.user)
self.assertEqual(response.cookies["pinax-referral"].value, f"{referral.code}:{referral_response.session_key}")

ReferralResponse.objects.all().delete()

response = self.client.get(referral.url.rstrip("/") + "/")
self.assertEqual(response.status_code, 302)
self.assertEqual(response["Location"], "https://example.com/")
self.assertEqual(referral.responses.count(), 1)
referral_response = referral.responses.first()
self.assertIsNone(referral_response.user)
self.assertEqual(response.cookies["pinax-referral"].value, f"{referral.code}:{referral_response.session_key}")
4 changes: 2 additions & 2 deletions pinax/referrals/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.urls import path
from django.urls import path, re_path

from .views import create_referral, process_referral

app_name = "pinax_referrals"

urlpatterns = [
path("", create_referral, name="create_referral"),
path("<str:code>", process_referral, name="process_referral")
re_path(r"^(?P<code>[\w-]+)/?$", process_referral, name="process_referral"),
]

0 comments on commit edde4a8

Please sign in to comment.