jam-cloud/spec/jam_websockets/router_spec.rb

230 lines
5.7 KiB
Ruby
Raw Normal View History

require 'spec_helper'
require 'thread'
LoginClient = Class.new do
attr_accessor :onmsgblock, :onopenblock, :encode_json, :client_id
def initiaize()
end
def onopen(&block)
@onopenblock = block
end
def onmessage(&block)
@onmsgblock = block
end
def close(&block)
@oncloseblock = block
end
def close_websocket()
end
def send(msg)
puts msg
end
def get_peername
return "\x00\x02\x93\v\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" # 37643, "localhost"
end
end
# does a login and returns client
def login(router, user, password)
message_factory = MessageFactory.new
client = LoginClient.new
login_ack = message_factory.login_ack("127.0.0.1")
router.should_receive(:send_to_client).with(client, login_ack)
client.should_receive(:onclose)
client.should_receive(:onerror)
client.should_receive(:request).and_return({ "query" => { "pb" => "true" } })
client.should_receive(:get_peername).and_return("\x00\x02\x93\v\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00")
@router.new_client(client)
client.onopenblock.call
# create a login message, and pass it into the router via onmsgblock.call
login = message_factory.login_with_user_pass(user.email, password)
# first log in
client.onmsgblock.call login.to_s
# then join jam session
return client
end
def login_jam_session(router, client, jam_session)
message_factory = MessageFactory.new
login_jam_session = message_factory.login_jam_session(jam_session.id)
login_ack = message_factory.login_jam_session_ack(false, nil);
router.should_receive(:send_to_client).with(client, login_ack)
client.onmsgblock.call login_jam_session.to_s
end
describe Router do
message_factory = MessageFactory.new
before do
@router = Router.new()
@router.start()
end
subject { @router }
after do
@router.stop
end
describe "servicability" do
it "should start and stop", :mq => true do
end
it "should register for client events", :mq => true do
client = double("client")
client.should_receive(:onopen)
client.should_receive(:onclose)
client.should_receive(:onerror)
client.should_receive(:onmessage)
client.should_receive(:encode_json=)
client.should_receive(:client_id=)
@router.new_client(client)
end
end
describe "topic routing helpers" do
it "create and delete user lookup set" do
user = double(User)
user.should_receive(:id).any_number_of_times.and_return("1")
client = double("client")
context = ClientContext.new(user, client)
@router.user_context_lookup.length.should == 0
@router.add_user(context)
@router.user_context_lookup.length.should == 1
@router.remove_user(context)
@router.user_context_lookup.length.should == 0
end
end
describe "login" do
it "should not allow login of bogus user", :mq => true do
TestClient = Class.new do
attr_accessor :onmsgblock, :onopenblock, :encode_json, :client_id
def initiaize()
end
def onopen(&block)
@onopenblock = block
end
def onmessage(&block)
@onmsgblock = block
end
def close_websocket()
end
def close()
end
end
client = TestClient.new
error_msg = message_factory.server_rejection_error("invalid login")
@router.should_receive(:send_to_client).with(client, error_msg)
client.should_receive(:close_websocket)
client.should_receive(:onclose)
client.should_receive(:onerror)
client.should_receive(:request).and_return({ "query" => { "pb" => "true" } })
@router.new_client(client)
client.onopenblock.call
# create a login message, and pass it into the router via onmsgblock.call
login = message_factory.login_with_user_pass("baduser@example.com", "foobar")
client.onmsgblock.call login.to_s
end
it "should allow login of valid user", :mq => true do
@user = User.new(:name => "Example User", :email => "user@example.com",
:password => "foobar", :password_confirmation => "foobar")
@user.save
client1 = login(@router, @user, "foobar")
end
it "should allow jam_session_join of valid user", :mq => true do
user1 = FactoryGirl.create(:user) # in the jam session
user2 = FactoryGirl.create(:user) # in the jam session
user3 = FactoryGirl.create(:user) # not in the jam session
jam_session = FactoryGirl.create(:jam_session, :creator => user1)
jam_session_member1 = FactoryGirl.create(:jam_session_member, :user => user1, :jam_session => jam_session)
jam_session_member2 = FactoryGirl.create(:jam_session_member, :user => user2, :jam_session => jam_session)
# make a jam_session and define two members
# create client 1, log him in, and log him in to jam session
client1 = login(@router, user1, "foobar")
login_jam_session(@router, client1, jam_session)
end
it "should allow two valid subscribers to communicate with session-directed messages", :mq => true do
EventMachine.run do
user1 = FactoryGirl.create(:user) # in the jam session
user2 = FactoryGirl.create(:user) # in the jam session
jam_session = FactoryGirl.create(:jam_session, :creator => user1)
jam_session_member1 = FactoryGirl.create(:jam_session_member, :user => user1, :jam_session => jam_session)
jam_session_member2 = FactoryGirl.create(:jam_session_member, :user => user2, :jam_session => jam_session)
# make a jam_session and define two members
# create client 1, log him in, and log him in to jam session
client1 = login(@router, user1, "foobar")
login_jam_session(@router, client1, jam_session)
client2 = login(@router, user2, "foobar")
login_jam_session(@router, client2, jam_session)
EM.stop
end
end
end
end