require "test_helper" class Api::V1::EventsControllerTest < ActionDispatch::IntegrationTest setup do ENV["API_KEY"] = "test_key" @user = User.create!(email: "test@example.com", password: "password123", password_confirmation: "password123") @event = Event.create!(name: "Test Event", slug: "test-event", description: "A description that is long enough for validation", latitude: 48.8566, longitude: 2.3522, venue_name: "Venue", venue_address: "Address", user: @user, start_time: 1.week.from_now, end_time: 1.week.from_now + 3.hours, state: :published) end test "should get index" do get api_v1_events_url, headers: headers_api_key assert_response :success assert_kind_of Array, json_response end test "should show event" do get api_v1_event_url(@event.id), headers: headers_api_key assert_response :success assert_equal @event.id, json_response["id"] end test "should create event" do assert_difference("Event.count") do post api_v1_events_url, params: { event: { name: "New Event", slug: "new-event", description: "New description that is long enough", latitude: 48.8566, longitude: 2.3522, venue_name: "New Venue", venue_address: "New Address", user_id: @user.id, start_time: "2024-01-01 10:00:00", end_time: "2024-01-01 13:00:00", state: "published" } }, as: :json, headers: headers_api_key end assert_response :created end test "should update event" do patch api_v1_event_url(@event.id), params: { event: { name: "Updated Event" } }, as: :json, headers: headers_api_key assert_response :ok @event.reload assert_equal "Updated Event", @event.name end test "should destroy event" do assert_difference("Event.count", -1) do delete api_v1_event_url(@event.id), headers: headers_api_key end assert_response :no_content end test "should store cart" do post store_cart_api_v1_event_url(@event), params: { cart: { ticket_type_id: 1, quantity: 2 } }, as: :json assert_response :success assert_equal @event.id, session[:event_id] end private def json_response JSON.parse(response.body) end def headers_api_key { "X-API-Key" => "test_key" } end end