Skip to content

Commit d9607f5

Browse files
committed
Add basic test for heartbeat channel
1 parent 012ffdb commit d9607f5

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

app/channels/application_cable/connection.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ def connect
1212
private
1313

1414
def find_verified_admin_user
15-
env["warden"].user(scope: :admin_user) || reject_unauthorized_connection
15+
env["warden"]&.user(scope: :admin_user) || reject_unauthorized_connection
1616
end
1717

18-
private
19-
2018
def report_error(e)
2119
Rails.logger.error(e)
2220
Honeybadger.notify(e)

app/channels/heartbeat_channel.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@ class HeartbeatChannel < ApplicationCable::Channel
22
def subscribed
33
stream_for current_admin_user
44
end
5-
6-
def unsubscribed
7-
end
85
end

spec/channels/connection_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require "rails_helper"
2+
3+
RSpec.describe ApplicationCable::Connection, type: :channel do
4+
it "successfully connects" do
5+
admin = instance_double(AdminUser, id: "323")
6+
connect "/cable", env: {"warden" => double(user: admin)}
7+
expect(connection.current_admin_user).to eq admin
8+
end
9+
10+
it "rejects connection" do
11+
expect { connect "/cable", env: {} }.to have_rejected_connection
12+
end
13+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "rails_helper"
2+
3+
RSpec.describe HeartbeatChannel, type: :channel do
4+
it "successfully subscribes" do
5+
stub_connection(current_admin_user: FactoryBot.create(:admin_user))
6+
7+
subscribe room_id: 42
8+
expect(subscription).to be_confirmed
9+
end
10+
end

spec/support/channels.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.configure do |config|
2+
config.include Warden::Test::Helpers, type: :channel
3+
end

spec/support/warden_helpers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def login_user(user = FactoryBot.create(:user))
1313
end
1414

1515
RSpec.configure do |config|
16+
config.include WardenHelpers, type: :channel
1617
config.include WardenHelpers, type: :request
1718
config.include WardenHelpers, type: :system
1819

0 commit comments

Comments
 (0)