spec/slack/real_time/client_spec.rb in slack-ruby-client-0.1.0 vs spec/slack/real_time/client_spec.rb in slack-ruby-client-0.2.0
- old
+ new
@@ -1,23 +1,51 @@
require 'spec_helper'
RSpec.describe Slack::RealTime::Client, vcr: { cassette_name: 'web/rtm_start' } do
let(:client) { Slack::RealTime::Client.new }
- let(:ws) { double(Faye::WebSocket::Client) }
+ let(:ws) { double(Faye::WebSocket::Client, on: true) }
let(:url) { 'wss://ms173.slack-msgs.com/websocket/lqcUiAvrKTP-uuid=' }
- let(:socket) { double(Slack::RealTime::Socket, connected?: true) }
+ before do
+ allow(EM).to receive(:run).and_yield
+ end
context 'started' do
describe '#start!' do
+ let(:socket) { double(Slack::RealTime::Socket, connected?: true) }
before do
- allow(EM).to receive(:run).and_yield
- allow(Slack::RealTime::Socket).to receive(:new).with(url).and_return(socket)
+ allow(Slack::RealTime::Socket).to receive(:new).with(url, ping: 30).and_return(socket)
allow(socket).to receive(:connect!).and_yield(ws)
- allow(ws).to receive(:on)
client.start!
end
- it 'sets url' do
- expect(client.url).to eq url
+ context 'properties provided upon connection' do
+ it 'sets url' do
+ expect(client.url).to eq url
+ end
+ it 'sets team' do
+ expect(client.team['domain']).to eq 'dblockdotorg'
+ end
+ it 'sets self' do
+ expect(client.self['id']).to eq 'U07518DTL'
+ end
+ it 'sets users' do
+ expect(client.users.count).to eq 7
+ expect(client.users.first['id']).to eq 'U07KECJ77'
+ end
+ it 'sets channels' do
+ expect(client.channels.count).to eq 8
+ expect(client.channels.first['name']).to eq 'demo'
+ end
+ it 'sets ims' do
+ expect(client.ims.count).to eq 2
+ expect(client.ims.first['user']).to eq 'USLACKBOT'
+ end
+ it 'sets bots' do
+ expect(client.bots.count).to eq 5
+ expect(client.bots.first['name']).to eq 'bot'
+ end
+ it 'sets groups' do
+ expect(client.groups.count).to eq 0
+ end
end
it 'uses web client to fetch url' do
expect(client.web_client).to be_a Slack::Web::Client
end
it 'remembers socket' do
@@ -42,9 +70,88 @@
end
describe '#next_id' do
it 'increments' do
previous_id = client.send(:next_id)
expect(client.send(:next_id)).to eq previous_id + 1
+ end
+ end
+ end
+ end
+ context 'with defaults' do
+ describe '#initialize' do
+ it 'sets ping' do
+ expect(client.websocket_ping).to eq 30
+ end
+ it "doesn't set proxy" do
+ expect(client.websocket_proxy).to be nil
+ end
+ Slack::RealTime::Config::ATTRIBUTES.each do |key|
+ it "sets #{key}" do
+ expect(client.send(key)).to eq Slack::RealTime::Config.send(key)
+ end
+ end
+ end
+ end
+ context 'with custom settings' do
+ describe '#initialize' do
+ Slack::RealTime::Config::ATTRIBUTES.each do |key|
+ context key do
+ let(:client) { Slack::RealTime::Client.new(key => 'custom') }
+ it "sets #{key}" do
+ expect(client.send(key)).to_not eq Slack::RealTime::Config.send(key)
+ expect(client.send(key)).to eq 'custom'
+ end
+ end
+ end
+ end
+ end
+ context 'global config' do
+ after do
+ Slack::RealTime::Client.config.reset
+ end
+ let(:client) { Slack::RealTime::Client.new }
+ context 'ping' do
+ before do
+ Slack::RealTime::Client.configure do |config|
+ config.websocket_ping = 15
+ end
+ end
+ describe '#initialize' do
+ it 'sets ping' do
+ expect(client.websocket_ping).to eq 15
+ end
+ it 'creates a connection with custom ping' do
+ expect(Faye::WebSocket::Client).to receive(:new).with(url, nil, ping: 15).and_return(ws)
+ client.start!
+ end
+ end
+ end
+ context 'proxy' do
+ before do
+ Slack::RealTime::Client.configure do |config|
+ config.websocket_proxy = {
+ origin: 'http://username:password@proxy.example.com',
+ headers: { 'User-Agent' => 'ruby' }
+ }
+ end
+ end
+ describe '#initialize' do
+ it 'sets proxy' do
+ expect(client.websocket_proxy).to eq(
+ origin: 'http://username:password@proxy.example.com',
+ headers: { 'User-Agent' => 'ruby' }
+ )
+ end
+ it 'creates a connection with custom proxy' do
+ expect(Faye::WebSocket::Client).to receive(:new).with(
+ url,
+ nil,
+ ping: 30,
+ proxy: {
+ origin: 'http://username:password@proxy.example.com',
+ headers: { 'User-Agent' => 'ruby' }
+ }).and_return(ws)
+ client.start!
end
end
end
end
end