From 6c4e86899e86156cc8426c941a50ab761135391e Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Mon, 23 Sep 2024 16:10:13 -0500 Subject: [PATCH] Tests: add channels tests --- Gemfile | 2 ++ Gemfile.lock | 12 +++++++++ docs/development.md | 2 +- .../application_cable/channel_test.rb | 24 +++++++++++++++++ .../application_cable/connection_test.rb | 27 +++++++++++++++++++ test/channels/export_channel_test.rb | 17 ++++++++++++ test/test_helper.rb | 7 +++++ 7 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 test/channels/application_cable/channel_test.rb create mode 100644 test/channels/application_cable/connection_test.rb create mode 100644 test/channels/export_channel_test.rb diff --git a/Gemfile b/Gemfile index ef1a4d56c..782341e99 100644 --- a/Gemfile +++ b/Gemfile @@ -146,9 +146,11 @@ group :test do gem 'm', '~> 1.5.0' gem 'minitest' gem 'minitest-ci', '~> 3.4.0' + gem "minitest-rails", "~> 7.0" gem 'minitest-reporters' gem 'rails-controller-testing' gem 'simplecov', require: false + gem 'webmock', '~> 3.23' end gem 'net-ftp' diff --git a/Gemfile.lock b/Gemfile.lock index cb412482f..6d31ae0b3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -270,6 +270,9 @@ GEM dry-validation (~> 1.0, >= 1.0.0) connection_pool (2.4.1) content_disposition (1.0.0) + crack (1.0.0) + bigdecimal + rexml crass (1.0.6) csv (3.3.0) database_cleaner (2.0.2) @@ -526,6 +529,9 @@ GEM minitest (5.25.1) minitest-ci (3.4.0) minitest (>= 5.0.6) + minitest-rails (7.0.1) + minitest (~> 5.10) + railties (~> 7.0.0) minitest-reporters (1.7.1) ansi builder @@ -819,6 +825,10 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) + webmock (3.23.1) + addressable (>= 2.8.0) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) webpacker (5.4.4) activesupport (>= 5.2) rack-proxy (>= 0.6.1) @@ -890,6 +900,7 @@ DEPENDENCIES mini_magick (~> 4.9.4) minitest minitest-ci (~> 3.4.0) + minitest-rails (~> 7.0) minitest-reporters net-ftp noticed @@ -924,6 +935,7 @@ DEPENDENCIES twitter-typeahead-rails (= 0.11.1.pre.corejavascript) vite_rails (~> 3.0) web-console + webmock (~> 3.23) webpacker (~> 5.x) whenever (~> 1.0.0) diff --git a/docs/development.md b/docs/development.md index a838319af..b829a251d 100644 --- a/docs/development.md +++ b/docs/development.md @@ -4,4 +4,4 @@ bundle exec rake geoportal:server ## Test Locally RAILS_ENV=test bundle exec rake geoportal:test -RAILS_ENV=test bundle exec rake test:system test \ No newline at end of file +RAILS_ENV=test bundle exec rails test:system test \ No newline at end of file diff --git a/test/channels/application_cable/channel_test.rb b/test/channels/application_cable/channel_test.rb new file mode 100644 index 000000000..3780b162b --- /dev/null +++ b/test/channels/application_cable/channel_test.rb @@ -0,0 +1,24 @@ +require 'test_helper' + +module ApplicationCable + class ChannelTest < ActiveSupport::TestCase + test "should be a subclass of ActionCable::Channel::Base" do + assert Channel < ActionCable::Channel::Base + end + + test "should be defined within ApplicationCable module" do + assert_equal ApplicationCable::Channel, Channel + end + + test "can be instantiated with connection and identifier" do + connection = Object.new + def connection.identifiers + [:test_identifier] + end + identifier = "test_channel" + assert_nothing_raised do + Channel.new(connection, identifier) + end + end + end +end \ No newline at end of file diff --git a/test/channels/application_cable/connection_test.rb b/test/channels/application_cable/connection_test.rb new file mode 100644 index 000000000..6b6293917 --- /dev/null +++ b/test/channels/application_cable/connection_test.rb @@ -0,0 +1,27 @@ +require 'test_helper' + +module ApplicationCable + class ConnectionTest < ActionCable::Connection::TestCase + def setup + @user = User.create(email: 'test@example.com', password: 'password') + end + + test "connects with authenticated user" do + # Simulate Devise authentication + warden = Minitest::Mock.new + warden.expect :user, @user + + connect env: { 'warden' => warden } + + assert_equal @user, connection.current_user + end + + test "rejects connection without authenticated user" do + # Simulate no authenticated user + warden = Minitest::Mock.new + warden.expect :user, nil + + assert_reject_connection { connect env: { 'warden' => warden } } + end + end +end \ No newline at end of file diff --git a/test/channels/export_channel_test.rb b/test/channels/export_channel_test.rb new file mode 100644 index 000000000..23bd3a04a --- /dev/null +++ b/test/channels/export_channel_test.rb @@ -0,0 +1,17 @@ +require 'test_helper' + +class ExportChannelTest < ActionCable::Channel::TestCase + test "subscribes to export_channel" do + subscribe + assert subscription.confirmed? + assert_has_stream 'export_channel' + end + + test "unsubscribes from export_channel" do + subscribe + assert subscription.confirmed? + + unsubscribe + assert_no_streams + end +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 7d373eb97..510e359de 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,6 +12,13 @@ require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' +require "minitest/rails" +require "minitest/reporters" + +require "webmock/minitest" +WebMock.enable! +WebMock.allow_net_connect! + require 'selenium/webdriver' class ActiveSupport::TestCase