aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test/channel/rejection_test.rb
blob: 683eafcac02c7320fcf7b6818013934feb4e1924 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

require "test_helper"
require "minitest/mock"
require "stubs/test_connection"
require "stubs/room"

class ActionCable::Channel::RejectionTest < ActionCable::TestCase
  class SecretChannel < ActionCable::Channel::Base
    def subscribed
      reject if params[:id] > 0
    end

    def secret_action
    end
  end

  setup do
    @user = User.new "lifo"
    @connection = TestConnection.new(@user)
  end

  test "subscription rejection" do
    subscriptions = Minitest::Mock.new
    subscriptions.expect(:remove_subscription, SecretChannel, [SecretChannel])

    @connection.stub(:subscriptions, subscriptions) do
      @channel = SecretChannel.new @connection, "{id: 1}", id: 1
      @channel.subscribe_to_channel

      expected = { "identifier" => "{id: 1}", "type" => "reject_subscription" }
      assert_equal expected, @connection.last_transmission
    end

    assert subscriptions.verify
  end

  test "does not execute action if subscription is rejected" do
    subscriptions = Minitest::Mock.new
    subscriptions.expect(:remove_subscription, SecretChannel, [SecretChannel])

    @connection.stub(:subscriptions, subscriptions) do
      @channel = SecretChannel.new @connection, "{id: 1}", id: 1
      @channel.subscribe_to_channel

      expected = { "identifier" => "{id: 1}", "type" => "reject_subscription" }
      assert_equal expected, @connection.last_transmission
      assert_equal 1, @connection.transmissions.size

      @channel.perform_action("action" => :secret_action)
      assert_equal 1, @connection.transmissions.size
    end

    assert subscriptions.verify
  end
end