diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2019-01-14 00:26:27 +0100 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2019-01-14 01:19:56 +0100 |
commit | 3631d7eee4bd034f2eefe1b9892d5fcd565579ac (patch) | |
tree | 39161bbb0f3ad71d1fc88673371c205895f9d62b /actioncable/test/connection | |
parent | 2ab9e968c7917d3d8a6e9ee31fa06f83a2dc0760 (diff) | |
download | rails-3631d7eee4bd034f2eefe1b9892d5fcd565579ac.tar.gz rails-3631d7eee4bd034f2eefe1b9892d5fcd565579ac.tar.bz2 rails-3631d7eee4bd034f2eefe1b9892d5fcd565579ac.zip |
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.
Diffstat (limited to 'actioncable/test/connection')
-rw-r--r-- | actioncable/test/connection/test_case_test.rb | 31 |
1 files changed, 18 insertions, 13 deletions
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 |