mirror of
https://github.com/discourse/discourse.git
synced 2025-04-16 22:19:02 +08:00
DEV: improve usability of subfolder specs
Previously people were not consistent about mocking which left internals in a fragile state when running subfolder specs. This introduces a simple helper `set_subfolder` which you can use to set the subfolder for the spec. It takes care of proper configuration of subfolder and teardown. ``` # usage set_subfolder "/my_amazing_subfolder" ``` You should no longer stub base_uri or global_settings
This commit is contained in:
parent
40d14ba166
commit
e7cf4579a8
@ -558,14 +558,12 @@ describe CookedPostProcessor do
|
||||
end
|
||||
|
||||
let(:cpp) { CookedPostProcessor.new(post, disable_loading_image: true) }
|
||||
let(:base_url) { "http://test.localhost/subfolder" }
|
||||
let(:base_uri) { "/subfolder" }
|
||||
|
||||
before do
|
||||
set_subfolder "/subfolder"
|
||||
|
||||
SiteSetting.max_image_height = 2000
|
||||
SiteSetting.create_thumbnails = true
|
||||
Discourse.stubs(:base_url).returns(base_url)
|
||||
Discourse.stubs(:base_uri).returns(base_uri)
|
||||
FastImage.expects(:size).returns([1750, 2000])
|
||||
OptimizedImage.expects(:resize).returns(true)
|
||||
|
||||
|
@ -114,11 +114,6 @@ describe FileStore::LocalStore do
|
||||
|
||||
end
|
||||
|
||||
def stub_for_subfolder
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
end
|
||||
|
||||
describe "#absolute_base_url" do
|
||||
|
||||
it "is present" do
|
||||
@ -126,7 +121,7 @@ describe FileStore::LocalStore do
|
||||
end
|
||||
|
||||
it "supports subfolder" do
|
||||
stub_for_subfolder
|
||||
set_subfolder "/forum"
|
||||
expect(store.absolute_base_url).to eq("http://test.localhost/forum/uploads/default")
|
||||
end
|
||||
|
||||
@ -139,7 +134,7 @@ describe FileStore::LocalStore do
|
||||
end
|
||||
|
||||
it "supports subfolder" do
|
||||
stub_for_subfolder
|
||||
set_subfolder "/forum"
|
||||
expect(store.relative_base_url).to eq("/forum/uploads/default")
|
||||
end
|
||||
|
||||
|
@ -386,8 +386,7 @@ describe FileStore::S3Store do
|
||||
|
||||
# none of this should matter at all
|
||||
# subfolder should not leak into uploads
|
||||
global_setting :relative_url_root, '/community'
|
||||
Discourse.stubs(:base_uri).returns("/community")
|
||||
set_subfolder "/community"
|
||||
|
||||
url = "//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com/livechat/original/gif.png"
|
||||
|
||||
|
@ -269,12 +269,8 @@ describe PrettyText do
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
|
||||
context "subfolder" do
|
||||
before do
|
||||
GlobalSetting.stubs(:relative_url_root).returns("/forum")
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
end
|
||||
|
||||
it "should have correct avatar url" do
|
||||
set_subfolder "/forum"
|
||||
md = <<~MD
|
||||
[quote="#{user.username}, post:123, topic:456, full:true"]
|
||||
ddd
|
||||
@ -331,12 +327,9 @@ describe PrettyText do
|
||||
end
|
||||
|
||||
context 'subfolder' do
|
||||
before do
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
end
|
||||
|
||||
it "handles user and group mentions correctly" do
|
||||
set_subfolder "/forum"
|
||||
|
||||
Fabricate(:user, username: 'user1')
|
||||
Fabricate(:group, name: 'groupA', mentionable_level: Group::ALIAS_LEVELS[:everyone])
|
||||
|
||||
|
@ -214,12 +214,8 @@ describe TopicView do
|
||||
end
|
||||
|
||||
context 'subfolder' do
|
||||
before do
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
end
|
||||
|
||||
it "provides the correct absolute url" do
|
||||
set_subfolder "/forum"
|
||||
expect(topic_view.absolute_url).to eq("http://test.localhost/forum/t/#{topic.slug}/#{topic.id}")
|
||||
end
|
||||
end
|
||||
|
@ -33,7 +33,8 @@ describe UrlHelper do
|
||||
store = stub
|
||||
store.expects(:has_been_uploaded?).returns(false)
|
||||
Discourse.stubs(:store).returns(store)
|
||||
Discourse.stubs(:base_uri).returns("/subpath")
|
||||
|
||||
set_subfolder "/subpath"
|
||||
expect(UrlHelper.is_local("/subpath/assets/javascripts/all.js")).to eq(true)
|
||||
end
|
||||
|
||||
|
@ -31,19 +31,15 @@ describe ApplicationHelper do
|
||||
global_setting :s3_cdn_url, 'https://s3cdn.com'
|
||||
end
|
||||
|
||||
after do
|
||||
ActionController::Base.config.relative_url_root = nil
|
||||
end
|
||||
|
||||
it "deals correctly with subfolder" do
|
||||
ActionController::Base.config.relative_url_root = "/community"
|
||||
set_subfolder "/community"
|
||||
expect(helper.preload_script("application")).to include('https://s3cdn.com/assets/application.js')
|
||||
end
|
||||
|
||||
it "replaces cdn URLs with s3 cdn subfolder paths" do
|
||||
global_setting :s3_cdn_url, 'https://s3cdn.com/s3_subpath'
|
||||
set_cdn_url "https://awesome.com"
|
||||
ActionController::Base.config.relative_url_root = "/community"
|
||||
set_subfolder "/community"
|
||||
expect(helper.preload_script("application")).to include('https://s3cdn.com/s3_subpath/assets/application.js')
|
||||
end
|
||||
|
||||
|
@ -225,8 +225,7 @@ describe UserNotifications do
|
||||
end
|
||||
|
||||
it "supports subfolder" do
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
set_subfolder "/forum"
|
||||
html = subject.html_part.body.to_s
|
||||
text = subject.text_part.body.to_s
|
||||
expect(html).to be_present
|
||||
|
@ -446,8 +446,7 @@ describe Category do
|
||||
end
|
||||
|
||||
it "correctly creates permalink when category slug is changed in subfolder install" do
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
set_subfolder '/forum'
|
||||
old_url = @category.url
|
||||
@category.update(slug: 'new-category')
|
||||
permalink = Permalink.last
|
||||
|
@ -204,8 +204,7 @@ RSpec.describe ApplicationController do
|
||||
end
|
||||
|
||||
it 'supports subfolder with permalinks' do
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
set_subfolder "/forum"
|
||||
|
||||
trashed_topic = create_post.topic
|
||||
trashed_topic.trash!
|
||||
|
@ -21,8 +21,7 @@ describe CategoriesController do
|
||||
end
|
||||
|
||||
it 'web crawler view has correct urls for subfolder install' do
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
set_subfolder "/forum"
|
||||
get '/categories', headers: { 'HTTP_USER_AGENT' => 'Googlebot' }
|
||||
html = Nokogiri::HTML(response.body)
|
||||
expect(html.css('body.crawler')).to be_present
|
||||
|
@ -339,8 +339,7 @@ RSpec.describe ListController do
|
||||
end
|
||||
|
||||
it 'renders links correctly with subfolder' do
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
set_subfolder "/forum"
|
||||
post = Fabricate(:post, topic: topic, user: user)
|
||||
get "/latest.rss"
|
||||
expect(response.status).to eq(200)
|
||||
@ -458,8 +457,7 @@ RSpec.describe ListController do
|
||||
end
|
||||
|
||||
it "renders RSS in subfolder correctly" do
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
set_subfolder "/forum"
|
||||
get "/c/#{category.slug}.rss"
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to_not include("/forum/forum")
|
||||
|
@ -18,8 +18,7 @@ describe PermalinksController do
|
||||
|
||||
it "should work for subfolder installs too" do
|
||||
permalink.update!(topic_id: topic.id)
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
set_subfolder "/forum"
|
||||
|
||||
get "/#{permalink.url}"
|
||||
|
||||
|
@ -49,7 +49,8 @@ RSpec.describe RobotsTxtController do
|
||||
|
||||
context 'subfolder' do
|
||||
it 'prefixes the rules with the directory' do
|
||||
Discourse.stubs(:base_uri).returns('/forum')
|
||||
set_subfolder "/forum"
|
||||
|
||||
get '/robots.txt'
|
||||
expect(response.body).to include("\nDisallow: /forum/admin")
|
||||
end
|
||||
|
@ -1861,7 +1861,8 @@ RSpec.describe TopicsController do
|
||||
|
||||
describe 'clear_notifications' do
|
||||
it 'correctly clears notifications if specified via cookie' do
|
||||
Discourse.stubs(:base_uri).returns("/eviltrout")
|
||||
set_subfolder "/eviltrout"
|
||||
|
||||
notification = Fabricate(:notification)
|
||||
sign_in(notification.user)
|
||||
|
||||
@ -2146,8 +2147,7 @@ RSpec.describe TopicsController do
|
||||
end
|
||||
|
||||
it 'renders rss of the topic correctly with subfolder' do
|
||||
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
||||
Discourse.stubs(:base_uri).returns("/forum")
|
||||
set_subfolder "/forum"
|
||||
get "/t/foo/#{topic.id}.rss"
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to_not include("/forum/forum")
|
||||
|
@ -192,16 +192,9 @@ describe UserBadgesController do
|
||||
end
|
||||
|
||||
describe 'with relative_url_root' do
|
||||
before do
|
||||
@orig_relative_url_root = ActionController::Base.config.relative_url_root
|
||||
ActionController::Base.config.relative_url_root = "/discuss"
|
||||
end
|
||||
|
||||
after do
|
||||
ActionController::Base.config.relative_url_root = @orig_relative_url_root
|
||||
end
|
||||
|
||||
it 'grants badge when valid post/topic link is given in reason' do
|
||||
set_subfolder "/discuss"
|
||||
|
||||
admin = Fabricate(:admin)
|
||||
post = create_post
|
||||
|
||||
|
@ -248,12 +248,7 @@ RSpec.describe InlineUploads do
|
||||
|
||||
context "subfolder" do
|
||||
before do
|
||||
global_setting :relative_url_root, "/community"
|
||||
ActionController::Base.config.relative_url_root = "/community"
|
||||
end
|
||||
|
||||
after do
|
||||
ActionController::Base.config.relative_url_root = nil
|
||||
set_subfolder "/community"
|
||||
end
|
||||
|
||||
it "should correct subfolder images" do
|
||||
|
@ -129,4 +129,14 @@ module Helpers
|
||||
ensure
|
||||
$stdout = old_stdout
|
||||
end
|
||||
|
||||
def set_subfolder(f)
|
||||
global_setting :relative_url_root, f
|
||||
old_root = ActionController::Base.config.relative_url_root
|
||||
ActionController::Base.config.relative_url_root = f
|
||||
|
||||
before_next_spec do
|
||||
ActionController::Base.config.relative_url_root = old_root
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user