File tree Expand file tree Collapse file tree
app/channels/application_cable
test/channels/application_cable Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,12 +13,13 @@ def find_shopkeeper
1313 env [ "warden" ] &.user ( :shopkeeper )
1414 end
1515
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 .
16+ # Extract the account UUID from the WebSocket upgrade request path,
17+ # matching how AccountMiddleware sets the current account for HTTP
18+ # requests. Display page URLs (display/shops/...) don't include an
19+ # account UUID, so this returns nil for public connections .
2020 def find_account
21- current_shopkeeper &.accounts &.order ( created_at : :asc ) &.first
21+ _ , account_id , = request . path . split ( "/" , 3 )
22+ Account . find_by ( id : account_id ) if AccountMiddleware ::UUID_MATCHER . match? ( account_id )
2223 end
2324 end
2425end
Original file line number Diff line number Diff line change @@ -14,10 +14,22 @@ class ApplicationCable::ConnectionTest < ActionCable::Connection::TestCase
1414 warden = Minitest ::Mock . new
1515 warden . expect ( :user , shopkeeper , [ :shopkeeper ] )
1616
17- connect env : { "warden" => warden }
17+ connect "/ #{ account . id } /cable" , env : { "warden" => warden }
1818
1919 assert_equal shopkeeper , connection . current_shopkeeper
2020 assert_equal account , connection . current_account
2121 warden . verify
2222 end
23+
24+ test "connection without account UUID in path has nil account" do
25+ shopkeeper = shopkeepers ( :one )
26+ warden = Minitest ::Mock . new
27+ warden . expect ( :user , shopkeeper , [ :shopkeeper ] )
28+
29+ connect "/cable" , env : { "warden" => warden }
30+
31+ assert_equal shopkeeper , connection . current_shopkeeper
32+ assert_nil connection . current_account
33+ warden . verify
34+ end
2335end
You can’t perform that action at this time.
0 commit comments