Add model methods for accurate net calculations (€0.50 + 1.5% fees), eligibility, refund handling Update promoter/payouts controller for index (pending events), create (eligibility checks) Integrate admin processing via Stripe::Transfer, webhook for status sync Enhance views: index pending cards, events/show preview/form Add comprehensive tests (models, controllers, service, integration); run migrations
388 lines
12 KiB
Ruby
Executable File
388 lines
12 KiB
Ruby
Executable File
require "test_helper"
|
|
|
|
class TicketTest < ActiveSupport::TestCase
|
|
# Test that Ticket model exists
|
|
test "should be a class" do
|
|
assert_kind_of Class, Ticket
|
|
end
|
|
|
|
# Test validations
|
|
test "should automatically generate qr_code if not provided" do
|
|
user = User.create!(
|
|
email: "test@example.com",
|
|
password: "password123",
|
|
password_confirmation: "password123"
|
|
)
|
|
|
|
event = Event.create!(
|
|
name: "Valid event Name",
|
|
slug: "valid-event-name",
|
|
description: "Valid description for the event that is long enough",
|
|
latitude: 48.8566,
|
|
longitude: 2.3522,
|
|
venue_name: "Test Venue",
|
|
venue_address: "123 Test Street",
|
|
user: user
|
|
)
|
|
|
|
ticket_type = TicketType.create!(
|
|
name: "Valid Ticket Type Name",
|
|
description: "Valid description for the ticket type that is long enough",
|
|
price_cents: 1000,
|
|
quantity: 50,
|
|
sale_start_at: Time.current,
|
|
sale_end_at: Time.current + 1.day,
|
|
requires_id: false,
|
|
event: event
|
|
)
|
|
|
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
|
ticket = Ticket.new(order: order, ticket_type: ticket_type, first_name: "Test", last_name: "User")
|
|
|
|
# QR code should be nil initially
|
|
assert_nil ticket.qr_code
|
|
|
|
# After validation, QR code should be generated automatically
|
|
ticket.valid?
|
|
assert_not_nil ticket.qr_code
|
|
|
|
# And the ticket should save successfully
|
|
assert ticket.save
|
|
end
|
|
|
|
test "should not save ticket with duplicate qr_code" do
|
|
# Create first ticket
|
|
ticket1 = Ticket.new(qr_code: "unique_qr_code_123")
|
|
ticket1.save
|
|
|
|
# Try to create second ticket with same QR code
|
|
ticket2 = Ticket.new(qr_code: "unique_qr_code_123")
|
|
assert_not ticket2.save
|
|
end
|
|
|
|
test "should not save ticket without order" do
|
|
ticket = Ticket.new(qr_code: "unique_qr_code_123")
|
|
assert_not ticket.save
|
|
end
|
|
|
|
test "should not save ticket without ticket_type_id" do
|
|
user = User.create!(
|
|
email: "test@example.com",
|
|
password: "password123",
|
|
password_confirmation: "password123"
|
|
)
|
|
|
|
event = Event.create!(
|
|
name: "Valid event Name",
|
|
slug: "valid-event-name",
|
|
description: "Valid description for the event that is long enough",
|
|
latitude: 48.8566,
|
|
longitude: 2.3522,
|
|
venue_name: "Test Venue",
|
|
venue_address: "123 Test Street",
|
|
user: user
|
|
)
|
|
|
|
order = Order.create!(user: user, event: event, total_amount_cents: 1000)
|
|
ticket = Ticket.new(qr_code: "unique_qr_code_123", order: order)
|
|
assert_not ticket.save
|
|
end
|
|
|
|
test "should set price from ticket type automatically" do
|
|
user = User.create!(
|
|
email: "test2@example.com",
|
|
password: "password123",
|
|
password_confirmation: "password123"
|
|
)
|
|
|
|
event = Event.create!(
|
|
name: "Valid event Name",
|
|
slug: "valid-event-name-2",
|
|
description: "Valid description for the event that is long enough",
|
|
latitude: 48.8566,
|
|
longitude: 2.3522,
|
|
venue_name: "Test Venue",
|
|
venue_address: "123 Test Street",
|
|
user: user
|
|
)
|
|
|
|
ticket_type = TicketType.create!(
|
|
name: "Valid Ticket Type Name",
|
|
description: "Valid description for the ticket type that is long enough",
|
|
price_cents: 1000,
|
|
quantity: 50,
|
|
sale_start_at: Time.current,
|
|
sale_end_at: Time.current + 1.day,
|
|
requires_id: false,
|
|
event: event
|
|
)
|
|
|
|
order = Order.create!(user: user, event: event, total_amount_cents: 1000)
|
|
ticket = Ticket.new(
|
|
qr_code: "unique_qr_code_123",
|
|
order: order,
|
|
ticket_type: ticket_type,
|
|
first_name: "John",
|
|
last_name: "Doe"
|
|
)
|
|
|
|
# price_cents should be nil initially
|
|
assert_nil ticket.price_cents
|
|
|
|
# After validation, it should be set from ticket_type
|
|
ticket.valid?
|
|
assert_equal 1000, ticket.price_cents
|
|
assert ticket.save
|
|
end
|
|
|
|
test "should not save ticket with invalid status" do
|
|
user = User.create!(
|
|
email: "test3@example.com",
|
|
password: "password123",
|
|
password_confirmation: "password123"
|
|
)
|
|
|
|
event = Event.create!(
|
|
name: "Valid event Name",
|
|
slug: "valid-event-name-3",
|
|
description: "Valid description for the event that is long enough",
|
|
latitude: 48.8566,
|
|
longitude: 2.3522,
|
|
venue_name: "Test Venue",
|
|
venue_address: "123 Test Street",
|
|
user: user
|
|
)
|
|
|
|
ticket_type = TicketType.create!(
|
|
name: "Valid Ticket Type Name",
|
|
description: "Valid description for the ticket type that is long enough",
|
|
price_cents: 1000,
|
|
quantity: 50,
|
|
sale_start_at: Time.current,
|
|
sale_end_at: Time.current + 1.day,
|
|
requires_id: false,
|
|
event: event
|
|
)
|
|
|
|
order = Order.create!(user: user, event: event, total_amount_cents: 1000)
|
|
ticket = Ticket.new(
|
|
qr_code: "unique_qr_code_123",
|
|
order: order,
|
|
ticket_type: ticket_type,
|
|
price_cents: 1000,
|
|
status: "invalid_status",
|
|
first_name: "John",
|
|
last_name: "Doe"
|
|
)
|
|
assert_not ticket.save
|
|
end
|
|
|
|
# Test associations
|
|
test "should have one user through order" do
|
|
association = Ticket.reflect_on_association(:user)
|
|
assert_equal :has_one, association.macro
|
|
assert_equal :order, association.options[:through]
|
|
end
|
|
|
|
test "should belong to ticket_type" do
|
|
association = Ticket.reflect_on_association(:ticket_type)
|
|
assert_equal :belongs_to, association.macro
|
|
end
|
|
|
|
test "should have one event through ticket_type" do
|
|
association = Ticket.reflect_on_association(:event)
|
|
assert_equal :has_one, association.macro
|
|
assert_equal :ticket_type, association.options[:through]
|
|
end
|
|
|
|
# Test callbacks
|
|
test "should set price from ticket_type on create" do
|
|
# This test would require setting up proper fixtures or creating associated records
|
|
# which is beyond the scope of basic model testing without a full test environment
|
|
assert true # Placeholder until we can set up proper testing environment
|
|
end
|
|
|
|
# Test valid statuses
|
|
test "should save ticket with valid active status" do
|
|
user = User.create!(
|
|
email: "test@example.com",
|
|
password: "password123",
|
|
password_confirmation: "password123"
|
|
)
|
|
|
|
event = Event.create!(
|
|
name: "Valid event Name",
|
|
slug: "valid-event-name",
|
|
description: "Valid description for the event that is long enough",
|
|
latitude: 48.8566,
|
|
longitude: 2.3522,
|
|
venue_name: "Test Venue",
|
|
venue_address: "123 Test Street",
|
|
user: user
|
|
)
|
|
|
|
ticket_type = TicketType.create!(
|
|
name: "Valid Ticket Type Name",
|
|
description: "Valid description for the ticket type that is long enough",
|
|
price_cents: 1000,
|
|
quantity: 50,
|
|
sale_start_at: Time.current,
|
|
sale_end_at: Time.current + 1.day,
|
|
requires_id: false,
|
|
event: event
|
|
)
|
|
|
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
|
ticket = Ticket.new(
|
|
qr_code: "unique_qr_code_123",
|
|
order: order,
|
|
ticket_type: ticket_type,
|
|
status: "active",
|
|
first_name: "John",
|
|
last_name: "Doe"
|
|
)
|
|
# The price_cents should be set automatically by the callback
|
|
assert ticket.save
|
|
end
|
|
|
|
test "should save ticket with valid used status" do
|
|
user = User.create!(
|
|
email: "test@example.com",
|
|
password: "password123",
|
|
password_confirmation: "password123"
|
|
)
|
|
|
|
event = Event.create!(
|
|
name: "Valid event Name",
|
|
slug: "valid-event-name",
|
|
description: "Valid description for the event that is long enough",
|
|
latitude: 48.8566,
|
|
longitude: 2.3522,
|
|
venue_name: "Test Venue",
|
|
venue_address: "123 Test Street",
|
|
user: user
|
|
)
|
|
|
|
ticket_type = TicketType.create!(
|
|
name: "Valid Ticket Type Name",
|
|
description: "Valid description for the ticket type that is long enough",
|
|
price_cents: 1000,
|
|
quantity: 50,
|
|
sale_start_at: Time.current,
|
|
sale_end_at: Time.current + 1.day,
|
|
requires_id: false,
|
|
event: event
|
|
)
|
|
|
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
|
ticket = Ticket.new(
|
|
qr_code: "unique_qr_code_456",
|
|
order: order,
|
|
ticket_type: ticket_type,
|
|
status: "used",
|
|
first_name: "Jane",
|
|
last_name: "Doe"
|
|
)
|
|
assert ticket.save
|
|
end
|
|
|
|
test "should save ticket with valid expired status" do
|
|
user = User.create!(
|
|
email: "test@example.com",
|
|
password: "password123",
|
|
password_confirmation: "password123"
|
|
)
|
|
|
|
event = Event.create!(
|
|
name: "Valid event Name",
|
|
slug: "valid-event-name",
|
|
description: "Valid description for the event that is long enough",
|
|
latitude: 48.8566,
|
|
longitude: 2.3522,
|
|
venue_name: "Test Venue",
|
|
venue_address: "123 Test Street",
|
|
user: user
|
|
)
|
|
|
|
ticket_type = TicketType.create!(
|
|
name: "Valid Ticket Type Name",
|
|
description: "Valid description for the ticket type that is long enough",
|
|
price_cents: 1000,
|
|
quantity: 50,
|
|
sale_start_at: Time.current,
|
|
sale_end_at: Time.current + 1.day,
|
|
requires_id: false,
|
|
event: event
|
|
)
|
|
|
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
|
ticket = Ticket.new(
|
|
qr_code: "unique_qr_code_789",
|
|
order: order,
|
|
ticket_type: ticket_type,
|
|
status: "expired",
|
|
first_name: "Bob",
|
|
last_name: "Smith"
|
|
)
|
|
assert ticket.save
|
|
end
|
|
|
|
test "should save ticket with valid refunded status" do
|
|
user = User.create!(
|
|
email: "test@example.com",
|
|
password: "password123",
|
|
password_confirmation: "password123"
|
|
)
|
|
|
|
event = Event.create!(
|
|
name: "Valid event Name",
|
|
slug: "valid-event-name",
|
|
description: "Valid description for the event that is long enough",
|
|
latitude: 48.8566,
|
|
longitude: 2.3522,
|
|
venue_name: "Test Venue",
|
|
venue_address: "123 Test Street",
|
|
user: user
|
|
)
|
|
|
|
ticket_type = TicketType.create!(
|
|
name: "Valid Ticket Type Name",
|
|
description: "Valid description for the ticket type that is long enough",
|
|
price_cents: 1000,
|
|
quantity: 50,
|
|
sale_start_at: Time.current,
|
|
sale_end_at: Time.current + 1.day,
|
|
requires_id: false,
|
|
event: event
|
|
)
|
|
|
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
|
ticket = Ticket.new(
|
|
qr_code: "unique_qr_code_999",
|
|
order: order,
|
|
ticket_type: ticket_type,
|
|
status: "refunded",
|
|
first_name: "Alice",
|
|
last_name: "Johnson"
|
|
)
|
|
assert ticket.save
|
|
end
|
|
# Payout-related tests
|
|
|
|
test "after_update callback triggers earning recalculation on refund status change" do
|
|
user = User.create!(email: "refund@example.com", password: "password123", password_confirmation: "password123")
|
|
event = Event.create!(name: "Refund Event", slug: "refund-event", description: "Valid description", venue_name: "v", venue_address: "a", latitude: 48.0, longitude: 2.0, start_time: 1.day.from_now, user: user, state: :published)
|
|
ticket_type = TicketType.create!(name: "Standard", price_cents: 1000, quantity: 1, sale_start_at: Time.current, sale_end_at: Time.current + 1.day, event: event)
|
|
order = Order.create!(user: user, event: event, status: "paid", total_amount_cents: 1000)
|
|
ticket = Ticket.create!(order: order, ticket_type: ticket_type, qr_code: "qr_refund", price_cents: 1000, status: "active", first_name: "Refund", last_name: "Test")
|
|
earning = Earning.create!(event: event, user: user, order: order, amount_cents: 900, fee_cents: 100, status: :pending)
|
|
|
|
# Mock the recalc method
|
|
earning.expects(:recalculate_on_refund).once
|
|
|
|
# Change status to refunded
|
|
ticket.status = "refunded"
|
|
ticket.save!
|
|
end
|
|
end
|