pusher-fake 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pusher-fake/server/chain_trap_handlers.rb +24 -0
  3. data/lib/pusher-fake/server.rb +10 -0
  4. data/lib/pusher-fake.rb +1 -1
  5. metadata +20 -81
  6. data/spec/features/api/channels_spec.rb +0 -86
  7. data/spec/features/api/users_spec.rb +0 -36
  8. data/spec/features/client/connect_spec.rb +0 -11
  9. data/spec/features/client/event_spec.rb +0 -49
  10. data/spec/features/client/presence_spec.rb +0 -58
  11. data/spec/features/client/subscribe_spec.rb +0 -84
  12. data/spec/features/server/event_spec.rb +0 -122
  13. data/spec/features/server/webhooks_spec.rb +0 -107
  14. data/spec/lib/pusher-fake/channel/presence_spec.rb +0 -249
  15. data/spec/lib/pusher-fake/channel/private_spec.rb +0 -180
  16. data/spec/lib/pusher-fake/channel/public_spec.rb +0 -206
  17. data/spec/lib/pusher-fake/channel_spec.rb +0 -122
  18. data/spec/lib/pusher-fake/configuration_spec.rb +0 -123
  19. data/spec/lib/pusher-fake/connection_spec.rb +0 -332
  20. data/spec/lib/pusher-fake/server/application_spec.rb +0 -604
  21. data/spec/lib/pusher-fake/server_spec.rb +0 -177
  22. data/spec/lib/pusher-fake/webhook_spec.rb +0 -67
  23. data/spec/lib/pusher_fake_spec.rb +0 -96
  24. data/spec/spec_helper.rb +0 -31
  25. data/spec/support/application/public/javascripts/vendor/polyfill.min.js +0 -1
  26. data/spec/support/application/public/javascripts/vendor/pusher-7.0.6.js +0 -4567
  27. data/spec/support/application/views/index.erb +0 -91
  28. data/spec/support/application.rb +0 -35
  29. data/spec/support/capybara.rb +0 -10
  30. data/spec/support/helpers/connect.rb +0 -21
  31. data/spec/support/helpers/event.rb +0 -11
  32. data/spec/support/helpers/subscription.rb +0 -31
  33. data/spec/support/helpers/user.rb +0 -13
  34. data/spec/support/matchers/have_configuration_option.rb +0 -19
  35. data/spec/support/pusher_fake.rb +0 -21
  36. data/spec/support/webhooks.rb +0 -40
@@ -1,122 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- feature "Server triggers event" do
6
- let(:event) { "message" }
7
- let(:other_user) { "Bob" }
8
- let(:public_channel) { "chat" }
9
- let(:private_channel) { "private-chat" }
10
-
11
- before do
12
- connect
13
- connect_as(other_user)
14
- end
15
-
16
- scenario "on a subscribed public channel" do
17
- subscribe_to(public_channel)
18
- subscribe_to_as(public_channel, other_user)
19
-
20
- trigger(public_channel, event)
21
-
22
- expect(page).to have_event(event, on: public_channel)
23
-
24
- using_session(other_user) do
25
- expect(page).to have_event(event, on: public_channel)
26
- end
27
- end
28
-
29
- scenario "on a previously subscribed public channel" do
30
- subscribe_to(public_channel)
31
- subscribe_to_as(public_channel, other_user)
32
- unsubscribe_from(public_channel)
33
-
34
- trigger(public_channel, event)
35
-
36
- expect(page).not_to have_event(event, on: public_channel)
37
-
38
- using_session(other_user) do
39
- expect(page).to have_event(event, on: public_channel)
40
- end
41
- end
42
-
43
- scenario "on an unsubscribed public channel" do
44
- trigger(public_channel, event)
45
-
46
- expect(page).not_to have_event(event, on: public_channel)
47
-
48
- using_session(other_user) do
49
- expect(page).not_to have_event(event, on: public_channel)
50
- end
51
- end
52
-
53
- scenario "on a subscribed private channel" do
54
- subscribe_to(private_channel)
55
- subscribe_to_as(private_channel, other_user)
56
-
57
- trigger(private_channel, event)
58
-
59
- expect(page).to have_event(event, on: private_channel)
60
-
61
- using_session(other_user) do
62
- expect(page).to have_event(event, on: private_channel)
63
- end
64
- end
65
-
66
- scenario "on a previously subscribed private channel" do
67
- subscribe_to(private_channel)
68
- subscribe_to_as(private_channel, other_user)
69
- unsubscribe_from(private_channel)
70
-
71
- trigger(private_channel, event)
72
-
73
- expect(page).not_to have_event(event, on: private_channel)
74
-
75
- using_session(other_user) do
76
- expect(page).to have_event(event, on: private_channel)
77
- end
78
- end
79
-
80
- scenario "on an unsubscribed private channel" do
81
- trigger(private_channel, event)
82
-
83
- expect(page).not_to have_event(event, on: private_channel)
84
-
85
- using_session(other_user) do
86
- expect(page).not_to have_event(event, on: private_channel)
87
- end
88
- end
89
-
90
- scenario "on multiple subscribed private channels" do
91
- subscribe_to("private-chat-1")
92
- subscribe_to_as("private-chat-2", other_user)
93
-
94
- trigger("private-chat-1", event)
95
- trigger("private-chat-2", event)
96
-
97
- expect(page).to have_event(event, on: "private-chat-1")
98
-
99
- using_session(other_user) do
100
- expect(page).to have_event(event, on: "private-chat-2")
101
- end
102
- end
103
-
104
- scenario "on a subscribed public channel, ignoring a user" do
105
- subscribe_to(public_channel)
106
- subscribe_to_as(public_channel, other_user)
107
-
108
- trigger(public_channel, event, socket_id: user_id(other_user))
109
-
110
- expect(page).to have_event(event, on: public_channel)
111
-
112
- using_session(other_user) do
113
- expect(page).not_to have_event(event, on: public_channel)
114
- end
115
- end
116
-
117
- protected
118
-
119
- def trigger(channel, event, options = {})
120
- Pusher.trigger(channel, event, {}, options)
121
- end
122
- end
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- feature "Receiving event webhooks" do
6
- let(:channel) { "room-1" }
7
- let(:other_user) { "Bob" }
8
- let(:cache_channel) { "cache-last-command" }
9
- let(:presence_channel) { "presence-room-1" }
10
-
11
- before do
12
- events.clear
13
-
14
- PusherFake.configuration.webhooks = ["http://127.0.0.1:8082"]
15
-
16
- connect
17
- connect_as(other_user)
18
- end
19
-
20
- scenario "occupying a channel" do
21
- subscribe_to(channel)
22
-
23
- expect(events).to include_event("channel_occupied", "channel" => channel)
24
-
25
- subscribe_to_as(channel, other_user)
26
-
27
- expect(events.size).to eq(1)
28
- end
29
-
30
- scenario "vacating a channel" do
31
- subscribe_to(channel)
32
- subscribe_to_as(channel, other_user)
33
-
34
- unsubscribe_from(channel)
35
-
36
- expect(events.size).to eq(1)
37
-
38
- unsubscribe_from_as(channel, other_user)
39
-
40
- expect(events).to include_event("channel_vacated", "channel" => channel)
41
- end
42
-
43
- scenario "subscribing to a presence channel" do
44
- subscribe_to(presence_channel)
45
-
46
- expect(events).to include_event(
47
- "member_added",
48
- "channel" => presence_channel, "user_id" => user_id
49
- )
50
-
51
- subscribe_to_as(presence_channel, other_user)
52
-
53
- expect(events).to include_event(
54
- "member_added",
55
- "channel" => presence_channel, "user_id" => user_id(other_user)
56
- )
57
- end
58
-
59
- scenario "unsubscribing from a presence channel" do
60
- subscribe_to(presence_channel)
61
- subscribe_to_as(presence_channel, other_user)
62
-
63
- unsubscribe_from(presence_channel)
64
-
65
- expect(events).to include_event("member_added",
66
- "channel" => presence_channel,
67
- "user_id" => user_id)
68
-
69
- unsubscribe_from_as(presence_channel, other_user)
70
-
71
- expect(events).to include_event("member_added",
72
- "channel" => presence_channel,
73
- "user_id" => user_id(other_user))
74
- end
75
-
76
- scenario "subscribing to a cache channel with no event" do
77
- subscribe_to(cache_channel)
78
-
79
- expect(events).to include_event("cache_miss",
80
- "channel" => cache_channel)
81
- end
82
-
83
- scenario "subscribing to a cache channel with an event" do
84
- subscribe_to(cache_channel)
85
- events.clear
86
- Pusher.trigger(cache_channel, "an event", {}, {})
87
-
88
- subscribe_to_as(cache_channel, other_user)
89
-
90
- expect(events).not_to include_event("cache_miss",
91
- "channel" => cache_channel)
92
- end
93
-
94
- protected
95
-
96
- def events
97
- sleep(1)
98
-
99
- WebhookHelper.mutex.synchronize do
100
- WebhookHelper.events
101
- end
102
- end
103
-
104
- def include_event(event, options = {})
105
- include(options.merge("name" => event))
106
- end
107
- end
@@ -1,249 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe PusherFake::Channel::Presence do
6
- subject { described_class }
7
-
8
- let(:name) { "channel" }
9
-
10
- it "inherits from private channel" do
11
- expect(subject.ancestors).to include(PusherFake::Channel::Private)
12
- end
13
-
14
- it "creates an empty members hash" do
15
- channel = subject.new(name)
16
-
17
- expect(channel.members).to eq({})
18
- end
19
- end
20
-
21
- describe PusherFake::Channel::Presence, "#add" do
22
- subject { described_class.new(name) }
23
-
24
- let(:name) { "name" }
25
- let(:user_id) { "1234" }
26
- let(:connection) { instance_double(PusherFake::Connection, emit: nil) }
27
- let(:connections) { instance_double(Array, push: nil, length: 0) }
28
- let(:channel_data) { { user_id: user_id } }
29
- let(:authentication) { "auth" }
30
- let(:subscription_data) { { presence: { hash: {}, count: 1 } } }
31
-
32
- let(:data) do
33
- { auth: authentication,
34
- channel_data: MultiJson.dump(channel_data) }
35
- end
36
-
37
- before do
38
- allow(PusherFake::Webhook).to receive(:trigger)
39
- allow(MultiJson).to receive(:load).and_return(channel_data)
40
- allow(subject).to receive(:connections).and_return(connections)
41
- allow(subject).to receive(:emit).and_return(nil)
42
- allow(subject).to receive(:subscription_data).and_return(subscription_data)
43
- end
44
-
45
- it "authorizes the connection" do
46
- allow(subject).to receive(:authorized?).and_return(nil)
47
-
48
- subject.add(connection, data)
49
-
50
- expect(subject).to have_received(:authorized?).with(connection, data)
51
- end
52
-
53
- it "parses the channel_data when authorized" do
54
- allow(subject).to receive(:authorized?).and_return(true)
55
-
56
- subject.add(connection, data)
57
-
58
- expect(MultiJson).to have_received(:load)
59
- .with(data[:channel_data], symbolize_keys: true)
60
- end
61
-
62
- it "assigns the channel_data to the members hash for current connection" do
63
- allow(subject).to receive(:authorized?).and_return(true)
64
-
65
- subject.add(connection, data)
66
-
67
- expect(subject.members[connection]).to eq(channel_data)
68
- end
69
-
70
- it "notifies the channel of the new member when authorized" do
71
- allow(subject).to receive(:authorized?).and_return(true)
72
-
73
- subject.add(connection, data)
74
-
75
- expect(subject).to have_received(:emit)
76
- .with("pusher_internal:member_added", channel_data)
77
- end
78
-
79
- it "successfully subscribes the connection when authorized" do
80
- allow(subject).to receive(:authorized?).and_return(true)
81
-
82
- subject.add(connection, data)
83
-
84
- expect(connection).to have_received(:emit)
85
- .with("pusher_internal:subscription_succeeded",
86
- subscription_data, subject.name)
87
- end
88
-
89
- it "adds the connection to the channel when authorized" do
90
- allow(subject).to receive(:authorized?).and_return(true)
91
-
92
- subject.add(connection, data)
93
-
94
- expect(connections).to have_received(:push).with(connection)
95
- end
96
-
97
- it "triggers occupied webhook for first connection added when authorized" do
98
- allow(subject).to receive(:authorized?).and_return(true)
99
- allow(subject).to receive(:connections).and_call_original
100
-
101
- 2.times { subject.add(connection, data) }
102
-
103
- expect(PusherFake::Webhook).to have_received(:trigger)
104
- .with("channel_occupied", channel: name).once
105
- end
106
-
107
- it "triggers the member added webhook when authorized" do
108
- allow(subject).to receive(:authorized?).and_return(true)
109
-
110
- subject.add(connection, data)
111
-
112
- expect(PusherFake::Webhook).to have_received(:trigger)
113
- .with("member_added", channel: name, user_id: user_id).once
114
- end
115
-
116
- it "unsuccessfully subscribes the connection when not authorized" do
117
- allow(subject).to receive(:authorized?).and_return(false)
118
-
119
- subject.add(connection, data)
120
-
121
- expect(connection).to have_received(:emit)
122
- .with("pusher_internal:subscription_error", {}, subject.name)
123
- end
124
-
125
- it "does not trigger channel occupied webhook when not authorized" do
126
- allow(subject).to receive(:authorized?).and_return(false)
127
- allow(subject).to receive(:connections).and_call_original
128
-
129
- 2.times { subject.add(connection, data) }
130
-
131
- expect(PusherFake::Webhook).not_to have_received(:trigger)
132
- end
133
-
134
- it "does not trigger the member added webhook when not authorized" do
135
- allow(subject).to receive(:authorized?).and_return(false)
136
-
137
- subject.add(connection, data)
138
-
139
- expect(PusherFake::Webhook).not_to have_received(:trigger)
140
- end
141
- end
142
-
143
- describe PusherFake::Channel::Presence, "#remove" do
144
- subject { described_class.new(name) }
145
-
146
- let(:name) { "name" }
147
- let(:user_id) { "1234" }
148
- let(:connection) { double }
149
- let(:channel_data) { { user_id: user_id } }
150
-
151
- before do
152
- allow(PusherFake::Webhook).to receive(:trigger)
153
- allow(subject).to receive(:connections).and_return([connection])
154
- allow(subject).to receive(:emit).and_return(nil)
155
-
156
- subject.members[connection] = channel_data
157
- end
158
-
159
- it "removes the connection from the channel" do
160
- subject.remove(connection)
161
-
162
- expect(subject.connections).to be_empty
163
- end
164
-
165
- it "removes the connection from the members hash" do
166
- subject.remove(connection)
167
-
168
- expect(subject.members).not_to have_key(connection)
169
- end
170
-
171
- it "triggers the member removed webhook" do
172
- subject.remove(connection)
173
-
174
- expect(PusherFake::Webhook).to have_received(:trigger)
175
- .with("member_removed", channel: name, user_id: user_id).once
176
- end
177
-
178
- it "notifies the channel of the removed member" do
179
- subject.remove(connection)
180
-
181
- expect(subject).to have_received(:emit)
182
- .with("pusher_internal:member_removed", channel_data)
183
- end
184
- end
185
-
186
- describe PusherFake::Channel::Presence,
187
- "#remove, for an unsubscribed connection" do
188
- subject { described_class.new(name) }
189
-
190
- let(:name) { "name" }
191
- let(:user_id) { "1234" }
192
- let(:connection) { double }
193
- let(:channel_data) { { user_id: user_id } }
194
-
195
- before do
196
- allow(subject).to receive(:connections).and_return([])
197
- allow(subject).to receive(:emit).and_return(nil)
198
- allow(subject).to receive(:trigger).and_return(nil)
199
- end
200
-
201
- it "does not raise an error" do
202
- expect do
203
- subject.remove(connection)
204
- end.not_to raise_error
205
- end
206
-
207
- it "does not trigger an event" do
208
- subject.remove(connection)
209
-
210
- expect(subject).not_to have_received(:trigger)
211
- .with("member_removed", channel: name, user_id: user_id)
212
- end
213
-
214
- it "does not emit an event" do
215
- subject.remove(connection)
216
-
217
- expect(subject).not_to have_received(:emit)
218
- .with("pusher_internal:member_removed", channel_data)
219
- end
220
- end
221
-
222
- describe PusherFake::Channel::Presence, "#subscription_data" do
223
- subject { described_class.new("name") }
224
-
225
- let(:one) { { user_id: 1, name: "Bob" } }
226
- let(:two) { { user_id: 2, name: "Beau" } }
227
- let(:data) { subject.subscription_data }
228
- let(:members) { { double => one } }
229
-
230
- before do
231
- allow(subject).to receive(:members).and_return(members)
232
- end
233
-
234
- it "returns hash with presence information" do
235
- expect(data).to eq(presence: {
236
- hash: { one[:user_id] => one[:user_info] },
237
- count: 1
238
- })
239
- end
240
-
241
- it "handles multiple members" do
242
- members[double] = two
243
-
244
- expect(data[:presence][:count]).to eq(2)
245
- expect(data[:presence][:hash]).to eq(
246
- one[:user_id] => one[:user_info], two[:user_id] => two[:user_info]
247
- )
248
- end
249
- end
@@ -1,180 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe PusherFake::Channel::Private do
6
- subject { described_class }
7
-
8
- it "inherits from public channel" do
9
- expect(subject.ancestors).to include(PusherFake::Channel::Public)
10
- end
11
- end
12
-
13
- describe PusherFake::Channel::Private, "#add" do
14
- subject { described_class.new(name) }
15
-
16
- let(:data) { { auth: authentication } }
17
- let(:name) { "name" }
18
- let(:connection) { instance_double(PusherFake::Connection, emit: nil) }
19
- let(:connections) { instance_double(Array, push: nil, length: 0) }
20
- let(:authentication) { "auth" }
21
-
22
- before do
23
- allow(PusherFake::Webhook).to receive(:trigger)
24
- allow(subject).to receive(:connections).and_return(connections)
25
- end
26
-
27
- it "authorizes the connection" do
28
- allow(subject).to receive(:authorized?).and_return(nil)
29
-
30
- subject.add(connection, data)
31
-
32
- expect(subject).to have_received(:authorized?).with(connection, data)
33
- end
34
-
35
- it "adds the connection to the channel when authorized" do
36
- allow(subject).to receive(:authorized?).and_return(true)
37
-
38
- subject.add(connection, data)
39
-
40
- expect(connections).to have_received(:push).with(connection)
41
- end
42
-
43
- it "successfully subscribes the connection when authorized" do
44
- allow(subject).to receive(:authorized?).and_return(true)
45
-
46
- subject.add(connection, data)
47
-
48
- expect(connection).to have_received(:emit)
49
- .with("pusher_internal:subscription_succeeded", {}, subject.name)
50
- end
51
-
52
- it "triggers occupied webhook for first connection added when authorized" do
53
- allow(subject).to receive(:authorized?).and_return(true)
54
- allow(subject).to receive(:connections).and_call_original
55
-
56
- 2.times { subject.add(connection, data) }
57
-
58
- expect(PusherFake::Webhook).to have_received(:trigger)
59
- .with("channel_occupied", channel: name).once
60
- end
61
-
62
- it "unsuccessfully subscribes the connection when not authorized" do
63
- allow(subject).to receive(:authorized?).and_return(false)
64
-
65
- subject.add(connection, data)
66
-
67
- expect(connection).to have_received(:emit)
68
- .with("pusher_internal:subscription_error", {}, subject.name)
69
- end
70
-
71
- it "does not trigger channel occupied webhook when not authorized" do
72
- allow(subject).to receive(:authorized?).and_return(false)
73
- allow(subject).to receive(:connections).and_call_original
74
-
75
- 2.times { subject.add(connection, data) }
76
-
77
- expect(PusherFake::Webhook).not_to have_received(:trigger)
78
- end
79
- end
80
-
81
- describe PusherFake::Channel::Private, "#authentication_for" do
82
- subject { described_class.new(name) }
83
-
84
- let(:id) { "1234" }
85
- let(:name) { "private-channel" }
86
- let(:digest) { instance_double(OpenSSL::Digest::SHA256) }
87
- let(:string) { [id, name].join(":") }
88
- let(:signature) { "signature" }
89
-
90
- let(:configuration) do
91
- instance_double(PusherFake::Configuration, key: "key", secret: "secret")
92
- end
93
-
94
- before do
95
- allow(PusherFake).to receive(:configuration).and_return(configuration)
96
- allow(OpenSSL::HMAC).to receive(:hexdigest).and_return(signature)
97
- allow(OpenSSL::Digest).to receive(:new).with("SHA256").and_return(digest)
98
- end
99
-
100
- it "generates a signature" do
101
- subject.authentication_for(id)
102
-
103
- expect(OpenSSL::HMAC).to have_received(:hexdigest)
104
- .with(digest, configuration.secret, string)
105
- end
106
-
107
- it "returns the authentication string" do
108
- string = subject.authentication_for(id)
109
-
110
- expect(string).to eq("#{configuration.key}:#{signature}")
111
- end
112
- end
113
-
114
- describe PusherFake::Channel::Private,
115
- "#authentication_for, with channel data" do
116
- subject { described_class.new(name) }
117
-
118
- let(:id) { "1234" }
119
- let(:name) { "private-channel" }
120
- let(:digest) { instance_double(OpenSSL::Digest::SHA256) }
121
- let(:string) { [id, name, channel_data].join(":") }
122
- let(:signature) { "signature" }
123
- let(:channel_data) { "{}" }
124
-
125
- let(:configuration) do
126
- instance_double(PusherFake::Configuration, key: "key", secret: "secret")
127
- end
128
-
129
- before do
130
- allow(PusherFake).to receive(:configuration).and_return(configuration)
131
- allow(OpenSSL::HMAC).to receive(:hexdigest).and_return(signature)
132
- allow(OpenSSL::Digest).to receive(:new).with("SHA256").and_return(digest)
133
- end
134
-
135
- it "generates a signature" do
136
- subject.authentication_for(id, channel_data)
137
-
138
- expect(OpenSSL::HMAC).to have_received(:hexdigest)
139
- .with(digest, configuration.secret, string)
140
- end
141
-
142
- it "returns the authentication string" do
143
- string = subject.authentication_for(id, channel_data)
144
-
145
- expect(string).to eq("#{configuration.key}:#{signature}")
146
- end
147
- end
148
-
149
- describe PusherFake::Channel::Private, "#authorized?" do
150
- subject { described_class.new(name) }
151
-
152
- let(:data) { { auth: authentication, channel_data: channel_data } }
153
- let(:name) { "private-channel" }
154
- let(:connection) { instance_double(PusherFake::Connection, id: "1") }
155
- let(:channel_data) { "{}" }
156
- let(:authentication) { "authentication" }
157
-
158
- before do
159
- allow(subject).to receive(:authentication_for)
160
- end
161
-
162
- it "generates authentication for the connection ID" do
163
- subject.authorized?(connection, data)
164
-
165
- expect(subject).to have_received(:authentication_for)
166
- .with(connection.id, channel_data)
167
- end
168
-
169
- it "returns true if the authentication matches" do
170
- allow(subject).to receive(:authentication_for).and_return(authentication)
171
-
172
- expect(subject).to be_authorized(connection, data)
173
- end
174
-
175
- it "returns false if the authentication matches" do
176
- allow(subject).to receive(:authentication_for).and_return("")
177
-
178
- expect(subject).not_to be_authorized(connection, data)
179
- end
180
- end