Skip to content

Commit 5b1a29f

Browse files
committed
feat: route_code로 url-safe한 character만 넣을 수 있도록 수정
1 parent 2e3611b commit 5b1a29f

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

app/cms/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dataclasses
44
import datetime
5+
import re
56
import typing
67

78
from core.models import BaseAbstractModel, BaseAbstractModelQuerySet
@@ -91,6 +92,10 @@ def route(self) -> str:
9192
return self.route_code
9293

9394
def clean(self) -> None:
95+
# route_code는 URL-Safe하도록 알파벳, 숫자, 언더바(_)로만 구성되어야 함
96+
if not re.match(r"^[a-zA-Z0-9_-]*$", self.route_code):
97+
raise ValidationError("route_code는 알파벳, 숫자, 언더바(_)로만 구성되어야 합니다.")
98+
9499
# Parent Sitemap과 Page가 같을 경우 ValidationError 발생
95100
if self.parent_sitemap_id and self.parent_sitemap_id == self.id:
96101
raise ValidationError("자기 자신을 부모로 설정할 수 없습니다.")

app/cms/test/site_route_calculation_test.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,36 @@ def create_sitemaps(data: dict[str, dict], parent: Sitemap = None) -> None:
8080
}
8181

8282

83+
@pytest.mark.parametrize(
84+
argnames=["route_code", "should_raise"],
85+
argvalues=[
86+
("valid_route", False),
87+
("", False),
88+
("/invalid_route", True), # 슬래시로 시작
89+
("valid_route_123", False),
90+
("valid-route", False),
91+
("valid_route_123!", True), # 특수문자 포함
92+
("valid_route_123@", True), # 특수문자 포함
93+
],
94+
)
95+
@pytest.mark.django_db
96+
def test_route_code_validation(route_code: str, should_raise: bool):
97+
# Given: Sitemap 객체 생성
98+
sitemap = Sitemap(
99+
route_code=route_code,
100+
name="Test Sitemap",
101+
page=Page.objects.create(title="Test Page", subtitle="Test Subtitle"),
102+
)
103+
104+
# When: Validation을 수행
105+
if should_raise:
106+
with pytest.raises(ValidationError) as excinfo:
107+
sitemap.clean()
108+
assert excinfo.value == "route_code는 알파벳, 숫자, 언더바(_)로만 구성되어야 합니다."
109+
else:
110+
sitemap.clean()
111+
112+
83113
@pytest.mark.django_db
84114
def test_clean_should_check_for_self_reference():
85115
# Given: Sitemap 객체 생성
@@ -95,7 +125,7 @@ def test_clean_should_check_for_self_reference():
95125
# Then: ValidationError가 발생해야 한다.
96126
with pytest.raises(ValidationError) as excinfo:
97127
sitemap.clean()
98-
assert str(excinfo.value) == "자기 자신을 부모로 설정할 수 없습니다."
128+
assert excinfo.value == "자기 자신을 부모로 설정할 수 없습니다."
99129

100130

101131
@pytest.mark.django_db
@@ -127,7 +157,7 @@ def test_clean_should_check_for_circular_reference():
127157
# Then: ValidationError가 발생해야 한다.
128158
with pytest.raises(ValidationError) as excinfo:
129159
root_sitemap.clean()
130-
assert str(excinfo.value) == "Parent Sitemap이 자식 Sitemap을 가리킬 수 없습니다."
160+
assert excinfo.value == "Parent Sitemap이 자식 Sitemap을 가리킬 수 없습니다."
131161

132162

133163
@pytest.mark.django_db
@@ -149,4 +179,4 @@ def test_clean_should_check_for_existing_route():
149179
# Then: ValidationError가 발생해야 한다.
150180
with pytest.raises(ValidationError) as excinfo:
151181
new_sitemap.clean()
152-
assert str(excinfo.value) == "`existing`라우트는 이미 존재하는 route입니다."
182+
assert excinfo.value == "`existing`라우트는 이미 존재하는 route입니다."

0 commit comments

Comments
 (0)