2014-05-14 20:48:42 +00:00
require 'spec_helper'
describe RsvpRequest do
2014-06-07 17:01:03 +00:00
before ( :each ) do
2014-06-10 04:35:38 +00:00
SessionInfoComment . delete_all
Notification . delete_all
2014-06-07 17:01:03 +00:00
RsvpRequestRsvpSlot . delete_all
RsvpRequest . delete_all
RsvpSlot . delete_all
Invitation . delete_all
MusicSession . delete_all
User . delete_all
@session_invitee = FactoryGirl . create ( :user )
@session_invitee . save
@non_session_invitee = FactoryGirl . create ( :user )
@non_session_invitee . save
@session_creator = FactoryGirl . create ( :user )
@session_creator . save
2014-06-07 20:37:50 +00:00
# session invitations require sender and receiver to be friends
FactoryGirl . create ( :friendship , :user = > @session_invitee , :friend = > @session_creator )
FactoryGirl . create ( :friendship , :user = > @session_creator , :friend = > @session_invitee )
2014-06-07 17:01:03 +00:00
@music_session = FactoryGirl . build ( :music_session , :creator = > @session_creator )
@music_session . save
@slot1 = FactoryGirl . build ( :rsvp_slot , :music_session = > @music_session , :instrument = > JamRuby :: Instrument . find ( 'electric guitar' ) )
@slot1 . save
@slot2 = FactoryGirl . build ( :rsvp_slot , :music_session = > @music_session , :instrument = > JamRuby :: Instrument . find ( 'drums' ) )
@slot2 . save
@invitation = FactoryGirl . build ( :invitation , :sender = > @session_creator , :receiver = > @session_invitee , :music_session = > @music_session )
@invitation . save
end
describe " create " do
it " should require a valid music session " do
expect { RsvpRequest . create ( { :session_id = > " 1234 " , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] } , @session_invitee ) } . to raise_error ( JamRuby :: StateError )
end
it " should require at least 1 slot " do
expect { RsvpRequest . create ( { :session_id = > @music_session . id } , @session_invitee ) } . to raise_error ( JamRuby :: StateError )
end
it " should not allow user to RSVP for slot he has already RSVPed to " do
2014-06-07 17:19:02 +00:00
# allow open RSVPs
@music_session . open_rsvps = true
@music_session . save
RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] } , @non_session_invitee )
expect { RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] } , @non_session_invitee ) } . to raise_error ( JamRuby :: StateError )
end
it " should allow invitee to RSVP to session with closed RSVPs " do
2014-06-10 04:35:38 +00:00
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " We be jammin! " } , @session_invitee )
2014-06-07 17:19:02 +00:00
# verify comment
2014-06-10 04:35:38 +00:00
comment = SessionInfoComment . find_by_creator_id ( @session_invitee )
comment . comment . should == " We be jammin! "
# verify 2 notifications were created for this music session (1 for RSVP and 1 for comment)
notifications = Notification . where ( :session_id = > @music_session . id )
notifications . count . should == 2
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_RSVP ) . count . should == 1
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_COMMENT ) . count . should == 1
2014-06-07 17:01:03 +00:00
end
it " should allow non-invitee to RSVP to session with open RSVPs " do
2014-06-07 17:19:02 +00:00
# allow open RSVPs
@music_session . open_rsvps = true
@music_session . save
expect { RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] } , @non_session_invitee ) } . to_not raise_error
2014-06-07 17:01:03 +00:00
# verify notification was created
2014-06-07 17:19:02 +00:00
n = Notification . find_by_source_user_id ( @non_session_invitee . id )
n . description . should == NotificationTypes :: SCHEDULED_SESSION_RSVP
2014-06-07 17:01:03 +00:00
end
it " should not allow user to RSVP to slot that has already been accepted " do
# allow open RSVPs
@music_session . open_rsvps = true
@music_session . save
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] } , @non_session_invitee )
# accept 1 of the slots
rs1 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot1 . id )
rs2 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot2 . id )
2014-06-19 02:32:21 +00:00
rs1 . chosen . should == nil
rs2 . chosen . should == nil
2014-06-07 17:01:03 +00:00
RsvpRequest . update ( { :id = > rsvp . id , :session_id = > @music_session . id , :rsvp_responses = > [ { :request_slot_id = > rs1 . id , :accept = > true } , { :request_slot_id = > rs2 . id , :accept = > false } ] } , @session_creator )
# attempt to create a request for the already accepted slot
expect { RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ rs1 . id ] } , @non_session_invitee ) } . to raise_error ( JamRuby :: StateError )
end
it " should not allow non-invitee to RSVP to session with closed RSVPs " do
2014-07-04 13:26:46 +00:00
@music_session . open_rsvps = false
@music_session . save!
2014-06-07 17:01:03 +00:00
expect { RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] } , @non_session_invitee ) } . to raise_error ( JamRuby :: PermissionError )
end
2014-06-16 12:11:05 +00:00
2014-06-19 02:32:21 +00:00
it " should allow RSVP creation with autoapprove option for open RSVP sessions " do
2014-06-16 12:11:05 +00:00
# allow open RSVPs
@music_session . open_rsvps = true
@music_session . save
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :autoapprove = > true } , @session_creator )
rs1 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot1 . id )
rs2 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot2 . id )
rs1 . chosen . should == true
rs2 . chosen . should == true
end
2014-06-19 02:32:21 +00:00
it " should allow RSVP creation with autoapprove option for closed RSVP sessions " do
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :autoapprove = > true } , @session_creator )
rs1 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot1 . id )
rs2 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot2 . id )
rs1 . chosen . should == true
rs2 . chosen . should == true
end
2014-06-07 17:01:03 +00:00
end
describe " index " do
it " should allow retrieval of RSVPs by session " do
2014-06-07 17:19:02 +00:00
# allow open RSVPs
@music_session . open_rsvps = true
@music_session . save
user2 = FactoryGirl . create ( :user )
RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " Let's Jam! " } , @non_session_invitee )
2014-06-10 04:35:38 +00:00
RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id ] , :message = > " Let's Jam 2! " } , user2 )
2014-06-07 17:19:02 +00:00
rsvps = RsvpRequest . index ( @music_session )
rsvps . count . should == 2
2014-06-07 17:01:03 +00:00
end
it " should allow retrieval of RSVPs by session and user " do
2014-06-07 17:19:02 +00:00
# allow open RSVPs
@music_session . open_rsvps = true
@music_session . save
user2 = FactoryGirl . create ( :user )
RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " Let's Jam! " } , @non_session_invitee )
2014-06-10 04:35:38 +00:00
RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id ] , :message = > " Let's Jam 2! " } , user2 )
2014-06-07 17:19:02 +00:00
rsvps = RsvpRequest . index ( @music_session , @non_session_invitee )
rsvps . count . should == 1
2014-06-07 17:01:03 +00:00
end
2014-06-16 05:46:52 +00:00
it " should allow retrieval of RSVps by session and approval status " do
# allow open RSVPs
@music_session . open_rsvps = true
@music_session . save
approved_rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] } , @non_session_invitee )
rs1 = RsvpRequestRsvpSlot . find_by_rsvp_request_id_and_rsvp_slot_id ( approved_rsvp . id , @slot1 . id )
rs2 = RsvpRequestRsvpSlot . find_by_rsvp_request_id_and_rsvp_slot_id ( approved_rsvp . id , @slot2 . id )
RsvpRequest . update ( { :id = > approved_rsvp . id , :session_id = > @music_session . id , :rsvp_responses = > [ { :request_slot_id = > rs1 . id , :accept = > true } , { :request_slot_id = > rs2 . id , :accept = > false } ] } , @session_creator )
user2 = FactoryGirl . create ( :user )
declined_rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot2 . id ] } , user2 )
rs2 = RsvpRequestRsvpSlot . find_by_rsvp_request_id_and_rsvp_slot_id ( declined_rsvp , @slot2 . id )
RsvpRequest . update ( { :id = > declined_rsvp . id , :session_id = > @music_session . id , :rsvp_responses = > [ { :request_slot_id = > rs2 . id , :accept = > false } ] } , @session_creator )
user3 = FactoryGirl . create ( :user )
pending_rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot2 . id ] } , user3 )
rsvps = RsvpRequest . index ( @music_session , nil , { :status = > " pending " } )
rsvps . count . should == 1
rsvps = RsvpRequest . index ( @music_session , user3 , { :status = > " pending " } )
rsvps . count . should == 1
rsvps = RsvpRequest . index ( @music_session , nil , { :status = > " approved " } )
rsvps . count . should == 1
rsvps = RsvpRequest . index ( @music_session , @non_session_invitee , { :status = > " approved " } )
rsvps . count . should == 1
rsvps = RsvpRequest . index ( @music_session )
rsvps . count . should == 3
end
2014-06-07 17:01:03 +00:00
end
describe " update " do
it " should only allow session organizer to approve request " do
2014-06-10 04:35:38 +00:00
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " Let's Jam! " } , @session_invitee )
2014-06-07 17:01:03 +00:00
# attempt to approve with non-organizer
2014-06-10 04:35:38 +00:00
rs1 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot1 . id )
rs2 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot2 . id )
expect { RsvpRequest . update ( { :id = > rsvp . id , :session_id = > @music_session . id , :rsvp_responses = > [ { :request_slot_id = > rs1 . id , :accept = > true } , { :request_slot_id = > rs2 . id , :accept = > true } ] } , @session_invitee ) } . to raise_error ( PermissionError )
2014-06-07 17:01:03 +00:00
# approve with organizer
2014-06-10 04:35:38 +00:00
rs1 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot1 . id )
rs2 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot2 . id )
expect { RsvpRequest . update ( { :id = > rsvp . id , :session_id = > @music_session . id , :rsvp_responses = > [ { :request_slot_id = > rs1 . id , :accept = > true } , { :request_slot_id = > rs2 . id , :accept = > true } ] } , @session_creator ) } . to_not raise_error
2014-06-07 17:01:03 +00:00
# verify notification was created
2014-06-10 04:35:38 +00:00
n = Notification . find_by_source_user_id ( @session_creator . id )
n . description . should == NotificationTypes :: SCHEDULED_SESSION_RSVP_APPROVED
2014-06-07 17:01:03 +00:00
end
it " should not allow approval of RSVP for a slot that has already been approved " do
2014-06-10 04:35:38 +00:00
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " Let's Jam! " } , @session_invitee )
# approve
rs1 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot1 . id )
rs2 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot2 . id )
expect { RsvpRequest . update ( { :id = > rsvp . id , :session_id = > @music_session . id , :rsvp_responses = > [ { :request_slot_id = > rs1 . id , :accept = > true } , { :request_slot_id = > rs2 . id , :accept = > true } ] } , @session_creator ) } . to_not raise_error
# approve again
rs1 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot1 . id )
rs2 = RsvpRequestRsvpSlot . find_by_rsvp_slot_id ( @slot2 . id )
expect { RsvpRequest . update ( { :id = > rsvp . id , :session_id = > @music_session . id , :rsvp_responses = > [ { :request_slot_id = > rs1 . id , :accept = > true } , { :request_slot_id = > rs2 . id , :accept = > true } ] } , @session_creator ) } . to raise_error ( StateError )
2014-06-07 17:01:03 +00:00
end
end
describe " cancel " do
2014-06-10 04:35:38 +00:00
it " should allow session organizer to cancel an RSVP to a single session " do
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " Let's Jam! " } , @session_invitee )
# verify comment
comment = SessionInfoComment . find_by_creator_id ( @session_invitee )
comment . comment . should == " Let's Jam! "
# cancel
expect { RsvpRequest . cancel ( { :id = > rsvp . id , :session_id = > @music_session . id , :cancelled = > " yes " , :message = > " Sorry, you're booted for this session " } , @session_creator ) } . to_not raise_error
rsvp = RsvpRequest . find_by_id ( rsvp . id )
rsvp . canceled . should == true
rsvp . cancel_all . should == false
# verify comment
comment = SessionInfoComment . find_by_creator_id ( @session_creator )
comment . comment . should == " Sorry, you're booted for this session "
# verify 4 notifications were created for this music session (1 for RSVP, 1 for cancellation, and 2 for comments)
notifications = Notification . where ( :session_id = > @music_session . id )
notifications . count . should == 4
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_RSVP ) . count . should == 1
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_RSVP_CANCELLED_ORG ) . count . should == 1
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_COMMENT ) . count . should == 2
2014-06-07 17:01:03 +00:00
end
2014-06-10 04:35:38 +00:00
it " should allow session organizer to cancel an RSVP for all future sessions " do
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " Let's Jam! " } , @session_invitee )
2014-06-07 17:01:03 +00:00
2014-06-10 04:35:38 +00:00
# verify comment
comment = SessionInfoComment . find_by_creator_id ( @session_invitee )
comment . comment . should == " Let's Jam! "
# cancel
expect { RsvpRequest . cancel ( { :id = > rsvp . id , :session_id = > @music_session . id , :cancelled = > " all " , :message = > " Sorry, you're booted for this session " } , @session_creator ) } . to_not raise_error
rsvp = RsvpRequest . find_by_id ( rsvp . id )
rsvp . canceled . should == true
rsvp . cancel_all . should == true
# verify comment
comment = SessionInfoComment . find_by_creator_id ( @session_creator )
comment . comment . should == " Sorry, you're booted for this session "
# verify 4 notifications were created for this music session (1 for RSVP, 1 for cancellation, and 2 for comments)
notifications = Notification . where ( :session_id = > @music_session . id )
notifications . count . should == 4
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_RSVP ) . count . should == 1
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_RSVP_CANCELLED_ORG ) . count . should == 1
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_COMMENT ) . count . should == 2
2014-06-07 17:01:03 +00:00
end
2014-06-10 04:35:38 +00:00
it " should allow RSVP creator to cancel an RSVP to a single session " do
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " Let's Jam! " } , @session_invitee )
# verify comment
comment = SessionInfoComment . find_by_creator_id ( @session_invitee )
comment . comment . should == " Let's Jam! "
# cancel
expect { RsvpRequest . cancel ( { :id = > rsvp . id , :session_id = > @music_session . id , :cancelled = > " yes " , :message = > " Sorry, I'm bailing for this session " } , @session_invitee ) } . to_not raise_error
rsvp = RsvpRequest . find_by_id ( rsvp . id )
rsvp . canceled . should == true
rsvp . cancel_all . should == false
# verify comment
comment = SessionInfoComment . find_by_creator_id ( @session_invitee )
comment . comment . should == " Sorry, I'm bailing for this session "
# verify 4 notifications were created for this music session (1 for RSVP, 1 for cancellation, and 2 for comments)
notifications = Notification . where ( :session_id = > @music_session . id )
notifications . count . should == 4
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_RSVP ) . count . should == 1
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_RSVP_CANCELLED ) . count . should == 1
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_COMMENT ) . count . should == 2
2014-06-07 17:01:03 +00:00
end
2014-06-10 04:35:38 +00:00
it " should allow RSVP creator to cancel an RSVP for all future sessions " do
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " Let's Jam! " } , @session_invitee )
# verify comment
comment = SessionInfoComment . find_by_creator_id ( @session_invitee )
comment . comment . should == " Let's Jam! "
# cancel
expect { RsvpRequest . cancel ( { :id = > rsvp . id , :session_id = > @music_session . id , :cancelled = > " all " , :message = > " Sorry, I'm bailing for all sessions " } , @session_invitee ) } . to_not raise_error
rsvp = RsvpRequest . find_by_id ( rsvp . id )
rsvp . canceled . should == true
rsvp . cancel_all . should == true
# verify comment
comment = SessionInfoComment . find_by_creator_id ( @session_invitee )
comment . comment . should == " Sorry, I'm bailing for all sessions "
# verify 4 notifications were created for this music session (1 for RSVP, 1 for cancellation, and 2 for comments)
notifications = Notification . where ( :session_id = > @music_session . id )
notifications . count . should == 4
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_RSVP ) . count . should == 1
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_RSVP_CANCELLED ) . count . should == 1
notifications . where ( :description = > NotificationTypes :: SCHEDULED_SESSION_COMMENT ) . count . should == 2
2014-06-07 17:01:03 +00:00
end
2014-06-10 04:35:38 +00:00
it " should not allow anyone else to cancel " do
user = FactoryGirl . create ( :user )
rsvp = RsvpRequest . create ( { :session_id = > @music_session . id , :rsvp_slots = > [ @slot1 . id , @slot2 . id ] , :message = > " Let's Jam! " } , @session_invitee )
# verify comment
comment = SessionInfoComment . find_by_creator_id ( @session_invitee )
comment . comment . should == " Let's Jam! "
# cancel
expect { RsvpRequest . cancel ( { :id = > rsvp . id , :session_id = > @music_session . id , :cancelled = > " all " , :message = > " I'm gonna cancel all your RSVPs " } , user ) } . to raise_error ( PermissionError )
2014-06-07 17:01:03 +00:00
end
2014-05-14 20:48:42 +00:00
end
2014-07-07 21:08:36 +00:00
describe " instrument_list " do
it " single instrument " do
rsvp_request = FactoryGirl . create ( :rsvp_request , rsvp_slots : [ @slot1 ] , user : @session_invitee )
rsvp_request . instrument_list . should == [ JamRuby :: Instrument . find ( 'electric guitar' ) ]
end
it " multiple instruments " do
rsvp_request = FactoryGirl . create ( :rsvp_request , rsvp_slots : [ @slot1 , @slot2 ] , user : @session_invitee )
rsvp_request . instrument_list . should == [ @slot1 . instrument , @slot2 . instrument ]
end
end
2014-05-14 20:48:42 +00:00
end