require "test_helper" class UserTest < ActiveSupport::TestCase # Test that User model exists test "should be a class" do assert_kind_of Class, User end # Test Devise modules test "should include devise modules" do user = User.new assert user.respond_to?(:email) assert user.respond_to?(:encrypted_password) end # Test associations test "should have many events" do association = User.reflect_on_association(:events) assert_equal :has_many, association.macro assert_equal :destroy, association.options[:dependent] end test "should have many tickets" do association = User.reflect_on_association(:tickets) assert_equal :has_many, association.macro assert_equal :destroy, association.options[:dependent] end # Test first_name validations test "should validate presence of first_name" do user = User.new(last_name: "Doe") refute user.valid?, "User with blank first_name should be invalid" assert_not_nil user.errors[:first_name], "No validation error for blank first_name" end test "should validate length of first_name" do # Test minimum length user = User.new(first_name: "A", last_name: "Doe") refute user.valid?, "User with first_name shorter than 3 chars should be invalid" assert_not_nil user.errors[:first_name], "No validation error for too short first_name" # Test maximum length user = User.new(first_name: "A" * 13, last_name: "Doe") refute user.valid?, "User with first_name longer than 12 chars should be invalid" assert_not_nil user.errors[:first_name], "No validation error for too long first_name" end # Test last_name validations test "should validate presence of last_name" do user = User.new(first_name: "John") refute user.valid?, "User with blank last_name should be invalid" assert_not_nil user.errors[:last_name], "No validation error for blank last_name" end test "should validate length of last_name" do # Test minimum length user = User.new(first_name: "John", last_name: "Do") refute user.valid?, "User with last_name shorter than 3 chars should be invalid" assert_not_nil user.errors[:last_name], "No validation error for too short last_name" # Test maximum length user = User.new(first_name: "John", last_name: "D" * 13) refute user.valid?, "User with last_name longer than 12 chars should be invalid" assert_not_nil user.errors[:last_name], "No validation error for too long last_name" end # Test onboarding functionality test "new users should need onboarding by default" do user = User.new(email: "test@example.com", password: "password123") assert user.needs_onboarding?, "New user should need onboarding" assert_not user.onboarding_completed?, "New user should not have completed onboarding" end test "should complete onboarding" do user = users(:one) user.update!(onboarding_completed: false) assert user.needs_onboarding?, "User should need onboarding initially" user.complete_onboarding! assert_not user.needs_onboarding?, "User should not need onboarding after completion" assert user.onboarding_completed?, "User should have completed onboarding" end test "needs_onboarding? should return correct value" do user = users(:one) user.update!(onboarding_completed: false) assert user.needs_onboarding?, "User with false onboarding_completed should need onboarding" user.update!(onboarding_completed: true) assert_not user.needs_onboarding?, "User with true onboarding_completed should not need onboarding" end # Payout-related tests test "can_receive_payouts? returns true if stripe account id present and charges enabled" do user = users(:one) user.update!(stripe_connected_account_id: "acct_12345", is_professionnal: true) # Mock Stripe API call Stripe::Account.expects(:retrieve).with("acct_12345").returns(stub(charges_enabled: true)) assert user.can_receive_payouts? end test "can_receive_payouts? returns false if no stripe account id" do user = users(:one) user.update!(is_professionnal: true) assert_not user.can_receive_payouts? end test "can_receive_payouts? returns false if not professional" do user = users(:one) user.update!(stripe_connected_account_id: "acct_12345") assert_not user.can_receive_payouts? end test "can_receive_payouts? returns false if charges not enabled" do user = users(:one) user.update!(stripe_connected_account_id: "acct_12345", is_professionnal: true) Stripe::Account.expects(:retrieve).with("acct_12345").returns(stub(charges_enabled: false)) assert_not user.can_receive_payouts? end test "can_receive_payouts? handles Stripe API error" do user = users(:one) user.update!(stripe_connected_account_id: "acct_invalid", is_professionnal: true) Stripe::Account.expects(:retrieve).with("acct_invalid").raises(Stripe::InvalidRequestError.new("Account not found")) assert_not user.can_receive_payouts? end end