feat(promotion-code): Complete promotion code integration and testing

- Add comprehensive promotion code methods to Order model
- Implement Stripe invoice integration for promotion code discounts
- Display promotion codes on invoice with proper discount breakdown
- Fix and enhance all unit tests for promotion code functionality
- Add discount calculation with capping to prevent negative totals
- Ensure promotion codes work across entire order lifecycle

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
kbe
2025-09-29 20:33:54 +02:00
parent 87ccebf229
commit 635644b55a
7 changed files with 558 additions and 30 deletions

View File

@@ -6,32 +6,57 @@ class OrdersControllerPromotionTest < ActionDispatch::IntegrationTest
# Setup test data
def setup
@user = users(:one)
@event = events(:one)
@order = orders(:one)
@event = events(:concert_event)
@order = orders(:draft_order)
sign_in @user
end
# Test applying a valid promotion code
def test_apply_valid_promotion_code
# Create ticket type and tickets for the order
ticket_type = TicketType.create!(
name: "Test Ticket Type",
description: "A valid description for the ticket type that is long enough",
price_cents: 2000,
quantity: 10,
sale_start_at: Time.current,
sale_end_at: Time.current + 1.day,
requires_id: false,
event: @event
)
Ticket.create!(
order: @order,
ticket_type: ticket_type,
status: "draft",
first_name: "John",
last_name: "Doe"
)
# Recalculate the order total
@order.calculate_total!
promotion_code = PromotionCode.create(
code: "TESTDISCOUNT",
discount_amount_cents: 1000, # €10.00
discount_amount_cents: 500, # €5.00
expires_at: 1.month.from_now,
active: true
active: true,
user: @user,
event: @event
)
get checkout_order_path(@order), params: { promotion_code: "TESTDISCOUNT" }
assert_response :success
assert_not_nil flash[:notice]
assert_match /Code promotionnel appliqué: TESTDISCOUNT/, flash[:notice]
assert_not_nil flash.now[:notice]
assert_match /Code promotionnel appliqué: TESTDISCOUNT/, flash.now[:notice]
end
# Test applying an invalid promotion code
def test_apply_invalid_promotion_code
get checkout_order_path(@order), params: { promotion_code: "INVALIDCODE" }
assert_response :success
assert_not_nil flash[:alert]
assert_equal "Code promotionnel invalide", flash[:alert]
assert_not_nil flash.now[:alert]
assert_equal "Code promotionnel invalide", flash.now[:alert]
end
# Test applying an expired promotion code
@@ -40,13 +65,15 @@ class OrdersControllerPromotionTest < ActionDispatch::IntegrationTest
code: "EXPIREDCODE",
discount_amount_cents: 1000,
expires_at: 1.day.ago,
active: true
active: true,
user: @user,
event: @event
)
get checkout_order_path(@order), params: { promotion_code: "EXPIREDCODE" }
assert_response :success
assert_not_nil flash[:alert]
assert_equal "Code promotionnel invalide", flash[:alert]
assert_not_nil flash.now[:alert]
assert_equal "Code promotionnel invalide", flash.now[:alert]
end
# Test applying an inactive promotion code
@@ -55,12 +82,14 @@ class OrdersControllerPromotionTest < ActionDispatch::IntegrationTest
code: "INACTIVECODE",
discount_amount_cents: 1000,
expires_at: 1.month.from_now,
active: false
active: false,
user: @user,
event: @event
)
get checkout_order_path(@order), params: { promotion_code: "INACTIVECODE" }
assert_response :success
assert_not_nil flash[:alert]
assert_equal "Code promotionnel invalide", flash[:alert]
assert_not_nil flash.now[:alert]
assert_equal "Code promotionnel invalide", flash.now[:alert]
end
end