2014-06-16 20:41:37 +00:00
require 'spec_helper'
describe ApiMusicSessionsController do
render_views
let ( :tracks ) { [ { 'sound' = > 'mono' , 'client_track_id' = > 'abc' , 'instrument_id' = > 'piano' } ] }
2014-06-17 19:10:24 +00:00
let ( :user ) { FactoryGirl . create ( :user , last_jam_locidispid : 1 ) }
let ( :conn ) { FactoryGirl . create ( :connection , :user = > user , ip_address : '1.1.1.1' , locidispid : 1 , addr : 1 ) }
let ( :other ) { FactoryGirl . create ( :user , last_jam_locidispid : 2 ) }
let ( :other_conn ) { FactoryGirl . create ( :connection , user : other , ip_address : '2.2.2.2' , locidispid : 2 , addr : 2 ) }
2014-06-16 20:41:37 +00:00
let ( :third_user ) { FactoryGirl . create ( :user ) }
2014-06-17 19:10:24 +00:00
let ( :network_score ) { 20 }
2014-06-16 20:41:37 +00:00
before ( :each ) do
controller . current_user = user
ActiveMusicSession . delete_all
MusicSession . delete_all
2014-06-17 19:10:24 +00:00
Score . delete_all
2014-06-16 20:41:37 +00:00
end
describe " ams_index " do
it " no results " do
get :ams_index , { client_id : conn . client_id }
response . should be_success
json = JSON . parse ( response . body , :symbolize_names = > true )
2014-08-18 15:37:55 +00:00
json [ :sessions ] . length . should == 0
2014-06-16 20:41:37 +00:00
end
it " just self " do
# create a session with self in it
ams = FactoryGirl . create ( :active_music_session , creator : user )
conn . join_the_session ( ams . music_session , true , tracks , user , 10 )
conn . errors . any? . should be_false
get :ams_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
2014-08-18 15:37:55 +00:00
json [ :sessions ] . length . should == 1
json [ :sessions ] [ 0 ] [ :active_music_session ] [ :participants ] [ 0 ] [ :user ] [ :full_score ] . should be_nil
2014-06-16 20:41:37 +00:00
end
it " someone else with no score with self " do
# create a session with someone else in it, but no score
ams = FactoryGirl . create ( :active_music_session , creator : other )
other_conn . join_the_session ( ams . music_session , true , tracks , user , 10 )
other_conn . errors . any? . should be_false
get :ams_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
2014-08-18 15:37:55 +00:00
json [ :sessions ] . length . should == 1
json [ :sessions ] [ 0 ] [ :active_music_session ] [ :participants ] [ 0 ] [ :user ] [ :full_score ] . should be_nil
2014-06-16 20:41:37 +00:00
end
it " someone else with a score with a self " do
# create a session with someone else in it, but no score
ams = FactoryGirl . create ( :active_music_session , creator : other )
other_conn . join_the_session ( ams . music_session , true , tracks , other , 10 )
other_conn . errors . any? . should be_false
2014-07-29 22:52:20 +00:00
Score . createx ( conn . locidispid , conn . client_id , conn . addr , other_conn . locidispid , other_conn . client_id , other_conn . addr , network_score , nil , nil , { auserid : user . id , buserid : other . id } )
2014-06-16 20:41:37 +00:00
get :ams_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
2014-08-18 15:37:55 +00:00
json [ :sessions ] . length . should == 1
2014-08-20 22:06:57 +00:00
json [ :sessions ] [ 0 ] [ :active_music_session ] [ :participants ]
2014-08-18 15:37:55 +00:00
json [ :sessions ] [ 0 ] [ :active_music_session ] [ :participants ] [ 0 ] [ :user ] [ :full_score ] . should == 35 # 5 + 20 + 10
2014-06-16 20:41:37 +00:00
end
it " someone else with a score with a self, and another with an approved RSVP " do
# create a session with someone else in it, but no score
ams = FactoryGirl . create ( :active_music_session , creator : other )
other_conn . join_the_session ( ams . music_session , true , tracks , other , 10 )
other_conn . errors . any? . should be_false
2014-06-16 23:13:39 +00:00
# we need to make sure that third_user, (the RSVP user) has a locidispid matching the searching user
third_user . last_jam_audio_latency = 10 # RSVP's are an 'offline' search, meaning they use user.last_jam_audio_latency instead of connection.last_jam_audio_latency
third_user . last_jam_locidispid = conn . locidispid
third_user . save!
2014-07-29 22:52:20 +00:00
Score . createx ( conn . locidispid , conn . client_id , conn . addr , other_conn . locidispid , other_conn . client_id , other_conn . addr , network_score , nil , nil , { auserid : user . id , buserid : other . id } )
2014-06-16 20:41:37 +00:00
2014-06-17 19:10:24 +00:00
# set up a second RSVP (other than the creators, pointing to the third_user)
2014-06-16 20:41:37 +00:00
rsvp_slot = FactoryGirl . create ( :rsvp_slot , music_session : ams . music_session , instrument : Instrument . find ( 'piano' ) )
rsvp_request = FactoryGirl . create ( :rsvp_request , user : third_user )
rsvp_request_rsvp_slot = FactoryGirl . create ( :rsvp_request_rsvp_slot , chosen : true , rsvp_request : rsvp_request , rsvp_slot : rsvp_slot )
get :ams_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
2014-08-18 15:37:55 +00:00
json [ :sessions ] . length . should == 1
json [ :sessions ] [ 0 ] [ :active_music_session ] [ :participants ] [ 0 ] [ :user ] [ :full_score ] . should_not be_nil
json [ :sessions ] [ 0 ] [ :approved_rsvps ] . length . should == 2
2014-06-17 19:10:24 +00:00
2014-08-18 15:37:55 +00:00
if json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 0 ] [ :id ] == third_user . id
found_third_user = json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 0 ]
found_creator = json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 1 ]
2014-06-17 19:10:24 +00:00
found_creator [ :id ] . should == other . id
else
2014-08-18 15:37:55 +00:00
found_third_user = json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 1 ]
found_creator = json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 0 ]
2014-06-17 19:10:24 +00:00
found_third_user [ :id ] . should == third_user . id
found_creator [ :id ] . should == other . id
end
2014-08-18 15:37:55 +00:00
found_third_user [ :full_score ] . should == nil
found_creator [ :full_score ] . should == ( ( network_score + user . last_jam_audio_latency + other . last_jam_audio_latency ) ) . ceil
2014-06-17 19:10:24 +00:00
end
end
describe " sms_index " do
it " no results " do
get :sms_index , { client_id : conn . client_id }
response . should be_success
json = JSON . parse ( response . body , :symbolize_names = > true )
2014-08-18 15:37:55 +00:00
json [ :sessions ] . length . should == 0
2014-06-17 19:10:24 +00:00
end
it " just self " do
# create a session with self in it
sms = FactoryGirl . create ( :music_session , creator : user )
get :sms_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
2014-08-18 15:37:55 +00:00
json [ :sessions ] . length . should == 1
json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 0 ] [ :id ] . should == user . id
json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 0 ] [ :full_score ] . should be_nil # you don't get scores to self
2014-06-16 20:41:37 +00:00
end
2014-06-17 19:10:24 +00:00
it " someone else with no score with self " do
# create a session with someone else in it, but no score
sms = FactoryGirl . create ( :music_session , creator : other )
get :sms_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
2014-08-18 15:37:55 +00:00
json [ :sessions ] . length . should == 1
json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 0 ] [ :id ] . should == other . id
json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 0 ] [ :full_score ] . should be_nil # there is no score with 'other '
2014-06-17 19:10:24 +00:00
end
it " scores with invitees and RSVP's " do
# create a session with someone else in it, but no score
sms = FactoryGirl . create ( :music_session , creator : other )
2014-07-29 22:52:20 +00:00
Score . createx ( conn . locidispid , conn . client_id , conn . addr , other_conn . locidispid , other_conn . client_id , other_conn . addr , network_score , nil , nil , { auserid : user . id , buserid : other . id } )
2014-06-17 19:10:24 +00:00
invitee = FactoryGirl . create ( :user , last_jam_audio_latency : 30 , last_jam_locidispid : 3 )
FactoryGirl . create ( :friendship , user : other , friend : invitee )
FactoryGirl . create ( :friendship , user : invitee , friend : other )
FactoryGirl . create ( :invitation , sender : other , receiver : invitee , music_session : sms )
2014-07-29 22:52:20 +00:00
Score . createx ( invitee . last_jam_locidispid , 'immaterial' , 1 , conn . locidispid , conn . client_id , conn . addr , network_score , nil , nil , { auserid : invitee . id , buserid : user . id } )
2014-06-17 19:10:24 +00:00
get :sms_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
2014-08-18 15:37:55 +00:00
json [ :sessions ] . length . should == 1
json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 0 ] [ :id ] . should == other . id
json [ :sessions ] [ 0 ] [ :approved_rsvps ] [ 0 ] [ :full_score ] . should == ( ( network_score + user . last_jam_audio_latency + other . last_jam_audio_latency ) ) . ceil
json [ :sessions ] [ 0 ] [ :invitations ] [ 0 ] [ :receiver_id ] . should == invitee . id
json [ :sessions ] [ 0 ] [ :invitations ] [ 0 ] [ :full_score ] . should == ( ( network_score + user . last_jam_audio_latency + invitee . last_jam_audio_latency ) ) . ceil
2014-06-17 19:10:24 +00:00
end
2014-06-16 20:41:37 +00:00
end
end