115 lines
3.1 KiB
Ruby
115 lines
3.1 KiB
Ruby
# require "eventmachine"
|
|
RSpec.describe Rex::Server::MessageBroker do
|
|
subject(:instance) { described_class.new(NoOpSerializer.new) }
|
|
|
|
let(:connection_a) { MockConnection.new }
|
|
let(:connection_b) { MockConnection.new }
|
|
|
|
let!(:connection_id_a) { instance.register(connection_a) }
|
|
let!(:connection_id_b) { instance.register(connection_b) }
|
|
|
|
describe "#send_to_all" do
|
|
subject { instance.send_to_all("Hello, World") }
|
|
|
|
it "sends the message to all connections" do
|
|
expect { subject }.to(
|
|
change { connection_a.messages }.from([]).to(["Hello, World"])
|
|
.and(
|
|
change { connection_b.messages }.from([]).to(["Hello, World"])
|
|
)
|
|
)
|
|
end
|
|
end
|
|
|
|
describe "#send_to_user" do
|
|
before do
|
|
instance.associate_connection_with_user(connection_id_a, "user_id")
|
|
end
|
|
|
|
subject { instance.send_to_user("user_id", "Hello, World") }
|
|
|
|
it "sends the message to the connection associated with the user" do
|
|
expect { subject }.to(
|
|
change { connection_a.messages }.to(["Hello, World"])
|
|
.and(
|
|
not_change { connection_b.messages }
|
|
)
|
|
)
|
|
end
|
|
|
|
context "when user id is unknown" do
|
|
subject { instance.send_to_user("unknown_user_id", "Hello, World") }
|
|
it "sends no messages" do
|
|
expect { subject }.to(
|
|
not_change { connection_a.messages }
|
|
.and(
|
|
not_change { connection_b.messages }
|
|
)
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#send_to_connection" do
|
|
subject { instance.send_to_connection(connection_id_a, "Hello, World") }
|
|
|
|
it "sends the message to the connection" do
|
|
expect { subject }.to(
|
|
change { connection_a.messages }.to(["Hello, World"])
|
|
.and(
|
|
not_change { connection_b.messages }
|
|
)
|
|
)
|
|
end
|
|
|
|
context "when the connection id is unknown" do
|
|
subject { instance.send_to_connection(-99, "Hello, World") }
|
|
|
|
it "sends no messages" do
|
|
expect { subject }.to(
|
|
not_change { connection_a.messages }
|
|
.and(
|
|
not_change { connection_b.messages }
|
|
)
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#user_id_for_connection" do
|
|
subject { instance.user_id_for_connection(connection_id_a) }
|
|
context "when connection has user associated with it" do
|
|
before do
|
|
instance.associate_connection_with_user(connection_id_a, "user_id")
|
|
end
|
|
|
|
it "returns the user id" do
|
|
expect(subject).to eq("user_id")
|
|
end
|
|
end
|
|
|
|
context "when connection has no user associated with it" do
|
|
it { is_expected.to be_nil }
|
|
end
|
|
|
|
context "when connection was unregistered after being associated" do
|
|
before do
|
|
instance.associate_connection_with_user(connection_id_a, "user_id")
|
|
instance.unregister(connection_id_a)
|
|
end
|
|
|
|
it { is_expected.to be_nil }
|
|
end
|
|
end
|
|
|
|
describe "#unregister" do
|
|
context 'when connection was never associated with a user' do
|
|
it 'does not crash' do
|
|
expect do
|
|
instance.unregister(connection_id_a)
|
|
end.not_to raise_error
|
|
end
|
|
end
|
|
end
|
|
end
|