pusher-fake 3.0.1 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pusher-fake/channel/presence.rb +1 -1
  3. data/lib/pusher-fake/channel/public.rb +32 -0
  4. data/lib/pusher-fake/server/chain_trap_handlers.rb +24 -0
  5. data/lib/pusher-fake/server.rb +10 -9
  6. data/lib/pusher-fake.rb +1 -1
  7. metadata +28 -90
  8. data/spec/features/api/channels_spec.rb +0 -86
  9. data/spec/features/api/users_spec.rb +0 -36
  10. data/spec/features/client/connect_spec.rb +0 -11
  11. data/spec/features/client/event_spec.rb +0 -49
  12. data/spec/features/client/presence_spec.rb +0 -58
  13. data/spec/features/client/subscribe_spec.rb +0 -69
  14. data/spec/features/server/event_spec.rb +0 -122
  15. data/spec/features/server/webhooks_spec.rb +0 -88
  16. data/spec/lib/pusher-fake/channel/presence_spec.rb +0 -249
  17. data/spec/lib/pusher-fake/channel/private_spec.rb +0 -180
  18. data/spec/lib/pusher-fake/channel/public_spec.rb +0 -148
  19. data/spec/lib/pusher-fake/channel_spec.rb +0 -122
  20. data/spec/lib/pusher-fake/configuration_spec.rb +0 -123
  21. data/spec/lib/pusher-fake/connection_spec.rb +0 -332
  22. data/spec/lib/pusher-fake/server/application_spec.rb +0 -604
  23. data/spec/lib/pusher-fake/server_spec.rb +0 -177
  24. data/spec/lib/pusher-fake/webhook_spec.rb +0 -67
  25. data/spec/lib/pusher_fake_spec.rb +0 -96
  26. data/spec/spec_helper.rb +0 -18
  27. data/spec/support/application/public/javascripts/vendor/polyfill.min.js +0 -1
  28. data/spec/support/application/public/javascripts/vendor/pusher-7.0.0.js +0 -4565
  29. data/spec/support/application/views/index.erb +0 -88
  30. data/spec/support/application.rb +0 -35
  31. data/spec/support/capybara.rb +0 -7
  32. data/spec/support/coveralls.rb +0 -9
  33. data/spec/support/helpers/connect.rb +0 -21
  34. data/spec/support/helpers/event.rb +0 -11
  35. data/spec/support/helpers/subscription.rb +0 -31
  36. data/spec/support/helpers/user.rb +0 -13
  37. data/spec/support/matchers/have_configuration_option.rb +0 -19
  38. data/spec/support/pusher_fake.rb +0 -21
  39. data/spec/support/webhooks.rb +0 -40
@@ -1,604 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- shared_examples_for "an API request" do
6
- subject { PusherFake::Server::Application }
7
-
8
- let(:hash) { double }
9
- let(:string) { double }
10
- let(:request) { instance_double(Rack::Request, path: path) }
11
- let(:response) { double }
12
- let(:environment) { double }
13
-
14
- before do
15
- allow(response).to receive(:finish).and_return(response)
16
-
17
- allow(MultiJson).to receive(:dump).and_return(string)
18
- allow(Rack::Request).to receive(:new).and_return(request)
19
- allow(Rack::Response).to receive(:new).and_return(response)
20
- end
21
-
22
- it "creates a request" do
23
- subject.call(environment)
24
-
25
- expect(Rack::Request).to have_received(:new).with(environment)
26
- end
27
-
28
- it "dumps the response hash to JSON" do
29
- subject.call(environment)
30
-
31
- expect(MultiJson).to have_received(:dump).with(hash)
32
- end
33
-
34
- it "creates a Rack response with the response JSON" do
35
- subject.call(environment)
36
-
37
- expect(Rack::Response).to have_received(:new).with(string)
38
- end
39
-
40
- it "finishes the response" do
41
- subject.call(environment)
42
-
43
- expect(response).to have_received(:finish).with(no_args)
44
- end
45
-
46
- it "returns the response" do
47
- result = subject.call(environment)
48
-
49
- expect(result).to eq(response)
50
- end
51
- end
52
-
53
- describe PusherFake::Server::Application, ".call, with a numeric ID" do
54
- it_behaves_like "an API request" do
55
- let(:id) { Time.now.to_i }
56
- let(:path) { "/apps/#{id}/events" }
57
-
58
- before do
59
- PusherFake.configuration.app_id = id
60
-
61
- allow(subject).to receive(:events).and_return(hash)
62
- end
63
-
64
- after do
65
- PusherFake.configuration.app_id = "PUSHER_APP_ID"
66
- end
67
- end
68
- end
69
-
70
- describe PusherFake::Server::Application, ".call, for triggering events" do
71
- it_behaves_like "an API request" do
72
- let(:id) { PusherFake.configuration.app_id }
73
- let(:path) { "/apps/#{id}/events" }
74
-
75
- before do
76
- allow(subject).to receive(:events).and_return(hash)
77
- end
78
-
79
- it "emits events" do
80
- subject.call(environment)
81
-
82
- expect(subject).to have_received(:events).with(request)
83
- end
84
- end
85
- end
86
-
87
- describe PusherFake::Server::Application,
88
- ".call, for triggering batch events" do
89
- it_behaves_like "an API request" do
90
- let(:id) { PusherFake.configuration.app_id }
91
- let(:path) { "/apps/#{id}/batch_events" }
92
-
93
- before do
94
- allow(subject).to receive(:batch_events).and_return(hash)
95
- end
96
-
97
- it "emits batch events" do
98
- subject.call(environment)
99
-
100
- expect(subject).to have_received(:batch_events).with(request)
101
- end
102
- end
103
- end
104
-
105
- describe PusherFake::Server::Application,
106
- ".call, retrieving occupied channels" do
107
- it_behaves_like "an API request" do
108
- let(:id) { PusherFake.configuration.app_id }
109
- let(:path) { "/apps/#{id}/channels" }
110
-
111
- before do
112
- allow(subject).to receive(:channels).and_return(hash)
113
- end
114
-
115
- it "filters the occupied channels" do
116
- subject.call(environment)
117
-
118
- expect(subject).to have_received(:channels).with(request)
119
- end
120
- end
121
- end
122
-
123
- describe PusherFake::Server::Application, ".call, with unknown path" do
124
- subject { described_class }
125
-
126
- let(:path) { "/apps/fake/events" }
127
- let(:request) { instance_double(Rack::Request, path: path) }
128
- let(:message) { "Unknown path: #{path}" }
129
- let(:response) { double }
130
- let(:environment) { double }
131
-
132
- before do
133
- allow(response).to receive(:finish).and_return(response)
134
-
135
- allow(Rack::Request).to receive(:new).and_return(request)
136
- allow(Rack::Response).to receive(:new).and_return(response)
137
- end
138
-
139
- it "creates a request" do
140
- subject.call(environment)
141
-
142
- expect(Rack::Request).to have_received(:new).with(environment)
143
- end
144
-
145
- it "creates a Rack response with the error message" do
146
- subject.call(environment)
147
-
148
- expect(Rack::Response).to have_received(:new).with(message, 400)
149
- end
150
-
151
- it "finishes the response" do
152
- subject.call(environment)
153
-
154
- expect(response).to have_received(:finish).with(no_args)
155
- end
156
-
157
- it "returns the response" do
158
- result = subject.call(environment)
159
-
160
- expect(result).to eq(response)
161
- end
162
- end
163
-
164
- describe PusherFake::Server::Application, ".call, raising an error" do
165
- subject { described_class }
166
-
167
- let(:id) { PusherFake.configuration.app_id }
168
- let(:path) { "/apps/#{id}/channels" }
169
- let(:message) { "Example error message." }
170
- let(:request) { instance_double(Rack::Request, path: path) }
171
- let(:response) { double }
172
- let(:environment) { double }
173
-
174
- before do
175
- allow(subject).to receive(:channels).and_raise(message)
176
-
177
- allow(response).to receive(:finish).and_return(response)
178
-
179
- allow(Rack::Request).to receive(:new).and_return(request)
180
- allow(Rack::Response).to receive(:new).and_return(response)
181
- end
182
-
183
- it "creates a request" do
184
- subject.call(environment)
185
-
186
- expect(Rack::Request).to have_received(:new).with(environment)
187
- end
188
-
189
- it "creates a Rack response with the error message" do
190
- subject.call(environment)
191
-
192
- expect(Rack::Response).to have_received(:new).with(message, 400)
193
- end
194
-
195
- it "finishes the response" do
196
- subject.call(environment)
197
-
198
- expect(response).to have_received(:finish).with(no_args)
199
- end
200
-
201
- it "returns the response" do
202
- result = subject.call(environment)
203
-
204
- expect(result).to eq(response)
205
- end
206
- end
207
-
208
- describe PusherFake::Server::Application, ".events" do
209
- subject { described_class }
210
-
211
- let(:body) { instance_double(StringIO, read: event_json) }
212
- let(:data) { { "example" => "data" } }
213
- let(:name) { "event-name" }
214
- let(:request) { instance_double(Rack::Request, body: body) }
215
- let(:channels) { %w(channel-1 channel-2) }
216
- let(:channel_1) { instance_double(PusherFake::Channel::Public, emit: true) }
217
- let(:channel_2) { instance_double(PusherFake::Channel::Public, emit: true) }
218
- let(:data_json) { data.to_json }
219
- let(:socket_id) { double }
220
- let(:event_json) { double }
221
-
222
- let(:event) do
223
- {
224
- "channels" => channels,
225
- "name" => name,
226
- "data" => data_json,
227
- "socket_id" => socket_id
228
- }
229
- end
230
-
231
- before do
232
- allow(MultiJson).to receive(:load).with(event_json).and_return(event)
233
- allow(MultiJson).to receive(:load).with(data_json).and_return(data)
234
- allow(PusherFake::Channel).to receive(:factory)
235
- .with(channels[0]).and_return(channel_1)
236
- allow(PusherFake::Channel).to receive(:factory)
237
- .with(channels[1]).and_return(channel_2)
238
- end
239
-
240
- it "parses the request body as JSON" do
241
- subject.events(request)
242
-
243
- expect(MultiJson).to have_received(:load).with(event_json)
244
- end
245
-
246
- it "parses the event data as JSON" do
247
- subject.events(request)
248
-
249
- expect(MultiJson).to have_received(:load).with(data_json)
250
- end
251
-
252
- it "handles invalid JSON for event data" do
253
- event["data"] = data = "fake"
254
-
255
- allow(MultiJson).to receive(:load)
256
- .with(data).and_raise(MultiJson::LoadError)
257
-
258
- expect { subject.events(request) }.not_to raise_error
259
- end
260
-
261
- it "creates channels by name" do
262
- subject.events(request)
263
-
264
- channels.each do |channel|
265
- expect(PusherFake::Channel).to have_received(:factory).with(channel)
266
- end
267
- end
268
-
269
- it "emits the event to the channels" do
270
- subject.events(request)
271
-
272
- expect(channel_1).to have_received(:emit)
273
- .with(name, data, socket_id: socket_id)
274
- expect(channel_2).to have_received(:emit)
275
- .with(name, data, socket_id: socket_id)
276
- end
277
- end
278
-
279
- describe PusherFake::Server::Application, ".batch_events" do
280
- subject { described_class }
281
-
282
- let(:body) { instance_double(StringIO, read: event_json) }
283
- let(:data) { { "example" => "data" } }
284
- let(:name) { "event-name" }
285
- let(:request) { instance_double(Rack::Request, body: body) }
286
- let(:channels) { %w(channel-1 channel-2) }
287
- let(:channel_1) { instance_double(PusherFake::Channel::Public, emit: true) }
288
- let(:channel_2) { instance_double(PusherFake::Channel::Public, emit: true) }
289
- let(:data_json) { data.to_json }
290
- let(:socket_id) { double }
291
- let(:event_json) { double }
292
-
293
- let(:batch) do
294
- {
295
- "batch" => [{
296
- "channels" => channels,
297
- "name" => name,
298
- "data" => data_json,
299
- "socket_id" => socket_id
300
- }]
301
- }
302
- end
303
-
304
- before do
305
- allow(MultiJson).to receive(:load).with(event_json).and_return(batch)
306
- allow(MultiJson).to receive(:load).with(data_json).and_return(data)
307
- allow(PusherFake::Channel).to receive(:factory)
308
- .with(channels[0]).and_return(channel_1)
309
- allow(PusherFake::Channel).to receive(:factory)
310
- .with(channels[1]).and_return(channel_2)
311
- end
312
-
313
- it "parses the request body as JSON" do
314
- subject.batch_events(request)
315
-
316
- expect(MultiJson).to have_received(:load).with(event_json)
317
- end
318
-
319
- it "parses the event data as JSON" do
320
- subject.batch_events(request)
321
-
322
- expect(MultiJson).to have_received(:load).with(data_json)
323
- end
324
-
325
- it "handles invalid JSON for event data" do
326
- batch["batch"].first["data"] = data = "fake"
327
-
328
- allow(MultiJson).to receive(:load)
329
- .with(data).and_raise(MultiJson::LoadError)
330
-
331
- expect { subject.batch_events(request) }.not_to raise_error
332
- end
333
-
334
- it "creates channels by name" do
335
- subject.batch_events(request)
336
-
337
- channels.each do |channel|
338
- expect(PusherFake::Channel).to have_received(:factory).with(channel)
339
- end
340
- end
341
-
342
- it "emits the event to the channels" do
343
- subject.batch_events(request)
344
-
345
- expect(channel_1).to have_received(:emit)
346
- .with(name, data, socket_id: socket_id)
347
- expect(channel_2).to have_received(:emit)
348
- .with(name, data, socket_id: socket_id)
349
- end
350
- end
351
-
352
- describe PusherFake::Server::Application, ".channels, requesting all" do
353
- subject { described_class }
354
-
355
- let(:request) { instance_double(Rack::Request, params: {}) }
356
- let(:channels) { { "channel-1" => double, "channel-2" => double } }
357
-
358
- before do
359
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
360
- end
361
-
362
- it "returns a hash of all the channels" do
363
- hash = subject.channels(request)
364
-
365
- expect(hash).to eq(channels: { "channel-1" => {}, "channel-2" => {} })
366
- end
367
- end
368
-
369
- describe PusherFake::Server::Application,
370
- ".channels, requesting channels with a filter" do
371
- subject { described_class }
372
-
373
- let(:params) { { "filter_by_prefix" => "public-" } }
374
- let(:request) { instance_double(Rack::Request, params: params) }
375
- let(:channels) { { "public-1" => double, "presence-1" => double } }
376
-
377
- before do
378
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
379
- end
380
-
381
- it "returns a hash of the channels matching the filter" do
382
- hash = subject.channels(request)
383
-
384
- expect(hash).to eq(channels: { "public-1" => {} })
385
- end
386
- end
387
-
388
- describe PusherFake::Server::Application,
389
- ".channels, requesting user count for channels with a filter" do
390
- subject { described_class }
391
-
392
- let(:request) { instance_double(Rack::Request, params: params) }
393
- let(:channels) { { "public-1" => double, "presence-1" => channel } }
394
-
395
- let(:channel) do
396
- instance_double(PusherFake::Channel::Presence,
397
- connections: [double, double])
398
- end
399
-
400
- let(:params) do
401
- { "filter_by_prefix" => "presence-", "info" => "user_count" }
402
- end
403
-
404
- before do
405
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
406
- end
407
-
408
- it "returns hash of channels matching the filter and includes user count" do
409
- hash = subject.channels(request)
410
-
411
- expect(hash).to eq(channels: { "presence-1" => { user_count: 2 } })
412
- end
413
- end
414
-
415
- describe PusherFake::Server::Application,
416
- ".channels, requesting all channels with no channels occupied" do
417
- subject { described_class }
418
-
419
- let(:request) { instance_double(Rack::Request, params: {}) }
420
-
421
- before do
422
- allow(PusherFake::Channel).to receive(:channels).and_return({})
423
- end
424
-
425
- it "returns a hash of no channels" do
426
- hash = subject.channels(request)
427
-
428
- expect(hash).to eq(channels: {})
429
- end
430
- end
431
-
432
- describe PusherFake::Server::Application,
433
- ".channels, requesting a user count on a non-presence channel" do
434
- subject { described_class }
435
-
436
- let(:params) { { "filter_by_prefix" => "public-", "info" => "user_count" } }
437
- let(:request) { instance_double(Rack::Request, params: params) }
438
-
439
- it "raises an error" do
440
- expect do
441
- subject.channels(request)
442
- end.to raise_error(subject::CHANNEL_FILTER_ERROR)
443
- end
444
- end
445
-
446
- describe PusherFake::Server::Application, ".channel, for an occupied channel" do
447
- subject { described_class }
448
-
449
- let(:name) { "public-1" }
450
- let(:request) { instance_double(Rack::Request, params: {}) }
451
- let(:channels) { { name => channel } }
452
-
453
- let(:channel) do
454
- instance_double(PusherFake::Channel::Presence, connections: [double])
455
- end
456
-
457
- before do
458
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
459
- end
460
-
461
- it "returns a hash with the occupied status" do
462
- hash = subject.channel(name, request)
463
-
464
- expect(hash).to eq(occupied: true)
465
- end
466
- end
467
-
468
- describe PusherFake::Server::Application, ".channel, for unoccupied channel" do
469
- subject { described_class }
470
-
471
- let(:name) { "public-1" }
472
- let(:request) { instance_double(Rack::Request, params: {}) }
473
- let(:channels) { { name => channel } }
474
-
475
- let(:channel) do
476
- instance_double(PusherFake::Channel::Presence, connections: [])
477
- end
478
-
479
- before do
480
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
481
- end
482
-
483
- it "returns a hash with the occupied status" do
484
- hash = subject.channel(name, request)
485
-
486
- expect(hash).to eq(occupied: false)
487
- end
488
- end
489
-
490
- describe PusherFake::Server::Application, ".channel, for an unknown channel" do
491
- subject { described_class }
492
-
493
- let(:request) { instance_double(Rack::Request, params: {}) }
494
- let(:channels) { {} }
495
-
496
- before do
497
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
498
- end
499
-
500
- it "returns a hash with the occupied status" do
501
- hash = subject.channel("fake", request)
502
-
503
- expect(hash).to eq(occupied: false)
504
- end
505
- end
506
-
507
- describe PusherFake::Server::Application,
508
- ".channel, request user count for a presence channel" do
509
- subject { described_class }
510
-
511
- let(:name) { "presence-1" }
512
- let(:params) { { "info" => "user_count" } }
513
- let(:request) { instance_double(Rack::Request, params: params) }
514
- let(:channels) { { name => channel } }
515
-
516
- let(:channel) do
517
- instance_double(PusherFake::Channel::Presence,
518
- connections: [double, double])
519
- end
520
-
521
- before do
522
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
523
- end
524
-
525
- it "returns a hash with the occupied status" do
526
- hash = subject.channel(name, request)
527
-
528
- expect(hash).to eq(occupied: true, user_count: 2)
529
- end
530
- end
531
-
532
- describe PusherFake::Server::Application,
533
- ".channel, requesting a user count on a non-presence channel" do
534
- subject { described_class }
535
-
536
- let(:params) { { "info" => "user_count" } }
537
- let(:request) { instance_double(Rack::Request, params: params) }
538
-
539
- it "raises an error" do
540
- expect do
541
- subject.channel("public-1", request)
542
- end.to raise_error(subject::CHANNEL_USER_COUNT_ERROR)
543
- end
544
- end
545
-
546
- describe PusherFake::Server::Application, ".users, for an occupied channel" do
547
- subject { described_class }
548
-
549
- let(:name) { "public-1" }
550
- let(:user_1) { instance_double(PusherFake::Connection, id: "1") }
551
- let(:user_2) { instance_double(PusherFake::Connection, id: "2") }
552
- let(:channels) { { name => channel } }
553
-
554
- let(:channel) do
555
- instance_double(PusherFake::Channel::Public, connections: [user_1, user_2])
556
- end
557
-
558
- before do
559
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
560
- end
561
-
562
- it "returns a hash with the occupied status" do
563
- hash = subject.users(name)
564
-
565
- expect(hash).to eq(users: [{ id: user_1.id }, { id: user_2.id }])
566
- end
567
- end
568
-
569
- describe PusherFake::Server::Application, ".users, for an empty channel" do
570
- subject { described_class }
571
-
572
- let(:name) { "public-1" }
573
- let(:channels) { { name => channel } }
574
-
575
- let(:channel) do
576
- instance_double(PusherFake::Channel::Public, connections: [])
577
- end
578
-
579
- before do
580
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
581
- end
582
-
583
- it "returns a hash with the occupied status" do
584
- hash = subject.users(name)
585
-
586
- expect(hash).to eq(users: [])
587
- end
588
- end
589
-
590
- describe PusherFake::Server::Application, ".users, for an unknown channel" do
591
- subject { described_class }
592
-
593
- let(:channels) { {} }
594
-
595
- before do
596
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
597
- end
598
-
599
- it "returns a hash with the occupied status" do
600
- hash = subject.users("fake")
601
-
602
- expect(hash).to eq(users: [])
603
- end
604
- end