File tree Expand file tree Collapse file tree
channels/application_cable
test/channels/application_cable Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11module ApplicationCable
22 class Channel < ActionCable ::Channel ::Base
3+ # All current channels are public (Turbo::StreamsChannel for display pages).
4+ # If an authenticated channel is added in the future, reject unauthorized
5+ # connections in that channel's #subscribed method:
6+ #
7+ # def subscribed
8+ # reject unless connection.current_shopkeeper
9+ # end
310 end
411end
Original file line number Diff line number Diff line change 11module ApplicationCable
22 class Connection < ActionCable ::Connection ::Base
3+ identified_by :current_shopkeeper , :current_account
4+
5+ def connect
6+ self . current_shopkeeper = find_shopkeeper
7+ self . current_account = find_account
8+ end
9+
10+ private
11+
12+ def find_shopkeeper
13+ env [ "warden" ] &.user ( :shopkeeper )
14+ end
15+
16+ # Display pages are public — anonymous connections are allowed.
17+ # Shopkeeper auth is header-based (devise_token_auth), so most
18+ # WebSocket connections will be anonymous. If an authenticated-only
19+ # channel is added in the future, reject in that channel's #subscribed.
20+ def find_account
21+ current_shopkeeper &.accounts &.order ( created_at : :asc ) &.first
22+ end
323 end
424end
Original file line number Diff line number Diff line change 1+ <%# These streams are public by design — display pages are unauthenticated %>
12<%= turbo_stream_from @shop, :tb_stream_full_reload_entire_page %>
23<%= turbo_stream_from @shop, :tb_stream_update_item_tags %>
34
Original file line number Diff line number Diff line change 11require "test_helper"
22
33class ApplicationCable ::ConnectionTest < ActionCable ::Connection ::TestCase
4- # test "connects with cookies" do
5- # cookies.signed[:user_id] = 42
6- #
7- # connect
8- #
9- # assert_equal connection.user_id, "42"
10- # end
4+ test "anonymous connection succeeds with nil shopkeeper and account" do
5+ connect
6+
7+ assert_nil connection . current_shopkeeper
8+ assert_nil connection . current_account
9+ end
10+
11+ test "authenticated connection identifies shopkeeper and account" do
12+ shopkeeper = shopkeepers ( :one )
13+ account = shopkeeper . create_default_account
14+ warden = Minitest ::Mock . new
15+ warden . expect ( :user , shopkeeper , [ :shopkeeper ] )
16+
17+ connect env : { "warden" => warden }
18+
19+ assert_equal shopkeeper , connection . current_shopkeeper
20+ assert_equal account , connection . current_account
21+ warden . verify
22+ end
1123end
You can’t perform that action at this time.
0 commit comments