diff --git a/app/models/ability.rb b/app/models/ability.rb index b833b6f4..d6b3ef21 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -66,7 +66,13 @@ class Ability can [:new, :create], Registration do |registration| conference = registration.conference - conference.registration_open? && !conference.registration_limit_exceeded? || conference.program.speakers.confirmed.include?(user) + if conference.user_registered? user + false + elsif conference.program.speakers.confirmed.include?(user) && conference.registration_period + true + else + conference.registration_open? && !conference.registration_limit_exceeded? + end end can :index, Organization diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 7936ddbb..26522913 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -89,11 +89,131 @@ describe 'User' do let(:other_self_organized_track) { create(:track, :self_organized) } it{ should be_able_to(:manage, user) } - it{ should be_able_to(:manage, registration_public) } it{ should be_able_to(:manage, registration_not_public) } - it{ should_not be_able_to(:new, Registration.new(conference_id: conference_with_closed_registration.id))} - it{ should_not be_able_to(:create, Registration.new(conference_id: conference_with_closed_registration.id))} + + # Test for user can register or not + context 'when user is not a speaker with event confirmed' do + let(:conference) { create(:conference) } + + context 'when the registration is closed' do + before :each do + create(:registration_period, conference: conference, start_date: Date.current - 6.days, end_date: Date.current - 6.days) + end + + it{ should_not be_able_to(:new, Registration.new(conference: conference)) } + it{ should_not be_able_to(:create, Registration.new(conference: conference)) } + end + + context 'when the registration period is not set' do + it{ should_not be_able_to(:new, Registration.new(conference: conference)) } + it{ should_not be_able_to(:create, Registration.new(conference: conference)) } + end + + context 'when user has already registered' do + before :each do + create(:registration, conference: conference, user: user) + end + + it{ should_not be_able_to(:new, Registration.new(conference: conference)) } + it{ should_not be_able_to(:create, Registration.new(conference: conference)) } + end + + context 'when registrations are open' do + before :each do + create(:registration_period, conference: conference) + end + + it{ should be_able_to(:new, Registration.new(conference: conference)) } + it{ should be_able_to(:create, Registration.new(conference: conference)) } + + context 'when user has not registered with no registration_limit_exceeded' do + before :each do + conference.registration_limit = 1 + end + + it{ should be_able_to(:new, Registration.new(conference: conference)) } + it{ should be_able_to(:create, Registration.new(conference: conference)) } + end + + context 'when user has not registered with registration_limit_exceeded' do + before :each do + conference.registration_limit = 1 + create(:registration, conference: conference, user: user2) + end + + it{ should_not be_able_to(:new, Registration.new(conference: conference)) } + it{ should_not be_able_to(:create, Registration.new(conference: conference)) } + end + end + end + + context 'when user is a speaker with event confirmed' do + let(:conference_with_speaker_confirmed) { create(:conference) } + let(:event_with_speaker_confirmed) { create(:event, state: 'confirmed', program: conference_with_speaker_confirmed.program) } + + before :each do + event_with_speaker_confirmed.speakers << user + end + + context 'when registration period is not set' do + it{ should_not be_able_to(:new, Registration.new(conference: conference_with_speaker_confirmed)) } + it{ should_not be_able_to(:create, Registration.new(conference: conference_with_speaker_confirmed)) } + end + + context 'when speaker has already registered' do + before :each do + create(:registration, conference: conference_with_speaker_confirmed, user: user) + end + + it{ should_not be_able_to(:new, Registration.new(conference: conference_with_speaker_confirmed)) } + it{ should_not be_able_to(:create, Registration.new(conference: conference_with_speaker_confirmed)) } + end + + context 'when registration period is set' do + before :each do + create(:registration_period, conference: conference_with_speaker_confirmed) + end + + context 'when registrations are open' do + context 'when speaker has not registered' do + it{ should be_able_to(:new, Registration.new(conference: conference_with_speaker_confirmed)) } + it{ should be_able_to(:create, Registration.new(conference: conference_with_speaker_confirmed)) } + end + + context 'when registration_limit_exceeded' do + before :each do + conference_with_speaker_confirmed.registration_limit = 1 + create(:registration, conference: conference_with_speaker_confirmed, user: user2) + end + + it{ should be_able_to(:new, Registration.new(conference: conference_with_speaker_confirmed)) } + it{ should be_able_to(:create, Registration.new(conference: conference_with_speaker_confirmed)) } + end + end + + context 'when registrations are closed' do + before :each do + create(:registration_period, conference: conference_with_speaker_confirmed, start_date: Date.current - 6.days, end_date: Date.current - 6.days) + end + + context 'when speaker has not registered' do + it{ should be_able_to(:new, Registration.new(conference: conference_with_speaker_confirmed)) } + it{ should be_able_to(:create, Registration.new(conference: conference_with_speaker_confirmed)) } + end + + context 'with registration_limit_exceeded' do + before :each do + conference_with_speaker_confirmed.registration_limit = 1 + create(:registration, conference: conference_with_speaker_confirmed, user: user2) + end + + it{ should be_able_to(:new, Registration.new(conference: conference_with_speaker_confirmed)) } + it{ should be_able_to(:create, Registration.new(conference: conference_with_speaker_confirmed)) } + end + end + end + end it{ should be_able_to(:index, Ticket) } it{ should be_able_to(:manage, TicketPurchase.new(user_id: user.id)) }