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,332 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- shared_examples_for "#process" do
6
- subject { PusherFake::Connection.new(double) }
7
-
8
- let(:json) { double }
9
-
10
- before do
11
- allow(PusherFake).to receive(:log)
12
- allow(MultiJson).to receive(:load).and_return(message)
13
- end
14
-
15
- it "parses the JSON data" do
16
- subject.process(json)
17
-
18
- expect(MultiJson).to have_received(:load).with(json, symbolize_keys: true)
19
- end
20
-
21
- it "logs receiving the event" do
22
- subject.process(json)
23
-
24
- expect(PusherFake).to have_received(:log)
25
- .with("RECV #{subject.id}: #{message}")
26
- end
27
- end
28
-
29
- describe PusherFake::Connection do
30
- subject { described_class }
31
-
32
- let(:socket) { double }
33
-
34
- it "assigns the provided socket" do
35
- connection = subject.new(socket)
36
-
37
- expect(connection.socket).to eq(socket)
38
- end
39
- end
40
-
41
- describe PusherFake::Connection, "#emit" do
42
- subject { described_class.new(socket) }
43
-
44
- let(:data) { { some: "data", good: true } }
45
- let(:json) { MultiJson.dump(message) }
46
- let(:event) { "name" }
47
- let(:channel) { "channel" }
48
- let(:message) { { event: event, data: MultiJson.dump(data) } }
49
- let(:channel_json) { MultiJson.dump(message.merge(channel: channel)) }
50
-
51
- let(:socket) do
52
- instance_double(EventMachine::WebSocket::Connection, send: nil)
53
- end
54
-
55
- before do
56
- allow(PusherFake).to receive(:log)
57
- end
58
-
59
- it "sends the event to the socket as JSON" do
60
- subject.emit(event, data)
61
-
62
- expect(socket).to have_received(:send).with(json)
63
- end
64
-
65
- it "sets a channel when provided" do
66
- subject.emit(event, data, channel)
67
-
68
- expect(socket).to have_received(:send).with(channel_json)
69
- end
70
-
71
- it "logs sending the event" do
72
- subject.emit(event, data)
73
-
74
- expect(PusherFake).to have_received(:log)
75
- .with("SEND #{subject.id}: #{message}")
76
- end
77
- end
78
-
79
- describe PusherFake::Connection, "#establish" do
80
- subject { described_class.new(socket) }
81
-
82
- let(:socket) { double }
83
-
84
- before do
85
- allow(subject).to receive(:emit)
86
- end
87
-
88
- it "emits the connection established event with the connection ID" do
89
- subject.establish
90
-
91
- expect(subject).to have_received(:emit)
92
- .with("pusher:connection_established",
93
- socket_id: subject.id, activity_timeout: 120)
94
- end
95
- end
96
-
97
- describe PusherFake::Connection, "#id" do
98
- subject { described_class.new(socket) }
99
-
100
- let(:id) { "1234.567" }
101
- let(:socket) { instance_double(Object, object_id: 1_234_567) }
102
-
103
- it "returns the object ID of the socket" do
104
- expect(subject.id).to eq(id)
105
- end
106
- end
107
-
108
- describe PusherFake::Connection, "#process, with a subscribe event" do
109
- it_behaves_like "#process" do
110
- let(:data) { { channel: name, auth: "auth" } }
111
- let(:name) { "channel" }
112
- let(:message) { { event: "pusher:subscribe", data: data } }
113
-
114
- let(:channel) do
115
- instance_double(PusherFake::Channel::Presence, add: nil)
116
- end
117
-
118
- before do
119
- allow(PusherFake::Channel).to receive(:factory).and_return(channel)
120
- end
121
-
122
- it "creates a channel from the event data" do
123
- subject.process(json)
124
-
125
- expect(PusherFake::Channel).to have_received(:factory).with(name)
126
- end
127
-
128
- it "attempts to add the connection to the channel" do
129
- subject.process(json)
130
-
131
- expect(channel).to have_received(:add).with(subject, data)
132
- end
133
- end
134
- end
135
-
136
- describe PusherFake::Connection, "#process, with an unsubscribe event" do
137
- it_behaves_like "#process" do
138
- let(:name) { "channel" }
139
- let(:message) { { event: "pusher:unsubscribe", channel: name } }
140
-
141
- let(:channel) do
142
- instance_double(PusherFake::Channel::Presence, remove: nil)
143
- end
144
-
145
- before do
146
- allow(PusherFake::Channel).to receive(:factory).and_return(channel)
147
- end
148
-
149
- it "creates a channel from the event data" do
150
- subject.process(json)
151
-
152
- expect(PusherFake::Channel).to have_received(:factory).with(name)
153
- end
154
-
155
- it "removes the connection from the channel" do
156
- subject.process(json)
157
-
158
- expect(channel).to have_received(:remove).with(subject)
159
- end
160
- end
161
- end
162
-
163
- describe PusherFake::Connection, "#process, with a ping event" do
164
- it_behaves_like "#process" do
165
- let(:message) { { event: "pusher:ping", data: {} } }
166
-
167
- before do
168
- allow(subject).to receive(:emit)
169
- allow(PusherFake::Channel).to receive(:factory)
170
- end
171
-
172
- it "does not create a channel" do
173
- subject.process(json)
174
-
175
- expect(PusherFake::Channel).not_to have_received(:factory)
176
- end
177
-
178
- it "emits a pong event" do
179
- subject.process(json)
180
-
181
- expect(subject).to have_received(:emit).with("pusher:pong")
182
- end
183
- end
184
- end
185
-
186
- describe PusherFake::Connection, "#process, with a client event" do
187
- it_behaves_like "#process" do
188
- let(:data) { {} }
189
- let(:name) { "channel" }
190
- let(:event) { "client-hello-world" }
191
- let(:message) { { event: event, data: data, channel: name } }
192
-
193
- let(:channel) do
194
- instance_double(PusherFake::Channel::Private,
195
- emit: nil, includes?: nil, is_a?: true)
196
- end
197
-
198
- before do
199
- allow(subject).to receive(:trigger)
200
- allow(PusherFake::Channel).to receive(:factory).and_return(channel)
201
- end
202
-
203
- it "creates a channel from the event data" do
204
- subject.process(json)
205
-
206
- expect(PusherFake::Channel).to have_received(:factory).with(name)
207
- end
208
-
209
- it "ensures the channel is private" do
210
- subject.process(json)
211
-
212
- expect(channel).to have_received(:is_a?)
213
- .with(PusherFake::Channel::Private)
214
- end
215
-
216
- it "checks if the connection is in the channel" do
217
- subject.process(json)
218
-
219
- expect(channel).to have_received(:includes?).with(subject)
220
- end
221
-
222
- it "emits the event when the connection is in the channel" do
223
- allow(channel).to receive(:includes?).and_return(true)
224
-
225
- subject.process(json)
226
-
227
- expect(channel).to have_received(:emit)
228
- .with(event, data, socket_id: subject.id)
229
- end
230
-
231
- it "does not emit the event when the channel is not private" do
232
- allow(channel).to receive(:is_a?).and_return(false)
233
- allow(channel).to receive(:includes?).and_return(true)
234
-
235
- subject.process(json)
236
-
237
- expect(channel).not_to have_received(:emit)
238
- end
239
-
240
- it "does not emit the event when the connection is not in the channel" do
241
- allow(channel).to receive(:includes?).and_return(false)
242
-
243
- subject.process(json)
244
-
245
- expect(channel).not_to have_received(:emit)
246
- end
247
- end
248
- end
249
-
250
- describe PusherFake::Connection,
251
- "#process, with a client event trigger a webhook" do
252
- it_behaves_like "#process" do
253
- let(:data) { { example: "data" } }
254
- let(:name) { "channel" }
255
- let(:event) { "client-hello-world" }
256
- let(:user_id) { 1 }
257
- let(:members) { { subject => { user_id: user_id } } }
258
- let(:message) { { event: event, channel: name } }
259
- let(:options) { { channel: name, event: event, socket_id: subject.id } }
260
-
261
- let(:channel) do
262
- instance_double(PusherFake::Channel::Presence,
263
- name: name, emit: nil, includes?: nil)
264
- end
265
-
266
- before do
267
- allow(channel).to receive(:trigger)
268
- allow(channel).to receive(:includes?).with(subject).and_return(true)
269
- allow(channel).to receive(:is_a?)
270
- .with(PusherFake::Channel::Private).and_return(true)
271
- allow(channel).to receive(:is_a?)
272
- .with(PusherFake::Channel::Presence).and_return(false)
273
-
274
- # NOTE: Hack to avoid race condition in unit tests.
275
- allow(Thread).to receive(:new).and_yield
276
-
277
- allow(PusherFake::Channel).to receive(:factory).and_return(channel)
278
- end
279
-
280
- it "triggers the client event webhook" do
281
- subject.process(json)
282
-
283
- expect(channel).to have_received(:trigger)
284
- .with("client_event", options).once
285
- end
286
-
287
- it "includes data in event when present" do
288
- message[:data] = data
289
-
290
- subject.process(json)
291
-
292
- expect(channel).to have_received(:trigger)
293
- .with("client_event", options.merge(data: MultiJson.dump(data))).once
294
- end
295
-
296
- it "includes user ID in event when on a presence channel" do
297
- allow(channel).to receive(:is_a?).and_return(true)
298
- allow(channel).to receive(:members).and_return(members)
299
-
300
- subject.process(json)
301
-
302
- expect(channel).to have_received(:trigger)
303
- .with("client_event", options.merge(user_id: user_id)).once
304
- end
305
- end
306
- end
307
-
308
- describe PusherFake::Connection, "#process, with an unknown event" do
309
- it_behaves_like "#process" do
310
- let(:data) { {} }
311
- let(:name) { "channel" }
312
- let(:event) { "hello-world" }
313
- let(:channel) { instance_double(PusherFake::Channel::Public, emit: nil) }
314
- let(:message) { { event: event, data: data, channel: name } }
315
-
316
- before do
317
- allow(PusherFake::Channel).to receive(:factory).and_return(channel)
318
- end
319
-
320
- it "does not create a channel" do
321
- subject.process(json)
322
-
323
- expect(PusherFake::Channel).not_to have_received(:factory)
324
- end
325
-
326
- it "does not emit the event" do
327
- subject.process(json)
328
-
329
- expect(channel).not_to have_received(:emit)
330
- end
331
- end
332
- end