radamanthus-skates 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,120 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+
3
+ describe Skates::Base::Stanza do
4
+
5
+ describe "initialize" do
6
+
7
+ context "when the stanza is an IQ" do
8
+
9
+ before(:each) do
10
+ xml = <<-EOXML
11
+ <iq type='get'
12
+ from='romeo@montague.net/orchard'
13
+ to='plays.shakespeare.lit'
14
+ id='info1'>
15
+ <query xmlns='http://jabber.org/protocol/disco#configuration'/>
16
+ </iq>
17
+ EOXML
18
+ xml = Nokogiri::XML(xml)
19
+ @stanza = Skates::Base::Stanza.new(xml.root)
20
+ end
21
+
22
+ it "should have the right from" do
23
+ @stanza.from.should == "romeo@montague.net/orchard"
24
+ end
25
+
26
+ it "should have the right id" do
27
+ @stanza.id.should == "info1"
28
+ end
29
+
30
+ it "should have the right to" do
31
+ @stanza.to.should == "plays.shakespeare.lit"
32
+ end
33
+
34
+ it "should have the right type" do
35
+ @stanza.type.should == "get"
36
+ end
37
+
38
+ it "should have the right name" do
39
+ @stanza.name.should == "iq"
40
+ end
41
+
42
+ end
43
+
44
+
45
+
46
+ context "when the stanza is a presence" do
47
+
48
+ before(:each) do
49
+ xml = <<-EOXML
50
+ <presence from='firehoser-test.superfeedr.com' to='testparsr@superfeedr.com/skates_client_7008465' type='error' />
51
+ EOXML
52
+ xml = Nokogiri::XML(xml)
53
+ @stanza = Skates::Base::Stanza.new(xml.root)
54
+ end
55
+
56
+ it "should have the right from" do
57
+ @stanza.from.should == "firehoser-test.superfeedr.com"
58
+ end
59
+
60
+ it "should have the right id" do
61
+ @stanza.id.should be_nil
62
+ end
63
+
64
+ it "should have the right to" do
65
+ @stanza.to.should == "testparsr@superfeedr.com/skates_client_7008465"
66
+ end
67
+
68
+ it "should have the right type" do
69
+ @stanza.type.should == "error"
70
+ end
71
+
72
+ it "should have the right name" do
73
+ @stanza.name.should == "presence"
74
+ end
75
+
76
+ end
77
+
78
+ context "when the stanza is a message" do
79
+
80
+ before(:each) do
81
+ xml = <<-EOXML
82
+ <message to="monitor@superfeedr.com" from="test-firehoser.superfeedr.com">
83
+ <event xmlns="http://jabber.org/protocol/pubsub#event">
84
+ <status xmlns="http://superfeedr.com/xmpp-pubsub-ext" feed="http://domain.tld/feed.xml">
85
+ <http code="200">All went very fine. Thanks for asking!</http>
86
+ <next_fetch>2010-02-03T01:32:58+01:00</next_fetch>
87
+ <title></title>
88
+ </status>
89
+ </event>
90
+ </message>
91
+ EOXML
92
+ xml = Nokogiri::XML(xml)
93
+ @stanza = Skates::Base::Stanza.new(xml.root)
94
+ end
95
+
96
+ it "should have the right from" do
97
+ @stanza.from.should == "test-firehoser.superfeedr.com"
98
+ end
99
+
100
+ it "should have the right id" do
101
+ @stanza.id.should be_nil
102
+ end
103
+
104
+ it "should have the right to" do
105
+ @stanza.to.should == "monitor@superfeedr.com"
106
+ end
107
+
108
+ it "should have the right type" do
109
+ @stanza.type.should be_nil
110
+ end
111
+
112
+ it "should have the right name" do
113
+ @stanza.name.should == "message"
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ end
@@ -0,0 +1,105 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+
3
+ describe Skates::Base::View do
4
+ describe :initialize do
5
+
6
+ before(:each) do
7
+ @view = Skates::Base::View.new("/a/path/to/views/file", {:a => "a", :b => 123, :c => {:d => "d", :e => "123"}})
8
+ end
9
+
10
+ it "should assign @view_template to path" do
11
+ @view.view_template == "/a/path/to/views/file"
12
+ end
13
+
14
+ it "should assign any variable passed in hash" do
15
+ {:a => "a", :b => 123, :c => {:d => "d", :e => "123"}}.each do |key, value|
16
+ @view.instance_variable_get("@#{key}").should == value
17
+ end
18
+ end
19
+ end
20
+
21
+ describe :evaluate do
22
+ before(:each) do
23
+ @view_template = "/a/path/to/views/file"
24
+ @view = Skates::Base::View.new(@view_template, {:a => "a", :b => 123, :c => {:d => "d", :e => "123"}})
25
+ @xml_string = <<-eoxml
26
+ xml.message(:to => "you", :from => "me", :type => :chat) do
27
+ xml.body("salut")
28
+ end
29
+ eoxml
30
+ Skates.views.stub!(:[]).with(@view_template).and_return(@xml_string)
31
+ end
32
+
33
+ it "should read the template file" do
34
+ Skates.views.should_receive(:[]).twice.with(@view_template).and_return(@xml_string)
35
+ @view.evaluate
36
+ end
37
+
38
+ it "should raise an error if the view file couldn't be found" do
39
+ Skates.views.stub!(:[]).with(@view_template).and_raise(nil)
40
+ lambda {
41
+ @view.evaluate
42
+ }.should raise_error(Skates::Base::ViewFileNotFound)
43
+ end
44
+
45
+ it "should return a Nokogiri NodeSet" do
46
+ Skates.views.stub!(:[]).with(@view_template).and_return(@xml_string)
47
+ @view.evaluate.should be_an_instance_of(Nokogiri::XML::NodeSet)
48
+ end
49
+
50
+ it "should call eval on the view file" do
51
+ Skates.views.stub!(:[]).with(@view_template).and_return(@xml_string)
52
+ @view.should_receive(:eval).with(@xml_string, an_instance_of(Binding), @view_template, 1)
53
+ @view.evaluate
54
+ end
55
+
56
+ it "should be able to access context's variables" do
57
+ @view = Skates::Base::View.new("/a/path/to/views/file", {:a => "a", :b => 123, :c => {:d => "d", :e => "123"}})
58
+ @view.instance_variable_get("@a").should == "a"
59
+ @view.instance_variable_get("@b").should == 123
60
+ @view.instance_variable_get("@c").should == {:e=>"123", :d=>"d"}
61
+ end
62
+ end
63
+
64
+ describe :render do
65
+ before(:each) do
66
+ @view_template = "/a/path/to/views/file"
67
+ @view = Skates::Base::View.new(@view_template, {:a => "a", :b => 123, :c => {:d => "d", :e => "123"}})
68
+
69
+ @xml_string = <<-eoxml
70
+ xml.message(:to => "you", :from => "me", :type => :chat) do |message|
71
+ message.body("salut")
72
+ render(message, {:partial => "partial", :locals => {:subtitle => "bonjour monde"}})
73
+ render(message, {:partial => "../other_views/partial", :locals => {:subtitle => "bonjour monde", :name => "Joe"}})
74
+ end
75
+ eoxml
76
+
77
+ @partial_string = <<-eoxml
78
+ xml.title("hello word")
79
+ xml.subtitle(subtitle)
80
+ eoxml
81
+
82
+ @partial_in_annother_controller_string = <<-eoxml
83
+ xml.name(name)
84
+ eoxml
85
+
86
+ Skates.views.stub!(:[]).with(@view_template).and_return(@xml_string)
87
+ Skates.views.stub!(:[]).with("/a/path/to/views/partial.xml.builder").and_return(@partial_string)
88
+ Skates.views.stub!(:[]).with("/a/path/to/other_views/partial.xml.builder").and_return(@partial_in_annother_controller_string)
89
+ end
90
+
91
+ it "should render the partial in the right context" do
92
+ @view.evaluate.xpath("//message/title").text.should == "hello word"
93
+ end
94
+
95
+ it "should allocate the locals variables" do
96
+ @view.evaluate.xpath("//message/subtitle").text.should == "bonjour monde"
97
+ end
98
+
99
+ it "should cleanup the path so only canonical paths are used" do
100
+ @view.evaluate.xpath("//message/name").text.should == "Joe"
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -0,0 +1,309 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+ require File.dirname(__FILE__) + '/../../em_mock'
3
+
4
+ describe Skates::ClientConnection do
5
+
6
+ include SkatesSpecHelper
7
+
8
+ before(:each) do
9
+ @params = {"jid" => "jid@server.tld", "password" => "password"}
10
+ @client = Skates::ClientConnection.connect(@params.merge({"host" => "0.0.0.0", "port" => 5222}), handler_mock)
11
+ @client.stub!(:send_xml).and_return(true)
12
+ end
13
+
14
+ describe "initialize" do
15
+ it "should set the state to :wait_for_stream" do
16
+ @client.instance_variable_get("@state").should == :wait_for_stream
17
+ end
18
+ end
19
+
20
+ describe "connect" do
21
+ end
22
+
23
+ describe "when resolving the hostname" do
24
+ before(:each) do
25
+ @params.delete("host")
26
+ @params.delete("port")
27
+ @srv = [
28
+ mock(Resolv::DNS::Resource, :priority => 10, :target => "12.13.14.15", :port => 1234),
29
+ mock(Resolv::DNS::Resource, :priority => 3, :target => "12.13.14.16", :port => 4567),
30
+ mock(Resolv::DNS::Resource, :priority => 100, :target => "12.13.14.17", :port => 8910)
31
+ ]
32
+ @mock_dns = mock(Object)
33
+ Resolv::DNS.stub!(:open).and_yield(@mock_dns)
34
+ @mock_dns.stub!(:getresources).and_return(@srv)
35
+ end
36
+
37
+ it "should get resources assiated with _xmpp-client._tcp.host.tld}" do
38
+ Resolv::DNS.should_receive(:open).and_yield(@mock_dns)
39
+ @mock_dns.should_receive(:getresources).with("_xmpp-client._tcp.server.tld", Resolv::DNS::Resource::IN::SRV).and_return(@srv)
40
+ Skates::ClientConnection.resolve("server.tld")
41
+ end
42
+
43
+ it "should call the block with the highest priority" do
44
+ Skates::ClientConnection.resolve("xmpp.server.tld") do |params|
45
+ params["host"].should == "12.13.14.16"
46
+ params["port"].should == 4567
47
+ true
48
+ end
49
+ end
50
+
51
+ it "should call the block as many times as needed if they're not connecting" do
52
+ conn = mock(Skates::ClientConnection, :_connect => false)
53
+ conn.should_receive(:_connect).exactly(3).times
54
+ Skates::ClientConnection.resolve("xmpp.server.tld") do |ip, port|
55
+ conn._connect(ip, port)
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "stream_stanza" do
61
+ it "should be of the right form" do
62
+ @client.stream_stanza.should == "<?xml version=\"1.0\"?>\n<stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" to=\"server.tld\" version=\"1.0\">\n "
63
+ end
64
+ end
65
+
66
+ describe "connection_completed" do
67
+ it "should send_xml the stream_stanza" do
68
+ @client.should_receive(:send_xml).with(@client.stream_stanza)
69
+ @client.connection_completed
70
+ end
71
+ end
72
+
73
+ describe "receive_stanza" do
74
+ before(:each) do
75
+ @doc = Nokogiri::XML::Document.new
76
+ end
77
+ describe "when connected" do
78
+ before(:each) do
79
+ @client.instance_variable_set("@state", :connected)
80
+ end
81
+ it "should call super"
82
+ end
83
+
84
+ describe "when wait_for_stream_authenticated" do
85
+ before(:each) do
86
+ @client.instance_variable_set("@state", :wait_for_stream_authenticated)
87
+ @stanza = Nokogiri::XML::Node.new("stream:stream", @doc)
88
+ @stanza["id"] = "123"
89
+ end
90
+ it "should change state to wait_for_bind if the stanza is stream:stream with an id" do
91
+ @client.receive_stanza(@stanza)
92
+ @client.instance_variable_get("@state").should == :wait_for_bind
93
+ end
94
+ end
95
+
96
+ describe "when wait_for_stream" do
97
+ before(:each) do
98
+ @client.instance_variable_set("@state", :wait_for_stream)
99
+ @stanza = Nokogiri::XML::Node.new("stream:stream", @doc)
100
+ @stanza["id"] = "123"
101
+ end
102
+ it "should change state to wait_for_auth_mechanisms if the stanza is stream:stream with an id" do
103
+ @client.receive_stanza(@stanza)
104
+ @client.instance_variable_get("@state").should == :wait_for_auth_mechanisms
105
+ end
106
+ end
107
+
108
+ describe "when wait_for_auth_mechanisms" do
109
+ before(:each) do
110
+ @client.instance_variable_set("@state", :wait_for_auth_mechanisms)
111
+ end
112
+
113
+ describe "if the stanza is stream:features" do
114
+ before(:each) do
115
+ @stanza = Nokogiri::XML::Node.new("stream:features", @doc)
116
+ @stanza["id"] = "123"
117
+ end
118
+
119
+ describe "if the stanza has startls" do
120
+ before(:each) do
121
+ @stanza.add_child(Nokogiri::XML::Node.new("starttls", @doc))
122
+ end
123
+ it "should send start tls" do
124
+ @client.should_receive(:send_xml).with('<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>')
125
+ @client.receive_stanza(@stanza)
126
+ end
127
+ end
128
+
129
+ describe "if the stanza has mechanisms" do
130
+ before(:each) do
131
+ mechanisms = Nokogiri::XML::Node.new("mechanisms", @doc)
132
+ mechanism = Nokogiri::XML::Node.new("mechanism", @doc)
133
+ mechanism.content = "PLAIN"
134
+ mechanisms.add_child(mechanism)
135
+ @stanza.add_child(mechanisms)
136
+ end
137
+
138
+ it "should send authentication" do
139
+ @client.should_receive(:send_xml).with("<auth mechanism=\"PLAIN\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">amlkQHNlcnZlci50bGQAamlkAHBhc3N3b3Jk</auth>")
140
+ @client.receive_stanza(@stanza)
141
+ end
142
+ end
143
+
144
+ end
145
+ end
146
+
147
+ describe "when wait_for_success" do
148
+ before(:each) do
149
+ @client.instance_variable_set("@state", :wait_for_success)
150
+ end
151
+ describe "when stanza is success" do
152
+ before(:each) do
153
+ @stanza = Nokogiri::XML::Node.new("success", @doc)
154
+ end
155
+
156
+ it "should reset the parser" do
157
+ @client.instance_variable_get("@parser").should_receive(:reset)
158
+ @client.receive_stanza(@stanza)
159
+ end
160
+
161
+ it "should send stream_stanza" do
162
+ @client.should_receive(:send_xml).with("<?xml version=\"1.0\"?>\n<stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" to=\"server.tld\" version=\"1.0\">\n ")
163
+ @client.receive_stanza(@stanza)
164
+ end
165
+
166
+ it "should change state to wait_for_stream_authenticated" do
167
+ @client.receive_stanza(@stanza)
168
+ @client.instance_variable_get("@state").should == :wait_for_stream_authenticated
169
+ end
170
+
171
+ end
172
+ describe "when stanza is failure" do
173
+ before(:each) do
174
+ @stanza = Nokogiri::XML::Node.new("failure", @doc)
175
+ end
176
+ it "should raise AuthenticationError if stanza has bad-auth" do
177
+ @stanza.add_child(Nokogiri::XML::Node.new("bad-auth", @doc))
178
+ lambda {
179
+ @client.receive_stanza(@stanza)
180
+ }.should raise_error(Skates::AuthenticationError)
181
+ end
182
+
183
+ it "should raise AuthenticationError if stanza has not-authorized" do
184
+ @stanza.add_child(Nokogiri::XML::Node.new("not-authorized", @doc))
185
+ lambda {
186
+ @client.receive_stanza(@stanza)
187
+ }.should raise_error(Skates::AuthenticationError)
188
+ end
189
+ end
190
+ end
191
+
192
+ describe "when wait_for_bind" do
193
+ before(:each) do
194
+ @client.instance_variable_set("@state", :wait_for_bind)
195
+ end
196
+
197
+ describe "if stanza is stream:features" do
198
+ before(:each) do
199
+ @stanza = Nokogiri::XML::Node.new("stream:features", @doc)
200
+ end
201
+
202
+ describe "if stanza has bind" do
203
+ before(:each) do
204
+ bind = Nokogiri::XML::Node.new("bind", @doc)
205
+ @stanza.add_child(bind)
206
+ end
207
+
208
+ it "should send_xml with the bind iq" do
209
+ @client.should_receive(:binding_iq_id).twice.and_return(123)
210
+ @client.should_receive(:send_xml).with("<iq type=\"set\" id=\"123\">\n <bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">\n <resource>skates_client_123</resource>\n </bind>\n</iq>")
211
+ @client.receive_stanza(@stanza)
212
+ end
213
+
214
+ it "should set the state to :wait_for_confirmed_binding" do
215
+ @client.receive_stanza(@stanza)
216
+ @client.instance_variable_get("@state").should == :wait_for_confirmed_binding
217
+ end
218
+ end
219
+ end
220
+ end
221
+
222
+ describe "when wait_for_confirmed_binding" do
223
+ before(:each) do
224
+ @client.instance_variable_set("@state", :wait_for_confirmed_binding)
225
+ end
226
+ describe "if stanza is iq with type=result and the righ binding_iq_id" do
227
+ before(:each) do
228
+ binding_iq_id = 123
229
+ @stanza = Nokogiri::XML::Node.new("iq", @doc)
230
+ @stanza["type"] = "result"
231
+ @stanza["id"] = binding_iq_id.to_s
232
+ @client.stub!(:binding_iq_id).and_return(binding_iq_id)
233
+ end
234
+
235
+ it "should send_xml with the session iq" do
236
+ @client.should_receive(:session_iq_id).and_return(123)
237
+ @client.should_receive(:send_xml).with("<iq type=\"set\" id=\"123\">\n <session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>\n</iq>")
238
+ @client.receive_stanza(@stanza)
239
+ end
240
+
241
+ it "should set the state to :wait_for_confirmed_session" do
242
+ @client.receive_stanza(@stanza)
243
+ @client.instance_variable_get("@state").should == :wait_for_confirmed_session
244
+ end
245
+ end
246
+
247
+ end
248
+
249
+ describe "when wait_for_confirmed_session" do
250
+ before(:each) do
251
+ @client.instance_variable_set("@state", :wait_for_confirmed_session)
252
+ end
253
+
254
+ describe "if stanza is iq with type=result and the righ session_iq_id" do
255
+ before(:each) do
256
+ session_iq_id = 123
257
+ @stanza = Nokogiri::XML::Node.new("iq", @doc)
258
+ @stanza["type"] = "result"
259
+ @stanza["id"] = session_iq_id.to_s
260
+ @client.stub!(:session_iq_id).and_return(session_iq_id)
261
+ end
262
+
263
+ it "should send_xml the initial presence" do
264
+ @client.should_receive(:send_xml).with("<presence/>")
265
+ @client.receive_stanza(@stanza)
266
+ end
267
+
268
+ it "should set the state to :connected" do
269
+ @client.receive_stanza(@stanza)
270
+ @client.instance_variable_get("@state").should == :connected
271
+ end
272
+ end
273
+ end
274
+
275
+ describe "when wait_for_proceed" do
276
+ before(:each) do
277
+ @client.instance_variable_set("@state", :wait_for_proceed)
278
+ @client.stub!(:start_tls).and_return(true)
279
+ end
280
+
281
+ it "should start_tls" do
282
+ @client.should_receive(:start_tls)
283
+ @client.receive_stanza(@stanza)
284
+ end
285
+
286
+ it "reset the parser" do
287
+ @client.instance_variable_get("@parser").should_receive(:reset)
288
+ @client.receive_stanza(@stanza)
289
+ end
290
+
291
+ it "should set the state to :wait_for_stream" do
292
+ @client.receive_stanza(@stanza)
293
+ @client.instance_variable_get("@state").should == :wait_for_stream
294
+ end
295
+
296
+ it "should send the stream stanza" do
297
+ @client.should_receive(:send_xml).with("<?xml version=\"1.0\"?>\n<stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" to=\"server.tld\" version=\"1.0\">\n ")
298
+ @client.receive_stanza(@stanza)
299
+ end
300
+ end
301
+ end
302
+
303
+ describe "stream_namespace" do
304
+ it "should return jabber:client" do
305
+ @client.stream_namespace.should == "jabber:client"
306
+ end
307
+ end
308
+
309
+ end