pusher 2.0.0 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/spec/channel_spec.rb DELETED
@@ -1,187 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'spec_helper'
3
-
4
- describe Pusher::Channel do
5
- before do
6
- @client = Pusher::Client.new({
7
- :app_id => '20',
8
- :key => '12345678900000001',
9
- :secret => '12345678900000001',
10
- :host => 'api.pusherapp.com',
11
- :port => 80,
12
- })
13
- @channel = @client['test_channel']
14
- end
15
-
16
- let(:pusher_url_regexp) { %r{/apps/20/events} }
17
-
18
- def stub_post(status, body = nil)
19
- options = {:status => status}
20
- options.merge!({:body => body}) if body
21
-
22
- stub_request(:post, pusher_url_regexp).to_return(options)
23
- end
24
-
25
- def stub_post_to_raise(e)
26
- stub_request(:post, pusher_url_regexp).to_raise(e)
27
- end
28
-
29
- describe '#trigger!' do
30
- it "should use @client.trigger internally" do
31
- expect(@client).to receive(:trigger)
32
- @channel.trigger!('new_event', 'Some data')
33
- end
34
- end
35
-
36
- describe '#trigger' do
37
- it "should log failure if error raised in http call" do
38
- stub_post_to_raise(HTTPClient::BadResponseError)
39
-
40
- expect(Pusher.logger).to receive(:error).with("Exception from WebMock (HTTPClient::BadResponseError) (Pusher::HTTPError)")
41
- expect(Pusher.logger).to receive(:debug) #backtrace
42
- @channel.trigger('new_event', 'Some data')
43
- end
44
-
45
- it "should log failure if Pusher returns an error response" do
46
- stub_post 401, "some signature info"
47
- expect(Pusher.logger).to receive(:error).with("some signature info (Pusher::AuthenticationError)")
48
- expect(Pusher.logger).to receive(:debug) #backtrace
49
- @channel.trigger('new_event', 'Some data')
50
- end
51
- end
52
-
53
- describe "#initialization" do
54
- it "should not be too long" do
55
- expect { @client['b'*201] }.to raise_error(Pusher::Error)
56
- end
57
-
58
- it "should not use bad characters" do
59
- expect { @client['*^!±`/""'] }.to raise_error(Pusher::Error)
60
- end
61
- end
62
-
63
- describe "#trigger_async" do
64
- it "should use @client.trigger_async internally" do
65
- expect(@client).to receive(:trigger_async)
66
- @channel.trigger_async('new_event', 'Some data')
67
- end
68
- end
69
-
70
- describe '#info' do
71
- it "should call the Client#channel_info" do
72
- expect(@client).to receive(:get)
73
- .with("/channels/mychannel", anything)
74
- .and_return({:occupied => true, :subscription_count => 12})
75
- @channel = @client['mychannel']
76
- @channel.info
77
- end
78
-
79
- it "should assemble the requested attributes into the info option" do
80
- expect(@client).to receive(:get)
81
- .with(anything, {:info => "user_count,connection_count"})
82
- .and_return({:occupied => true, :subscription_count => 12, :user_count => 12})
83
- @channel = @client['presence-foo']
84
- @channel.info(%w{user_count connection_count})
85
- end
86
- end
87
-
88
- describe '#users' do
89
- it "should call the Client#channel_users" do
90
- expect(@client).to receive(:get).with("/channels/presence-mychannel/users", {}).and_return({:users => {'id' => '4'}})
91
- @channel = @client['presence-mychannel']
92
- @channel.users
93
- end
94
- end
95
-
96
- describe "#authentication_string" do
97
- def authentication_string(*data)
98
- lambda { @channel.authentication_string(*data) }
99
- end
100
-
101
- it "should return an authentication string given a socket id" do
102
- auth = @channel.authentication_string('1.1')
103
-
104
- expect(auth).to eq('12345678900000001:02259dff9a2a3f71ea8ab29ac0c0c0ef7996c8f3fd3702be5533f30da7d7fed4')
105
- end
106
-
107
- it "should raise error if authentication is invalid" do
108
- [nil, ''].each do |invalid|
109
- expect(authentication_string(invalid)).to raise_error Pusher::Error
110
- end
111
- end
112
-
113
- describe 'with extra string argument' do
114
- it 'should be a string or nil' do
115
- expect(authentication_string('1.1', 123)).to raise_error Pusher::Error
116
- expect(authentication_string('1.1', {})).to raise_error Pusher::Error
117
-
118
- expect(authentication_string('1.1', 'boom')).not_to raise_error
119
- expect(authentication_string('1.1', nil)).not_to raise_error
120
- end
121
-
122
- it "should return an authentication string given a socket id and custom args" do
123
- auth = @channel.authentication_string('1.1', 'foobar')
124
-
125
- expect(auth).to eq("12345678900000001:#{hmac(@client.secret, "1.1:test_channel:foobar")}")
126
- end
127
- end
128
- end
129
-
130
- describe '#authenticate' do
131
- before :each do
132
- @custom_data = {:uid => 123, :info => {:name => 'Foo'}}
133
- end
134
-
135
- it 'should return a hash with signature including custom data and data as json string' do
136
- allow(MultiJson).to receive(:encode).with(@custom_data).and_return 'a json string'
137
-
138
- response = @channel.authenticate('1.1', @custom_data)
139
-
140
- expect(response).to eq({
141
- :auth => "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:a json string")}",
142
- :channel_data => 'a json string'
143
- })
144
- end
145
-
146
- it 'should fail on invalid socket_ids' do
147
- expect {
148
- @channel.authenticate('1.1:')
149
- }.to raise_error Pusher::Error
150
-
151
- expect {
152
- @channel.authenticate('1.1foo', 'channel')
153
- }.to raise_error Pusher::Error
154
-
155
- expect {
156
- @channel.authenticate(':1.1')
157
- }.to raise_error Pusher::Error
158
-
159
- expect {
160
- @channel.authenticate('foo1.1', 'channel')
161
- }.to raise_error Pusher::Error
162
-
163
- expect {
164
- @channel.authenticate('foo', 'channel')
165
- }.to raise_error Pusher::Error
166
- end
167
- end
168
-
169
- describe `#shared_secret` do
170
- before(:each) do
171
- @channel.instance_variable_set(:@name, 'private-encrypted-1')
172
- end
173
-
174
- it 'should return a shared_secret based on the channel name and encryption master key' do
175
- key = '3W1pfB/Etr+ZIlfMWwZP3gz8jEeCt4s2pe6Vpr+2c3M='
176
- shared_secret = @channel.shared_secret(key)
177
- expect(Base64.strict_encode64(shared_secret)).to eq(
178
- "6zeEp/chneRPS1cbK/hGeG860UhHomxSN6hTgzwT20I="
179
- )
180
- end
181
-
182
- it 'should return nil if missing encryption master key' do
183
- shared_secret = @channel.shared_secret(nil)
184
- expect(shared_secret).to be_nil
185
- end
186
- end
187
- end