require "test_helper" class OrdersControllerPromotionTest < ActionDispatch::IntegrationTest include Devise::Test::IntegrationHelpers # Setup test data def setup @user = users(: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: 500, # €5.00 expires_at: 1.month.from_now, active: true, user: @user, event: @event ) get checkout_order_path(@order), params: { promotion_code: "TESTDISCOUNT" } assert_response :success 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.now[:alert] assert_equal "Code promotionnel invalide", flash.now[:alert] end # Test applying an expired promotion code def test_apply_expired_promotion_code promotion_code = PromotionCode.create( code: "EXPIREDCODE", discount_amount_cents: 1000, expires_at: 1.day.ago, active: true, user: @user, event: @event ) get checkout_order_path(@order), params: { promotion_code: "EXPIREDCODE" } assert_response :success assert_not_nil flash.now[:alert] assert_equal "Code promotionnel invalide", flash.now[:alert] end # Test applying an inactive promotion code def test_apply_inactive_promotion_code promotion_code = PromotionCode.create( code: "INACTIVECODE", discount_amount_cents: 1000, expires_at: 1.month.from_now, active: false, user: @user, event: @event ) get checkout_order_path(@order), params: { promotion_code: "INACTIVECODE" } assert_response :success assert_not_nil flash.now[:alert] assert_equal "Code promotionnel invalide", flash.now[:alert] end end