diff --git a/ruby/lib/jam_ruby/models/rsvp_request.rb b/ruby/lib/jam_ruby/models/rsvp_request.rb index ed8ea3736..1eb651bd9 100644 --- a/ruby/lib/jam_ruby/models/rsvp_request.rb +++ b/ruby/lib/jam_ruby/models/rsvp_request.rb @@ -39,18 +39,18 @@ module JamRuby raise StateError, "Invalid session." end - # verify invitation exists for this user and session - invitation = Invitation.where("music_session_id = ? AND receiver_id = ?", music_session.id, user.id) - - if invitation.blank? && !music_session.open_rsvps - raise PermissionError, "Only a session invitee can create an RSVP for this session." - end - # verify slot IDs exist in request if params[:rsvp_slots].blank? raise StateError, "You must select at least 1 slot." end + # verify invitation exists for this user and session + invitation = Invitation.where("music_session_id = ? AND receiver_id = ?", music_session.id, user.id) + + if invitation.first.nil? && !music_session.open_rsvps + raise PermissionError, "Only a session invitee can create an RSVP for this session." + end + RsvpRequest.transaction do @rsvp = RsvpRequest.new @rsvp.user = user diff --git a/ruby/spec/factories.rb b/ruby/spec/factories.rb index 8d3c6cb61..4498ee90c 100644 --- a/ruby/spec/factories.rb +++ b/ruby/spec/factories.rb @@ -93,6 +93,7 @@ FactoryGirl.define do recurring_mode 'once' genre JamRuby::Genre.first association :creator, :factory => :user + open_rsvps true factory :recurring_music_session_weekly do recurring_mode 'weekly' @@ -476,21 +477,12 @@ FactoryGirl.define do end factory :rsvp_slot, class: JamRuby::RsvpSlot do - association :instrument, factory: :instrument - association :music_session, factory: :music_session - # association :rsvp_request_slot, factory: :rsvp_request_slot proficiency_level 'beginner' end factory :rsvp_request, class: JamRuby::RsvpRequest do - association :user, factory: :user - association :rsvp_slot, factory: :rsvp_slot - association :rsvp_request_slot, factory: :rsvp_request_slot canceled false - end - - factory :rsvp_request_slot, class: JamRuby::RsvpRequestRsvpSlot do - chosen false + cancel_all false end factory :latency_tester, :class => JamRuby::LatencyTester do diff --git a/ruby/spec/jam_ruby/models/rsvp_request_spec.rb b/ruby/spec/jam_ruby/models/rsvp_request_spec.rb index 50a2ff2ef..8d84b7c8b 100644 --- a/ruby/spec/jam_ruby/models/rsvp_request_spec.rb +++ b/ruby/spec/jam_ruby/models/rsvp_request_spec.rb @@ -2,7 +2,135 @@ require 'spec_helper' describe RsvpRequest do - it "success" do - # FactoryGirl.create(:rsvp_request) + before(:each) do + 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 + + @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 + # u = FactoryGirl.create(:user) + # r = FactoryGirl.create(:user) + # s = FactoryGirl.create(:music_session) + # invitation = Invitation.new(:sender => u, :receiver => r, :music_session => s) + # invitation.save + + # x = Invitation.all + # puts x.count + + 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 + end + + it "should allow non-invitee to RSVP to session with open RSVPs" do + + # verify notification was created + 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) + + request_slots = RsvpRequestRsvpSlot.all + + # accept 1 of the slots + rs1 = RsvpRequestRsvpSlot.find_by_rsvp_slot_id(@slot1.id) + rs2 = RsvpRequestRsvpSlot.find_by_rsvp_slot_id(@slot2.id) + 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 + @music_session.open_rsvps = false + @music_session.save + + expect {RsvpRequest.create({:session_id => @music_session.id, :rsvp_slots => [@slot1.id, @slot2.id]}, @non_session_invitee)}.to raise_error(JamRuby::PermissionError) + end + + it "should allow invitee to RSVP to session with closed RSVPs" do + rsvp = RsvpRequest.create({:session_id => @music_session.id, :rsvp_slots => [@slot1.id, @slot2.id], :message => "Let's Jam!"}, @session_invitee) + + # verify comment + + # verify 2 notification were created + end + end + + describe "index" do + it "should allow retrieval of RSVPs by session" do + end + + it "should allow retrieval of RSVPs by session and user" do + end + end + + describe "update" do + it "should only allow session organizer to approve request" do + # attempt to approve with non-organizer + + # approve with organizer + + # verify notification was created + end + + it "should not allow approval of RSVP for a slot that has already been approved" do + end + end + + + describe "cancel" do + it "should allow session organizer to cancel" do + end + + it "should allow RSVP creator to cancel" do + # test comment + + # verify notification was created + end + + it "should not allow anyone else to cancel" do + end + + it "should allow user to cancel a single session" do + end + + it "should allow user to cancel all future sessions" do + end end end \ No newline at end of file