Registration for Events

This commit is contained in:
Stella Rouzi 2016-04-22 18:18:46 +03:00
parent f188fd5575
commit 01ec43e53a
35 changed files with 522 additions and 46 deletions

View file

@ -10,6 +10,8 @@ describe Event do
describe 'association' do
it { is_expected.to belong_to :program }
it { is_expected.to belong_to :event_type }
it { is_expected.to have_many :events_registrations }
it { is_expected.to have_many :registrations }
end
describe 'validation' do
@ -22,6 +24,51 @@ describe Event do
it { is_expected.to validate_presence_of(:program) }
it { is_expected.to validate_presence_of(:event_type) }
describe '#max_attendees_and_require_registration' do
it 'allows user to set max_attendees, only if require_registration is set' do
event.require_registration = true
event.max_attendees = 2
expect(event.valid?).to eq true
end
it 'does not allow max_attendees to be set without require_registration' do
event.max_attendees = 2
expect(event.valid?).to eq false
expect(event.errors[:require_registration]).to eq ['must be enabled, when you set max_attendees']
end
it 'does not allow require_registration to be set without max_attendees' do
event.require_registration = true
event.max_attendees = nil
expect(event.valid?).to eq false
expect(event.errors[:max_attendees]).to eq ['must be enabled, when you set require_registration']
end
end
describe 'max_attendees_no_more_than_room_size' do
before :each do
event.room = create(:room, size: 3)
event.require_registration = true
end
it 'it is valid, if max_attendees is less than room size' do
event.max_attendees = 2
expect(event.valid?).to eq true
expect(event.errors.full_messages).to eq []
end
it 'it is not valid, if max_attendees attribute is bigger than size of room' do
event.max_attendees = 4
expect(event.valid?).to eq false
expect(event.errors[:max_attendees]).to eq ['cannot be more than the room\'s capacity (3)']
end
end
describe '#abstract_limit' do
before :each do
event.event_type.maximum_abstract_length = 2
@ -89,6 +136,41 @@ describe Event do
end
end
describe '#scheduled?' do
it { expect(event.scheduled?).to eq false }
it 'returns true if the event is scheduled' do
event.room = create(:room)
event.start_time = conference.start_date.to_time
expect(event.scheduled?).to eq true
end
end
describe '#registration_possible?' do
describe 'when the event requires registration' do
before :each do
event.require_registration = true
event.max_attendees = 3
event.registrations << create(:registration)
end
it 'returns true, if the limit has not been reached' do
expect(event.registration_possible?).to eq true
end
it 'returns false, if the limit has been reached' do
event.registrations << create(:registration)
event.registrations << create(:registration)
expect(event.registration_possible?).to eq false
end
end
describe 'when the event does not require registration' do
it 'returns false' do
expect(event.registration_possible?).to eq false
end
end
end
describe '#voted?' do
it 'returns nil if the event has no votes' do
expect(event.voted?(event, user)).to eq nil

View file

@ -4,7 +4,7 @@ describe 'Registration' do
subject { create(:registration) }
let!(:user) { create(:user) }
let!(:conference) { create(:conference) }
let!(:registration1) { create(:registration, conference: conference, user: user) }
let!(:registration) { create(:registration, conference: conference, user: user) }
describe 'validation' do
it 'has a valid factory' do
@ -33,7 +33,7 @@ describe 'Registration' do
it { is_expected.to have_and_belong_to_many(:qanswers) }
it { is_expected.to have_and_belong_to_many(:vchoices) }
it { is_expected.to have_many(:events_registrations) }
it { is_expected.to have_many(:workshops) }
it { is_expected.to have_many(:events) }
end
describe 'after create' do
@ -69,12 +69,34 @@ describe 'Registration' do
end
end
describe 'registration_to_events_only_if_present' do
context 'valid' do
it 'when user registers for events happening while user is at the conference' do
registration.arrival = conference.start_date
registration.departure = conference.end_date
registration.events << create(:event, program: conference.program, start_time: conference.end_date)
expect(registration.valid?).to eq true
end
end
context 'invalid' do
it 'when user registers for events happening while user is not at the conference' do
registration.arrival = conference.start_date
registration.departure = conference.start_date
registration.events << create(:event, program: conference.program, start_time: conference.end_date)
expect(registration.valid?).to eq false
end
end
end
describe '#destroy_purchased_tickets' do
it 'destroys purchased tickets if tickets are purchased' do
create(:ticket_purchase, conference: conference, user: user)
expect(user.registrations.size).to be 1
expect(user.ticket_purchases.size).to be 1
registration1.destroy
registration.destroy
expect(user.registrations.size).to be 0
expect(user.ticket_purchases.size).to be 0
end
@ -82,7 +104,7 @@ describe 'Registration' do
it 'destroys no tickets if no tickets are purchased' do
expect(user.registrations.size).to be 1
expect(user.ticket_purchases.size).to be 0
registration1.destroy
registration.destroy
expect(user.registrations.size).to be 0
expect(user.ticket_purchases.size).to be 0
end

View file

@ -11,6 +11,10 @@ describe User do
let(:organizer) { create(:user, role_ids: [organizer_role.id]) }
let(:user) { create(:user) }
let(:event1) { create(:event, program: conference.program) }
let(:another_conference) { create(:conference) }
let(:event2) { create(:event, program: another_conference.program) }
describe 'validation' do
it 'has a valid factory' do
expect(build(:user)).to be_valid
@ -26,6 +30,7 @@ describe User do
it { is_expected.to have_many(:event_users).dependent(:destroy) }
it { is_expected.to have_many(:events).through(:event_users) }
it { is_expected.to have_many(:registrations).dependent(:destroy) }
it { is_expected.to have_many(:events_registrations).through(:registrations) }
it { is_expected.to have_many(:ticket_purchases).dependent(:destroy) }
it { is_expected.to have_many(:tickets).through(:ticket_purchases) }
it { is_expected.to have_many(:votes).dependent(:destroy) }
@ -341,4 +346,17 @@ describe User do
expect(second_user.is_admin).to be false
end
end
describe 'has_many events_registrations' do
before :each do
registration1 = create(:registration, user: user, conference: conference)
registration2 = create(:registration, user: user, conference: another_conference)
@events_registration1 = create(:events_registration, registration: registration1, event: event1)
@events_registration2 = create(:events_registration, registration: registration2, event: event2)
end
it 'returns all the events the user registered to' do
expect(user.events_registrations).to eq [@events_registration1, @events_registration2]
end
end
end