pusher-fake 1.6.0 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pusher-fake.rb +4 -1
- data/lib/pusher-fake/channel.rb +9 -3
- data/lib/pusher-fake/channel/presence.rb +11 -9
- data/lib/pusher-fake/channel/private.rb +8 -5
- data/lib/pusher-fake/channel/public.rb +13 -14
- data/lib/pusher-fake/configuration.rb +2 -1
- data/lib/pusher-fake/connection.rb +41 -29
- data/lib/pusher-fake/cucumber.rb +2 -1
- data/lib/pusher-fake/server.rb +46 -45
- data/lib/pusher-fake/server/application.rb +89 -50
- data/lib/pusher-fake/support/base.rb +4 -9
- data/lib/pusher-fake/webhook.rb +3 -1
- data/spec/features/api/channels_spec.rb +7 -5
- data/spec/features/api/users_spec.rb +1 -1
- data/spec/features/client/event_spec.rb +6 -4
- data/spec/features/client/subscribe_spec.rb +8 -6
- data/spec/features/server/event_spec.rb +7 -7
- data/spec/features/server/webhooks_spec.rb +16 -4
- data/spec/lib/pusher-fake/channel/presence_spec.rb +57 -49
- data/spec/lib/pusher-fake/channel/private_spec.rb +42 -31
- data/spec/lib/pusher-fake/channel/public_spec.rb +37 -27
- data/spec/lib/pusher-fake/channel_spec.rb +51 -91
- data/spec/lib/pusher-fake/configuration_spec.rb +11 -5
- data/spec/lib/pusher-fake/connection_spec.rb +65 -39
- data/spec/lib/pusher-fake/server/application_spec.rb +219 -94
- data/spec/lib/pusher-fake/server_spec.rb +31 -41
- data/spec/lib/pusher-fake/webhook_spec.rb +29 -18
- data/spec/lib/pusher_fake_spec.rb +17 -15
- data/spec/support/application.rb +21 -19
- data/spec/support/application/public/javascripts/vendor/{pusher-3.1.0.js → pusher-3.2.1.js} +69 -48
- data/spec/support/application/views/index.erb +1 -1
- data/spec/support/capybara.rb +1 -3
- data/spec/support/helpers/connect.rb +1 -3
- data/spec/support/matchers/have_configuration_option.rb +2 -2
- data/spec/support/{pusher-fake.rb → pusher_fake.rb} +2 -1
- data/spec/support/webhooks.rb +5 -3
- metadata +38 -10
@@ -1,13 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
end
|
4
|
-
|
5
|
-
if Pusher.key.nil?
|
6
|
-
warn("Warning: `Pusher.key` is not set. Should be set before including PusherFake.")
|
7
|
-
end
|
1
|
+
%w(app_id key secret).each do |setting|
|
2
|
+
next unless Pusher.public_send(setting).nil?
|
8
3
|
|
9
|
-
|
10
|
-
|
4
|
+
warn("Warning: Pusher.#{setting} is not set." \
|
5
|
+
"Should be set before including PusherFake")
|
11
6
|
end
|
12
7
|
|
13
8
|
# Use the same API key and secret as the live version.
|
data/lib/pusher-fake/webhook.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
module PusherFake
|
2
|
+
# Webhook triggering.
|
2
3
|
class Webhook
|
3
4
|
class << self
|
4
5
|
def trigger(name, data = {})
|
@@ -17,7 +18,8 @@ module PusherFake
|
|
17
18
|
private
|
18
19
|
|
19
20
|
def headers_for(payload)
|
20
|
-
{
|
21
|
+
{
|
22
|
+
"Content-Type" => "application/json",
|
21
23
|
"X-Pusher-Key" => PusherFake.configuration.key,
|
22
24
|
"X-Pusher-Signature" => signature_for(payload)
|
23
25
|
}
|
@@ -39,10 +39,10 @@ feature "Requesting channel API endpoint" do
|
|
39
39
|
expect(result[presence_name]["user_count"]).to eq(1)
|
40
40
|
end
|
41
41
|
|
42
|
-
|
43
|
-
expect
|
42
|
+
scenario "all channels, with invalid info attributes" do
|
43
|
+
expect do
|
44
44
|
channels(info: "user_count")
|
45
|
-
|
45
|
+
end.to raise_error(/user_count may only be requested for presence channels/)
|
46
46
|
end
|
47
47
|
|
48
48
|
scenario "channel, with no occupants" do
|
@@ -65,9 +65,11 @@ feature "Requesting channel API endpoint" do
|
|
65
65
|
end
|
66
66
|
|
67
67
|
scenario "channel, with invalid info attributes" do
|
68
|
-
expect
|
68
|
+
expect do
|
69
69
|
channel(info: "user_count")
|
70
|
-
|
70
|
+
end.to raise_error(
|
71
|
+
/Cannot retrieve the user count unless the channel is a presence channel/
|
72
|
+
)
|
71
73
|
end
|
72
74
|
|
73
75
|
protected
|
@@ -17,7 +17,7 @@ feature "Client triggers a client event" do
|
|
17
17
|
|
18
18
|
trigger(private_channel, event)
|
19
19
|
|
20
|
-
expect(page).
|
20
|
+
expect(page).not_to have_event(event, on: private_channel)
|
21
21
|
|
22
22
|
using_session(other_user) do
|
23
23
|
expect(page).to have_event(event, on: private_channel)
|
@@ -30,16 +30,18 @@ feature "Client triggers a client event" do
|
|
30
30
|
|
31
31
|
trigger(public_channel, event)
|
32
32
|
|
33
|
-
expect(page).
|
33
|
+
expect(page).not_to have_event(event, on: public_channel)
|
34
34
|
|
35
35
|
using_session(other_user) do
|
36
|
-
expect(page).
|
36
|
+
expect(page).not_to have_event(event, on: public_channel)
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
protected
|
41
41
|
|
42
42
|
def trigger(channel, event)
|
43
|
-
page.execute_script(
|
43
|
+
page.execute_script(
|
44
|
+
"Helpers.trigger(#{MultiJson.dump(channel)}, #{MultiJson.dump(event)})"
|
45
|
+
)
|
44
46
|
end
|
45
47
|
end
|
@@ -34,19 +34,19 @@ feature "Client subscribing to a channel" do
|
|
34
34
|
end
|
35
35
|
|
36
36
|
scenario "unsuccessfully subscribes to a private channel" do
|
37
|
-
|
37
|
+
override_socket_id("13.37")
|
38
38
|
|
39
39
|
attempt_to_subscribe_to("private-message-bob")
|
40
40
|
|
41
|
-
expect(page).
|
41
|
+
expect(page).not_to have_content("Subscribed to private-message-bob.")
|
42
42
|
end
|
43
43
|
|
44
44
|
scenario "unsuccessfully subscribes to a presence channel" do
|
45
|
-
|
45
|
+
override_socket_id("13.37")
|
46
46
|
|
47
47
|
attempt_to_subscribe_to("presence-game-1")
|
48
48
|
|
49
|
-
expect(page).
|
49
|
+
expect(page).not_to have_content("Subscribed to presence-game-1.")
|
50
50
|
end
|
51
51
|
|
52
52
|
protected
|
@@ -55,7 +55,9 @@ feature "Client subscribing to a channel" do
|
|
55
55
|
page.execute_script("Helpers.subscribe(#{MultiJson.dump(channel)})")
|
56
56
|
end
|
57
57
|
|
58
|
-
def
|
59
|
-
page.execute_script(
|
58
|
+
def override_socket_id(value)
|
59
|
+
page.execute_script(
|
60
|
+
"Pusher.instance.connection.socket_id = #{MultiJson.dump(value)};"
|
61
|
+
)
|
60
62
|
end
|
61
63
|
end
|
@@ -31,7 +31,7 @@ feature "Server triggers event" do
|
|
31
31
|
|
32
32
|
trigger(public_channel, event)
|
33
33
|
|
34
|
-
expect(page).
|
34
|
+
expect(page).not_to have_event(event, on: public_channel)
|
35
35
|
|
36
36
|
using_session(other_user) do
|
37
37
|
expect(page).to have_event(event, on: public_channel)
|
@@ -41,10 +41,10 @@ feature "Server triggers event" do
|
|
41
41
|
scenario "on an unsubscribed public channel" do
|
42
42
|
trigger(public_channel, event)
|
43
43
|
|
44
|
-
expect(page).
|
44
|
+
expect(page).not_to have_event(event, on: public_channel)
|
45
45
|
|
46
46
|
using_session(other_user) do
|
47
|
-
expect(page).
|
47
|
+
expect(page).not_to have_event(event, on: public_channel)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -68,7 +68,7 @@ feature "Server triggers event" do
|
|
68
68
|
|
69
69
|
trigger(private_channel, event)
|
70
70
|
|
71
|
-
expect(page).
|
71
|
+
expect(page).not_to have_event(event, on: private_channel)
|
72
72
|
|
73
73
|
using_session(other_user) do
|
74
74
|
expect(page).to have_event(event, on: private_channel)
|
@@ -78,10 +78,10 @@ feature "Server triggers event" do
|
|
78
78
|
scenario "on an unsubscribed private channel" do
|
79
79
|
trigger(private_channel, event)
|
80
80
|
|
81
|
-
expect(page).
|
81
|
+
expect(page).not_to have_event(event, on: private_channel)
|
82
82
|
|
83
83
|
using_session(other_user) do
|
84
|
-
expect(page).
|
84
|
+
expect(page).not_to have_event(event, on: private_channel)
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
@@ -108,7 +108,7 @@ feature "Server triggers event" do
|
|
108
108
|
expect(page).to have_event(event, on: public_channel)
|
109
109
|
|
110
110
|
using_session(other_user) do
|
111
|
-
expect(page).
|
111
|
+
expect(page).not_to have_event(event, on: public_channel)
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# rubocop:disable Style/GlobalVars
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
|
3
5
|
feature "Receiving event webhooks" do
|
@@ -36,11 +38,17 @@ feature "Receiving event webhooks" do
|
|
36
38
|
scenario "subscribing to a presence channel" do
|
37
39
|
subscribe_to(presence_channel)
|
38
40
|
|
39
|
-
expect($events).to include_event(
|
41
|
+
expect($events).to include_event(
|
42
|
+
"member_added",
|
43
|
+
"channel" => presence_channel, "user_id" => user_id
|
44
|
+
)
|
40
45
|
|
41
46
|
subscribe_to_as(presence_channel, other_user)
|
42
47
|
|
43
|
-
expect($events).to include_event(
|
48
|
+
expect($events).to include_event(
|
49
|
+
"member_added",
|
50
|
+
"channel" => presence_channel, "user_id" => user_id(other_user)
|
51
|
+
)
|
44
52
|
end
|
45
53
|
|
46
54
|
scenario "unsubscribing from a presence channel" do
|
@@ -49,11 +57,15 @@ feature "Receiving event webhooks" do
|
|
49
57
|
|
50
58
|
unsubscribe_from(presence_channel)
|
51
59
|
|
52
|
-
expect($events).to include_event("member_added",
|
60
|
+
expect($events).to include_event("member_added",
|
61
|
+
"channel" => presence_channel,
|
62
|
+
"user_id" => user_id)
|
53
63
|
|
54
64
|
unsubscribe_from_as(presence_channel, other_user)
|
55
65
|
|
56
|
-
expect($events).to include_event("member_added",
|
66
|
+
expect($events).to include_event("member_added",
|
67
|
+
"channel" => presence_channel,
|
68
|
+
"user_id" => user_id(other_user))
|
57
69
|
end
|
58
70
|
|
59
71
|
protected
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe PusherFake::Channel::Presence do
|
4
|
-
|
4
|
+
subject { described_class }
|
5
5
|
|
6
|
-
|
6
|
+
let(:name) { "channel" }
|
7
7
|
|
8
8
|
it "inherits from private channel" do
|
9
9
|
expect(subject.ancestors).to include(PusherFake::Channel::Private)
|
@@ -17,16 +17,20 @@ describe PusherFake::Channel::Presence do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
describe PusherFake::Channel::Presence, "#add" do
|
20
|
-
|
20
|
+
subject { described_class.new(name) }
|
21
|
+
|
21
22
|
let(:name) { "name" }
|
22
23
|
let(:user_id) { "1234" }
|
23
|
-
let(:connection) {
|
24
|
-
let(:connections) {
|
24
|
+
let(:connection) { instance_double(PusherFake::Connection, emit: nil) }
|
25
|
+
let(:connections) { instance_double(Array, push: nil, length: 0) }
|
25
26
|
let(:channel_data) { { user_id: user_id } }
|
26
27
|
let(:authentication) { "auth" }
|
27
28
|
let(:subscription_data) { { presence: { hash: {}, count: 1 } } }
|
28
29
|
|
29
|
-
|
30
|
+
let(:data) do
|
31
|
+
{ auth: authentication,
|
32
|
+
channel_data: MultiJson.dump(channel_data) }
|
33
|
+
end
|
30
34
|
|
31
35
|
before do
|
32
36
|
allow(PusherFake::Webhook).to receive(:trigger)
|
@@ -49,10 +53,11 @@ describe PusherFake::Channel::Presence, "#add" do
|
|
49
53
|
|
50
54
|
subject.add(connection, data)
|
51
55
|
|
52
|
-
expect(MultiJson).to have_received(:load)
|
56
|
+
expect(MultiJson).to have_received(:load)
|
57
|
+
.with(data[:channel_data], symbolize_keys: true)
|
53
58
|
end
|
54
59
|
|
55
|
-
it "assigns the
|
60
|
+
it "assigns the channel_data to the members hash for current connection" do
|
56
61
|
allow(subject).to receive(:authorized?).and_return(true)
|
57
62
|
|
58
63
|
subject.add(connection, data)
|
@@ -65,7 +70,8 @@ describe PusherFake::Channel::Presence, "#add" do
|
|
65
70
|
|
66
71
|
subject.add(connection, data)
|
67
72
|
|
68
|
-
expect(subject).to have_received(:emit)
|
73
|
+
expect(subject).to have_received(:emit)
|
74
|
+
.with("pusher_internal:member_added", channel_data)
|
69
75
|
end
|
70
76
|
|
71
77
|
it "successfully subscribes the connection when authorized" do
|
@@ -74,7 +80,8 @@ describe PusherFake::Channel::Presence, "#add" do
|
|
74
80
|
subject.add(connection, data)
|
75
81
|
|
76
82
|
expect(connection).to have_received(:emit)
|
77
|
-
.with("pusher_internal:subscription_succeeded",
|
83
|
+
.with("pusher_internal:subscription_succeeded",
|
84
|
+
subscription_data, subject.name)
|
78
85
|
end
|
79
86
|
|
80
87
|
it "adds the connection to the channel when authorized" do
|
@@ -85,13 +92,14 @@ describe PusherFake::Channel::Presence, "#add" do
|
|
85
92
|
expect(connections).to have_received(:push).with(connection)
|
86
93
|
end
|
87
94
|
|
88
|
-
it "triggers
|
95
|
+
it "triggers occupied webhook for first connection added when authorized" do
|
89
96
|
allow(subject).to receive(:authorized?).and_return(true)
|
90
97
|
allow(subject).to receive(:connections).and_call_original
|
91
98
|
|
92
99
|
2.times { subject.add(connection, data) }
|
93
100
|
|
94
|
-
expect(PusherFake::Webhook).to have_received(:trigger)
|
101
|
+
expect(PusherFake::Webhook).to have_received(:trigger)
|
102
|
+
.with("channel_occupied", channel: name).once
|
95
103
|
end
|
96
104
|
|
97
105
|
it "triggers the member added webhook when authorized" do
|
@@ -99,7 +107,8 @@ describe PusherFake::Channel::Presence, "#add" do
|
|
99
107
|
|
100
108
|
subject.add(connection, data)
|
101
109
|
|
102
|
-
expect(PusherFake::Webhook).to have_received(:trigger)
|
110
|
+
expect(PusherFake::Webhook).to have_received(:trigger)
|
111
|
+
.with("member_added", channel: name, user_id: user_id).once
|
103
112
|
end
|
104
113
|
|
105
114
|
it "unsuccessfully subscribes the connection when not authorized" do
|
@@ -107,7 +116,8 @@ describe PusherFake::Channel::Presence, "#add" do
|
|
107
116
|
|
108
117
|
subject.add(connection, data)
|
109
118
|
|
110
|
-
expect(connection).to have_received(:emit)
|
119
|
+
expect(connection).to have_received(:emit)
|
120
|
+
.with("pusher_internal:subscription_error", {}, subject.name)
|
111
121
|
end
|
112
122
|
|
113
123
|
it "does not trigger channel occupied webhook when not authorized" do
|
@@ -116,7 +126,7 @@ describe PusherFake::Channel::Presence, "#add" do
|
|
116
126
|
|
117
127
|
2.times { subject.add(connection, data) }
|
118
128
|
|
119
|
-
expect(PusherFake::Webhook).
|
129
|
+
expect(PusherFake::Webhook).not_to have_received(:trigger)
|
120
130
|
end
|
121
131
|
|
122
132
|
it "does not trigger the member added webhook when not authorized" do
|
@@ -124,18 +134,18 @@ describe PusherFake::Channel::Presence, "#add" do
|
|
124
134
|
|
125
135
|
subject.add(connection, data)
|
126
136
|
|
127
|
-
expect(PusherFake::Webhook).
|
137
|
+
expect(PusherFake::Webhook).not_to have_received(:trigger)
|
128
138
|
end
|
129
139
|
end
|
130
140
|
|
131
141
|
describe PusherFake::Channel::Presence, "#remove" do
|
142
|
+
subject { described_class.new(name) }
|
143
|
+
|
132
144
|
let(:name) { "name" }
|
133
145
|
let(:user_id) { "1234" }
|
134
146
|
let(:connection) { double }
|
135
147
|
let(:channel_data) { { user_id: user_id } }
|
136
148
|
|
137
|
-
subject { PusherFake::Channel::Presence.new(name) }
|
138
|
-
|
139
149
|
before do
|
140
150
|
allow(PusherFake::Webhook).to receive(:trigger)
|
141
151
|
allow(subject).to receive(:connections).and_return([connection])
|
@@ -153,30 +163,33 @@ describe PusherFake::Channel::Presence, "#remove" do
|
|
153
163
|
it "removes the connection from the members hash" do
|
154
164
|
subject.remove(connection)
|
155
165
|
|
156
|
-
expect(subject.members).
|
166
|
+
expect(subject.members).not_to have_key(connection)
|
157
167
|
end
|
158
168
|
|
159
169
|
it "triggers the member removed webhook" do
|
160
170
|
subject.remove(connection)
|
161
171
|
|
162
|
-
expect(PusherFake::Webhook).to have_received(:trigger)
|
172
|
+
expect(PusherFake::Webhook).to have_received(:trigger)
|
173
|
+
.with("member_removed", channel: name, user_id: user_id).once
|
163
174
|
end
|
164
175
|
|
165
176
|
it "notifies the channel of the removed member" do
|
166
177
|
subject.remove(connection)
|
167
178
|
|
168
|
-
expect(subject).to have_received(:emit)
|
179
|
+
expect(subject).to have_received(:emit)
|
180
|
+
.with("pusher_internal:member_removed", channel_data)
|
169
181
|
end
|
170
182
|
end
|
171
183
|
|
172
|
-
describe PusherFake::Channel::Presence,
|
184
|
+
describe PusherFake::Channel::Presence,
|
185
|
+
"#remove, for an unsubscribed connection" do
|
186
|
+
subject { described_class.new(name) }
|
187
|
+
|
173
188
|
let(:name) { "name" }
|
174
189
|
let(:user_id) { "1234" }
|
175
190
|
let(:connection) { double }
|
176
191
|
let(:channel_data) { { user_id: user_id } }
|
177
192
|
|
178
|
-
subject { PusherFake::Channel::Presence.new(name) }
|
179
|
-
|
180
193
|
before do
|
181
194
|
allow(subject).to receive(:connections).and_return([])
|
182
195
|
allow(subject).to receive(:emit).and_return(nil)
|
@@ -184,56 +197,51 @@ describe PusherFake::Channel::Presence, "#remove, for an unsubscribed connection
|
|
184
197
|
end
|
185
198
|
|
186
199
|
it "does not raise an error" do
|
187
|
-
expect
|
200
|
+
expect do
|
188
201
|
subject.remove(connection)
|
189
|
-
|
202
|
+
end.not_to raise_error
|
190
203
|
end
|
191
204
|
|
192
205
|
it "does not trigger an event" do
|
193
206
|
subject.remove(connection)
|
194
207
|
|
195
|
-
expect(subject).
|
208
|
+
expect(subject).not_to have_received(:trigger)
|
209
|
+
.with("member_removed", channel: name, user_id: user_id)
|
196
210
|
end
|
197
211
|
|
198
212
|
it "does not emit an event" do
|
199
213
|
subject.remove(connection)
|
200
214
|
|
201
|
-
expect(subject).
|
215
|
+
expect(subject).not_to have_received(:emit)
|
216
|
+
.with("pusher_internal:member_removed", channel_data)
|
202
217
|
end
|
203
218
|
end
|
204
219
|
|
205
220
|
describe PusherFake::Channel::Presence, "#subscription_data" do
|
206
|
-
|
207
|
-
let(:member) { { user_id: 1, name: "Bob" } }
|
208
|
-
let(:members) { { double => member } }
|
221
|
+
subject { described_class.new("name") }
|
209
222
|
|
210
|
-
|
223
|
+
let(:one) { { user_id: 1, name: "Bob" } }
|
224
|
+
let(:two) { { user_id: 2, name: "Beau" } }
|
225
|
+
let(:data) { subject.subscription_data }
|
226
|
+
let(:members) { { double => one } }
|
211
227
|
|
212
228
|
before do
|
213
229
|
allow(subject).to receive(:members).and_return(members)
|
214
230
|
end
|
215
231
|
|
216
232
|
it "returns hash with presence information" do
|
217
|
-
data
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
hash: { member[:user_id] => member[:user_info] },
|
222
|
-
count: 1
|
223
|
-
}
|
224
|
-
})
|
233
|
+
expect(data).to eq(presence: {
|
234
|
+
hash: { one[:user_id] => one[:user_info] },
|
235
|
+
count: 1
|
236
|
+
})
|
225
237
|
end
|
226
238
|
|
227
239
|
it "handles multiple members" do
|
228
|
-
members[double] =
|
229
|
-
|
230
|
-
data = subject.subscription_data
|
240
|
+
members[double] = two
|
231
241
|
|
232
|
-
expect(data).to eq(
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
}
|
237
|
-
})
|
242
|
+
expect(data[:presence][:count]).to eq(2)
|
243
|
+
expect(data[:presence][:hash]).to eq(
|
244
|
+
one[:user_id] => one[:user_info], two[:user_id] => two[:user_info]
|
245
|
+
)
|
238
246
|
end
|
239
247
|
end
|