Skip to content

Commit

Permalink
[FIX] Offer's slug doesn't support unicode (django-oscar#4172)
Browse files Browse the repository at this point in the history
* fix 🔧 add unicode support in slug urls

* feat ⭐ add tests to check urls with unicode characters
  • Loading branch information
samar-hassan authored Sep 26, 2023
1 parent 66c771b commit 1c25682
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/oscar/apps/catalogue/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def get_urls(self):
self.category_view.as_view(),
name="category",
),
path("ranges/<slug:slug>/", self.range_view.as_view(), name="range"),
re_path(
r"^ranges/(?P<slug>[\w-]+)/$", self.range_view.as_view(), name="range"
),
]
return self.post_process_urls(urls)

Expand Down
6 changes: 3 additions & 3 deletions src/oscar/apps/dashboard/catalogue/apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.urls import path
from django.urls import path, re_path
from django.utils.translation import gettext_lazy as _

from oscar.core.application import OscarDashboardConfig
Expand Down Expand Up @@ -108,8 +108,8 @@ def get_urls(self):
self.product_create_redirect_view.as_view(),
name="catalogue-product-create",
),
path(
"products/create/<slug:product_class_slug>/",
re_path(
r"^products/create/(?P<product_class_slug>[\w-]+)/$",
self.product_createupdate_view.as_view(),
name="catalogue-product-create",
),
Expand Down
6 changes: 4 additions & 2 deletions src/oscar/apps/dashboard/communications/apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.urls import path
from django.urls import path, re_path
from django.utils.translation import gettext_lazy as _

from oscar.core.application import OscarDashboardConfig
Expand All @@ -22,6 +22,8 @@ def ready(self):
def get_urls(self):
urls = [
path("", self.list_view.as_view(), name="comms-list"),
path("<slug:slug>/", self.update_view.as_view(), name="comms-update"),
re_path(
r"^(?P<slug>[\w-]+)/$", self.update_view.as_view(), name="comms-update"
),
]
return self.post_process_urls(urls)
4 changes: 2 additions & 2 deletions src/oscar/apps/offer/apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.urls import path
from django.urls import path, re_path
from django.utils.translation import gettext_lazy as _

from oscar.core.application import OscarConfig
Expand All @@ -22,6 +22,6 @@ def ready(self):
def get_urls(self):
urls = [
path("", self.list_view.as_view(), name="list"),
path("<slug:slug>/", self.detail_view.as_view(), name="detail"),
re_path(r"^(?P<slug>[\w-]+)/$", self.detail_view.as_view(), name="detail"),
]
return self.post_process_urls(urls)
15 changes: 15 additions & 0 deletions tests/functional/dashboard/test_catalogue.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from http import client as http_client

from django.urls import reverse
from django.test import TestCase

from oscar.core.compat import get_user_model
from oscar.core.loading import get_class, get_model
from oscar.test.factories import (
CategoryFactory,
Expand All @@ -12,6 +14,7 @@
)
from oscar.test.testcases import WebTestCase, add_permissions

User = get_user_model()
Product = get_model("catalogue", "Product")
ProductClass = get_model("catalogue", "ProductClass")
ProductCategory = get_model("catalogue", "ProductCategory")
Expand Down Expand Up @@ -317,3 +320,15 @@ def test_can_create_a_child_product(self):

def test_cant_create_child_product_for_invalid_parents(self):
pass


class TestProductCreatePageWithUnicodeSlug(TestCase):
def setUp(self):
self.slug = "Ûul-wįth-weird-chars"
ProductClass.objects.create(name="Book", slug=self.slug)
self.user = User.objects.create(is_staff=True)
self.client.force_login(self.user)

def test_url_with_unicode_characters(self):
response = self.client.get(f"/dashboard/catalogue/products/create/{self.slug}/")
self.assertEqual(200, response.status_code)
19 changes: 19 additions & 0 deletions tests/functional/dashboard/test_communication.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from django.core import mail
from django.urls import reverse
from django.test import TestCase

from oscar.core.compat import get_user_model
from oscar.core.loading import get_model
from oscar.test.factories import UserFactory
from oscar.test.testcases import WebTestCase

CommunicationEventType = get_model("communication", "CommunicationEventType")
User = get_user_model()


class TestAnAdmin(WebTestCase):
Expand Down Expand Up @@ -36,3 +39,19 @@ def test_can_send_a_preview_email(self):
form.submit("send_preview")

assert len(mail.outbox) == 1


class TestCommsUpdatePageWithUnicodeSlug(TestCase):
def setUp(self):
self.slug = "Ûul-wįth-weird-chars"
self.commtype = CommunicationEventType.objects.create(
name="comm-event",
category=CommunicationEventType.USER_RELATED,
code="Ûul-wįth-weird-chars",
)
self.user = User.objects.create(is_staff=True)
self.client.force_login(self.user)

def test_url_with_unicode_characters(self):
response = self.client.get(f"/dashboard/comms/{self.slug}/")
self.assertEqual(200, response.status_code)
30 changes: 30 additions & 0 deletions tests/functional/test_offer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
from django.test import TestCase

from oscar.test.factories.offer import (
BenefitFactory,
ConditionalOfferFactory,
ConditionFactory,
RangeFactory,
)
from oscar.test.testcases import WebTestCase


class TestTheOfferListPage(WebTestCase):
def test_exists(self):
response = self.app.get("/offers/")
self.assertEqual(200, response.status_code)


class TestOfferDetailsPageWithUnicodeSlug(TestCase):
def setUp(self):
self.slug = "Ûul-wįth-weird-chars"
self.offer = ConditionalOfferFactory(
condition=ConditionFactory(), benefit=BenefitFactory(), slug=self.slug
)

def test_url_with_unicode_characters(self):
response = self.client.get(f"/offers/{self.slug}/")
self.assertEqual(200, response.status_code)


class TestRangeDetailsPageWithUnicodeSlug(TestCase):
def setUp(self):
self.slug = "Ûul-wįth-weird-chars"
self.range = RangeFactory(slug=self.slug, is_public=True)

def test_url_with_unicode_characters(self):
response = self.client.get(f"/catalogue/ranges/{self.slug}/")
self.assertEqual(200, response.status_code)

0 comments on commit 1c25682

Please sign in to comment.