2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
require "test_helper"
|
2018-05-27 14:50:04 -04:00
|
|
|
require "minitest/mock"
|
2016-08-06 13:15:15 -04:00
|
|
|
require "stubs/test_connection"
|
|
|
|
require "stubs/room"
|
2015-01-15 12:28:02 -05:00
|
|
|
|
2018-06-08 16:19:39 -04:00
|
|
|
class ActionCable::Channel::BaseTest < ActionCable::TestCase
|
2015-07-22 17:10:22 -04:00
|
|
|
class ActionCable::Channel::Base
|
|
|
|
def kick
|
|
|
|
@last_action = [ :kick ]
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class BasicChannel < ActionCable::Channel::Base
|
|
|
|
def chatters
|
|
|
|
@last_action = [ :chatters ]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ChatChannel < BasicChannel
|
2015-07-11 21:27:44 -04:00
|
|
|
attr_reader :room, :last_action
|
2015-08-24 13:35:59 -04:00
|
|
|
after_subscribe :toggle_subscribed
|
|
|
|
after_unsubscribe :toggle_subscribed
|
2015-07-11 21:27:44 -04:00
|
|
|
|
2018-12-12 08:42:34 -05:00
|
|
|
class SomeCustomError < StandardError; end
|
|
|
|
rescue_from SomeCustomError, with: :error_handler
|
|
|
|
|
2015-10-16 03:32:46 -04:00
|
|
|
def initialize(*)
|
|
|
|
@subscribed = false
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-07-11 21:27:44 -04:00
|
|
|
def subscribed
|
|
|
|
@room = Room.new params[:id]
|
|
|
|
@actions = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsubscribed
|
|
|
|
@room = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def toggle_subscribed
|
|
|
|
@subscribed = !@subscribed
|
|
|
|
end
|
|
|
|
|
|
|
|
def leave
|
|
|
|
@last_action = [ :leave ]
|
|
|
|
end
|
|
|
|
|
|
|
|
def speak(data)
|
|
|
|
@last_action = [ :speak, data ]
|
|
|
|
end
|
|
|
|
|
2015-07-22 17:10:22 -04:00
|
|
|
def topic(data)
|
|
|
|
@last_action = [ :topic, data ]
|
|
|
|
end
|
|
|
|
|
2015-07-11 21:27:44 -04:00
|
|
|
def subscribed?
|
|
|
|
@subscribed
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_latest
|
2019-09-06 14:21:45 -04:00
|
|
|
transmit({ data: "latest" })
|
2015-07-11 21:27:44 -04:00
|
|
|
end
|
|
|
|
|
2015-12-18 10:15:14 -05:00
|
|
|
def receive
|
2015-12-18 10:53:04 -05:00
|
|
|
@last_action = [ :receive ]
|
2015-12-18 10:15:14 -05:00
|
|
|
end
|
|
|
|
|
2018-12-12 08:42:34 -05:00
|
|
|
def error_action
|
|
|
|
raise SomeCustomError
|
|
|
|
end
|
|
|
|
|
2015-07-11 21:27:44 -04:00
|
|
|
private
|
|
|
|
def rm_rf
|
|
|
|
@last_action = [ :rm_rf ]
|
|
|
|
end
|
2018-12-12 08:42:34 -05:00
|
|
|
|
|
|
|
def error_handler
|
|
|
|
@last_action = [ :error_action ]
|
|
|
|
end
|
2015-07-11 21:27:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@user = User.new "lifo"
|
|
|
|
@connection = TestConnection.new(@user)
|
2016-08-06 13:44:11 -04:00
|
|
|
@channel = ChatChannel.new @connection, "{id: 1}", id: 1
|
2015-07-11 21:27:44 -04:00
|
|
|
end
|
|
|
|
|
2016-09-20 19:57:10 -04:00
|
|
|
test "should subscribe to a channel" do
|
|
|
|
@channel.subscribe_to_channel
|
2015-07-11 21:27:44 -04:00
|
|
|
assert_equal 1, @channel.room.id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "on subscribe callbacks" do
|
2016-09-20 19:57:10 -04:00
|
|
|
@channel.subscribe_to_channel
|
2015-07-11 21:27:44 -04:00
|
|
|
assert @channel.subscribed
|
|
|
|
end
|
|
|
|
|
|
|
|
test "channel params" do
|
|
|
|
assert_equal({ id: 1 }, @channel.params)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unsubscribing from a channel" do
|
2016-09-20 19:57:10 -04:00
|
|
|
@channel.subscribe_to_channel
|
|
|
|
|
2015-07-11 21:27:44 -04:00
|
|
|
assert @channel.room
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @channel, :subscribed?
|
2015-07-11 21:27:44 -04:00
|
|
|
|
|
|
|
@channel.unsubscribe_from_channel
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not @channel.room
|
|
|
|
assert_not_predicate @channel, :subscribed?
|
2015-07-11 21:27:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "connection identifiers" do
|
|
|
|
assert_equal @user.name, @channel.current_user.name
|
|
|
|
end
|
|
|
|
|
|
|
|
test "callable action without any argument" do
|
2016-08-06 13:15:15 -04:00
|
|
|
@channel.perform_action "action" => :leave
|
2015-07-11 21:27:44 -04:00
|
|
|
assert_equal [ :leave ], @channel.last_action
|
|
|
|
end
|
|
|
|
|
|
|
|
test "callable action with arguments" do
|
2016-08-06 13:15:15 -04:00
|
|
|
data = { "action" => :speak, "content" => "Hello World" }
|
2015-07-11 21:27:44 -04:00
|
|
|
|
|
|
|
@channel.perform_action data
|
|
|
|
assert_equal [ :speak, data ], @channel.last_action
|
|
|
|
end
|
|
|
|
|
2015-07-22 17:10:22 -04:00
|
|
|
test "should not dispatch a private method" do
|
2016-08-06 13:15:15 -04:00
|
|
|
@channel.perform_action "action" => :rm_rf
|
2015-07-11 21:27:44 -04:00
|
|
|
assert_nil @channel.last_action
|
|
|
|
end
|
|
|
|
|
2015-07-22 17:10:22 -04:00
|
|
|
test "should not dispatch a public method defined on Base" do
|
2016-08-06 13:15:15 -04:00
|
|
|
@channel.perform_action "action" => :kick
|
2015-07-22 17:10:22 -04:00
|
|
|
assert_nil @channel.last_action
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should dispatch a public method defined on Base and redefined on channel" do
|
2016-08-06 13:15:15 -04:00
|
|
|
data = { "action" => :topic, "content" => "This is Sparta!" }
|
2015-07-22 17:10:22 -04:00
|
|
|
|
|
|
|
@channel.perform_action data
|
|
|
|
assert_equal [ :topic, data ], @channel.last_action
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should dispatch calling a public method defined in an ancestor" do
|
2016-08-06 13:15:15 -04:00
|
|
|
@channel.perform_action "action" => :chatters
|
2015-07-22 17:10:22 -04:00
|
|
|
assert_equal [ :chatters ], @channel.last_action
|
|
|
|
end
|
|
|
|
|
2015-12-18 11:09:16 -05:00
|
|
|
test "should dispatch receive action when perform_action is called with empty action" do
|
2016-08-06 13:15:15 -04:00
|
|
|
data = { "content" => "hello" }
|
2015-12-18 10:15:14 -05:00
|
|
|
@channel.perform_action data
|
2015-12-18 10:53:04 -05:00
|
|
|
assert_equal [ :receive ], @channel.last_action
|
2015-12-18 10:15:14 -05:00
|
|
|
end
|
|
|
|
|
2015-07-11 21:27:44 -04:00
|
|
|
test "transmitting data" do
|
2016-08-06 13:15:15 -04:00
|
|
|
@channel.perform_action "action" => :get_latest
|
2015-07-11 21:27:44 -04:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
expected = { "identifier" => "{id: 1}", "message" => { "data" => "latest" } }
|
2015-07-11 21:27:44 -04:00
|
|
|
assert_equal expected, @connection.last_transmission
|
|
|
|
end
|
2015-10-16 22:05:33 -04:00
|
|
|
|
2016-09-19 05:29:23 -04:00
|
|
|
test "do not send subscription confirmation on initialize" do
|
|
|
|
assert_nil @connection.last_transmission
|
|
|
|
end
|
|
|
|
|
2016-09-20 19:57:10 -04:00
|
|
|
test "subscription confirmation on subscribe_to_channel" do
|
2016-03-11 18:32:02 -05:00
|
|
|
expected = { "identifier" => "{id: 1}", "type" => "confirm_subscription" }
|
2016-09-20 19:57:10 -04:00
|
|
|
@channel.subscribe_to_channel
|
2015-10-16 22:05:33 -04:00
|
|
|
assert_equal expected, @connection.last_transmission
|
|
|
|
end
|
|
|
|
|
2015-12-18 09:32:45 -05:00
|
|
|
test "actions available on Channel" do
|
2018-12-12 08:42:34 -05:00
|
|
|
available_actions = %w(room last_action subscribed unsubscribed toggle_subscribed leave speak subscribed? get_latest receive chatters topic error_action).to_set
|
2015-12-18 09:32:45 -05:00
|
|
|
assert_equal available_actions, ChatChannel.action_methods
|
|
|
|
end
|
2015-12-18 10:39:38 -05:00
|
|
|
|
|
|
|
test "invalid action on Channel" do
|
|
|
|
assert_logged("Unable to process ActionCable::Channel::BaseTest::ChatChannel#invalid_action") do
|
2016-08-06 13:15:15 -04:00
|
|
|
@channel.perform_action "action" => :invalid_action
|
2015-12-18 10:39:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-16 14:06:35 -05:00
|
|
|
test "notification for perform_action" do
|
2018-12-20 12:44:01 -05:00
|
|
|
events = []
|
|
|
|
ActiveSupport::Notifications.subscribe "perform_action.action_cable" do |*args|
|
|
|
|
events << ActiveSupport::Notifications::Event.new(*args)
|
|
|
|
end
|
2016-02-16 14:06:35 -05:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
data = { "action" => :speak, "content" => "hello" }
|
|
|
|
@channel.perform_action data
|
2016-02-16 14:06:35 -05:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
assert_equal 1, events.length
|
|
|
|
assert_equal "perform_action.action_cable", events[0].name
|
|
|
|
assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
|
|
|
|
assert_equal :speak, events[0].payload[:action]
|
|
|
|
assert_equal data, events[0].payload[:data]
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Notifications.unsubscribe "perform_action.action_cable"
|
2016-02-16 14:06:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "notification for transmit" do
|
2018-12-20 12:44:01 -05:00
|
|
|
events = []
|
|
|
|
ActiveSupport::Notifications.subscribe "transmit.action_cable" do |*args|
|
|
|
|
events << ActiveSupport::Notifications::Event.new(*args)
|
2016-02-16 14:06:35 -05:00
|
|
|
end
|
2018-12-20 12:44:01 -05:00
|
|
|
|
|
|
|
@channel.perform_action "action" => :get_latest
|
|
|
|
expected_data = { data: "latest" }
|
|
|
|
|
|
|
|
assert_equal 1, events.length
|
|
|
|
assert_equal "transmit.action_cable", events[0].name
|
|
|
|
assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
|
|
|
|
assert_equal expected_data, events[0].payload[:data]
|
|
|
|
assert_nil events[0].payload[:via]
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Notifications.unsubscribe "transmit.action_cable"
|
2016-02-16 14:06:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "notification for transmit_subscription_confirmation" do
|
2018-12-20 12:44:01 -05:00
|
|
|
@channel.subscribe_to_channel
|
2016-09-19 05:29:23 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
events = []
|
|
|
|
ActiveSupport::Notifications.subscribe "transmit_subscription_confirmation.action_cable" do |*args|
|
|
|
|
events << ActiveSupport::Notifications::Event.new(*args)
|
|
|
|
end
|
2016-02-16 14:06:35 -05:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
@channel.stub(:subscription_confirmation_sent?, false) do
|
|
|
|
@channel.send(:transmit_subscription_confirmation)
|
2016-02-16 14:06:35 -05:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
assert_equal 1, events.length
|
|
|
|
assert_equal "transmit_subscription_confirmation.action_cable", events[0].name
|
|
|
|
assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
|
2016-02-16 14:06:35 -05:00
|
|
|
end
|
2018-12-20 12:44:01 -05:00
|
|
|
ensure
|
|
|
|
ActiveSupport::Notifications.unsubscribe "transmit_subscription_confirmation.action_cable"
|
2016-02-16 14:06:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "notification for transmit_subscription_rejection" do
|
2018-12-20 12:44:01 -05:00
|
|
|
events = []
|
|
|
|
ActiveSupport::Notifications.subscribe "transmit_subscription_rejection.action_cable" do |*args|
|
|
|
|
events << ActiveSupport::Notifications::Event.new(*args)
|
|
|
|
end
|
2016-02-16 14:06:35 -05:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
@channel.send(:transmit_subscription_rejection)
|
2016-02-16 14:06:35 -05:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
assert_equal 1, events.length
|
|
|
|
assert_equal "transmit_subscription_rejection.action_cable", events[0].name
|
|
|
|
assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Notifications.unsubscribe "transmit_subscription_rejection.action_cable"
|
2016-02-16 14:06:35 -05:00
|
|
|
end
|
|
|
|
|
2018-12-12 08:42:34 -05:00
|
|
|
test "behaves like rescuable" do
|
|
|
|
@channel.perform_action "action" => :error_action
|
|
|
|
assert_equal [ :error_action ], @channel.last_action
|
|
|
|
end
|
|
|
|
|
2015-12-18 17:44:58 -05:00
|
|
|
private
|
|
|
|
def assert_logged(message)
|
|
|
|
old_logger = @connection.logger
|
|
|
|
log = StringIO.new
|
|
|
|
@connection.instance_variable_set(:@logger, Logger.new(log))
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
|
|
|
|
log.rewind
|
|
|
|
assert_match message, log.read
|
|
|
|
ensure
|
|
|
|
@connection.instance_variable_set(:@logger, old_logger)
|
|
|
|
end
|
2015-12-18 10:39:38 -05:00
|
|
|
end
|
2015-07-11 21:27:44 -04:00
|
|
|
end
|