|
9 | 9 | HTTP_403_FORBIDDEN, |
10 | 10 | ) |
11 | 11 | from shop.conftest import FAR_FUTURE, FAR_PAST |
12 | | -from shop.product.models import Category, CategoryGroup, Product, Tag |
| 12 | +from shop.product.models import Category, CategoryGroup, OptionGroup, Product, Tag |
13 | 13 | from shop.test.helpers import CategoryGroupsAdminApi, OptionGroupsAdminApi, ProductsAdminApi, TagsAdminApi |
14 | 14 |
|
15 | 15 |
|
@@ -95,6 +95,27 @@ def test_admin_product_partial_update_validates_orderable_before_visible_end(api |
95 | 95 | assert "orderable_ends_at" in str(response.json()) |
96 | 96 |
|
97 | 97 |
|
| 98 | +@pytest.mark.django_db |
| 99 | +def test_admin_product_partial_update_rejects_inverted_visible_window(api_client, product): |
| 100 | + # visible_starts_at(2100) > visible_ends_at(fixture default FAR_FUTURE=2099) → 400. |
| 101 | + response = ProductsAdminApi(http_client=api_client).update( |
| 102 | + product.id, {"visible_starts_at": datetime(2100, 1, 1, tzinfo=timezone.utc).isoformat()} |
| 103 | + ) |
| 104 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 105 | + assert "visible_starts_at" in str(response.json()) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.django_db |
| 109 | +def test_admin_product_partial_update_rejects_inverted_orderable_window(api_client, product): |
| 110 | + # orderable_starts_at(2098) > orderable_ends_at(fixture default FAR_FUTURE=2099) 인 케이스를 만들기 위해 |
| 111 | + # ends_at 을 starts_at 보다 앞으로 patch — orderable_ends_at(2010) < orderable_starts_at(FAR_PAST=2020). |
| 112 | + response = ProductsAdminApi(http_client=api_client).update( |
| 113 | + product.id, {"orderable_ends_at": datetime(2010, 1, 1, tzinfo=timezone.utc).isoformat()} |
| 114 | + ) |
| 115 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 116 | + assert "orderable_starts_at" in str(response.json()) |
| 117 | + |
| 118 | + |
98 | 119 | @pytest.mark.django_db |
99 | 120 | def test_admin_product_partial_update_merged_uses_instance_value_for_missing_field(api_client, product): |
100 | 121 | # patch 에 visible_starts_at 만 보내고 orderable_* 미포함 → merged 가 instance 값 fallback 사용 → 성공. |
@@ -143,6 +164,183 @@ def test_admin_option_group_create_rejects_custom_response_without_pattern(api_c |
143 | 164 | assert "custom_response_pattern" in str(response.json()) |
144 | 165 |
|
145 | 166 |
|
| 167 | +@pytest.mark.django_db |
| 168 | +def test_admin_option_group_create_rejects_orderable_starts_before_product_starts(api_client, product): |
| 169 | + # product.orderable_starts_at(FAR_PAST=2020) 보다 앞 (2019) → 거절. |
| 170 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 171 | + { |
| 172 | + "product": str(product.id), |
| 173 | + "name_ko": "얼리버드", |
| 174 | + "name_en": "Earlybird", |
| 175 | + "orderable_starts_at": datetime(2019, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 176 | + } |
| 177 | + ) |
| 178 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 179 | + assert "orderable_starts_at" in str(response.json()) |
| 180 | + |
| 181 | + |
| 182 | +@pytest.mark.django_db |
| 183 | +def test_admin_option_group_create_rejects_orderable_ends_after_product_ends(api_client, product): |
| 184 | + # product.orderable_ends_at(FAR_FUTURE=2099) 보다 뒤 (2100) → 거절. |
| 185 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 186 | + { |
| 187 | + "product": str(product.id), |
| 188 | + "name_ko": "후반", |
| 189 | + "name_en": "Late", |
| 190 | + "orderable_ends_at": datetime(2100, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 191 | + } |
| 192 | + ) |
| 193 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 194 | + assert "orderable_ends_at" in str(response.json()) |
| 195 | + |
| 196 | + |
| 197 | +@pytest.mark.parametrize("kind", ["visible", "orderable"]) |
| 198 | +@pytest.mark.django_db |
| 199 | +def test_admin_option_group_create_rejects_inverted_window(api_client, product, kind): |
| 200 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 201 | + { |
| 202 | + "product": str(product.id), |
| 203 | + "name_ko": "옵션", |
| 204 | + "name_en": "Opt", |
| 205 | + f"{kind}_starts_at": datetime(2050, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 206 | + f"{kind}_ends_at": datetime(2030, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 207 | + } |
| 208 | + ) |
| 209 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 210 | + assert f"{kind}_starts_at" in str(response.json()) |
| 211 | + |
| 212 | + |
| 213 | +@pytest.mark.parametrize("kind", ["visible", "orderable"]) |
| 214 | +@pytest.mark.django_db |
| 215 | +def test_admin_option_group_create_rejects_starts_before_product_starts(api_client, product, kind): |
| 216 | + # group_starts_at < product_starts_at (FAR_PAST=2020) → 거절. visible / orderable 동일 분기. |
| 217 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 218 | + { |
| 219 | + "product": str(product.id), |
| 220 | + "name_ko": "옵션", |
| 221 | + "name_en": "Opt", |
| 222 | + f"{kind}_starts_at": datetime(2019, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 223 | + } |
| 224 | + ) |
| 225 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 226 | + assert f"{kind}_starts_at" in str(response.json()) |
| 227 | + |
| 228 | + |
| 229 | +@pytest.mark.parametrize("kind", ["visible", "orderable"]) |
| 230 | +@pytest.mark.django_db |
| 231 | +def test_admin_option_group_create_rejects_ends_after_product_ends(api_client, product, kind): |
| 232 | + # group_ends_at > product_ends_at (FAR_FUTURE=2099) → 거절. |
| 233 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 234 | + { |
| 235 | + "product": str(product.id), |
| 236 | + "name_ko": "옵션", |
| 237 | + "name_en": "Opt", |
| 238 | + f"{kind}_ends_at": datetime(2100, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 239 | + } |
| 240 | + ) |
| 241 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 242 | + assert f"{kind}_ends_at" in str(response.json()) |
| 243 | + |
| 244 | + |
| 245 | +@pytest.mark.django_db |
| 246 | +def test_admin_option_group_create_allows_period_within_product_window(api_client, product): |
| 247 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 248 | + { |
| 249 | + "product": str(product.id), |
| 250 | + "name_ko": "얼리버드", |
| 251 | + "name_en": "Earlybird", |
| 252 | + "orderable_starts_at": datetime(2030, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 253 | + "orderable_ends_at": datetime(2031, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 254 | + } |
| 255 | + ) |
| 256 | + assert response.status_code == HTTP_201_CREATED |
| 257 | + |
| 258 | + |
| 259 | +@pytest.mark.parametrize( |
| 260 | + "window_field", |
| 261 | + ["visible_starts_at", "visible_ends_at", "orderable_starts_at", "orderable_ends_at"], |
| 262 | +) |
| 263 | +@pytest.mark.django_db |
| 264 | +def test_admin_option_group_create_rejects_required_group_with_explicit_window(api_client, product, window_field): |
| 265 | + # min_quantity_per_product >= 1 인 필수 그룹은 visible/orderable starts_at/ends_at 을 별도 지정할 수 없음 — |
| 266 | + # 그룹 윈도우가 상품과 어긋나면 필수 옵션이 비어 상품을 살 수 없는 죽은 구간이 생긴다. |
| 267 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 268 | + { |
| 269 | + "product": str(product.id), |
| 270 | + "name_ko": "필수옵션", |
| 271 | + "name_en": "Required", |
| 272 | + "min_quantity_per_product": 1, |
| 273 | + window_field: datetime(2030, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 274 | + } |
| 275 | + ) |
| 276 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 277 | + assert window_field in str(response.json()) |
| 278 | + |
| 279 | + |
| 280 | +@pytest.mark.parametrize( |
| 281 | + "window_field", |
| 282 | + ["visible_starts_at", "visible_ends_at", "orderable_starts_at", "orderable_ends_at"], |
| 283 | +) |
| 284 | +@pytest.mark.django_db |
| 285 | +def test_admin_option_group_partial_update_rejects_setting_min_quantity_when_window_already_set( |
| 286 | + api_client, product, window_field |
| 287 | +): |
| 288 | + # 역방향 — 윈도우가 이미 설정된 그룹을 min_quantity_per_product>=1 로 patch → merged 가 instance 윈도우를 사용해 거절. |
| 289 | + group = OptionGroup.objects.create( |
| 290 | + product=product, name="기간옵션", **{window_field: datetime(2030, 1, 1, tzinfo=timezone.utc)} |
| 291 | + ) |
| 292 | + response = OptionGroupsAdminApi(http_client=api_client).update(group.id, {"min_quantity_per_product": 1}) |
| 293 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 294 | + assert window_field in str(response.json()) |
| 295 | + |
| 296 | + |
| 297 | +@pytest.mark.parametrize("kind", ["visible", "orderable"]) |
| 298 | +@pytest.mark.django_db |
| 299 | +def test_admin_option_group_create_rejects_ends_at_before_product_starts_at(api_client, product, kind): |
| 300 | + # P2-A: 한 쪽 boundary 만 명시 — starts_at=None → product fallback(FAR_PAST=2020), ends_at=2019. |
| 301 | + # admin 이 model effective_*_period 의 inverted 케이스를 차단해야 함. |
| 302 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 303 | + { |
| 304 | + "product": str(product.id), |
| 305 | + "name_ko": "옵션", |
| 306 | + "name_en": "Opt", |
| 307 | + f"{kind}_ends_at": datetime(2019, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 308 | + } |
| 309 | + ) |
| 310 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 311 | + assert f"{kind}_ends_at" in str(response.json()) |
| 312 | + |
| 313 | + |
| 314 | +@pytest.mark.parametrize("kind", ["visible", "orderable"]) |
| 315 | +@pytest.mark.django_db |
| 316 | +def test_admin_option_group_create_rejects_starts_at_after_product_ends_at(api_client, product, kind): |
| 317 | + # P2-A: starts_at=2100 > product.*_ends_at(FAR_FUTURE=2099) → effective inverted. |
| 318 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 319 | + { |
| 320 | + "product": str(product.id), |
| 321 | + "name_ko": "옵션", |
| 322 | + "name_en": "Opt", |
| 323 | + f"{kind}_starts_at": datetime(2100, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 324 | + } |
| 325 | + ) |
| 326 | + assert response.status_code == HTTP_400_BAD_REQUEST |
| 327 | + assert f"{kind}_starts_at" in str(response.json()) |
| 328 | + |
| 329 | + |
| 330 | +@pytest.mark.django_db |
| 331 | +def test_admin_option_group_create_allows_non_required_group_with_explicit_window(api_client, product): |
| 332 | + # 비필수 그룹(min_quantity_per_product=0) 은 starts_at 명시 가능. |
| 333 | + response = OptionGroupsAdminApi(http_client=api_client).create( |
| 334 | + { |
| 335 | + "product": str(product.id), |
| 336 | + "name_ko": "선택옵션", |
| 337 | + "name_en": "Optional", |
| 338 | + "orderable_starts_at": datetime(2030, 1, 1, tzinfo=timezone.utc).isoformat(), |
| 339 | + } |
| 340 | + ) |
| 341 | + assert response.status_code == HTTP_201_CREATED |
| 342 | + |
| 343 | + |
146 | 344 | @pytest.mark.parametrize("status", list(Product.CurrentStatus)) |
147 | 345 | @pytest.mark.django_db |
148 | 346 | def test_admin_product_list_filters_by_status(api_client, products_by_status, status): |
|
0 commit comments