Skip to content

A platform for community discussion. Free, open, simple.

DEV: Do not process requests initiated by browser in a different example (#25809)

Alan Guo Xiang Tan6e9fbb5babf5
diff --git a/config/initializers/200-first_middlewares.rb b/config/initializers/200-first_middlewares.rb
index 79bbb199040..618a083dedb 100644
--- a/config/initializers/200-first_middlewares.rb
+++ b/config/initializers/200-first_middlewares.rb
@@ -33,6 +33,10 @@ if Rails.env.test?
RailsMultisite::DiscoursePatches.config
class BlockRequestsMiddleware
+ RSPEC_CURRENT_EXAMPLE_COOKIE_STRING = "rspec_current_example_location"
+
+ cattr_accessor :current_example_location
+
@@block_requests = false
def self.block_requests!
@@ -48,7 +52,17 @@ if Rails.env.test?
end
def call(env)
- if @@block_requests
+ request = Rack::Request.new(env)
+
+ if request.xhr? &&
+ (
+ @@block_requests ||
+ (
+ self.class.current_example_location.present? &&
+ self.class.current_example_location !=
+ request.cookies[RSPEC_CURRENT_EXAMPLE_COOKIE_STRING]
+ )
+ )
[503, { "Content-Type" => "text/plain" }, ["Blocked by BlockRequestsMiddleware"]]
else
@app.call(env)
diff --git a/plugins/chat/spec/system/chat_new_message_spec.rb b/plugins/chat/spec/system/chat_new_message_spec.rb
index a3116a038d6..1ee12b31d9f 100644
--- a/plugins/chat/spec/system/chat_new_message_spec.rb
+++ b/plugins/chat/spec/system/chat_new_message_spec.rb
@@ -27,7 +27,7 @@ RSpec.describe "Chat New Message from params", type: :system do
expect(page).to have_current_path("/chat/c/#{user_2.username}/#{Chat::Channel.last.id}")
end
- xit "redirects to chat channel if recipients param is missing" do
+ it "redirects to chat channel if recipients param is missing" do
visit("/chat/new-message")
# chat service selects public channel from getIdealFirstChannelId
diff --git a/plugins/chat/spec/system/single_thread_spec.rb b/plugins/chat/spec/system/single_thread_spec.rb
index 3cd32345c4b..382d70d7915 100644
--- a/plugins/chat/spec/system/single_thread_spec.rb
+++ b/plugins/chat/spec/system/single_thread_spec.rb
@@ -20,7 +20,7 @@ describe "Single thread in side panel", type: :system do
before { channel.update!(threading_enabled: false) }
- it "does not open the side panel for a single thread", capture_log: true do
+ it "does not open the side panel for a single thread" do
thread =
chat_thread_chain_bootstrap(channel: channel, users: [current_user, Fabricate(:user)])
chat_page.visit_channel(channel)
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index c34c465bb14..af8f3d93dac 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -152,6 +152,7 @@ module TestSetup
Middleware::AnonymousCache.disable_anon_cache
BlockRequestsMiddleware.allow_requests!
+ BlockRequestsMiddleware.current_example_location = nil
end
end
@@ -592,6 +593,14 @@ RSpec.configure do |config|
ActiveRecord::Base.connection.schema_cache.add(table)
end
+ ApplicationController.before_action do
+ if BlockRequestsMiddleware.current_example_location
+ cookies[
+ BlockRequestsMiddleware::RSPEC_CURRENT_EXAMPLE_COOKIE_STRING
+ ] = BlockRequestsMiddleware.current_example_location
+ end
+ end
+
system_tests_initialized = true
end
@@ -609,6 +618,8 @@ RSpec.configure do |config|
driven_by driver.join("_").to_sym
setup_system_test
+
+ BlockRequestsMiddleware.current_example_location = example.location
end
config.after(:each, type: :system) do |example|