Files
aperonight/test/controllers/orders_controller_promotion_test.rb

103 lines
3.1 KiB
Ruby

require "test_helper"
require "securerandom"
class OrdersControllerPromotionTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
# Setup test data
def setup
@user = users(:one)
@event = events(:concert_event)
# Create a new order for the test to ensure proper associations
@order = @user.orders.create!(event: @event, status: "draft", expires_at: 15.minutes.from_now, total_amount_cents: 2000)
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 = Ticket.create!(
order: @order,
ticket_type: ticket_type,
status: "draft",
first_name: "John",
last_name: "Doe",
price_cents: 2000
)
# Recalculate the order total
@order.calculate_total!
# Use a unique code for each test run
unique_code = "TESTDISCOUNT_#{SecureRandom.hex(4)}"
promotion_code = PromotionCode.create(
code: unique_code,
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: unique_code }
puts "Response status: #{response.status}"
puts "Response body: #{response.body}" if response.status != 200
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