2012-10-21 01:55:49 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
|
|
# these tests avoid the use of ActiveRecord and FactoryGirl to do blackbox, non test-instrumented tests
|
2014-07-20 02:35:48 +00:00
|
|
|
describe ConnectionManager, no_transaction: true do
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-11-16 04:35:40 +00:00
|
|
|
TRACKS = [{"instrument_id" => "electric guitar", "sound" => "mono", "client_track_id" => "some_client_track_id"}]
|
2014-05-01 19:09:33 +00:00
|
|
|
STALE_TIME = 40
|
|
|
|
|
EXPIRE_TIME = 60
|
|
|
|
|
STALE_BUT_NOT_EXPIRED = 50
|
|
|
|
|
DEFINITELY_EXPIRED = 70
|
2014-09-13 03:30:51 +00:00
|
|
|
REACHABLE = true
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2014-06-19 19:05:33 +00:00
|
|
|
let(:channel_id) {'1'}
|
|
|
|
|
|
2012-10-23 00:59:35 +00:00
|
|
|
before do
|
2012-10-21 01:55:49 +00:00
|
|
|
@conn = PG::Connection.new(:dbname => SpecDb::TEST_DB_NAME, :user => "postgres", :password => "postgres", :host => "localhost")
|
2012-10-23 00:59:35 +00:00
|
|
|
@connman = ConnectionManager.new(:conn => @conn)
|
2012-10-21 01:55:49 +00:00
|
|
|
@message_factory = MessageFactory.new
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-18 02:59:59 +00:00
|
|
|
def create_user(first_name, last_name, email, options = {:musician => true})
|
2013-11-03 07:49:51 +00:00
|
|
|
@conn.exec("INSERT INTO users (first_name, last_name, email, musician, encrypted_password, city, state, country) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id", [first_name, last_name, email, options[:musician], '1', 'Apex', 'NC', 'US']) do |result|
|
2012-10-21 15:05:06 +00:00
|
|
|
return result.getvalue(0, 0)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def assert_num_connections(client_id, expected_num_connections)
|
|
|
|
|
# make sure the connection is still there
|
|
|
|
|
@conn.exec("SELECT count(*) FROM connections where client_id = $1", [client_id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).to_i.should == expected_num_connections
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def assert_session_exists(music_session_id, exists)
|
2014-05-06 23:02:00 +00:00
|
|
|
@conn.exec("SELECT count(*) FROM active_music_sessions where id = $1", [music_session_id]) do |result|
|
2012-10-21 15:05:06 +00:00
|
|
|
if exists
|
|
|
|
|
result.getvalue(0, 0).should == "1"
|
|
|
|
|
else
|
|
|
|
|
result.getvalue(0, 0).should == "0"
|
|
|
|
|
end
|
2012-10-21 01:55:49 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "can't create two client_ids of same value" do
|
|
|
|
|
client_id = "client_id1"
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id = create_user("test", "user1", "user1@jamkazam.com")
|
2014-03-03 20:31:25 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
user.musician_instruments << FactoryGirl.build(:musician_instrument, user: user)
|
|
|
|
|
user.save!
|
|
|
|
|
user = nil
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
|
|
|
|
expect { @connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE) }.to raise_error(PG::Error)
|
2012-10-21 01:55:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "create connection then delete it" do
|
2014-03-03 20:54:07 +00:00
|
|
|
|
2012-10-21 01:55:49 +00:00
|
|
|
client_id = "client_id2"
|
2014-03-03 20:31:25 +00:00
|
|
|
#user_id = create_user("test", "user2", "user2@jamkazam.com")
|
|
|
|
|
user = FactoryGirl.create(:user)
|
|
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
count = @connman.create_connection(user.id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2014-03-03 20:31:25 +00:00
|
|
|
|
2013-08-07 15:35:27 +00:00
|
|
|
count.should == 1
|
2014-03-03 20:31:25 +00:00
|
|
|
|
2012-10-21 01:55:49 +00:00
|
|
|
# make sure the connection is seen
|
2012-11-30 15:23:43 +00:00
|
|
|
|
2014-03-03 20:31:25 +00:00
|
|
|
@conn.exec("SELECT count(*) FROM connections where user_id = $1", [user.id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).to_i.should == 1
|
2012-10-21 01:55:49 +00:00
|
|
|
end
|
|
|
|
|
|
2013-02-06 13:14:00 +00:00
|
|
|
cc = Connection.find_by_client_id!(client_id)
|
|
|
|
|
cc.connected?.should be_true
|
2014-03-03 20:31:25 +00:00
|
|
|
cc.ip_address.should eql("1.1.1.1")
|
|
|
|
|
cc.addr.should == 0x01010101
|
2014-03-03 20:54:07 +00:00
|
|
|
cc.locidispid.should == 17192000002
|
|
|
|
|
|
|
|
|
|
count = @connman.delete_connection(client_id)
|
|
|
|
|
count.should == 0
|
|
|
|
|
|
|
|
|
|
@conn.exec("SELECT count(*) FROM connections where user_id = $1", [user.id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).to_i.should == 0
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "create connection, reconnect, then delete it" do
|
|
|
|
|
|
|
|
|
|
client_id = "client_id3"
|
|
|
|
|
#user_id = create_user("test", "user2", "user2@jamkazam.com")
|
|
|
|
|
user = FactoryGirl.create(:user)
|
|
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
count = @connman.create_connection(user.id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2014-03-03 20:54:07 +00:00
|
|
|
|
|
|
|
|
count.should == 1
|
|
|
|
|
|
|
|
|
|
# make sure the connection is seen
|
|
|
|
|
|
|
|
|
|
@conn.exec("SELECT count(*) FROM connections where user_id = $1", [user.id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).to_i.should == 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
cc = Connection.find_by_client_id!(client_id)
|
|
|
|
|
cc.connected?.should be_true
|
|
|
|
|
cc.ip_address.should eql("1.1.1.1")
|
|
|
|
|
cc.addr.should == 0x01010101
|
|
|
|
|
cc.locidispid.should == 17192000002
|
2014-09-17 15:21:42 +00:00
|
|
|
cc.udp_reachable.should == true
|
2014-03-03 20:31:25 +00:00
|
|
|
|
2014-09-17 15:21:42 +00:00
|
|
|
@connman.reconnect(cc, channel_id, nil, "33.1.2.3", STALE_TIME, EXPIRE_TIME, false)
|
2014-03-03 20:54:07 +00:00
|
|
|
|
|
|
|
|
cc = Connection.find_by_client_id!(client_id)
|
|
|
|
|
cc.connected?.should be_true
|
|
|
|
|
cc.ip_address.should eql("33.1.2.3")
|
|
|
|
|
cc.addr.should == 0x21010203
|
|
|
|
|
cc.locidispid.should == 30350000003
|
2014-09-17 15:21:42 +00:00
|
|
|
cc.udp_reachable.should == false
|
|
|
|
|
|
|
|
|
|
count = @connman.delete_connection(client_id)
|
|
|
|
|
count.should == 0
|
|
|
|
|
|
|
|
|
|
@conn.exec("SELECT count(*) FROM connections where user_id = $1", [user.id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).to_i.should == 0
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "create connection, reconnect via heartbeat" do
|
|
|
|
|
|
|
|
|
|
client_id = "client_id3"
|
|
|
|
|
#user_id = create_user("test", "user2", "user2@jamkazam.com")
|
|
|
|
|
user = FactoryGirl.create(:user)
|
|
|
|
|
|
|
|
|
|
count = @connman.create_connection(user.id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, false)
|
|
|
|
|
|
|
|
|
|
count.should == 1
|
|
|
|
|
|
|
|
|
|
# make sure the connection is seen
|
|
|
|
|
|
|
|
|
|
@conn.exec("SELECT count(*) FROM connections where user_id = $1", [user.id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).to_i.should == 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
cc = Connection.find_by_client_id!(client_id)
|
|
|
|
|
cc.connected?.should be_true
|
|
|
|
|
cc.ip_address.should eql("1.1.1.1")
|
|
|
|
|
cc.addr.should == 0x01010101
|
|
|
|
|
cc.locidispid.should == 17192000002
|
|
|
|
|
cc.udp_reachable.should == false
|
|
|
|
|
|
|
|
|
|
@connman.reconnect(cc, channel_id, nil, "33.1.2.3", STALE_TIME, EXPIRE_TIME, nil) # heartbeat passes nil in for udp_reachable
|
|
|
|
|
|
|
|
|
|
cc = Connection.find_by_client_id!(client_id)
|
|
|
|
|
cc.connected?.should be_true
|
|
|
|
|
cc.ip_address.should eql("33.1.2.3")
|
|
|
|
|
cc.addr.should == 0x21010203
|
|
|
|
|
cc.locidispid.should == 30350000003
|
|
|
|
|
cc.udp_reachable.should == false
|
2014-03-03 20:54:07 +00:00
|
|
|
|
2013-08-07 15:35:27 +00:00
|
|
|
count = @connman.delete_connection(client_id)
|
|
|
|
|
count.should == 0
|
2012-10-23 00:59:35 +00:00
|
|
|
|
2014-03-03 20:31:25 +00:00
|
|
|
@conn.exec("SELECT count(*) FROM connections where user_id = $1", [user.id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).to_i.should == 0
|
2012-10-21 01:55:49 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# it "create connection creates user joined message appropriately" do
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# client_id = "client_id3"
|
|
|
|
|
# client_id2 = "client_id3_1"
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# user_id = create_user("test", "user3", "user3@jamkazam.com")
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# # we should get a message saying that this user is online
|
|
|
|
|
# friend_update = @message_factory.friend_update(user_id, true)
|
|
|
|
|
# @connman.mq_router.should_receive(:publish_to_friends).with([], friend_update, user_id)
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2014-03-07 20:20:34 +00:00
|
|
|
# @connman.create_connection(user_id, client_id, "1.1.1.1", 'client')
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# # but a second connection from the same user should cause no such message
|
|
|
|
|
# @connman.should_receive(:publish_to_friends).exactly(0).times
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2014-03-07 20:20:34 +00:00
|
|
|
# @connman.create_connection(user_id, client_id2, "1.1.1.1", 'client')
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# end
|
2012-10-21 01:55:49 +00:00
|
|
|
|
|
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# it "deletes connection creates user left message appropriately" do
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# client_id = "client_id4"
|
|
|
|
|
# client_id2 = "client_id4_1"
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# user_id = create_user("test", "user4", "user4@jamkazam.com")
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# # we should get a message saying that this user is online
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2014-03-07 20:20:34 +00:00
|
|
|
# @connman.create_connection(user_id, client_id, "1.1.1.1", 'client')
|
|
|
|
|
# @connman.create_connection(user_id, client_id2, "1.1.1.1", 'client')
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# # deleting one of the two connections should cause no messages
|
|
|
|
|
# @connman.should_receive(:publish_to_friends).exactly(0).times
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# @connman.delete_connection(client_id)
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# # but deleting the final connection should cause a left message
|
|
|
|
|
# friend_update = @message_factory.friend_update(user_id, false)
|
|
|
|
|
# @connman.mq_router.should_receive(:publish_to_friends).with([], friend_update, user_id)
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# @connman.delete_connection(client_id2)
|
|
|
|
|
# end
|
2012-10-21 01:55:49 +00:00
|
|
|
|
|
|
|
|
it "lookup of friends should find mutual friends only" do
|
|
|
|
|
|
|
|
|
|
def create_friend(user_id, friend_id)
|
|
|
|
|
@conn.exec("INSERT INTO friendships(user_id, friend_id) VALUES ($1, $2)", [user_id, friend_id])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def delete_friend(user_id, friend_id)
|
|
|
|
|
@conn.exec("DELETE FROM friendships WHERE user_id = $1 AND friend_id = $2", [user_id, friend_id])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
client_id = "client_id5"
|
|
|
|
|
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id1 = create_user("test", "user5", "user5@jamkazam.com")
|
|
|
|
|
user_id2 = create_user("test", "user6", "user6@jamkazam.com")
|
|
|
|
|
user_id3 = create_user("test", "user7", "user7@jamkazam.com")
|
2012-10-21 01:55:49 +00:00
|
|
|
|
|
|
|
|
@connman.gather_friends(@conn, user_id1).should == []
|
|
|
|
|
@connman.gather_friends(@conn, user_id2).should == []
|
|
|
|
|
@connman.gather_friends(@conn, user_id3).should == []
|
|
|
|
|
|
|
|
|
|
# create one-way link
|
|
|
|
|
create_friend(user_id1, user_id2)
|
|
|
|
|
|
|
|
|
|
@connman.gather_friends(@conn, user_id1).should == []
|
|
|
|
|
@connman.gather_friends(@conn, user_id2).should == []
|
|
|
|
|
@connman.gather_friends(@conn, user_id3).should == []
|
|
|
|
|
|
|
|
|
|
# create one-way link back the other way
|
|
|
|
|
create_friend(user_id2, user_id1)
|
|
|
|
|
|
|
|
|
|
@connman.gather_friends(@conn, user_id1).should == [user_id2]
|
|
|
|
|
@connman.gather_friends(@conn, user_id2).should == [user_id1]
|
|
|
|
|
@connman.gather_friends(@conn, user_id3).should == []
|
|
|
|
|
|
|
|
|
|
# make sure a new link to user 1 > user 3 doesn't disrupt anything
|
|
|
|
|
create_friend(user_id1, user_id3)
|
|
|
|
|
|
|
|
|
|
@connman.gather_friends(@conn, user_id1).should == [user_id2]
|
|
|
|
|
@connman.gather_friends(@conn, user_id2).should == [user_id1]
|
|
|
|
|
@connman.gather_friends(@conn, user_id3).should == []
|
|
|
|
|
|
|
|
|
|
# make sure a new link to user 1 > user 3 doesn't disrupt anything
|
|
|
|
|
create_friend(user_id3, user_id1)
|
|
|
|
|
|
|
|
|
|
@connman.gather_friends(@conn, user_id1).should =~ [user_id2, user_id3]
|
|
|
|
|
@connman.gather_friends(@conn, user_id2).should == [user_id1]
|
|
|
|
|
@connman.gather_friends(@conn, user_id3).should == [user_id1]
|
2012-10-21 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
2013-02-06 13:14:00 +00:00
|
|
|
it "flag stale connection" do
|
2012-10-21 15:05:06 +00:00
|
|
|
client_id = "client_id8"
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id = create_user("test", "user8", "user8@jamkazam.com")
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2013-02-06 13:14:00 +00:00
|
|
|
|
|
|
|
|
num = JamRuby::Connection.count(:conditions => ['aasm_state = ?','connected'])
|
|
|
|
|
num.should == 1
|
|
|
|
|
assert_num_connections(client_id, num)
|
2014-05-01 19:09:33 +00:00
|
|
|
@connman.flag_stale_connections()
|
2013-02-06 13:14:00 +00:00
|
|
|
assert_num_connections(client_id, num)
|
|
|
|
|
|
2014-05-01 19:09:33 +00:00
|
|
|
conn = Connection.find_by_client_id(client_id)
|
|
|
|
|
set_updated_at(conn, Time.now - STALE_BUT_NOT_EXPIRED)
|
2013-02-06 13:14:00 +00:00
|
|
|
|
|
|
|
|
num = JamRuby::Connection.count(:conditions => ["updated_at < (NOW() - interval '#{1} second') AND aasm_state = 'connected'"])
|
|
|
|
|
num.should == 1
|
2013-02-11 06:02:25 +00:00
|
|
|
# this should change the aasm_state to stale
|
2014-05-01 19:09:33 +00:00
|
|
|
@connman.flag_stale_connections()
|
2013-02-06 13:14:00 +00:00
|
|
|
|
|
|
|
|
num = JamRuby::Connection.count(:conditions => ["updated_at < (NOW() - interval '#{1} second') AND aasm_state = 'connected'"])
|
|
|
|
|
num.should == 0
|
|
|
|
|
|
2013-02-11 06:02:25 +00:00
|
|
|
num = JamRuby::Connection.count(:conditions => ["updated_at < (NOW() - interval '#{1} second') AND aasm_state = 'stale'"])
|
2013-02-06 13:14:00 +00:00
|
|
|
num.should == 1
|
|
|
|
|
assert_num_connections(client_id, 1)
|
2012-10-21 15:05:06 +00:00
|
|
|
|
2014-05-01 19:09:33 +00:00
|
|
|
conn = Connection.find_by_client_id(client_id)
|
|
|
|
|
set_updated_at(conn, Time.now - DEFINITELY_EXPIRED)
|
|
|
|
|
|
|
|
|
|
cids = @connman.stale_connection_client_ids()
|
2013-02-21 17:36:58 +00:00
|
|
|
cids.size.should == 1
|
2014-04-30 03:01:28 +00:00
|
|
|
cids[0][:client_id].should == client_id
|
2014-05-01 19:09:33 +00:00
|
|
|
cids[0][:client_type].should == Connection::TYPE_CLIENT
|
2014-04-30 03:01:28 +00:00
|
|
|
cids[0][:music_session_id].should be_nil
|
|
|
|
|
cids[0][:user_id].should == user_id
|
|
|
|
|
|
2014-05-01 19:09:33 +00:00
|
|
|
cids.each { |cid| @connman.delete_connection(cid[:client_id]) }
|
2013-02-21 17:36:58 +00:00
|
|
|
|
2013-02-06 13:14:00 +00:00
|
|
|
assert_num_connections(client_id, 0)
|
|
|
|
|
end
|
|
|
|
|
|
2013-02-11 06:02:25 +00:00
|
|
|
it "expires stale connection" do
|
2013-02-06 13:14:00 +00:00
|
|
|
client_id = "client_id8"
|
|
|
|
|
user_id = create_user("test", "user8", "user8@jamkazam.com")
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2012-10-21 15:05:06 +00:00
|
|
|
|
2014-05-01 19:09:33 +00:00
|
|
|
conn = Connection.find_by_client_id(client_id)
|
|
|
|
|
set_updated_at(conn, Time.now - STALE_BUT_NOT_EXPIRED)
|
2012-10-21 15:05:06 +00:00
|
|
|
|
2014-05-01 19:09:33 +00:00
|
|
|
@connman.flag_stale_connections
|
2014-03-07 20:20:34 +00:00
|
|
|
assert_num_connections(client_id, 1)
|
2013-02-11 06:02:25 +00:00
|
|
|
# assert_num_connections(client_id, JamRuby::Connection.count(:conditions => ['aasm_state = ?','stale']))
|
2012-10-21 15:05:06 +00:00
|
|
|
|
2014-05-01 19:09:33 +00:00
|
|
|
@connman.expire_stale_connections
|
2012-10-21 15:05:06 +00:00
|
|
|
assert_num_connections(client_id, 1)
|
|
|
|
|
|
2014-05-01 19:09:33 +00:00
|
|
|
set_updated_at(conn, Time.now - DEFINITELY_EXPIRED)
|
2013-02-11 06:02:25 +00:00
|
|
|
# this should delete the stale connection
|
2014-05-01 19:09:33 +00:00
|
|
|
@connman.expire_stale_connections
|
2012-10-21 15:05:06 +00:00
|
|
|
assert_num_connections(client_id, 0)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "connections with music_sessions associated" do
|
|
|
|
|
|
|
|
|
|
client_id = "client_id9"
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id = create_user("test", "user9", "user9@jamkazam.com")
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = FactoryGirl.create(:active_music_session, user_id: user_id)
|
|
|
|
|
music_session_id = music_session.id
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2014-06-09 20:43:16 +00:00
|
|
|
connection = @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10)
|
2012-10-21 15:05:06 +00:00
|
|
|
|
2012-11-30 15:23:43 +00:00
|
|
|
connection.errors.any?.should be_false
|
2014-03-07 20:20:34 +00:00
|
|
|
|
2012-10-21 15:05:06 +00:00
|
|
|
assert_session_exists(music_session_id, true)
|
|
|
|
|
|
|
|
|
|
@conn.exec("SELECT music_session_id FROM connections WHERE client_id = $1", [client_id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).should == music_session_id
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-30 15:23:43 +00:00
|
|
|
@connman.delete_connection(client_id)
|
2012-10-21 15:05:06 +00:00
|
|
|
assert_num_connections(client_id, 0)
|
|
|
|
|
|
|
|
|
|
assert_session_exists(music_session_id, false)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "join_music_session fails if no connection" do
|
|
|
|
|
|
|
|
|
|
client_id = "client_id10"
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id = create_user("test", "user10", "user10@jamkazam.com")
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = FactoryGirl.create(:active_music_session, user_id: user_id)
|
|
|
|
|
music_session_id = music_session.id
|
2012-10-21 15:05:06 +00:00
|
|
|
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
2014-06-09 20:43:16 +00:00
|
|
|
expect { @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10) }.to raise_error(ActiveRecord::RecordNotFound)
|
2012-11-18 02:59:59 +00:00
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "join_music_session fails if user is a fan but wants to join as a musician" do
|
|
|
|
|
|
2012-11-30 15:23:43 +00:00
|
|
|
client_id = "client_id10.11"
|
|
|
|
|
client_id2 = "client_id10.12"
|
|
|
|
|
user_id = create_user("test", "user10.11", "user10.11@jamkazam.com", :musician => true)
|
|
|
|
|
user_id2 = create_user("test", "user10.12", "user10.12@jamkazam.com", :musician => false)
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
|
|
|
|
@connman.create_connection(user_id2, client_id2, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2012-11-18 02:59:59 +00:00
|
|
|
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = FactoryGirl.create(:active_music_session, user_id: user_id)
|
|
|
|
|
music_session_id = music_session.id
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
2014-06-09 20:43:16 +00:00
|
|
|
@connman.join_music_session(user, client_id, music_session, true, TRACKS, 10)
|
2013-06-26 15:16:28 +00:00
|
|
|
|
|
|
|
|
user = User.find(user_id2)
|
2012-11-18 02:59:59 +00:00
|
|
|
|
2014-06-09 20:43:16 +00:00
|
|
|
connection = @connman.join_music_session(user, client_id2, music_session, true, TRACKS, 10)
|
2012-11-30 15:23:43 +00:00
|
|
|
connection.errors.size.should == 1
|
2013-11-03 20:55:55 +00:00
|
|
|
connection.errors.get(:as_musician).should == [ValidationMessages::FAN_CAN_NOT_JOIN_AS_MUSICIAN]
|
2012-11-18 02:59:59 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-30 15:23:43 +00:00
|
|
|
it "as_musician is coerced to boolean" do
|
2012-11-18 02:59:59 +00:00
|
|
|
client_id = "client_id10.2"
|
|
|
|
|
|
2014-05-06 13:34:38 +00:00
|
|
|
user_id = create_user("test", "user10.2", "user10.2@jamkazam.com")
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2012-11-18 02:59:59 +00:00
|
|
|
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = FactoryGirl.create(:active_music_session, user_id: user_id)
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
2014-06-09 20:43:16 +00:00
|
|
|
connection = @connman.join_music_session(user, client_id, music_session, 'blarg', TRACKS, 10)
|
2012-11-30 15:23:43 +00:00
|
|
|
connection.errors.size.should == 0
|
|
|
|
|
connection.as_musician.should be_false
|
2012-11-18 02:59:59 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-18 03:41:12 +00:00
|
|
|
it "join_music_session fails if fan_access=false and the user is a fan" do
|
2012-11-18 02:59:59 +00:00
|
|
|
|
2012-11-30 15:23:43 +00:00
|
|
|
musician_client_id = "client_id10.3"
|
|
|
|
|
fan_client_id = "client_id10.4"
|
2013-06-26 15:16:28 +00:00
|
|
|
musician_id = create_user("test", "user10.3", "user10.3@jamkazam.com")
|
|
|
|
|
fan_id = create_user("test", "user10.4", "user10.4@jamkazam.com", :musician => false)
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(musician_id, musician_client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
|
|
|
|
@connman.create_connection(fan_id, fan_client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2013-06-26 15:16:28 +00:00
|
|
|
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = FactoryGirl.create(:active_music_session, :fan_access => false, user_id: musician_id)
|
|
|
|
|
music_session_id = music_session.id
|
2013-06-26 15:16:28 +00:00
|
|
|
|
|
|
|
|
user = User.find(musician_id)
|
|
|
|
|
|
2014-06-09 20:43:16 +00:00
|
|
|
@connman.join_music_session(user, musician_client_id, music_session, true, TRACKS, 10)
|
2012-11-30 15:23:43 +00:00
|
|
|
|
|
|
|
|
# now join the session as a fan, bt fan_access = false
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(fan_id)
|
2014-06-09 20:43:16 +00:00
|
|
|
connection = @connman.join_music_session(user, fan_client_id, music_session, false, TRACKS, 10)
|
2014-03-07 20:20:34 +00:00
|
|
|
connection.errors.size.should == 1
|
2012-10-21 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-02 06:51:52 +00:00
|
|
|
it "join_music_session fails if incorrect user_id specified" do
|
|
|
|
|
|
|
|
|
|
client_id = "client_id20"
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id = create_user("test", "user20", "user20@jamkazam.com")
|
|
|
|
|
user_id2 = create_user("test", "user21", "user21@jamkazam.com")
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = FactoryGirl.create(:active_music_session, user_id: user_id)
|
|
|
|
|
music_session_id = music_session.id
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id2)
|
|
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2012-11-30 15:23:43 +00:00
|
|
|
# specify real user id, but not associated with this session
|
2014-06-09 20:43:16 +00:00
|
|
|
expect { @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10) } .to raise_error(ActiveRecord::RecordNotFound)
|
2012-11-02 06:51:52 +00:00
|
|
|
end
|
|
|
|
|
|
2012-10-21 15:05:06 +00:00
|
|
|
it "join_music_session fails if no music_session" do
|
|
|
|
|
client_id = "client_id11"
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id = create_user("test", "user11", "user11@jamkazam.com")
|
2012-10-21 15:05:06 +00:00
|
|
|
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id)
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = ActiveMusicSession.new
|
2013-06-26 15:16:28 +00:00
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2014-06-09 20:43:16 +00:00
|
|
|
connection = @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10)
|
2012-11-30 15:23:43 +00:00
|
|
|
connection.errors.size.should == 1
|
2013-11-03 20:55:55 +00:00
|
|
|
connection.errors.get(:music_session).should == [ValidationMessages::MUSIC_SESSION_MUST_BE_SPECIFIED]
|
2012-11-30 15:23:43 +00:00
|
|
|
end
|
2012-10-23 00:59:35 +00:00
|
|
|
|
2012-11-30 15:23:43 +00:00
|
|
|
it "join_music_session fails if approval_required and no invitation, but generates join_request" do
|
|
|
|
|
client_id = "client_id11.1"
|
|
|
|
|
user_id = create_user("test", "user11.1", "user11.1@jamkazam.com")
|
|
|
|
|
user_id2 = create_user("test", "user11.2", "user11.2@jamkazam.com")
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = FactoryGirl.create(:active_music_session, :approval_required => true, user_id: user_id)
|
|
|
|
|
music_session_id = music_session.id
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id2)
|
|
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2012-11-30 15:23:43 +00:00
|
|
|
# specify real user id, but not associated with this session
|
2014-06-09 20:43:16 +00:00
|
|
|
expect { @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10) } .to raise_error(ActiveRecord::RecordNotFound)
|
2012-10-21 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-30 15:23:43 +00:00
|
|
|
|
2012-10-21 15:05:06 +00:00
|
|
|
it "leave_music_session fails if no music_session" do
|
|
|
|
|
|
|
|
|
|
client_id = "client_id12"
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id = create_user("test", "user12", "user12@jamkazam.com")
|
2012-10-21 15:05:06 +00:00
|
|
|
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id)
|
2014-05-06 13:34:38 +00:00
|
|
|
dummy_music_session = ActiveMusicSession.new
|
2013-06-26 15:16:28 +00:00
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2012-10-21 15:05:06 +00:00
|
|
|
|
2013-08-07 15:35:27 +00:00
|
|
|
expect { @connman.leave_music_session(user, Connection.find_by_client_id(client_id), dummy_music_session) }.to raise_error(JamRuby::StateError)
|
2012-10-21 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "leave_music_session fails if in different music_session" do
|
|
|
|
|
|
|
|
|
|
client_id = "client_id13"
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id = create_user("test", "user13", "user13@jamkazam.com")
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = FactoryGirl.create(:active_music_session, user_id: user_id)
|
|
|
|
|
music_session_id = music_session.id
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
2014-05-06 13:34:38 +00:00
|
|
|
dummy_music_session = ActiveMusicSession.new
|
2013-06-26 15:16:28 +00:00
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2014-06-09 20:43:16 +00:00
|
|
|
@connman.join_music_session(user, client_id, music_session, true, TRACKS, 10)
|
2013-08-07 15:35:27 +00:00
|
|
|
expect { @connman.leave_music_session(user, Connection.find_by_client_id(client_id), dummy_music_session) }.to raise_error(JamRuby::StateError)
|
2012-10-21 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "leave_music_session works" do
|
|
|
|
|
|
|
|
|
|
client_id = "client_id14"
|
2012-11-15 03:24:30 +00:00
|
|
|
user_id = create_user("test", "user14", "user14@jamkazam.com")
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session = FactoryGirl.create(:active_music_session, user_id: user_id)
|
|
|
|
|
music_session_id = music_session.id
|
2013-06-26 15:16:28 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2014-06-09 20:43:16 +00:00
|
|
|
@connman.join_music_session(user, client_id, music_session, true, TRACKS, 10)
|
2012-10-21 15:05:06 +00:00
|
|
|
|
|
|
|
|
assert_session_exists(music_session_id, true)
|
|
|
|
|
|
|
|
|
|
@conn.exec("SELECT music_session_id FROM connections WHERE client_id = $1", [client_id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).should == music_session_id
|
|
|
|
|
end
|
|
|
|
|
|
2013-08-07 15:35:27 +00:00
|
|
|
@connman.leave_music_session(user, Connection.find_by_client_id(client_id), music_session)
|
2012-10-21 15:05:06 +00:00
|
|
|
|
|
|
|
|
@conn.exec("SELECT music_session_id FROM connections WHERE client_id = $1", [client_id]) do |result|
|
|
|
|
|
result.getvalue(0, 0).should == nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
assert_session_exists(music_session_id, false)
|
|
|
|
|
|
2012-11-30 15:23:43 +00:00
|
|
|
@connman.delete_connection(client_id)
|
2012-10-21 01:55:49 +00:00
|
|
|
|
2012-10-21 15:05:06 +00:00
|
|
|
assert_num_connections(client_id, 0)
|
2012-10-21 01:55:49 +00:00
|
|
|
end
|
2014-02-24 01:46:38 +00:00
|
|
|
|
|
|
|
|
it "join_music_session fails if user has music_session already active" do
|
2014-05-01 03:01:43 +00:00
|
|
|
|
|
|
|
|
# there are two different problems: user can only be in one active music_session at a time,
|
|
|
|
|
# and a connection can only point to one active music_session at a time. this is a test of
|
|
|
|
|
# the former but we need a test of the latter, too.
|
|
|
|
|
|
2014-02-25 04:25:22 +00:00
|
|
|
pending
|
2014-02-24 01:46:38 +00:00
|
|
|
|
2014-05-01 03:01:43 +00:00
|
|
|
end
|
2014-02-24 01:46:38 +00:00
|
|
|
|
2014-05-01 03:01:43 +00:00
|
|
|
it "join_music_session fails if connection has music_session already active" do
|
2014-02-24 01:46:38 +00:00
|
|
|
|
2014-05-01 03:01:43 +00:00
|
|
|
# there are two different problems: user can only be in one active music_session at a time,
|
|
|
|
|
# and a connection can only point to one active music_session at a time. this is a test of
|
|
|
|
|
# the latter but we need a test of the former, too.
|
2014-02-24 01:46:38 +00:00
|
|
|
|
2014-05-01 03:01:43 +00:00
|
|
|
user_id = create_user("test", "user11", "user11@jamkazam.com")
|
|
|
|
|
|
|
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
|
|
|
|
client_id1 = Faker::Number.number(20)
|
2014-09-13 03:30:51 +00:00
|
|
|
@connman.create_connection(user_id, client_id1, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE)
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session1 = FactoryGirl.create(:active_music_session, :user_id => user_id)
|
2014-06-09 20:43:16 +00:00
|
|
|
connection1 = @connman.join_music_session(user, client_id1, music_session1, true, TRACKS, 10)
|
2014-05-01 03:01:43 +00:00
|
|
|
connection1.errors.size.should == 0
|
|
|
|
|
|
2014-05-06 13:34:38 +00:00
|
|
|
music_session2 = FactoryGirl.create(:active_music_session, :user_id => user_id)
|
2014-06-09 20:43:16 +00:00
|
|
|
connection2 = @connman.join_music_session(user, client_id1, music_session2, true, TRACKS, 10)
|
2014-05-01 03:01:43 +00:00
|
|
|
|
|
|
|
|
connection2.errors.size.should == 1
|
|
|
|
|
connection2.errors.get(:music_session).should == [ValidationMessages::CANT_JOIN_MULTIPLE_SESSIONS]
|
|
|
|
|
|
|
|
|
|
# client_id2 = Faker::Number.number(20)
|
|
|
|
|
# @connman.create_connection(user_id, client_id2, "2.2.2.2", 'client')
|
|
|
|
|
# music_session2 = MusicSession.find(create_music_session(user_id))
|
|
|
|
|
# connection2 = @connman.join_music_session(user, client_id2, music_session2, true, TRACKS)
|
|
|
|
|
#
|
|
|
|
|
# connection2.errors.size.should == 1
|
|
|
|
|
# connection2.errors.get(:music_session).should == [ValidationMessages::CANT_JOIN_MULTIPLE_SESSIONS]
|
|
|
|
|
#
|
|
|
|
|
# user.update_attribute(:admin, true)
|
|
|
|
|
# client_id = Faker::Number.number(20)
|
|
|
|
|
# @connman.create_connection(user_id, client_id, "1.1.1.1", 'client')
|
|
|
|
|
# music_session = MusicSession.find(create_music_session(user_id))
|
|
|
|
|
# connection = @connman.join_music_session(user, client_id, music_session, true, TRACKS)
|
|
|
|
|
# client_id = Faker::Number.number(20)
|
|
|
|
|
# @connman.create_connection(user_id, client_id, Faker::Internet.ip_v4_address, 'client')
|
|
|
|
|
# music_session = MusicSession.find(create_music_session(user_id))
|
|
|
|
|
# connection = @connman.join_music_session(user, client_id, music_session, true, TRACKS)
|
|
|
|
|
# connection.errors.size.should == 0
|
2014-02-24 01:46:38 +00:00
|
|
|
end
|
|
|
|
|
|
2012-10-21 01:55:49 +00:00
|
|
|
end
|
|
|
|
|
|