From 3631d7eee4bd034f2eefe1b9892d5fcd565579ac Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Mon, 14 Jan 2019 00:26:27 +0100 Subject: Update Action Cable connection testing. * Don't reimplement assert_raises Also test what happens in case there's no explicit rejection. * Avoid OpenStruct. Remove space beneath private. * Simplify verification methods for code under test. * Match documentation with other Rails docs. Also remove mention of the custom path argument for now. Unsure how useful that really is. --- actioncable/test/connection/test_case_test.rb | 31 ++++++++++++++++----------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'actioncable/test/connection/test_case_test.rb') diff --git a/actioncable/test/connection/test_case_test.rb b/actioncable/test/connection/test_case_test.rb index 76cfb2c07c..3b19465d7b 100644 --- a/actioncable/test/connection/test_case_test.rb +++ b/actioncable/test/connection/test_case_test.rb @@ -75,10 +75,8 @@ class Connection < ActionCable::Connection::Base end private - def verify_user - reject_unauthorized_connection unless cookies.signed[:user_id].present? - cookies.signed[:user_id] + cookies.signed[:user_id].presence || reject_unauthorized_connection end end @@ -101,6 +99,14 @@ class ConnectionTest < ActionCable::Connection::TestCase def test_connection_rejected assert_reject_connection { connect } end + + def test_connection_rejected_assertion_message + error = assert_raises Minitest::Assertion do + assert_reject_connection { "Intentionally doesn't connect." } + end + + assert_match(/Expected to reject connection/, error.message) + end end class EncryptedCookiesConnection < ActionCable::Connection::Base @@ -111,10 +117,8 @@ class EncryptedCookiesConnection < ActionCable::Connection::Base end private - def verify_user - reject_unauthorized_connection unless cookies.encrypted[:user_id].present? - cookies.encrypted[:user_id] + cookies.encrypted[:user_id].presence || reject_unauthorized_connection end end @@ -142,10 +146,8 @@ class SessionConnection < ActionCable::Connection::Base end private - def verify_user - reject_unauthorized_connection unless request.session[:user_id].present? - request.session[:user_id] + request.session[:user_id].presence || reject_unauthorized_connection end end @@ -170,11 +172,9 @@ class EnvConnection < ActionCable::Connection::Base end private - def verify_user # Warden-like authentication - reject_unauthorized_connection unless env["authenticator"]&.user.present? - env["authenticator"].user + env["authenticator"]&.user || reject_unauthorized_connection end end @@ -182,7 +182,12 @@ class EnvConnectionTest < ActionCable::Connection::TestCase tests EnvConnection def test_connected_with_env - connect env: { "authenticator" => OpenStruct.new(user: "David") } + authenticator = Class.new do + def user; "David"; end + end + + connect env: { "authenticator" => authenticator.new } + assert_equal "David", connection.user end -- cgit v1.2.3