test: add gift card functional tests

Six new tests covering gift card page access, settings enable/disable,
settings validation, manage page, and applying invalid gift card codes.
This commit is contained in:
russell@unturf.com 2026-03-07 17:11:38 -05:00
parent 3b1d00cbfb
commit e90e080c44

View file

@ -4430,3 +4430,127 @@ class TestAnalytics(_AuthenticatedBase):
# Now data-has-bucket="1" should be present
res = self.testapp.get(f"/s/{shop.id}/{shop.slug}", status=200)
self.assertIn('data-has-bucket="1"', res.text)
class TestGiftCardFunctional(_AuthenticatedBase):
"""Functional tests for gift card features."""
def _enable_gift_cards(self, shop, min_dollars="5.00", max_dollars="250.00"):
"""Helper to enable gift cards on a shop via settings POST."""
res = self.testapp.post(
f"/s/{shop.id}/settings",
{
"form_section": "gift-card-settings",
"gift-card-enabled-checkbox": "on",
"gift_card_min": min_dollars,
"gift_card_max": max_dollars,
"submit": "Save Settings",
},
status=302,
)
return res
def test_gift_card_page_not_enabled(self):
"""Gift card page redirects when gift cards are disabled (default)."""
shop = self._create_shop_helper()
# Gift cards are disabled by default, so GET should redirect
res = self.testapp.get(f"/s/{shop.id}/gift-card", status=302)
def test_gift_card_enable_settings(self):
"""Enable gift cards via shop settings and verify DB state."""
shop = self._create_shop_helper()
res = self.testapp.post(
f"/s/{shop.id}/settings",
{
"form_section": "gift-card-settings",
"gift-card-enabled-checkbox": "on",
"gift_card_min": "5.00",
"gift_card_max": "250.00",
"submit": "Save Settings",
},
status=302,
)
res = res.follow()
flash = self._get_flash_messages(res)
self.assertIn("Gift cards enabled", flash)
self.dbsession.refresh(shop)
self.assertTrue(shop.gift_card_enabled)
self.assertEqual(shop.gift_card_min_in_cents, 500)
self.assertEqual(shop.gift_card_max_in_cents, 25000)
def test_gift_card_page_enabled(self):
"""Gift card page returns 200 when gift cards are enabled."""
shop = self._create_shop_helper()
self._enable_gift_cards(shop)
self.dbsession.refresh(shop)
res = self.testapp.get(f"/s/{shop.id}/gift-card", status=200)
self.assertIn("Gift Card", res.text)
def test_gift_card_manage_page(self):
"""Gift card manage page returns 200 for shop owner."""
shop = self._create_shop_helper()
self._enable_gift_cards(shop)
res = self.testapp.get(f"/s/{shop.id}/gift-cards/manage", status=200)
def test_gift_card_apply_invalid_code(self):
"""Applying a nonexistent gift card code shows error flash."""
shop = self._create_shop_helper()
# Create a product on the shop
redirect_res = self.testapp.post(
f"/p/new?shop_id={shop.id}", self.product1_params
)
res = redirect_res.follow()
product = get_all_products(self.dbsession).all()[0]
# Log out shop owner, log in as customer
self.testapp.get("/log-out")
self.log_in_user(self.user2_creds)
# Add product to cart
csrf_token = self.get_csrf_token(shop.uuid_str)
self.testapp.post(
"/cart/add",
{
"product_id": product.id,
"shop_id": shop.id,
"csrf_token": csrf_token,
},
)
# Try to apply an invalid gift card code
csrf_token = self.get_csrf_token(shop.uuid_str)
res = self.testapp.post(
"/gift-card/apply",
{
"gift_card_code": "GC-DOESNOTEXIST",
"csrf_token": csrf_token,
},
status=302,
)
res = res.follow()
flash = self._get_flash_messages(res)
self.assertIn("does not exist", flash)
def test_gift_card_settings_validation(self):
"""Setting min > max shows validation error."""
shop = self._create_shop_helper()
res = self.testapp.post(
f"/s/{shop.id}/settings",
{
"form_section": "gift-card-settings",
"gift-card-enabled-checkbox": "on",
"gift_card_min": "500.00",
"gift_card_max": "100.00",
"submit": "Save Settings",
},
status=302,
)
res = res.follow()
flash = self._get_flash_messages(res)
self.assertIn("Maximum must be greater than or equal to minimum", flash)