Skip to content

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

DEV: Upgrade to Rails 8.1

Loïc Guitautd50160f239c5
diff --git a/Gemfile b/Gemfile
index fa4550d2687..68d1a702cd4 100644
--- a/Gemfile
+++ b/Gemfile
@@ -8,13 +8,13 @@ source "https://rubygems.org"
gem "bootsnap", require: false, platform: :mri
-gem "actionmailer", "~> 8.0.0"
-gem "actionpack", "~> 8.0.0"
-gem "actionview", "~> 8.0.0"
-gem "activemodel", "~> 8.0.0"
-gem "activerecord", "~> 8.0.0"
-gem "activesupport", "~> 8.0.0"
-gem "railties", "~> 8.0.0"
+gem "actionmailer", "~> 8.1.0"
+gem "actionpack", "~> 8.1.0"
+gem "actionview", "~> 8.1.0"
+gem "activemodel", "~> 8.1.0"
+gem "activerecord", "~> 8.1.0"
+gem "activesupport", "~> 8.1.0"
+gem "railties", "~> 8.1.0"
gem "propshaft"
gem "json"
@@ -273,6 +273,9 @@ gem "cgi", ">= 0.3.6", require: false
gem "tzinfo-data"
gem "csv", require: false
+# Rails 8.1 drops its own dependency on `benchmark`.
+gem "benchmark", require: false
+
# dependencies for the automation plugin
gem "iso8601"
gem "rrule"
diff --git a/Gemfile.lock b/Gemfile.lock
index 34210be261f..b768dfc1e68 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -55,16 +55,16 @@ GEM
remote: https://rubygems.org/
specs:
Ascii85 (2.0.1)
- actionmailer (8.0.5.1)
- actionpack (= 8.0.5.1)
- actionview (= 8.0.5.1)
- activejob (= 8.0.5.1)
- activesupport (= 8.0.5.1)
+ actionmailer (8.1.3.1)
+ actionpack (= 8.1.3.1)
+ actionview (= 8.1.3.1)
+ activejob (= 8.1.3.1)
+ activesupport (= 8.1.3.1)
mail (>= 2.8.0)
rails-dom-testing (~> 2.2)
- actionpack (8.0.5.1)
- actionview (= 8.0.5.1)
- activesupport (= 8.0.5.1)
+ actionpack (8.1.3.1)
+ actionview (= 8.1.3.1)
+ activesupport (= 8.1.3.1)
nokogiri (>= 1.8.5)
rack (>= 2.2.4)
rack-session (>= 1.0.1)
@@ -72,8 +72,8 @@ GEM
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
useragent (~> 0.16)
- actionview (8.0.5.1)
- activesupport (= 8.0.5.1)
+ actionview (8.1.3.1)
+ activesupport (= 8.1.3.1)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
@@ -82,23 +82,23 @@ GEM
actionview (>= 6.0.a)
active_model_serializers (0.8.4)
activemodel (>= 3.0)
- activejob (8.0.5.1)
- activesupport (= 8.0.5.1)
+ activejob (8.1.3.1)
+ activesupport (= 8.1.3.1)
globalid (>= 0.3.6)
- activemodel (8.0.5.1)
- activesupport (= 8.0.5.1)
- activerecord (8.0.5.1)
- activemodel (= 8.0.5.1)
- activesupport (= 8.0.5.1)
+ activemodel (8.1.3.1)
+ activesupport (= 8.1.3.1)
+ activerecord (8.1.3.1)
+ activemodel (= 8.1.3.1)
+ activesupport (= 8.1.3.1)
timeout (>= 0.4.0)
- activesupport (8.0.5.1)
+ activesupport (8.1.3.1)
base64
- benchmark (>= 0.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
+ json
logger (>= 1.4.2)
minitest (>= 5.1)
securerandom (>= 0.3)
@@ -543,9 +543,9 @@ GEM
rails_multisite (9.0.0)
activerecord (>= 7.2)
railties (>= 7.2)
- railties (8.0.5.1)
- actionpack (= 8.0.5.1)
- activesupport (= 8.0.5.1)
+ railties (8.1.3.1)
+ actionpack (= 8.1.3.1)
+ activesupport (= 8.1.3.1)
irb (~> 1.13)
rackup (>= 1.0.0)
rake (>= 12.2)
@@ -824,14 +824,14 @@ PLATFORMS
DEPENDENCIES
Ascii85
- actionmailer (~> 8.0.0)
- actionpack (~> 8.0.0)
- actionview (~> 8.0.0)
+ actionmailer (~> 8.1.0)
+ actionpack (~> 8.1.0)
+ actionview (~> 8.1.0)
actionview_precompiler
active_model_serializers (~> 0.8.3)
- activemodel (~> 8.0.0)
- activerecord (~> 8.0.0)
- activesupport (~> 8.0.0)
+ activemodel (~> 8.1.0)
+ activerecord (~> 8.1.0)
+ activesupport (~> 8.1.0)
addressable
afm
annotaterb
@@ -840,6 +840,7 @@ DEPENDENCIES
aws-sdk-s3
aws-sdk-sns
aws-sdk-sts
+ benchmark
better_errors
bootsnap
bullet
@@ -939,7 +940,7 @@ DEPENDENCIES
rails-dom-testing
rails_failover
rails_multisite
- railties (~> 8.0.0)
+ railties (~> 8.1.0)
raindrops
rake
rb-fsevent
@@ -995,15 +996,15 @@ DEPENDENCIES
CHECKSUMS
Ascii85 (2.0.1) sha256=15cb5d941808543cbb9e7e6aea3c8ec3877f154c3461e8b3673e97f7ecedbe5a
- actionmailer (8.0.5.1) sha256=c3d2b3f96e1989ea25f51699786a97fcb2536eb7abfc2a667cb8f2376ec08403
- actionpack (8.0.5.1) sha256=a5595c9d824d68884ddc4d3965ab78c897760d3752e190df7efe897371caa1eb
- actionview (8.0.5.1) sha256=472a108b9cc2295c4ac3ff09b028045e619875801f48c556f0085210b9cb1440
+ actionmailer (8.1.3.1) sha256=88ea441b28ff02a0c6c006468892642a3d9942affce9d294e81a74504aa5c43c
+ actionpack (8.1.3.1) sha256=974cb7154548e81f470b1b0f247b99cb38e87825899dca58610596e2817723d0
+ actionview (8.1.3.1) sha256=2da68b8414c47b43bfbed1ce69c5afe1c04f78c267aacb5660a4cab5ca12cfb6
actionview_precompiler (0.4.0) sha256=33b6bd6ec4c1b856e02fdf5f6512c9eb4a92ac1c0545e941b3e354b7d540ed1c
active_model_serializers (0.8.4) sha256=7350e3d3b6a5946bbec033241d908013cc85ff4d584230e6aa074225547c754c
- activejob (8.0.5.1) sha256=142407a21b6c3cbc6ddd92ca111ac18ea5c40298eb94d81845cd897a072a6880
- activemodel (8.0.5.1) sha256=559be32aa9c40db7a3ee0aef926d4508a9ebd22f96f7276c11326d21a7dff4a4
- activerecord (8.0.5.1) sha256=9252968fce404d75eb17092498a440d472167f2f8deee32b4658d6552b1eeea7
- activesupport (8.0.5.1) sha256=329a4280c4fbcfcf338ae2cb9df28b0b14527929dba105e10b3604516d998710
+ activejob (8.1.3.1) sha256=1c8dd275df930df40deecffec63d913a550a33fd94bd298f69721dd96939954a
+ activemodel (8.1.3.1) sha256=99cc02ce2faec371d14440949d85787ebd23a907c9baef0a9d4bcd4d21888f88
+ activerecord (8.1.3.1) sha256=0a2fb6c28f4938f6b013a3a549bec0a7e37d535f3dc8990e804bcc3258c0403b
+ activesupport (8.1.3.1) sha256=85458765f25ea48b9019c46b6bb3fa5683197bf4280d9f06710a6e8d7a831376
addressable (2.9.0) sha256=7fdf6ac3660f7f4e867a0838be3f6cf722ace541dd97767fa42bc6cfa980c7af
afm (1.0.0) sha256=5bd4d6f6241e7014ef090985ec6f4c3e9745f6de0828ddd58bc1efdd138f4545
annotaterb (4.20.0) sha256=871b2e898d1d60c23bdc59b72a5b840678a56355bf5f6fdeb8c79d317ff98bf7
@@ -1222,7 +1223,7 @@ CHECKSUMS
rails-html-sanitizer (1.7.1) sha256=e797a7c9b01e567307e317c576b49ab4168017e63eea4dba9ce3cb587e2f22c2
rails_failover (2.3.0) sha256=eed6ea0674fd6f9f6b070ad297ad2ead121ecf9202920f6068b6a4f29d9491c9
rails_multisite (9.0.0) sha256=2bc88f0405cb88d1055712095a11c4eb8ce1d4a36599d1226c6779946be495e6
- railties (8.0.5.1) sha256=da1958e1d9dab04691a2f8721b3ff7fab323715d37f103c19972dedfd644d5c7
+ railties (8.1.3.1) sha256=2388a232579a00cefea4487de66c8553c3408c1300abdc6cf1799d86ffb04487
rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a
raindrops (0.20.1) sha256=aa0eb9ff6834f2d9e232ba688bd49cb30be893bc5a3452e74722c94c1fab4730
rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 7fd23d3f1ea..edf4bc1f45a 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -165,7 +165,7 @@ class ApplicationController < ActionController::Base
render_json_error I18n.t("site_setting_missing", name: e.message), status: 500
end
- rescue_from ActionController::RoutingError, PluginDisabled do
+ rescue_from ActionController::RoutingError, ActionDispatch::MissingController, PluginDisabled do
# This error is raised outside of the normal request response cycle and is called via the
# `DiscoursePublicExceptions` middleware which creates a new instance of the ApplicationController.
# As a result, controller actions hooks are not called and we need to explicitly call `dont_cache_page` here.
diff --git a/app/models/problem_check.rb b/app/models/problem_check.rb
index 1174f9bfeb4..12db7402ddd 100644
--- a/app/models/problem_check.rb
+++ b/app/models/problem_check.rb
@@ -21,49 +21,53 @@ class ProblemCheck
attr_reader :checks
end
- include ActiveSupport::Configurable
-
- config_accessor :enabled, default: true, instance_writer: false
- config_accessor :priority, default: "low", instance_writer: false
+ class_attribute :enabled, default: true, instance_writer: false, instance_predicate: false
+ class_attribute :priority, default: "low", instance_writer: false, instance_predicate: false
# Determines if the check should be performed at a regular interval, and if
# so how often. If left blank, the check will be performed every time the
# admin dashboard is loaded, or the data is otherwise requested.
#
- config_accessor :perform_every, default: nil, instance_writer: false
+ class_attribute :perform_every, default: nil, instance_writer: false, instance_predicate: false
# How many times the check should retry before registering a problem. Only
# works for scheduled checks.
#
- config_accessor :max_retries, default: 2, instance_writer: false
+ class_attribute :max_retries, default: 2, instance_writer: false, instance_predicate: false
# The retry delay after a failed check. Only works for scheduled checks with
# more than one retry configured.
#
- config_accessor :retry_after, default: 30.seconds, instance_writer: false
+ class_attribute :retry_after,
+ default: 30.seconds,
+ instance_writer: false,
+ instance_predicate: false
# How many consecutive times the check can fail without notifying admins.
# This can be used to give some leeway for transient problems. Note that
# retries are not counted. So a check that ultimately fails after e.g. two
# retries is counted as one "blip".
#
- config_accessor :max_blips, default: 0, instance_writer: false
+ class_attribute :max_blips, default: 0, instance_writer: false, instance_predicate: false
# Indicates that the problem check is an "inline" check. This provides a
# low level construct for registering problems ad-hoc within application
# code, without having to extract the checking logic into a dedicated
# problem check.
#
- config_accessor :inline, default: false, instance_writer: false
+ class_attribute :inline, default: false, instance_writer: false, instance_predicate: false
# Used to set up multiple targets for the check. For example, a check that
# operates on groups may need to specify which groups to work on.
#
- config_accessor :targets, default: -> { [NO_TARGET] }, instance_writer: false
+ class_attribute :targets,
+ default: -> { [NO_TARGET] },
+ instance_writer: false,
+ instance_predicate: false
# Problem check classes need to be registered here in order to be enabled.
#
- # Note: This list must come after the `config_accessor` declarations.
+ # Note: This list must come after the `class_attribute` declarations.
#
CORE_PROBLEM_CHECKS = [
ProblemCheck::BadFaviconUrl,
@@ -230,7 +234,7 @@ class ProblemCheck
Problem.new(
I18n.t(override_key || translation_key, base_path: Discourse.base_path, **problem_details),
- priority: config.priority,
+ priority:,
identifier:,
target: target_identifier,
details: notice_details,
diff --git a/config/application.rb b/config/application.rb
index 68d3f2d8299..d259b607d5c 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -89,7 +89,9 @@ module Discourse
# tiny file needed by site settings
require "highlight_js"
- config.load_defaults 8.0
+ config.load_defaults 8.1
+ # Existing permalink targets and redirect settings can contain path-relative URLs.
+ config.action_controller.action_on_path_relative_redirect = :log
config.yjit = GlobalSetting.yjit_enabled
config.active_record.cache_versioning = false # our custom cache class doesn’t support this
config.action_controller.forgery_protection_origin_check = false
@@ -102,6 +104,9 @@ module Discourse
]
config.active_support.key_generator_hash_digest_class = OpenSSL::Digest::SHA1
config.action_dispatch.cookies_serializer = :message_pack
+
+ # Missing controllers use the same not-found response as missing routes.
+ config.action_dispatch.rescue_responses["ActionDispatch::MissingController"] = :not_found
config.action_controller.wrap_parameters_by_default = false
config.active_support.cache_format_version = 7.1
config.active_record.dump_schema_after_migration = false
diff --git a/config/routes.rb b/config/routes.rb
index 1ed5081fa7a..adaea9575fc 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -696,7 +696,7 @@ Discourse::Application.routes.draw do
get "#{root_path}/search/users" => "users#search_users"
get(
- { "#{root_path}/account-created/" => "users#account_created" }.merge(
+ **{ "#{root_path}/account-created/" => "users#account_created" }.merge(
index == 1 ? { as: :users_account_created } : { as: :old_account_created },
),
)
@@ -704,7 +704,7 @@ Discourse::Application.routes.draw do
get "#{root_path}/account-created/resent" => "users#account_created"
get "#{root_path}/account-created/edit-email" => "users#account_created"
get(
- { "#{root_path}/password-reset/:token" => "users#password_reset_show" }.merge(
+ **{ "#{root_path}/password-reset/:token" => "users#password_reset_show" }.merge(
index == 1 ? { as: :password_reset_token } : {},
),
)
@@ -718,7 +718,7 @@ Discourse::Application.routes.draw do
token: /[0-9a-f]+/,
}
put(
- {
+ **{
"#{root_path}/activate-account/:token" => "users#perform_account_activation",
:constraints => {
token: /[0-9a-f]+/,
@@ -733,7 +733,7 @@ Discourse::Application.routes.draw do
put "#{root_path}/confirm-new-email/:token" => "users_email#confirm_new_email"
get(
- {
+ **{
"#{root_path}/confirm-admin/:token" => "users#confirm_admin",
:constraints => {
token: /[0-9a-f]+/,
@@ -782,7 +782,7 @@ Discourse::Application.routes.draw do
format: :json,
}
get(
- {
+ **{
"#{root_path}/:username" => "users#show",
:constraints => {
username: RouteFormat.username,
diff --git a/lib/csrf_token_verifier.rb b/lib/csrf_token_verifier.rb
index 545f450478f..6c1b143f719 100644
--- a/lib/csrf_token_verifier.rb
+++ b/lib/csrf_token_verifier.rb
@@ -5,7 +5,7 @@ class CSRFTokenVerifier
class InvalidCSRFToken < StandardError
end
- include ActiveSupport::Configurable
+ class_attribute :config, instance_predicate: false, default: ActiveSupport::OrderedOptions.new
include ActionController::RequestForgeryProtection
# Use config from ActionController::Base
diff --git a/lib/custom_renderer.rb b/lib/custom_renderer.rb
index af779760024..0d318e71010 100644
--- a/lib/custom_renderer.rb
+++ b/lib/custom_renderer.rb
@@ -1,7 +1,6 @@
# frozen_string_literal: true
class CustomRenderer < AbstractController::Base
- include ActiveSupport::Configurable
include AbstractController::Rendering
include AbstractController::Helpers
include AbstractController::Translation
diff --git a/lib/freedom_patches/erb_util.rb b/lib/freedom_patches/erb_util.rb
new file mode 100644
index 00000000000..ce8fd841152
--- /dev/null
+++ b/lib/freedom_patches/erb_util.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+# Repair invalid UTF-8 before HTML escaping, including strings from objects' `to_s` methods.
+# Otherwise, escaping can raise or copy invalid bytes into the response body.
+module FreedomPatches
+ module ErbUtil
+ def unwrapped_html_escape(value)
+ value = value.to_s
+ return super(value) if value.html_safe?
+ super(tidy_bytes(value))
+ end
+
+ def html_escape_once(value)
+ super(tidy_bytes(value.to_s))
+ end
+
+ private
+
+ def tidy_bytes(value)
+ return value if value.valid_encoding?
+ ActiveSupport::Multibyte::Unicode.tidy_bytes(value)
+ end
+ end
+end
+
+ERB::Util.singleton_class.prepend(FreedomPatches::ErbUtil)
+ERB::Util.prepend(FreedomPatches::ErbUtil)
diff --git a/lib/freedom_patches/message_pack_extensions.rb b/lib/freedom_patches/message_pack_extensions.rb
deleted file mode 100644
index 227c5d92384..00000000000
--- a/lib/freedom_patches/message_pack_extensions.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-# frozen_string_literal: true
-
-# Patch from https://github.com/rails/rails/pull/54584.
-# TODO: Drop this once Rails 8.1 is released.
-
-require "active_support/message_pack"
-require "active_support/core_ext/string/output_safety"
-
-module MessagePackExtensions
- def install(registry)
- super
- registry.register_type 18, ActiveSupport::SafeBuffer, packer: :to_s, unpacker: :new
- end
-end
-
-ActiveSupport::MessagePack::Extensions.prepend(MessagePackExtensions)
diff --git a/lib/freedom_patches/schema_migration_details.rb b/lib/freedom_patches/schema_migration_details.rb
index c02d9c51ace..2538eb15b37 100644
--- a/lib/freedom_patches/schema_migration_details.rb
+++ b/lib/freedom_patches/schema_migration_details.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true
+require "benchmark"
+
module FreedomPatches
module SchemaMigrationDetails
def exec_migration(conn, direction)
diff --git a/lib/text_cleaner.rb b/lib/text_cleaner.rb
index aeb8ab20fa9..cd79f1028d0 100644
--- a/lib/text_cleaner.rb
+++ b/lib/text_cleaner.rb
@@ -4,9 +4,6 @@
# Clean up a text
#
-# We use ActiveSupport mb_chars from here to properly support non ascii downcase
-require "active_support/core_ext/string/multibyte"
-
class TextCleaner
def self.title_options
# cf. http://meta.discourse.org/t/should-we-have-auto-replace-rules-in-titles/5687
@@ -42,13 +39,12 @@ class TextCleaner
text.gsub!(/\?+/, "?") if opts[:deduplicate_question_marks]
# Replace all-caps text with regular case letters
- text = downcase(text.mb_chars, opts).to_s if opts[:replace_all_upper_case] &&
- (text == upcase(text.mb_chars, opts))
+ text = downcase(text, opts) if opts[:replace_all_upper_case] && (text == upcase(text, opts))
# Capitalize first letter, but only when entire first word is lowercase
first, rest = text.split(" ", 2)
- if first && opts[:capitalize_first_letter] && first == downcase(first.mb_chars, opts)
- text = +"#{capitalize(first.mb_chars, opts)}#{rest ? " " + rest : ""}"
+ if first && opts[:capitalize_first_letter] && first == downcase(first, opts)
+ text = +"#{capitalize(first, opts)}#{rest ? " " + rest : ""}"
end
# Remove unnecessary periods at the end
diff --git a/plugins/discourse-events/app/models/discourse_events/events/event.rb b/plugins/discourse-events/app/models/discourse_events/events/event.rb
index ecf375dd0ad..d99aed8ca24 100644
--- a/plugins/discourse-events/app/models/discourse_events/events/event.rb
+++ b/plugins/discourse-events/app/models/discourse_events/events/event.rb
@@ -773,10 +773,12 @@ module DiscourseEvents
end
def reset_invitees_for_next_occurrence
- invitees.where(
- "status != :going OR recurring = FALSE",
- going: Invitee.statuses[:going],
- ).update_all(status: nil, notified: false, recurring: false)
+ # Keep the columns qualified: PostgreSQL updates a joined relation through an alias, so an
+ # unqualified name matches two copies of the table.
+ invitees
+ .where.not(status: Invitee.statuses[:going])
+ .or(invitees.where(recurring: false))
+ .update_all(status: nil, notified: false, recurring: false)
end
def notify_if_new_event
diff --git a/plugins/discourse-events/spec/models/discourse_events/events/event_spec.rb b/plugins/discourse-events/spec/models/discourse_events/events/event_spec.rb
index 5e238724a33..4fd96817a94 100644
--- a/plugins/discourse-events/spec/models/discourse_events/events/event_spec.rb
+++ b/plugins/discourse-events/spec/models/discourse_events/events/event_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-describe DiscourseEvents::Events::Event do
+RSpec.describe DiscourseEvents::Events::Event do
before do
freeze_time DateTime.parse("2020-04-24 14:10")
Jobs.run_immediately!
@@ -1160,6 +1160,84 @@ describe DiscourseEvents::Events::Event do
before { freeze_time }
+ context "when changing recurring event dates" do
+ subject(:update_dates) do
+ event.update_with_params!(
+ original_starts_at: 2.days.ago,
+ original_ends_at: 2.days.ago + 1.hour,
+ )
+ end
+
+ fab!(:user, :user)
+
+ let(:event) do
+ Fabricate(
+ :event,
+ recurrence: "every_week",
+ original_starts_at: 1.day.ago,
+ original_ends_at: 1.day.ago + 1.hour,
+ )
+ end
+ let(:status) { DiscourseEvents::Events::Invitee.statuses[:going] }
+ let(:recurring) { true }
+ let!(:invitee) { Fabricate(:invitee, event:, user:, status:, notified: true) }
+ let(:reset_attendance) { { "status" => nil, "notified" => false, "recurring" => false } }
+
+ before do
+ # Preserve stored combinations that the invitee save callback would normalize.
+ invitee.update_columns(recurring:)
+ end
+
+ context "with a going recurring invitee" do
+ it "preserves attendance and notification state" do
+ expect { update_dates }.not_to change {
+ invitee.reload.attributes.slice("status", "notified", "recurring")
+ }
+ end
+ end
+
+ context "with a going invitee without recurring attendance" do
+ let(:recurring) { false }
+
+ it "resets attendance and notification state" do
+ expect { update_dates }.to change {
+ invitee.reload.attributes.slice("status", "notified", "recurring")
+ }.to(reset_attendance)
+ end
+ end
+
+ context "with a declined recurring invitee" do
+ let(:status) { DiscourseEvents::Events::Invitee.statuses[:not_going] }
+
+ it "resets attendance and notification state" do
+ expect { update_dates }.to change {
+ invitee.reload.attributes.slice("status", "notified", "recurring")
+ }.to(reset_attendance)
+ end
+ end
+
+ context "with a NULL status and recurring attendance" do
+ let(:status) { nil }
+
+ it "preserves attendance and notification state" do
+ expect { update_dates }.not_to change {
+ invitee.reload.attributes.slice("status", "notified", "recurring")
+ }
+ end
+ end
+
+ context "with a NULL status without recurring attendance" do
+ let(:status) { nil }
+ let(:recurring) { false }
+
+ it "resets notification state" do
+ expect { update_dates }.to change {
+ invitee.reload.attributes.slice("status", "notified", "recurring")
+ }.to(reset_attendance)
+ end
+ end
+ end
+
context "with a private event" do
let!(:event_1) do
Fabricate(
diff --git a/spec/integrity/middleware_order_spec.rb b/spec/integrity/middleware_order_spec.rb
index 67089050731..9c5425d921c 100644
--- a/spec/integrity/middleware_order_spec.rb
+++ b/spec/integrity/middleware_order_spec.rb
@@ -31,7 +31,6 @@ RSpec.describe "Middleware order" do
Middleware::CspScriptNonceInjector,
Middleware::AnonymousCache,
ContentSecurityPolicy::Middleware,
- ActionDispatch::PermissionsPolicy::Middleware,
Rack::Head,
Rack::ConditionalGet,
Rack::TempfileReaper,
diff --git a/spec/lib/freedom_patches/erb_util_spec.rb b/spec/lib/freedom_patches/erb_util_spec.rb
new file mode 100644
index 00000000000..6d50baf7f23
--- /dev/null
+++ b/spec/lib/freedom_patches/erb_util_spec.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+RSpec.describe EmberAssets do
+ describe ".plain_error" do
+ subject(:error) { described_class.plain_error("Build failed: <caf\xE9> & \"file\"") }
+
+ it "repairs invalid UTF-8 and escapes the HTML error message" do
+ expect(error["messageHtml"]).to eq("Build failed: &lt;café&gt; &amp; &quot;file&quot;")
+ end
+ end
+end
+
+RSpec.describe ERB::Util do
+ let(:text) { "<caf\xE9> &amp;" }
+ let(:value) { string_wrapper.new(text) }
+ let(:string_wrapper) do
+ Struct.new(:text) do
+ def to_s
+ text
+ end
+ end
+ end
+
+ describe ".html_escape" do
+ subject(:escaped_text) { described_class.html_escape(value) }
+
+ it "repairs invalid UTF-8 from an object's string before escaping" do
+ expect(escaped_text).to eq("&lt;café&gt; &amp;amp;")
+ end
+
+ context "with an HTML-safe string" do
+ let(:value) { text.html_safe }
+
+ it "preserves the string without repair or escaping" do
+ expect(escaped_text).to eq(value)
+ end
+ end
+
+ context "with an object whose string is HTML-safe" do
+ let(:text) { super().html_safe }
+
+ it "preserves the string without repair or escaping" do
+ expect(escaped_text).to eq(text)
+ end
+ end
+ end
+
+ describe ".html_escape_once" do
+ subject(:escaped_text) { described_class.html_escape_once(value) }
+
+ it "repairs an object's string and preserves existing HTML entities" do
+ expect(escaped_text).to eq("&lt;café&gt; &amp;")
+ end
+
+ context "with an HTML-safe string" do
+ let(:value) { text.html_safe }
+
+ it "repairs and escapes the string while preserving existing HTML entities" do
+ expect(escaped_text).to eq("&lt;café&gt; &amp;")
+ end
+ end
+ end
+end
diff --git a/spec/lib/middleware/discourse_public_exceptions_spec.rb b/spec/lib/middleware/discourse_public_exceptions_spec.rb
index 4f2fc04ffd1..156876aaa97 100644
--- a/spec/lib/middleware/discourse_public_exceptions_spec.rb
+++ b/spec/lib/middleware/discourse_public_exceptions_spec.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true
RSpec.describe Middleware::DiscoursePublicExceptions do
+ subject(:response) { described_class.new("/test").call(request_env) }
+
let(:fake_logger) { FakeLogger.new }
before { Rails.logger.broadcast_to(fake_logger) }
@@ -28,4 +30,53 @@ RSpec.describe Middleware::DiscoursePublicExceptions do
expect(fake_logger.warnings.length).to eq(0)
end
+
+ describe "#call" do
+ let(:request_env) do
+ env(
+ "PATH_INFO" => "/404",
+ "HTTP_ACCEPT" => "application/json",
+ "action_dispatch.exception" => exception,
+ )
+ end
+
+ shared_examples "a JSON not found response" do
+ it "returns the not found error", :aggregate_failures do
+ expect(response[0]).to eq(404)
+ expect(response[1]["Content-Type"]).to eq("application/json; charset=utf-8")
+ expect(JSON.parse(response[2].join)).to eq(
+ "errors" => [I18n.t("not_found")],
+ "error_type" => "not_found",
+ )
+ end
+
+ it "prevents response caching" do
+ expect(response[1]["Cache-Control"]).to eq("no-cache, no-store")
+ end
+
+ context "when back and forward cache compatibility is enabled" do
+ before do
+ allow(SiteSetting).to receive(:cache_control_bfcache_compatibility).and_return(true)
+ end
+
+ it "requires private response revalidation" do
+ expect(response[1]["Cache-Control"]).to eq("no-cache, private")
+ end
+ end
+ end
+
+ before { allow(SiteSetting).to receive(:cache_control_bfcache_compatibility).and_return(false) }
+
+ context "when the route does not exist" do
+ let(:exception) { ActionController::RoutingError.new("No route matches") }
+
+ it_behaves_like "a JSON not found response"
+ end
+
+ context "when the controller does not exist" do
+ let(:exception) { ActionDispatch::MissingController.new("No controller matches") }
+
+ it_behaves_like "a JSON not found response"
+ end
+ end
end
diff --git a/spec/lib/server_session_spec.rb b/spec/lib/server_session_spec.rb
index 145ed1da113..8a7797417e0 100644
--- a/spec/lib/server_session_spec.rb
+++ b/spec/lib/server_session_spec.rb
@@ -33,6 +33,15 @@ RSpec.describe ServerSession do
expect(session[:my_hash]).to eq(hash)
end
+ context "when the value is HTML-safe text" do
+ let(:hash) { "<strong>hello</strong>".html_safe }
+
+ it "preserves the text and its HTML safety", :aggregate_failures do
+ expect(session[:my_hash]).to eq(hash)
+ expect(session[:my_hash]).to be_html_safe
+ end
+ end
+
context "when key is a string" do
it "returns the proper value" do
expect(session["my_hash"]).to eq(hash)
diff --git a/spec/requests/omniauth_callbacks_controller_spec.rb b/spec/requests/omniauth_callbacks_controller_spec.rb
index 706d0849dac..9851d6be58f 100644
--- a/spec/requests/omniauth_callbacks_controller_spec.rb
+++ b/spec/requests/omniauth_callbacks_controller_spec.rb
@@ -219,6 +219,7 @@ RSpec.describe Users::OmniauthCallbacksController do
post "/auth/google_oauth2", params: { authenticity_token: token }
expect(response.status).to eq(302)
+ expect(URI(response.location).path).to eq("/auth/google_oauth2/callback")
end
it "allows authentication without CSRF protection when it is the only method" do
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 4caa4006516..94d86a607ee 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -6834,7 +6834,7 @@ RSpec.describe TopicsController do
expect(response.status).to eq(200)
- post_timing = PostTiming.first
+ post_timing = PostTiming.take
expect(post_timing.topic).to eq(topic)
expect(post_timing.user).to eq(user)
@@ -6860,7 +6860,7 @@ RSpec.describe TopicsController do
}
expect(response.status).to eq(200)
- post_timing = PostTiming.first
+ post_timing = PostTiming.take
expect(post_timing.topic).to eq(topic)
expect(post_timing.user).to eq(user)
diff --git a/spec/services/user_anonymizer_spec.rb b/spec/services/user_anonymizer_spec.rb
index e323a4ffcfe..9a017ac3cc1 100644
--- a/spec/services/user_anonymizer_spec.rb
+++ b/spec/services/user_anonymizer_spec.rb
@@ -467,7 +467,7 @@ RSpec.describe UserAnonymizer do
expect(screened_email.reload.ip_address).to eq(anon_ip)
expect(search_log.reload.ip_address).to eq(anon_ip)
expect(topic_link_click.reload.ip_address).to eq(anon_ip)
- topic_view = TopicViewItem.where(topic_id: topic.id, user_id: user.id).first
+ topic_view = TopicViewItem.find_by(topic_id: topic.id, user_id: user.id)
expect(topic_view.ip_address).to eq(anon_ip)
expect(delete_history.reload.ip_address).to eq(anon_ip)
expect(user_history.reload.ip_address).to eq(anon_ip)