From 430d6308a81816428223cb243f06613c387a2da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Thu, 6 Apr 2023 17:55:30 +0200 Subject: [PATCH] FIX: Render links with subfolders properly in Discobot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, some links aren’t properly built in Discobot when Discourse is hosted in a subfolder. This is because we’re providing `Discourse.base_url` to the Rails helpers which contains the base URL *with* the prefix. But Rails helpers already handle this prefix so the resulting link gets the prefix twice. The fix is quite simple: use `Discourse.base_url_no_prefix` instead of `Discourse.base_url`. --- .../new_user_narrative.rb | 5 +++- .../new_user_narrative_spec.rb | 27 +++++++++++-------- spec/support/helpers.rb | 1 + 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb index fc1a69ea195..7ad317c77e4 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb @@ -638,7 +638,10 @@ module DiscourseNarrativeBot end def url_helpers(url, opts = {}) - Rails.application.routes.url_helpers.public_send(url, opts.merge(host: Discourse.base_url)) + Rails.application.routes.url_helpers.public_send( + url, + opts.merge(host: Discourse.base_url_no_prefix), + ) end end end diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb index 500a0b68af0..37654e5886e 100644 --- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb +++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb @@ -286,22 +286,27 @@ RSpec.describe DiscourseNarrativeBot::NewUserNarrative do ) end - it "should create the right reply when the bookmark is created" do - post.update!(user: discobot_user) - narrative.expects(:enqueue_timeout_job).with(user) - - narrative.input(:bookmark, user, post: post) - new_post = Post.last - profile_page_url = "#{Discourse.base_url}/u/#{user.username}" - - expected_raw = <<~RAW + context "when the bookmark is created" do + let(:profile_page_url) { "#{Discourse.base_url_no_prefix}/prefix/u/#{user.username}" } + let(:new_post) { Post.last } + let(:user_state) { narrative.get_data(user)[:state].to_sym } + let(:expected_raw) { <<~RAW } #{I18n.t("discourse_narrative_bot.new_user_narrative.bookmark.reply", bookmark_url: "#{profile_page_url}/activity/bookmarks", base_uri: "")} #{I18n.t("discourse_narrative_bot.new_user_narrative.onebox.instructions", base_uri: "")} RAW - expect(new_post.raw).to eq(expected_raw.chomp) - expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_onebox) + before do + set_subfolder("/prefix") + post.update!(user: discobot_user) + narrative.stubs(:enqueue_timeout_job).with(user) + end + + it "creates the right reply" do + narrative.input(:bookmark, user, post: post) + expect(new_post.raw).to eq(expected_raw.chomp) + expect(user_state).to eq(:tutorial_onebox) + end end it "should skip tutorials in SiteSetting.discourse_narrative_bot_skip_tutorials" do diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index ff611df35e3..6cc73913d98 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -153,6 +153,7 @@ module Helpers global_setting :relative_url_root, f old_root = ActionController::Base.config.relative_url_root ActionController::Base.config.relative_url_root = f + Rails.application.routes.stubs(:relative_url_root).returns(f) before_next_spec { ActionController::Base.config.relative_url_root = old_root } end