rails_chat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,67 @@
1
+ require "spec_helper"
2
+
3
+ describe RailsChat::FayeExtension do
4
+ before(:each) do
5
+ RailsChat.reset_config
6
+ @faye = RailsChat::FayeExtension.new
7
+ @message = {"channel" => "/meta/subscribe", "ext" => {}}
8
+ end
9
+
10
+ it "adds an error on an incoming subscription with a bad signature" do
11
+ @message["subscription"] = "hello"
12
+ @message["ext"]["rails_chat_signature"] = "bad"
13
+ @message["ext"]["rails_chat_timestamp"] = "123"
14
+ message = @faye.incoming(@message, lambda { |m| m })
15
+ message["error"].should eq("Incorrect signature.")
16
+ end
17
+
18
+ it "has no error when the signature matches the subscription" do
19
+ sub = RailsChat.subscription(:channel => "hello")
20
+ @message["subscription"] = sub[:channel]
21
+ @message["ext"]["rails_chat_signature"] = sub[:signature]
22
+ @message["ext"]["rails_chat_timestamp"] = sub[:timestamp]
23
+ message = @faye.incoming(@message, lambda { |m| m })
24
+ message["error"].should be_nil
25
+ end
26
+
27
+ it "has an error when signature just expired" do
28
+ RailsChat.config[:signature_expiration] = 1
29
+ sub = RailsChat.subscription(:timestamp => 123, :channel => "hello")
30
+ @message["subscription"] = sub[:channel]
31
+ @message["ext"]["rails_chat_signature"] = sub[:signature]
32
+ @message["ext"]["rails_chat_timestamp"] = sub[:timestamp]
33
+ message = @faye.incoming(@message, lambda { |m| m })
34
+ message["error"].should eq("Signature has expired.")
35
+ end
36
+
37
+ it "has an error when trying to publish to a custom channel with a bad token" do
38
+ RailsChat.config[:secret_token] = "good"
39
+ @message["channel"] = "/custom/channel"
40
+ @message["ext"]["rails_chat_token"] = "bad"
41
+ message = @faye.incoming(@message, lambda { |m| m })
42
+ message["error"].should eq("Incorrect token.")
43
+ end
44
+
45
+ it "raises an exception when attempting to call a custom channel without a secret_token set" do
46
+ @message["channel"] = "/custom/channel"
47
+ @message["ext"]["rails_chat_token"] = "bad"
48
+ lambda {
49
+ message = @faye.incoming(@message, lambda { |m| m })
50
+ }.should raise_error("No secret_token config set, ensure rails_chat.yml is loaded properly.")
51
+ end
52
+
53
+ it "has no error on other meta calls" do
54
+ @message["channel"] = "/meta/connect"
55
+ message = @faye.incoming(@message, lambda { |m| m })
56
+ message["error"].should be_nil
57
+ end
58
+
59
+ it "should not let message carry the RailsChat token after server's validation" do
60
+ RailsChat.config[:secret_token] = "good"
61
+ @message["channel"] = "/custom/channel"
62
+ @message["ext"]["rails_chat_token"] = RailsChat.config[:secret_token]
63
+ message = @faye.incoming(@message, lambda { |m| m })
64
+ message['ext']["rails_chat_token"].should be_nil
65
+ end
66
+
67
+ end
@@ -0,0 +1,141 @@
1
+ require "spec_helper"
2
+
3
+ describe RailsChat do
4
+ before(:each) do
5
+ RailsChat.reset_config
6
+ end
7
+
8
+ it "defaults server to nil" do
9
+ RailsChat.config[:server].should be_nil
10
+ end
11
+
12
+ it "defaults signature_expiration to nil" do
13
+ RailsChat.config[:signature_expiration].should be_nil
14
+ end
15
+
16
+ it "defaults subscription timestamp to current time in milliseconds" do
17
+ time = Time.now
18
+ Time.stub!(:now).and_return(time)
19
+ RailsChat.subscription[:timestamp].should eq((time.to_f * 1000).round)
20
+ end
21
+
22
+ it "loads a simple configuration file via load_config" do
23
+ RailsChat.load_config("spec/fixtures/rails_chat.yml", "production")
24
+ RailsChat.config[:server].should eq("http://example.com/faye")
25
+ RailsChat.config[:secret_token].should eq("PRODUCTION_SECRET_TOKEN")
26
+ RailsChat.config[:signature_expiration].should eq(600)
27
+ end
28
+
29
+ it "raises an exception if an invalid environment is passed to load_config" do
30
+ lambda {
31
+ RailsChat.load_config("spec/fixtures/rails_chat.yml", :test)
32
+ }.should raise_error ArgumentError
33
+ end
34
+
35
+ it "includes channel, server, and custom time in subscription" do
36
+ RailsChat.config[:server] = "server"
37
+ subscription = RailsChat.subscription(:timestamp => 123, :channel => "hello")
38
+ subscription[:timestamp].should eq(123)
39
+ subscription[:channel].should eq("hello")
40
+ subscription[:server].should eq("server")
41
+ end
42
+
43
+ it "does a sha1 digest of channel, timestamp, and secret token" do
44
+ RailsChat.config[:secret_token] = "token"
45
+ subscription = RailsChat.subscription(:timestamp => 123, :channel => "channel")
46
+ subscription[:signature].should eq(Digest::SHA1.hexdigest("tokenchannel123"))
47
+ end
48
+
49
+ it "formats a message hash given a channel and a string for eval" do
50
+ RailsChat.config[:secret_token] = "token"
51
+ RailsChat.message("chan", "foo").should eq(
52
+ :ext => {:rails_chat_token => "token"},
53
+ :channel => "chan",
54
+ :data => {
55
+ :channel => "chan",
56
+ :eval => "foo"
57
+ }
58
+ )
59
+ end
60
+
61
+ it "formats a message hash given a channel and a hash" do
62
+ RailsChat.config[:secret_token] = "token"
63
+ RailsChat.message("chan", :foo => "bar").should eq(
64
+ :ext => {:rails_chat_token => "token"},
65
+ :channel => "chan",
66
+ :data => {
67
+ :channel => "chan",
68
+ :data => {:foo => "bar"}
69
+ }
70
+ )
71
+ end
72
+
73
+ it "publish message as json to server using Net::HTTP" do
74
+ RailsChat.config[:server] = "http://localhost"
75
+ message = 'foo'
76
+ form = mock(:post).as_null_object
77
+ http = mock(:http).as_null_object
78
+
79
+ Net::HTTP::Post.should_receive(:new).with('/').and_return(form)
80
+ form.should_receive(:set_form_data).with(message: 'foo'.to_json)
81
+
82
+ Net::HTTP.should_receive(:new).with('localhost', 80).and_return(http)
83
+ http.should_receive(:start).and_yield(http)
84
+ http.should_receive(:request).with(form).and_return(:result)
85
+
86
+ RailsChat.publish_message(message).should eq(:result)
87
+ end
88
+
89
+ it "it should use HTTPS if the server URL says so" do
90
+ RailsChat.config[:server] = "https://localhost"
91
+ http = mock(:http).as_null_object
92
+
93
+ Net::HTTP.should_receive(:new).and_return(http)
94
+ http.should_receive(:use_ssl=).with(true)
95
+
96
+ RailsChat.publish_message('foo')
97
+ end
98
+
99
+ it "it should not use HTTPS if the server URL says not to" do
100
+ RailsChat.config[:server] = "http://localhost"
101
+ http = mock(:http).as_null_object
102
+
103
+ Net::HTTP.should_receive(:new).and_return(http)
104
+ http.should_receive(:use_ssl=).with(false)
105
+
106
+ RailsChat.publish_message('foo')
107
+ end
108
+
109
+ it "raises an exception if no server is specified when calling publish_message" do
110
+ lambda {
111
+ RailsChat.publish_message("foo")
112
+ }.should raise_error(RailsChat::Error)
113
+ end
114
+
115
+ it "publish_to passes message to publish_message call" do
116
+ RailsChat.should_receive(:message).with("chan", "foo").and_return("message")
117
+ RailsChat.should_receive(:publish_message).with("message").and_return(:result)
118
+ RailsChat.publish_to("chan", "foo").should eq(:result)
119
+ end
120
+
121
+ it "has a Faye rack app instance" do
122
+ RailsChat.faye_app.should be_kind_of(Faye::RackAdapter)
123
+ end
124
+
125
+ it "says signature has expired when time passed in is greater than expiration" do
126
+ RailsChat.config[:signature_expiration] = 30*60
127
+ time = RailsChat.subscription[:timestamp] - 31*60*1000
128
+ RailsChat.signature_expired?(time).should be_true
129
+ end
130
+
131
+ it "says signature has not expired when time passed in is less than expiration" do
132
+ RailsChat.config[:signature_expiration] = 30*60
133
+ time = RailsChat.subscription[:timestamp] - 29*60*1000
134
+ RailsChat.signature_expired?(time).should be_false
135
+ end
136
+
137
+ it "says signature has not expired when expiration is nil" do
138
+ RailsChat.config[:signature_expiration] = nil
139
+ RailsChat.signature_expired?(0).should be_false
140
+ end
141
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'json'
4
+ require 'faye'
5
+ Bundler.require(:default)
6
+
7
+ RSpec.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_chat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ! '[CISROR Team, Erfan Mansuri]'
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faye
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: thin
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.8.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.8.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: jasmine
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 1.1.1
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 1.1.1
94
+ description: ! 'RailsChat is a Ruby gem for use with Rails to publish and subscribe
95
+ to messages through Faye. It allows you to easily provide real-time updates through
96
+ an open socket without tying up a Rails process. All channels are private so users
97
+ can only listen to events you subscribe them to. Refrence gem: https://github.com/ryanb/private_pub'
98
+ email:
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - app/assets/stylesheets/emoticons.css
104
+ - app/assets/images/emoticons.png
105
+ - app/assets/javascripts/rails_chat.js
106
+ - app/assets/javascripts/emoticons.js
107
+ - app/assets/javascripts/emoticons_defination.js
108
+ - lib/rails_chat.rb
109
+ - lib/rails_chat/view_helpers.rb
110
+ - lib/rails_chat/faye_extension.rb
111
+ - lib/rails_chat/engine.rb
112
+ - lib/generators/rails_chat/templates/rails_chat.ru
113
+ - lib/generators/rails_chat/templates/rails_chat.yml
114
+ - lib/generators/rails_chat/install_generator.rb
115
+ - spec/rails_chat/faye_extension_spec.rb
116
+ - spec/fixtures/rails_chat.yml
117
+ - spec/javascripts/support/jasmine_runner.rb
118
+ - spec/javascripts/support/jasmine_config.rb
119
+ - spec/javascripts/support/jasmine.yml
120
+ - spec/javascripts/rails_chat_spec.js
121
+ - spec/spec_helper.rb
122
+ - spec/rails_chat_spec.rb
123
+ - Rakefile
124
+ - Gemfile
125
+ - LICENSE
126
+ - README.md
127
+ homepage:
128
+ licenses: []
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: 1.3.4
145
+ requirements: []
146
+ rubyforge_project: rails_chat
147
+ rubygems_version: 1.8.24
148
+ signing_key:
149
+ specification_version: 3
150
+ summary: ! 'RailsChat is a Ruby gem for use with Rails to publish and subscribe to
151
+ messages through Faye. It allows you to easily provide real-time updates through
152
+ an open socket without tying up a Rails process. All channels are private so users
153
+ can only listen to events you subscribe them to. Refrence gem: https://github.com/ryanb/private_pub'
154
+ test_files: []