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

make pinax-referrals tolerant to the ending slash in URL #81

Merged
merged 1 commit into from
Jun 7, 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
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"),
]
Loading