diff --git a/app/controllers/concerns/authentication.rb b/app/controllers/concerns/authentication.rb index db23800..e609d33 100644 --- a/app/controllers/concerns/authentication.rb +++ b/app/controllers/concerns/authentication.rb @@ -71,6 +71,13 @@ module Authentication Current.session&.destroy! reset_session remove_authentication_cookie + disconnect_remote_connections + end + + def disconnect_remote_connections + Current.user&.reset_remote_connections + rescue => error + Rails.logger.warn "Could not disconnect remote connections on sign out: #{error.class}" end def authenticated_as(session) diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index 92b860f..4b20dd9 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -56,6 +56,30 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest assert_nil Session.find_by(id: session.id) end + test "destroy closes the signed-out user's live connections" do + sign_in :david + + remote_connections = mock + remote_connections.expects(:disconnect).with(reconnect: true) + ActionCable.server.stubs(:remote_connections).returns(mock.tap { |m| m.expects(:where).with(current_user: users(:david)).returns(remote_connections) }) + + delete session_url + + assert_redirected_to root_url + end + + test "destroy still signs out when the realtime service is unreachable" do + sign_in :david + session = users(:david).sessions.last + ActionCable.server.stubs(:remote_connections).raises(RuntimeError.new("cable down")) + + delete session_url + + assert_redirected_to root_url + assert_not cookies[:session_token].present? + assert_nil Session.find_by(id: session.id) + end + test "destroy removes the push subscription for the device" do sign_in :david