socky 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,143 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe Socky::Connection do
4
+
5
+ context "class" do
6
+ it "should have connections array" do
7
+ described_class.connections.should_not be_nil
8
+ described_class.connections.class.should eql(Array)
9
+ end
10
+ it "should have empty connections array at start" do
11
+ described_class.connections.should be_empty
12
+ end
13
+ it "should create instance and assign socket to self" do
14
+ socket = mock(:socket)
15
+ connection = described_class.new(socket)
16
+ connection.socket.should equal(socket)
17
+ end
18
+ end
19
+
20
+ context "instance" do
21
+ before(:each) do
22
+ described_class.connections.clear
23
+ socket = mock(:socket, :request => { "Query" => {}})
24
+ @connection = described_class.new(socket)
25
+ end
26
+
27
+ context "#admin" do
28
+ it "should return true for socket request data admin equal to '1' and 'true'" do
29
+ ["1","true"].each do |value|
30
+ @connection.socket.request["Query"]["admin"] = value
31
+ @connection.admin.should be_true
32
+ end
33
+ end
34
+ it "should return false for socket request data admin equal to anything except '1' and 'true'" do
35
+ [nil,"0","false","abstract"].each do |value|
36
+ @connection.socket.request["Query"]["admin"] = value
37
+ @connection.admin.should be_false
38
+ end
39
+ end
40
+ end
41
+ it "#client should return client_id from socket request data" do
42
+ @connection.socket.request["Query"]["client_id"] = "abstract"
43
+ @connection.client.should eql("abstract")
44
+ end
45
+ it "#secret should return client_secret from socket request data" do
46
+ @connection.socket.request["Query"]["client_secret"] = "abstract"
47
+ @connection.secret.should eql("abstract")
48
+ end
49
+ context "#channels" do
50
+ it "should ba array" do
51
+ @connection.channels.should_not be_nil
52
+ @connection.channels.class.should eql(Array)
53
+ end
54
+ it "should return empty table if socket request data 'channels' is nil" do
55
+ @connection.socket.request["Query"]["channels"] = nil
56
+ @connection.channels.should eql([])
57
+ end
58
+ it "should return table of channels if provided and separated by comma" do
59
+ @connection.socket.request["Query"]["channels"] = "aaa,bbb,ccc"
60
+ @connection.channels.should eql(["aaa","bbb","ccc"])
61
+ end
62
+ it "should not allow empty names of channels" do
63
+ @connection.socket.request["Query"]["channels"] = "aaa,,ccc"
64
+ @connection.channels.should eql(["aaa","ccc"])
65
+ end
66
+ it "should strip trailing spaces in channel names" do
67
+ @connection.socket.request["Query"]["channels"] = " aaa\n , \n , ccc "
68
+ @connection.channels.should eql(["aaa","ccc"])
69
+ end
70
+ end
71
+ it "#subscribe should send subscribe request" do
72
+ @connection.stub!(:subscribe_request)
73
+ @connection.should_receive(:subscribe_request)
74
+ @connection.subscribe
75
+ end
76
+ it "#unsubscribe should send unsubscribe request" do
77
+ @connection.stub!(:unsubscribe_request)
78
+ @connection.should_receive(:unsubscribe_request)
79
+ @connection.unsubscribe
80
+ end
81
+ context "#process_message" do
82
+ it "should send process request to socky message class if connection is by admin" do
83
+ @connection.stub!(:admin).and_return(true)
84
+ Socky::Message.stub!(:process)
85
+ Socky::Message.should_receive(:process).with(@connection, "abstract")
86
+ @connection.process_message("abstract")
87
+ end
88
+ it "should send user message that he can not send messages if connection is not admin" do
89
+ @connection.stub!(:admin).and_return(false)
90
+ @connection.stub!(:send_message)
91
+ @connection.should_receive(:send_message).with("You are not authorized to post messages")
92
+ @connection.process_message("abstract")
93
+ end
94
+ end
95
+ it "#send_message should send message by socket" do
96
+ @connection.socket.stub!(:send)
97
+ @connection.socket.should_receive(:send).with("abstract")
98
+ @connection.send_message("abstract")
99
+ end
100
+ it "#disconnect should close connection after writing" do
101
+ @connection.socket.stub!(:close_connection_after_writing)
102
+ @connection.socket.should_receive(:close_connection_after_writing)
103
+ @connection.disconnect
104
+ end
105
+ context "#add_to_pool" do
106
+ it "should add self to class connection list if self isn't already on list or self isn't admin" do
107
+ @connection.stub!(:admin).and_return(false)
108
+ @connection.add_to_pool
109
+ described_class.connections.should include(@connection)
110
+ described_class.connections.should have(1).item
111
+ end
112
+ it "should not add self to class connection list if self is already on it" do
113
+ described_class.connections << @connection
114
+ described_class.connections.should have(1).item
115
+ @connection.stub!(:admin).and_return(false)
116
+ @connection.add_to_pool
117
+ described_class.connections.should include(@connection)
118
+ described_class.connections.should have(1).item
119
+ end
120
+ it "should not add self to class connection list if self is admin" do
121
+ @connection.stub!(:admin).and_return(true)
122
+ @connection.add_to_pool
123
+ described_class.connections.should_not include(@connection)
124
+ end
125
+ end
126
+ it "#remove_from_pool should delete self from class connection list" do
127
+ described_class.connections << @connection
128
+ described_class.connections.should have(1).item
129
+ @connection.remove_from_pool
130
+ described_class.connections.should_not include(@connection)
131
+ described_class.connections.should have(0).items
132
+ end
133
+ it "#to_json should return self as hash of object_id, client_id and channels" do
134
+ @connection.socket.request["Query"]["client_id"] = "abstract"
135
+ @connection.socket.request["Query"]["channels"] = "first,second,third"
136
+ json = @connection.to_json
137
+ JSON.parse(json).should eql({ "id" => @connection.object_id,
138
+ "client_id" => @connection.client,
139
+ "channels" => @connection.channels})
140
+ end
141
+ end
142
+
143
+ end
@@ -0,0 +1,260 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe Socky::Message do
4
+ before(:each) do
5
+ @connection = mock(:connection, :name => "connection", :send_message => nil)
6
+ end
7
+
8
+ context "class" do
9
+ context "#process" do
10
+ before(:each) { @message = mock(:message, :process => nil) }
11
+ it "should create new instance of self with provided params" do
12
+ described_class.stub!(:new).and_return(@message)
13
+ described_class.should_receive(:new).with(@connection, "test")
14
+ described_class.process(@connection, "test")
15
+ end
16
+ it "should send #process to instance if valid message" do
17
+ described_class.stub!(:new).and_return(@message)
18
+ @message.should_receive(:process)
19
+ described_class.process(@connection, "test")
20
+ end
21
+ it "should rescue from error if creating instance raise error" do
22
+ described_class.stub!(:new).and_raise(Socky::SockyError)
23
+ lambda{ described_class.process(@connection, "test") }.should_not raise_error
24
+ end
25
+ it "should rescue from error if parsing instance raise error" do
26
+ @message.stub!(:process).and_raise(Socky::SockyError)
27
+ described_class.stub!(:new).and_return(@message)
28
+ lambda{ described_class.process(@connection, "test") }.should_not raise_error
29
+ end
30
+ end
31
+ context "#new" do
32
+ it "should create message with provided creator and message is message is JSON hash" do
33
+ message = described_class.new(@connection, {:test => true}.to_json)
34
+ message.params.should eql(:test => true)
35
+ message.creator.should eql(@connection)
36
+ end
37
+ it "should raise error if message is not JSON" do
38
+ lambda{described_class.new(@connection, {:test => true})}.should raise_error Socky::SockyError
39
+ lambda{described_class.new(@connection, "test")}.should raise_error Socky::SockyError
40
+ end
41
+ it "should raise error if message is JSON but not JSON hash" do
42
+ lambda{described_class.new(@connection, "test".to_json)}.should raise_error Socky::SockyError
43
+ end
44
+ end
45
+ end
46
+
47
+ context "instance" do
48
+ before(:each) { @message = described_class.new(@connection, {}.to_json) }
49
+ context "#process" do
50
+ before(:each) do
51
+ @message.stub!(:verify_secret!)
52
+ @message.stub!(:execute)
53
+ end
54
+ it "should verify secret" do
55
+ @message.should_receive(:verify_secret!)
56
+ @message.process
57
+ end
58
+ it "should execute" do
59
+ @message.should_receive(:execute)
60
+ @message.process
61
+ end
62
+ end
63
+ context "#verify_secret!" do
64
+ it "should not raise error if socky option :secret is nil" do
65
+ Socky.stub!(:options).and_return({:secret => nil})
66
+ lambda{ @message.verify_secret! }.should_not raise_error Socky::SockyError
67
+ end
68
+ it "should raise error if socky options :secret is not nil but message secret have different value" do
69
+ Socky.stub!(:options).and_return({:secret => "test"})
70
+ @message.stub!(:options).and_return({:secret => "another"})
71
+ lambda{ @message.verify_secret! }.should raise_error Socky::SockyError
72
+ end
73
+ it "should not raise error if socky options :secret is not nil and message secret have the some value" do
74
+ Socky.stub!(:options).and_return({:secret => "test"})
75
+ @message.stub!(:params).and_return({:secret => "test"})
76
+ lambda{ @message.verify_secret! }.should_not raise_error Socky::SockyError
77
+ end
78
+ end
79
+ context "#execute" do
80
+ it "should call #broadcast if message command is :broadcast" do
81
+ @message.stub!(:params).and_return({:command => :broadcast})
82
+ @message.stub!(:broadcast)
83
+ @message.should_receive(:broadcast)
84
+ @message.execute
85
+ end
86
+ it "should not distinguish between string and symbol in command" do
87
+ @message.stub!(:params).and_return({:command => 'broadcast'})
88
+ @message.stub!(:broadcast)
89
+ @message.should_receive(:broadcast)
90
+ @message.execute
91
+ end
92
+ it "should call #query if message command is :query" do
93
+ @message.stub!(:params).and_return({:command => :query})
94
+ @message.stub!(:query)
95
+ @message.should_receive(:query)
96
+ @message.execute
97
+ end
98
+ it "should raise error if message command is nil" do
99
+ @message.stub!(:params).and_return({:command => nil})
100
+ lambda {@message.execute}.should raise_error Socky::SockyError
101
+ end
102
+ it "should raise error if message command is neither :broadcast nor :query" do
103
+ @message.stub!(:params).and_return({:command => "invalid"})
104
+ lambda {@message.execute}.should raise_error Socky::SockyError
105
+ end
106
+ end
107
+ context "#broadcast" do
108
+ it "should call #broadcast_to_clients if message type is :to_clients" do
109
+ @message.stub!(:params).and_return({:type => :to_clients})
110
+ @message.stub!(:broadcast_to_clients)
111
+ @message.should_receive(:broadcast_to_clients)
112
+ @message.broadcast
113
+ end
114
+ it "should not distinguish between string and symbol in type" do
115
+ @message.stub!(:params).and_return({:type => 'to_clients'})
116
+ @message.stub!(:broadcast_to_clients)
117
+ @message.should_receive(:broadcast_to_clients)
118
+ @message.broadcast
119
+ end
120
+ it "should call #broadcast_to_channels if message type is :to_channels" do
121
+ @message.stub!(:params).and_return({:type => :to_channels})
122
+ @message.stub!(:broadcast_to_channels)
123
+ @message.should_receive(:broadcast_to_channels)
124
+ @message.broadcast
125
+ end
126
+ it "should call #broadcast_to_clients_on_channels if message type is :to_clients_on_channels" do
127
+ @message.stub!(:params).and_return({:type => :to_clients_on_channels})
128
+ @message.stub!(:broadcast_to_clients_on_channels)
129
+ @message.should_receive(:broadcast_to_clients_on_channels)
130
+ @message.broadcast
131
+ end
132
+ it "should raise error if message type is nil" do
133
+ @message.stub!(:params).and_return({:type => nil})
134
+ lambda {@message.broadcast}.should raise_error Socky::SockyError
135
+ end
136
+ it "should raise error if message type is not one of :to_clients, :to_channels or :to_cleints_on_channels" do
137
+ @message.stub!(:params).and_return({:type => "invalid"})
138
+ lambda {@message.broadcast}.should raise_error Socky::SockyError
139
+ end
140
+ end
141
+ context "#broadcast_to_clients" do
142
+ before(:each) do
143
+ @message.stub!(:verify)
144
+ Socky::Server.stub!(:send_to_clients)
145
+ end
146
+ it "should verify :clients params existence" do
147
+ @message.should_receive(:verify).with(:clients)
148
+ @message.broadcast_to_clients
149
+ end
150
+ it "should call socky server #send_to_clients with own params" do
151
+ @message.stub!(:params).and_return(:body => "some message", :clients => "some clients")
152
+ Socky::Server.should_receive(:send_to_clients).with("some message", "some clients")
153
+ @message.broadcast_to_clients
154
+ end
155
+ end
156
+ context "#broadcast_to_channels" do
157
+ before(:each) do
158
+ @message.stub!(:verify)
159
+ Socky::Server.stub!(:send_to_channels)
160
+ end
161
+ it "should verify :channels params existence" do
162
+ @message.should_receive(:verify).with(:channels)
163
+ @message.broadcast_to_channels
164
+ end
165
+ it "should call socky server #send_to_channels with own params" do
166
+ @message.stub!(:params).and_return(:body => "some message", :channels => "some channels")
167
+ Socky::Server.should_receive(:send_to_channels).with("some message", "some channels")
168
+ @message.broadcast_to_channels
169
+ end
170
+ end
171
+ context "#broadcast_to_clients_on_channels" do
172
+ before(:each) do
173
+ @message.stub!(:verify)
174
+ Socky::Server.stub!(:send_to_clients_on_channels)
175
+ end
176
+ it "should verify :clients params existence" do
177
+ @message.should_receive(:verify).with(:clients)
178
+ @message.broadcast_to_clients_on_channels
179
+ end
180
+ it "should verify :channels params existence" do
181
+ @message.should_receive(:verify).with(:channels)
182
+ @message.broadcast_to_clients_on_channels
183
+ end
184
+ it "should call socky server #send_to_clients_on_channels with own params" do
185
+ @message.stub!(:params).and_return(:body => "some message", :clients => "some clients", :channels => "some channels")
186
+ Socky::Server.should_receive(:send_to_clients_on_channels).with("some message", "some clients", "some channels")
187
+ @message.broadcast_to_clients_on_channels
188
+ end
189
+ end
190
+ context "#query" do
191
+ it "should call #query_show_connections if message type is :show_connections" do
192
+ @message.stub!(:params).and_return({:type => :show_connections})
193
+ @message.stub!(:query_show_connections)
194
+ @message.should_receive(:query_show_connections)
195
+ @message.query
196
+ end
197
+ it "should not distinguish between string and symbol in type" do
198
+ @message.stub!(:params).and_return({:type => 'show_connections'})
199
+ @message.stub!(:query_show_connections)
200
+ @message.should_receive(:query_show_connections)
201
+ @message.query
202
+ end
203
+ it "should raise error if message type is nil" do
204
+ @message.stub!(:params).and_return({:type => nil})
205
+ lambda{ @message.query }.should raise_error Socky::SockyError
206
+ end
207
+ it "should raise error if message type is not :show_connections" do
208
+ @message.stub!(:params).and_return({:type => "invalid"})
209
+ lambda{ @message.query }.should raise_error Socky::SockyError
210
+ end
211
+ end
212
+ context "#query_show_connections" do
213
+ before(:each) do
214
+ Socky::Connection.stub!(:find_all).and_return("find results")
215
+ @message.stub!(:respond)
216
+ end
217
+ it "should ask for all connections" do
218
+ Socky::Connection.should_receive(:find_all)
219
+ @message.query_show_connections
220
+ end
221
+ it "should respond current connection list" do
222
+ @message.should_receive(:respond).with("find results")
223
+ @message.query_show_connections
224
+ end
225
+ end
226
+ context "#verify" do
227
+ it "should convert posted field to empty array if field is nil" do
228
+ @message.params[:clients] = nil
229
+ @message.verify :clients
230
+ @message.params[:clients].should eql([])
231
+ end
232
+ it "should convert posted field to array containing field value if field is not array" do
233
+ @message.params[:clients] = "some client"
234
+ @message.verify :clients
235
+ @message.params[:clients].should eql(["some client"])
236
+ end
237
+ it "should not convert field if field is array" do
238
+ @message.params[:clients] = ["some client"]
239
+ @message.verify :clients
240
+ @message.params[:clients].should eql(["some client"])
241
+ end
242
+ it "should change every array value to string" do
243
+ @message.params[:clients] = [1,2,3]
244
+ @message.verify :clients
245
+ @message.params[:clients].should eql(["1","2","3"])
246
+ end
247
+ end
248
+ context "#respond" do
249
+ it "should call creator #send_message" do
250
+ @connection.should_receive(:send_message)
251
+ @message.respond({:test => true})
252
+ end
253
+ it "should convert message to json" do
254
+ @connection.should_receive(:send_message).with({:test => true}.to_json)
255
+ @message.respond({:test => true})
256
+ end
257
+ end
258
+
259
+ end
260
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe Socky::Misc do
4
+ include Socky::Misc
5
+
6
+ shared_examples_for "socky_misc" do
7
+ it "#option should return Socky.options" do
8
+ Socky.stub!(:options).and_return({:test => true})
9
+ @object.options.should eql({:test => true})
10
+ end
11
+ it "#options= should set Socky.options" do
12
+ default_options = Socky.options
13
+ begin
14
+ Socky.options = {}
15
+ @object.options = {:test => true}
16
+ Socky.options.should eql({:test => true})
17
+ ensure
18
+ Socky.options = default_options
19
+ end
20
+ end
21
+ it "#name should return self formatted name" do
22
+ @object.name.should eql("#{@object.class.to_s.split("::").last}(#{@object.object_id})")
23
+ end
24
+ it "#log_path should return Socky.log_path" do
25
+ Socky.stub!(:log_path).and_return("abstract")
26
+ @object.log_path.should eql("abstract")
27
+ end
28
+ it "#config_path should return Socky.config_path" do
29
+ Socky.stub!(:config_path).and_return("abstract")
30
+ @object.config_path.should eql("abstract")
31
+ end
32
+ it "#info should send joined args to Socky.logger.info" do
33
+ Socky.logger.stub!(:info)
34
+ Socky.logger.should_receive(:info).with("first second third")
35
+ @object.info(["first","second","third"])
36
+ end
37
+ it "#debug should send joined args to Socky.logger.debug" do
38
+ Socky.logger.stub!(:debug)
39
+ Socky.logger.should_receive(:debug).with("first second third")
40
+ @object.debug(["first","second","third"])
41
+ end
42
+ it "#error should send self name and error message to self.debug" do
43
+ @object.stub!(:debug)
44
+ error = mock(:abstract_error, :class => "AbstractError", :message => "abstract")
45
+ @object.should_receive(:debug).with([@object.name,"raised:","AbstractError","abstract"])
46
+ @object.error(@object.name, error)
47
+ end
48
+ context "#symbolize_keys" do
49
+ it "return args if args are not hash" do
50
+ @object.symbolize_keys("abstract").should eql("abstract")
51
+ end
52
+ it "return hash with symbolized keys if args is hash" do
53
+ hash = {"aaa" => "a", 123 => "b"}
54
+ @object.symbolize_keys(hash).should eql({:aaa => "a", 123 => "b"})
55
+ end
56
+ end
57
+ end
58
+
59
+ context "class" do
60
+ before(:each) do
61
+ @object = self.class
62
+ end
63
+ it_should_behave_like "socky_misc"
64
+ end
65
+
66
+ context "instance" do
67
+ before(:each) do
68
+ @object = self
69
+ end
70
+ it_should_behave_like "socky_misc"
71
+ end
72
+
73
+ end
74
+